Skip to content

Commit db19f9d

Browse files
feat(overmind): add fork operator
1 parent b7214e6 commit db19f9d

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

packages/node_modules/overmind/src/createActionFactory.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ type OperatorCallback<Context, Value, NewValue = Value> = (
1313
) => NewValue | Promise<NewValue>
1414

1515
interface Operators<State, Context, InitialValue, Value> {
16+
fork<Paths>(
17+
cb: (value: Value, context: Context) => keyof Paths,
18+
paths: Paths
19+
): InitialValue extends void
20+
? NoValueAction<State, Context, InitialValue, Value>
21+
: Action<State, Context, InitialValue, Value>
1622
mutation(
1723
cb: (value: Value, state: State) => any
1824
): InitialValue extends void
@@ -99,6 +105,27 @@ export default function createActionFactory<State, Context>(proxyStateTree) {
99105
runOperators
100106
) as any,
101107
{
108+
fork<Paths>(
109+
cb: (value: Value, context: Context) => keyof Paths,
110+
paths: Paths
111+
) {
112+
const operator = (value, context) => {
113+
const path = cb(value, context)
114+
115+
return (paths[path] as any).map(() => value)(value, context, path)
116+
}
117+
const [
118+
chain,
119+
initialActionId,
120+
runOperators,
121+
] = this.createOperatorResult('fork', cb.name, operator)
122+
123+
return actionFactory<InitialValue, Value>(
124+
chain,
125+
initialActionId,
126+
runOperators
127+
)
128+
},
102129
mutation(cb: (value: Value, state: State) => any) {
103130
const operator = (value, context) => {
104131
proxyStateTree.startMutationTracking()
@@ -116,7 +143,7 @@ export default function createActionFactory<State, Context>(proxyStateTree) {
116143
runOperators,
117144
] = this.createOperatorResult('mutation', cb.name, operator)
118145

119-
return actionFactory<InitialValue>(
146+
return actionFactory<InitialValue, Value>(
120147
chain,
121148
initialActionId,
122149
runOperators

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ describe('Overmind', () => {
4848
})
4949

5050
describe('OPERATORS', () => {
51+
test('fork', () => {
52+
expect.assertions(2)
53+
let calledFoo = false
54+
const app = new App({
55+
state: {},
56+
actions: (action) => {
57+
return {
58+
doThis: action().fork(() => 'foo', {
59+
foo: action()
60+
.do(() => (calledFoo = true))
61+
.map(() => '123'),
62+
bar: action().map(() => 123),
63+
}),
64+
}
65+
},
66+
})
67+
expect(app.actions.doThis()).toBe(undefined)
68+
expect(calledFoo).toBe(true)
69+
})
5170
test('mutate', () => {
5271
const app = new App({
5372
state: {

packages/node_modules/overmind/src/views/react.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ export default class ReactApp<
5050

5151
if (isClassComponent) {
5252
const originalRender = Component.prototype.render
53+
const originalWillUnmount = Component.prototype.componentWillUnmount
5354

55+
Component.prototype.componentWillUnmount = function() {
56+
this.__mutationListener && this.__mutationListener.dispose()
57+
originalWillUnmount && originalWillUnmount.call(this)
58+
}
5459
Component.prototype.render = function() {
5560
const trackId = instance.trackState()
5661
const value = originalRender.call(this)
@@ -88,6 +93,9 @@ export default class ReactApp<
8893
>
8994
> {
9095
__mutationListener: any
96+
componentWillUnmount() {
97+
this.__mutationListener && this.__mutationListener.dispose()
98+
}
9199
renderStatelessComponent() {
92100
const trackId = instance.trackState()
93101
const value = (Component as any)(

0 commit comments

Comments
 (0)