Skip to content

Commit d430a50

Browse files
feat(overmind): allow run operator to run async with returned promise
1 parent b0821c0 commit d430a50

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

packages/node_modules/overmind-devtools/src/app/state.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ type State = {
2020
expandedStatePaths: string[]
2121
expandAllActionDetails: boolean
2222
expandedComponents: string[]
23-
currentAction: Derive<Action>
24-
currentApp: Derive<App>
25-
componentsMounted: Derive<Component[]>
26-
componentsUpdateCount: Derive<number>
27-
componentsStatePathCount: Derive<number>
28-
flushes: Derive<Flush[]>
29-
flushesMutationsCount: Derive<number>
30-
flushesStatePathCount: Derive<number>
31-
groupedComponents: Derive<GroupedComponents>
23+
currentAction: Derive<Action, State>
24+
currentApp: Derive<App, State>
25+
componentsMounted: Derive<Component[], State>
26+
componentsUpdateCount: Derive<number, State>
27+
componentsStatePathCount: Derive<number, State>
28+
flushes: Derive<Flush[], State>
29+
flushesMutationsCount: Derive<number, State>
30+
flushesStatePathCount: Derive<number, State>
31+
groupedComponents: Derive<GroupedComponents, State>
3232
}
3333

3434
const state: State = {

packages/node_modules/overmind/src/index.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ export type Action<Value = void> = TAction<App, Value>
3737

3838
export type Context = TContext<App>
3939

40-
export type Derive<Value, Parent extends object = {}> = TDerive<
41-
App,
42-
Value,
43-
Parent
44-
>
40+
export type Derive<Value, Parent extends object> = TDerive<App, Value, Parent>
4541

4642
export type Reaction = TReaction<App>
4743

@@ -646,16 +642,23 @@ export function map<Input, Output, BaseContext = Context>(
646642
}
647643

648644
export function run<Input, BaseContext = Context>(
649-
operation: (input: TValueContext<BaseContext, Input>) => void
645+
operation: (input: TValueContext<BaseContext, Input>) => any
650646
): TOperator<Input, Input, BaseContext> {
651647
const instance = (err, context, next) => {
652648
if (err) next(err)
653649
else {
654650
startDebugOperator('run', operation, context)
655-
operation(context)
656-
const newContext = createContext(context, context.value)
657-
stopDebugOperator(newContext)
658-
next(null, newContext)
651+
const runNext = () => {
652+
const newContext = createContext(context, context.value)
653+
stopDebugOperator(newContext)
654+
next(null, newContext)
655+
}
656+
const result = operation(context)
657+
if (result instanceof Promise) {
658+
result.then(runNext).catch(next)
659+
} else {
660+
runNext()
661+
}
659662
}
660663
}
661664
instance[IS_PIPE] = true

packages/node_modules/overmind/src/pipe.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,19 @@ describe('PIPE', () => {
180180
expect(value).toEqual('foo')
181181
})
182182
})
183+
test('run (async)', () => {
184+
expect.assertions(2)
185+
let hasRun = false
186+
const test: Operator<string, string> = pipe(
187+
run(({ value }) => Promise.resolve(value + '!!!')),
188+
run(() => (hasRun = true))
189+
)
190+
const action = createMockAction(test)
191+
const actionRun = action('foo')
192+
expect(hasRun).toBe(false)
193+
return actionRun.then((value) => {
194+
expect(value).toEqual('foo')
195+
})
196+
})
183197
})
184198
})

0 commit comments

Comments
 (0)