Skip to content

Commit 992310c

Browse files
Merge pull request cerebral#144 from cerebral/fixes2
Fixes2
2 parents a232b20 + 69ac209 commit 992310c

File tree

6 files changed

+68
-35
lines changed

6 files changed

+68
-35
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/derived.test.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ type State = {
77

88
describe('Derived', () => {
99
test('should instantiate app with derived state', () => {
10-
const derived: Derive<string> = (state) => state.foo.toUpperCase()
11-
const state = {
10+
type State = {
11+
foo: string
12+
upperFoo: Derive<string, State>
13+
}
14+
const state: State = {
1215
foo: 'bar',
13-
upperFoo: derived,
16+
upperFoo: (state) => state.foo.toUpperCase(),
1417
}
1518
const config = {
1619
state,
@@ -20,7 +23,7 @@ describe('Derived', () => {
2023
state: typeof state
2124
}>
2225

23-
type Derive<Value> = TDerive<IApp, Value>
26+
type Derive<Value, Parent extends object> = TDerive<IApp, Value, Parent>
2427

2528
const app = new Overmind(config)
2629

@@ -30,14 +33,17 @@ describe('Derived', () => {
3033
test('should track derived state', () => {
3134
let renderCount = 0
3235
const changeFoo: Action = ({ state }) => (state.foo = 'bar2')
33-
34-
const derived: Derive<string> = (state) => state.foo.toUpperCase()
36+
type State = {
37+
foo: string
38+
upperFoo: Derive<string, State>
39+
}
40+
const state: State = {
41+
foo: 'bar',
42+
upperFoo: (state) => state.foo.toUpperCase(),
43+
}
3544

3645
const config = {
37-
state: {
38-
foo: 'bar',
39-
upperFoo: derived,
40-
},
46+
state,
4147
actions: {
4248
changeFoo,
4349
},
@@ -50,7 +56,7 @@ describe('Derived', () => {
5056
actions: typeof config.actions
5157
}>
5258
type Action<Input = void> = TAction<IApp, Input>
53-
type Derive<Value> = TDerive<IApp, Value>
59+
type Derive<Value, Parent extends object> = TDerive<IApp, Value, Parent>
5460

5561
const app = new Overmind(config)
5662
function render() {
@@ -67,18 +73,21 @@ describe('Derived', () => {
6773
})
6874
test('should not require flush to flag as dirty', () => {
6975
expect.assertions(1)
76+
type State = {
77+
foo: string
78+
upperFoo: Derive<string, State>
79+
}
80+
const state: State = {
81+
foo: 'bar',
82+
upperFoo: (state) => state.foo.toUpperCase(),
83+
}
7084
const changeFoo: Action = ({ state }) => {
7185
state.foo = 'bar2'
7286
expect(state.upperFoo).toBe('BAR2')
7387
}
7488

75-
const derived: Derive<string> = (state) => state.foo.toUpperCase()
76-
7789
const config = {
78-
state: {
79-
foo: 'bar',
80-
upperFoo: derived,
81-
},
90+
state,
8291
actions: {
8392
changeFoo,
8493
},
@@ -91,7 +100,7 @@ describe('Derived', () => {
91100
actions: typeof config.actions
92101
}>
93102
type Action<Input = void> = TAction<IApp, Input>
94-
type Derive<Value> = TDerive<IApp, Value>
103+
type Derive<Value, Parent extends object> = TDerive<IApp, Value, Parent>
95104

96105
const app = new Overmind(config)
97106

@@ -124,7 +133,7 @@ describe('Derived', () => {
124133
actions: typeof config.actions
125134
}>
126135
type Action<Input = void> = TAction<IApp, Input>
127-
type Derive<Value, Parent = any> = TDerive<IApp, Value, Parent>
136+
type Derive<Value, Parent extends object> = TDerive<IApp, Value, Parent>
128137

129138
const app = new Overmind(config)
130139

packages/node_modules/overmind/src/index.ts

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

3838
export type Context = TContext<App>
3939

40-
export type Derive<Value, Parent = any> = TDerive<App, Value, Parent>
40+
export type Derive<Value, Parent extends object> = TDerive<App, Value, Parent>
4141

4242
export type Reaction = TReaction<App>
4343

@@ -527,6 +527,10 @@ export function pipe(...operators) {
527527
return next(null, operatorContext)
528528

529529
if (operatorContext.value instanceof Promise) {
530+
context.execution.emit(EventType.OPERATOR_ASYNC, {
531+
...context.execution,
532+
isAsync: true,
533+
})
530534
operatorContext.value
531535
.then((promiseValue) =>
532536
run(null, { ...operatorContext, value: promiseValue })
@@ -638,14 +642,19 @@ export function map<Input, Output, BaseContext = Context>(
638642
}
639643

640644
export function run<Input, BaseContext = Context>(
641-
operation: (input: TValueContext<BaseContext, Input>) => void
645+
operation: (input: TValueContext<BaseContext, Input>) => any
642646
): TOperator<Input, Input, BaseContext> {
643647
const instance = (err, context, next) => {
644648
if (err) next(err)
645649
else {
646650
startDebugOperator('run', operation, context)
647-
operation(context)
648-
const newContext = createContext(context, context.value)
651+
const result = operation(context)
652+
const newContext = createContext(
653+
context,
654+
result instanceof Promise
655+
? result.then(() => context.value)
656+
: context.value
657+
)
649658
stopDebugOperator(newContext)
650659
next(null, newContext)
651660
}

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
})

packages/node_modules/overmind/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ export type TOperator<Input, Output, OperatorContext extends TContext<any>> = (
5050
final?: (err, Error, val?: TValueContext<OperatorContext, Output>) => void
5151
) => void
5252

53-
export type TDerive<App extends BaseApp, Value, Parent = any> = (
54-
parent: Parent,
53+
export type TDerive<App extends BaseApp, Value, Parent extends object = {}> = (
54+
parent: ResolveState<Parent>,
5555
state: App['state']
5656
) => Value
5757

packages/overmind-website/api/operators.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ h(Example, { name: "api/operators_operator_pipe" })
7171
```
7272

7373
## run
74-
This operator is useful to run effects. It will just pass the current value a long.
74+
This operator is useful to run effects. It will just pass the current value a long. You may return a promise which will hold further
75+
execution until it is resolved.
7576

7677
```marksy
7778
h(Example, { name: "api/operators_operator_run" })

0 commit comments

Comments
 (0)