This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from piotr-oles/dev
0.5.2 - Change command definition + Redux external fix
- Loading branch information
Showing
20 changed files
with
148 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import { Dispatch, Action } from 'redux'; | ||
|
||
/** | ||
* Dispatch method that adds promise field for commands dispatches results. | ||
* It allows executors to synchronize dispatches. | ||
*/ | ||
export interface ExecutableDispatch<S> extends Dispatch<S> { | ||
<A extends Action>(action: A): A & { promise?: Promise<void> }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
import { Command } from "./Command"; | ||
import { ExecutableDispatch } from "./ExecutableDispatch"; | ||
import { Action } from 'redux'; | ||
import { ExecutableDispatch } from './ExecutableDispatch'; | ||
|
||
export type Executor<S> = <C extends Command>(command: C, dispatch: ExecutableDispatch<S>, state: S) => Promise<void> | void; | ||
export type NarrowExecutor<S, C extends Command> = (command: C, dispatch: ExecutableDispatch<S>, state: S) => Promise<void> | void; | ||
/** | ||
* Executor is an simple function that executes some side effects on given command. | ||
* They can return promise if side effect is asynchronous. | ||
*/ | ||
export type Executor<S> = <A extends Action>(command: A, dispatch: ExecutableDispatch<S>, state: S) => Promise<void> | void; | ||
|
||
/** | ||
* It's executor limited to given action (command) type. | ||
*/ | ||
export type NarrowExecutor<S, A extends Action> = (command: A, dispatch: ExecutableDispatch<S>, state: S) => Promise<void> | void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,27 @@ | ||
|
||
import { Action } from 'redux'; | ||
import { Executor, NarrowExecutor } from './Executor'; | ||
import { Command } from './Command'; | ||
import { ExecutableDispatch } from './ExecutableDispatch'; | ||
|
||
export function handleCommand<S, C extends Command>(type: C['type'], executor: NarrowExecutor<S, C>): Executor<S> { | ||
return function wideExecutor(command: Command, dispatch: ExecutableDispatch<S>, state: S): Promise<void> | void { | ||
/** | ||
* Wraps executor to handle only one type of command. | ||
* | ||
* @param type Type which our target commands should have. | ||
* @param executor Wrapped executor | ||
* @returns Executor that runs wrapped executor only for commands with given type. | ||
*/ | ||
export function handleCommand<S, A extends Action>(type: string, executor: NarrowExecutor<S, A>): Executor<S> { | ||
if (!type || type.length < 3 || ')' !== type[type.length - 1] || '(' !== type[type.length - 2]) { | ||
throw new Error(`Expected type to be valid command type with '()' ending. Given '${type}' type. Maybe typo?`); | ||
} | ||
|
||
if (typeof executor !== 'function') { | ||
throw new Error('Expected the executor to be a function.'); | ||
} | ||
|
||
return function wideExecutor(command: Action, dispatch: ExecutableDispatch<S>, state: S): Promise<void> | void { | ||
if (command && command.type === type) { | ||
return executor(command as C, dispatch, state); | ||
return executor(command as A, dispatch, state); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,15 @@ | ||
|
||
export function isCommand(action: any): boolean { | ||
return !!(action && action.type && true === action.command); | ||
/** | ||
* Checks if given object is command (action with type that ends with () substring). | ||
* | ||
* @param object Object to check | ||
*/ | ||
export function isCommand(object: any): boolean { | ||
return !!( | ||
object && | ||
object.type && | ||
object.type.length >= 3 && | ||
')' === object.type[object.type.length - 1] && // last char is ) | ||
'(' === object.type[object.type.length - 2] // and before it there is ( char | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,24 @@ | ||
import { Action } from 'redux'; | ||
import { Executor } from './Executor'; | ||
import { Command } from './Command'; | ||
import { ExecutableDispatch } from './ExecutableDispatch'; | ||
|
||
/** | ||
* Mount executor to operate on some substate. | ||
* | ||
* @param selector Selector to map state to substate | ||
* @param executor Executor that runs on substate | ||
* @returns Executor that runs on state | ||
*/ | ||
export function mountExecutor<S1, S2>(selector: (state: S1) => S2, executor: Executor<S2>): Executor<S1> { | ||
return function mountedExecutor<C extends Command>(command: C, dispatch: ExecutableDispatch<S1>, state: S1): Promise<void> | void { | ||
if (typeof selector !== 'function') { | ||
throw new Error('Expected the selector to be a function.'); | ||
} | ||
|
||
if (typeof executor !== 'function') { | ||
throw new Error('Expected the executor to be a function.'); | ||
} | ||
|
||
return function mountedExecutor<A extends Action>(command: A, dispatch: ExecutableDispatch<S1>, state: S1): Promise<void> | void { | ||
return executor(command, dispatch, selector(state)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters