-
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
Showing
8 changed files
with
153 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { compose } from './compose' | ||
|
||
type Any = number | string | ||
type Arbitrary = { | ||
[key in string]: Any | Any[] | Record<string, Any> | ||
} | ||
|
||
describe(compose.name, () => { | ||
it('should return a thunk', () => { | ||
expect(compose()).toEqual(expect.any(Function)) | ||
}) | ||
|
||
it('should return the original object when there are no tasks', async () => { | ||
const input = {} | ||
expect(await compose()(input)).toBe(input) | ||
}) | ||
|
||
it('should pass all arguments through to the composed tasks', async () => { | ||
const task = jest.fn() | ||
const input = {} | ||
await compose(task)(input, 1, 2, 3) | ||
expect(task).toHaveBeenCalledWith(input, 1, 2, 3) | ||
}) | ||
|
||
it('should handle basic composition of regular functions', async () => { | ||
const reduce = compose<Arbitrary>(() => (draft) => { | ||
draft.a = 1 | ||
}) | ||
expect(await reduce({})).toStrictEqual({ | ||
a: 1, | ||
}) | ||
}) | ||
|
||
it('should handle composition of async functions', async () => { | ||
const reduce = compose<Arbitrary>(async () => { | ||
await delay(100) | ||
return (draft) => { | ||
draft.a = 1 | ||
} | ||
}) | ||
|
||
const runner = reduce({}) | ||
jest.runAllTimers() | ||
expect(await runner).toStrictEqual({ | ||
a: 1, | ||
}) | ||
}) | ||
|
||
it('should always resolve new state in the order of composition', async () => { | ||
const reduce = compose<number[]>( | ||
async () => { | ||
await delay(200) | ||
return (draft) => { | ||
draft.push(1) | ||
} | ||
}, | ||
async () => { | ||
await delay(300) | ||
return (draft) => { | ||
draft.push(2) | ||
} | ||
}, | ||
async () => { | ||
await delay(100) | ||
return (draft) => { | ||
draft.push(3) | ||
} | ||
} | ||
) | ||
|
||
const runner = reduce([]) | ||
jest.runAllTimers() | ||
expect(await runner).toStrictEqual([1, 2, 3]) | ||
}) | ||
}) |
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,26 @@ | ||
import { produce } from 'immer' | ||
|
||
import { AnyArray, ComposeTask } from '~/types' | ||
|
||
import { isFunction } from '~/util' | ||
|
||
export const compose = | ||
<State, Args extends AnyArray = unknown[]>( | ||
...tasks: ComposeTask<State, Args>[] | ||
) => | ||
async (state: State, ...args: Args[]): Promise<State> => { | ||
const recipes = await Promise.all( | ||
tasks.map((task) => task(state, ...args)) | ||
) | ||
|
||
let newState = state | ||
for (const recipe of recipes) { | ||
if (recipe === undefined || !isFunction(recipe)) { | ||
continue | ||
} | ||
|
||
newState = await produce(newState, recipe) | ||
} | ||
|
||
return newState | ||
} |
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 @@ | ||
export { compose } from './compose' |
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 @@ | ||
export { compose } from './compose' |
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,18 @@ | ||
import { Draft } from 'immer' | ||
|
||
export { Draft } from 'immer' | ||
|
||
export type AnyObject = Record<string, unknown> | ||
export type AnyArray = unknown[] | ||
|
||
export type ThunkRecipe<State> = (draft: Draft<State>) => Draft<State> | void | ||
export type ComposeTask<State, Args extends AnyArray = unknown[]> = ( | ||
state: State, | ||
...args: Args[] | ||
) => Promise<ThunkRecipe<State>> | ThunkRecipe<State> | void | ||
|
||
export interface Compose { | ||
<State, Args extends AnyArray = unknown[]>( | ||
...tasks: ComposeTask<State, Args>[] | ||
): Promise<(state: State, ...args: Args) => Promise<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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './is' |
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,30 @@ | ||
import { isFunction } from './is' | ||
|
||
const arrowFn = () => { | ||
/* noop */ | ||
} | ||
const asyncArrowFn = async () => { | ||
/* noop */ | ||
} | ||
function expressionFn() { | ||
/* noop */ | ||
} | ||
async function asyncExpressionFn() { | ||
/* noop */ | ||
} | ||
|
||
describe(isFunction.name, () => { | ||
it.each` | ||
input | expected | ||
${undefined} | ${false} | ||
${null} | ${false} | ||
${1} | ${false} | ||
${{}} | ${false} | ||
${arrowFn} | ${true} | ||
${expressionFn} | ${true} | ||
${asyncArrowFn} | ${true} | ||
${asyncExpressionFn} | ${true} | ||
`('should return $expected for $input', ({ input, expected }) => { | ||
expect(isFunction(input)).toBe(expected) | ||
}) | ||
}) |
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 @@ | ||
export const isFunction = (fn: unknown): boolean => typeof fn === 'function' |