-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stronger scoped transform type enforcement
- Loading branch information
1 parent
9fb2b4f
commit dce4010
Showing
6 changed files
with
94 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,19 @@ | ||
import { isPromise, getScope, setScope } from './utils'; | ||
import { getScope, isPromise, setScope } from './utils'; | ||
import { IScopedTransformer } from './types'; | ||
|
||
export function scopedTransformer< | ||
TState extends Record<keyof TState, unknown>, | ||
TScope | ||
>(scope: string) { | ||
return function<TArgs extends any[]>( | ||
definition: ( | ||
currentScope: TScope, | ||
...args: TArgs | ||
) => TScope | Promise<TScope>, | ||
) { | ||
return function(currentState: TState, ...args: TArgs) { | ||
const s = getScope<TState, TScope>(currentState, scope); | ||
const result = definition(s, ...args); | ||
if (isPromise(result)) { | ||
return result.then(promiseResult => | ||
setScope({ ...currentState }, promiseResult, scope), | ||
); | ||
} | ||
return setScope({ ...currentState }, result, scope); | ||
export function scopedTransformer<TState>(): IScopedTransformer<TState> { | ||
return function scopedFn(scope: string[]) { | ||
return function(definition: (currentScope: any, ...args: any) => unknown) { | ||
return function(currentState: any, ...args: any) { | ||
const s = getScope<TState, any>(currentState, scope); | ||
const result = definition(s, ...args); | ||
if (isPromise(result)) { | ||
return result.then(promiseResult => | ||
setScope({ ...currentState }, promiseResult, scope), | ||
); | ||
} | ||
return setScope({ ...currentState }, result, scope); | ||
}; | ||
}; | ||
}; | ||
} |
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