-
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.
- Loading branch information
1 parent
652a3c9
commit 0cdf8a4
Showing
9 changed files
with
137 additions
and
182 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { getScope, isPromise, setScope } from './utils'; | ||
import { IScope } from './types'; | ||
|
||
class Scope<TState> implements IScope<TState, unknown> { | ||
public get path() { | ||
return this.properties; | ||
} | ||
|
||
constructor(private properties: string[]) {} | ||
|
||
public transformer<TArgs extends any[]>( | ||
definition: (currentScope: unknown, ...args: TArgs) => unknown, | ||
): (currentState: TState, ...args: TArgs) => TState | Promise<TState> { | ||
return (currentState: any, ...args: any) => { | ||
const s = getScope<TState, any>(currentState, this.properties); | ||
const result = definition(s, ...args); | ||
if (isPromise(result)) { | ||
return result.then(promiseResult => | ||
setScope({ ...currentState }, promiseResult, this.properties), | ||
); | ||
} | ||
return setScope({ ...currentState }, result, this.properties); | ||
}; | ||
} | ||
} | ||
|
||
function scope< | ||
TState, | ||
K1 extends keyof TState, | ||
K2 extends keyof TState[K1], | ||
K3 extends keyof TState[K1][K2], | ||
K4 extends keyof TState[K1][K2][K3], | ||
K5 extends keyof TState[K1][K2][K3][K4] | ||
>( | ||
scope1: K1, | ||
scope2: K2, | ||
scope3: K3, | ||
scope4: K4, | ||
scope5: K5, | ||
): IScope<TState, TState[K1][K2][K3][K4][K5]>; | ||
function scope< | ||
TState, | ||
K1 extends keyof TState, | ||
K2 extends keyof TState[K1], | ||
K3 extends keyof TState[K1][K2], | ||
K4 extends keyof TState[K1][K2][K3] | ||
>( | ||
scope1: K1, | ||
scope2: K2, | ||
scope3: K3, | ||
scope4: K4, | ||
): IScope<TState, TState[K1][K2][K3][K4]>; | ||
function scope< | ||
TState, | ||
K1 extends keyof TState, | ||
K2 extends keyof TState[K1], | ||
K3 extends keyof TState[K1][K2] | ||
>(scope1: K1, scope2: K2, scope3: K3): IScope<TState, TState[K1][K2][K3]>; | ||
function scope<TState, K1 extends keyof TState, K2 extends keyof TState[K1]>( | ||
scope1: K1, | ||
scope2: K2, | ||
): IScope<TState, TState[K1][K2]>; | ||
function scope<TState, K1 extends keyof TState>( | ||
scope1: K1, | ||
): IScope<TState, TState[K1]>; | ||
function scope<TState>(...properties: string[]): any { | ||
return new Scope<TState>(properties); | ||
} | ||
|
||
export { scope }; |
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
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,12 @@ | ||
import { scopedTransformer } from 'staat'; | ||
import { scope } from 'staat'; | ||
import { WelcomeState, AppState } from './types'; | ||
|
||
export const initialState: WelcomeState = {}; | ||
|
||
const transformer = scopedTransformer<AppState>()('welcome'); | ||
const welcomeScope = scope<AppState, 'welcome'>('welcome'); | ||
|
||
export const setName = transformer( | ||
(currentState: WelcomeState, name: string) => { | ||
export const setName = welcomeScope.transformer( | ||
(currentState, name: string) => { | ||
return { ...currentState, name }; | ||
}, | ||
); |
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
Oops, something went wrong.