Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redux routine: Add types #21313

Merged
merged 6 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
"is-promise": "^4.0.0",
"lodash": "^4.17.21",
"memize": "^1.1.0",
"redux": "^4.1.0",
"turbo-combine-reducers": "^1.0.2",
"use-memo-one": "^1.1.1"
},
"peerDependencies": {
"redux": "^4.1.0"
},
"publishConfig": {
"access": "public"
}
Expand Down
3 changes: 1 addition & 2 deletions packages/data/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
{ "path": "../element" },
{ "path": "../is-shallow-equal" },
{ "path": "../priority-queue" },
// Needs to be added once it is typed
// { "path": "../redux-routine" }
{ "path": "../redux-routine" }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Love it

],
"include": [
"src/redux-store/metadata/actions.js"
Expand Down
4 changes: 4 additions & 0 deletions packages/redux-routine/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Features

- Added and published TypeScript annotations

## 4.1.0 (2021-05-20)

## 4.0.0 (2021-05-14)
Expand Down
4 changes: 2 additions & 2 deletions packages/redux-routine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ execution is not continued.

_Parameters_

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

_Returns_

- `Function`: Co-routine runtime
- `import('redux').Middleware`: Co-routine runtime


<!-- END TOKEN(Autogenerated API docs) -->
Expand Down
2 changes: 2 additions & 0 deletions packages/redux-routine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"types": "build-types",
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.13.10",
"is-promise": "^4.0.0",
"lodash": "^4.17.21",
"redux": "^4.1.0",
sarayourfriend marked this conversation as resolved.
Show resolved Hide resolved
"rungen": "^0.3.2"
},
"publishConfig": {
Expand Down
4 changes: 2 additions & 2 deletions packages/redux-routine/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import createRuntime from './runtime';
* value of the yield assignment. If the control handler returns undefined, the
* execution is not continued.
*
* @param {Object} controls Object of control handlers.
* @param {Record<string, (value: import('redux').AnyAction) => Promise<boolean> | boolean>} controls Object of control handlers.
*
* @return {Function} Co-routine runtime
* @return {import('redux').Middleware} Co-routine runtime
*/
export default function createMiddleware( controls = {} ) {
return ( store ) => {
Expand Down
12 changes: 7 additions & 5 deletions packages/redux-routine/src/is-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
*/
import { isPlainObject, isString } from 'lodash';

/* eslint-disable jsdoc/valid-types */
/**
* Returns true if the given object quacks like an action.
*
* @param {*} object Object to test
* @param {any} object Object to test
ciampo marked this conversation as resolved.
Show resolved Hide resolved
*
* @return {boolean} Whether object is an action.
* @return {object is import('redux').AnyAction} Whether object is an action.
*/
export function isAction( object ) {
return isPlainObject( object ) && isString( object.type );
Expand All @@ -18,11 +19,12 @@ export function isAction( object ) {
* Returns true if the given object quacks like an action and has a specific
* action type
*
* @param {*} object Object to test
* @param {string} expectedType The expected type for the action.
* @param {unknown} object Object to test
* @param {string} expectedType The expected type for the action.
*
* @return {boolean} Whether object is an action and is of specific type.
* @return {object is import('redux').AnyAction} Whether object is an action and is of specific type.
*/
export function isActionOfType( object, expectedType ) {
/* eslint-enable jsdoc/valid-types */
return isAction( object ) && object.type === expectedType;
}
6 changes: 4 additions & 2 deletions packages/redux-routine/src/is-generator.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/* eslint-disable jsdoc/valid-types */
/**
* Returns true if the given object is a generator, or false otherwise.
*
* @see https://www.ecma-international.org/ecma-262/6.0/#sec-generator-objects
*
* @param {*} object Object to test.
* @param {any} object Object to test.
ciampo marked this conversation as resolved.
Show resolved Hide resolved
*
* @return {boolean} Whether object is a generator.
* @return {object is Generator} Whether object is a generator.
*/
export default function isGenerator( object ) {
/* eslint-enable jsdoc/valid-types */
// Check that iterator (next) and iterable (Symbol.iterator) interfaces are satisfied.
// These checks seem to be compatible with several generator helpers as well as the native implementation.
return (
Expand Down
17 changes: 17 additions & 0 deletions packages/redux-routine/src/rungen.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
declare module 'rungen' {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youknowriad Would you be interested in publishing types for rungen? We could include the types in the package without requiring any code changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever works best for you. I'm also happy to add collaborators on that package, I didn't touch it for some time now 😬

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Providing the types from the library itself seems ideal to me, although we don't need to wait for it here.

type Control = (
value: any,
next: any,
iterate: any,
yieldNext: ( result: boolean ) => void,
yieldError: ( err: Error ) => void
) => Promise< boolean > | boolean;

function create(
...args: any[]
): (
action: any,
successCallback: ( result: any ) => void,
errorCallaback: () => void
) => void;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/**
* External dependencies
*/
import { create } from 'rungen';
import { create, Control } from 'rungen';
import { map } from 'lodash';
import isPromise from 'is-promise';
import type { Dispatch, AnyAction } from 'redux';

/**
* Internal dependencies
Expand All @@ -13,15 +14,19 @@ import { isActionOfType, isAction } from './is-action';
/**
* Create a co-routine runtime.
*
* @param {Object} controls Object of control handlers.
* @param {Function} dispatch Unhandled action dispatch.
*
* @return {Function} co-routine runtime
* @param controls Object of control handlers.
* @param dispatch Unhandled action dispatch.
*/
export default function createRuntime( controls = {}, dispatch ) {
export default function createRuntime(
controls: Record<
string,
( value: any ) => Promise< boolean > | boolean
> = {},
dispatch: Dispatch
) {
const rungenControls = map(
controls,
( control, actionType ) => (
( control, actionType ): Control => (
value,
next,
iterate,
Expand All @@ -42,7 +47,10 @@ export default function createRuntime( controls = {}, dispatch ) {
}
);

const unhandledActionControl = ( value, next ) => {
const unhandledActionControl = (
value: AnyAction | unknown,
next: () => void
) => {
if ( ! isAction( value ) ) {
return false;
}
Expand All @@ -54,7 +62,7 @@ export default function createRuntime( controls = {}, dispatch ) {

const rungenRuntime = create( rungenControls );

return ( action ) =>
return ( action: AnyAction | Generator ) =>
new Promise( ( resolve, reject ) =>
rungenRuntime(
action,
Expand Down
9 changes: 9 additions & 0 deletions packages/redux-routine/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types",
"noUnusedParameters": false
},
"include": [ "src/**/*" ]
}
sarayourfriend marked this conversation as resolved.
Show resolved Hide resolved