Skip to content

Commit 280494f

Browse files
im3dabasiaim3dabasiamanzoorwanijk
authored
Migrate redux-routine package to TS (#70686)
Co-authored-by: im3dabasia <im3dabasia1@git.wordpress.org> Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org>
1 parent 4440b42 commit 280494f

File tree

6 files changed

+61
-40
lines changed

6 files changed

+61
-40
lines changed

packages/redux-routine/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ Creates a Redux middleware, given an object of controls where each key is an act
6565

6666
_Parameters_
6767

68-
- _controls_ `Record<string, (value: import('redux').AnyAction) => Promise<boolean> | boolean>`: Object of control handlers.
68+
- _controls_ `Record< string, ( value: AnyAction ) => Promise< boolean > | boolean >`: Object of control handlers.
6969

7070
_Returns_
7171

72-
- `import('redux').Middleware`: Co-routine runtime
72+
- `Middleware`: Co-routine runtime
7373

7474
<!-- END TOKEN(Autogenerated API docs) -->
7575

packages/redux-routine/src/index.js renamed to packages/redux-routine/src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import type { Middleware, AnyAction } from 'redux';
5+
16
/**
27
* Internal dependencies
38
*/
@@ -12,11 +17,16 @@ import createRuntime from './runtime';
1217
* value of the yield assignment. If the control handler returns undefined, the
1318
* execution is not continued.
1419
*
15-
* @param {Record<string, (value: import('redux').AnyAction) => Promise<boolean> | boolean>} controls Object of control handlers.
20+
* @param controls Object of control handlers.
1621
*
17-
* @return {import('redux').Middleware} Co-routine runtime
22+
* @return Co-routine runtime
1823
*/
19-
export default function createMiddleware( controls = {} ) {
24+
export default function createMiddleware(
25+
controls: Record<
26+
string,
27+
( value: AnyAction ) => Promise< boolean > | boolean
28+
> = {}
29+
): Middleware {
2030
return ( store ) => {
2131
const runtime = createRuntime( controls, store.dispatch );
2232
return ( next ) => ( action ) => {

packages/redux-routine/src/is-action.js

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { isPlainObject } from 'is-plain-object';
5+
import type { Action } from 'redux';
6+
7+
/**
8+
* Returns true if the given object quacks like an action.
9+
*
10+
* @param object Object to test
11+
*
12+
* @return Whether object is an action.
13+
*/
14+
export function isAction( object: unknown ): object is Action {
15+
return isPlainObject( object ) && typeof object.type === 'string';
16+
}
17+
18+
/**
19+
* Returns true if the given object quacks like an action and has a specific
20+
* action type
21+
*
22+
* @param object Object to test
23+
* @param expectedType The expected type for the action.
24+
*
25+
* @return Whether object is an action and is of specific type.
26+
*/
27+
export function isActionOfType(
28+
object: unknown,
29+
expectedType: string
30+
): object is Action {
31+
return isAction( object ) && object.type === expectedType;
32+
}

packages/redux-routine/src/is-generator.js renamed to packages/redux-routine/src/is-generator.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
/* eslint-disable jsdoc/valid-types */
21
/**
32
* Returns true if the given object is a generator, or false otherwise.
43
*
54
* @see https://www.ecma-international.org/ecma-262/6.0/#sec-generator-objects
65
*
7-
* @param {any} object Object to test.
6+
* @param object Object to test.
87
*
9-
* @return {object is Generator} Whether object is a generator.
8+
* @return Whether object is a generator.
109
*/
11-
export default function isGenerator( object ) {
12-
/* eslint-enable jsdoc/valid-types */
10+
export default function isGenerator( object: any ): object is Generator {
1311
// Check that iterator (next) and iterable (Symbol.iterator) interfaces are satisfied.
1412
// These checks seem to be compatible with several generator helpers as well as the native implementation.
1513
return (
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Add type guard for isPlainObject until the below PR gets merged:
3+
*
4+
* @see https://github.com/jonschlinkert/is-plain-object/pull/29
5+
*/
6+
7+
declare module 'is-plain-object' {
8+
export function isPlainObject(
9+
value: unknown
10+
): value is Record< PropertyKey, unknown >;
11+
}

0 commit comments

Comments
 (0)