Skip to content

Commit 0370e12

Browse files
feat(overmind): remove fromOperator as it causes typing issues
BREAKING CHANGE: fromOperator is removed, use plain operators
1 parent 44b4724 commit 0370e12

File tree

13 files changed

+133
-175
lines changed

13 files changed

+133
-175
lines changed

packages/node_modules/overmind-devtools/src/overmind/actions.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Action, pipe, OnInitialize, Operator, fromOperator } from 'overmind'
1+
import { Action, pipe, OnInitialize, Operator } from 'overmind'
22
import {
33
Message,
44
Tab,
@@ -69,13 +69,11 @@ const handleClientMessage: Operator<Message, any> = pipe(
6969
})
7070
)
7171

72-
export const onMessage: Action<Message> = fromOperator(
73-
pipe(
74-
isPortExistsMessage({
75-
true: setPortExists,
76-
false: handleClientMessage,
77-
})
78-
)
72+
export const onMessage: Operator<Message> = pipe(
73+
isPortExistsMessage({
74+
true: setPortExists,
75+
false: handleClientMessage,
76+
})
7977
)
8078

8179
export const setError: Action<string> = ({ value: error, state }) =>

packages/node_modules/overmind/src/index.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,24 @@ export class Overmind<Config extends Configuration> implements Configuration {
340340
this.eventHub.emit(EventType.ACTION_START, execution)
341341

342342
action[IS_OPERATOR]
343-
? resolve(
344-
action(
345-
this.createContext(value, execution, this.proxyStateTree)
346-
)
343+
? action(
344+
null,
345+
{
346+
value,
347+
state: this.proxyStateTree.state,
348+
actions: this.actions,
349+
execution,
350+
effects: this.trackEffects(this.effects, execution),
351+
},
352+
(err, finalContext) => {
353+
finalContext &&
354+
this.eventHub.emit(EventType.ACTION_END, {
355+
...finalContext.execution,
356+
operatorId: finalContext.execution.operatorId - 1,
357+
})
358+
if (err) reject(err)
359+
else resolve(this.options.testMode && finalContext.execution)
360+
}
347361
)
348362
: resolve(
349363
action(
@@ -613,28 +627,6 @@ export type Operator<Input = void, Output = Input> = IOperator<
613627
Output
614628
>
615629

616-
export function fromOperator<Config, Input>(
617-
operator: IOperator<Config, Input, any>
618-
): IAction<Config, Input> {
619-
const func = (context) => {
620-
return new Promise((resolve, reject) => {
621-
operator(null, context, (err, finalContext) => {
622-
finalContext &&
623-
context.execution.emit(EventType.ACTION_END, {
624-
...finalContext.execution,
625-
operatorId: finalContext.execution.operatorId - 1,
626-
})
627-
if (err) reject(err)
628-
else resolve()
629-
})
630-
})
631-
}
632-
633-
func[IS_OPERATOR] = true
634-
635-
return func
636-
}
637-
638630
export function pipe<Config extends Configuration, A, B>(
639631
aOperator: IOperator<Config, A, B>
640632
): IOperator<Config, A, B>
@@ -758,14 +750,14 @@ function stopDebugOperator(context, value) {
758750
value.then((promiseValue) => {
759751
context.execution.emit(EventType.OPERATOR_END, {
760752
...context.execution,
761-
result: promiseValue,
753+
result: safeValue(promiseValue),
762754
isAsync: true,
763755
})
764756
})
765757
} else {
766758
context.execution.emit(EventType.OPERATOR_END, {
767759
...context.execution,
768-
result: value,
760+
result: safeValue(value),
769761
isAsync: false,
770762
})
771763
}

packages/node_modules/overmind/src/internalTypes.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ export type ResolveActions<
174174
? [TActionValue<Actions[T]>] extends [void]
175175
? () => Promise<void>
176176
: (value: TActionValue<Actions[T]>) => Promise<void>
177+
: Actions[T] extends IOperator<any, any, any>
178+
? [TOperationValue<Actions[T]>] extends [void]
179+
? () => Promise<void>
180+
: (value: TOperationValue<Actions[T]>) => Promise<void>
177181
: Actions[T] extends NestedActions
178182
? ResolveActions<Actions[T]>
179183
: never
@@ -199,6 +203,10 @@ export type ResolveMockActions<
199203
? [TActionValue<Actions[T]>] extends [void]
200204
? () => Promise<MockResult>
201205
: (value: TActionValue<Actions[T]>) => Promise<MockResult>
206+
: Actions[T] extends IOperator<any, any, any>
207+
? [TOperationValue<Actions[T]>] extends [void]
208+
? () => Promise<MockResult>
209+
: (value: TOperationValue<Actions[T]>) => Promise<MockResult>
202210
: Actions[T] extends NestedMockActions
203211
? ResolveMockActions<Actions[T]>
204212
: never

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@ import {
1212
action,
1313
parallel,
1414
IConfig,
15-
fromOperator,
1615
IAction,
1716
} from './'
1817

19-
describe.only('OPERATORS', () => {
18+
describe('OPERATORS', () => {
2019
test('map', () => {
2120
expect.assertions(1)
22-
const operator: Operator<string> = pipe(
21+
const test: Operator<string> = pipe(
2322
map(({ value }) => value.toUpperCase()),
2423
action(({ value, state }) => (state.foo = value))
2524
)
26-
const test: Action<string> = fromOperator(operator)
2725

2826
const state = {
2927
foo: 'bar',
@@ -51,13 +49,11 @@ describe.only('OPERATORS', () => {
5149

5250
test('map (async)', () => {
5351
expect.assertions(1)
54-
const operator: Operator<string> = pipe(
52+
const test: Operator<string> = pipe(
5553
map(({ value }) => Promise.resolve(value.toUpperCase())),
5654
action(({ value, state }) => (state.foo = value))
5755
)
5856

59-
const test: Action<string> = fromOperator(operator)
60-
6157
const state = {
6258
foo: 'bar',
6359
}
@@ -85,13 +81,12 @@ describe.only('OPERATORS', () => {
8581
test('forEach', () => {
8682
expect.assertions(1)
8783
let runCount = 0
88-
const operator: Operator<string[]> = pipe(
84+
const test: Operator<string[]> = pipe(
8985
forEach((_, val, next) => {
9086
runCount++
9187
next(null, val)
9288
})
9389
)
94-
const test: Action<string[]> = fromOperator(operator)
9590

9691
const config = {
9792
actions: {
@@ -115,7 +110,7 @@ describe.only('OPERATORS', () => {
115110
test('parallel', () => {
116111
expect.assertions(1)
117112
let runCount = 0
118-
const operator: Operator<string> = pipe(
113+
const test: Operator<string> = pipe(
119114
parallel(
120115
(_, value, next) => {
121116
runCount++
@@ -127,7 +122,6 @@ describe.only('OPERATORS', () => {
127122
}
128123
)
129124
)
130-
const test: Action<string> = fromOperator(operator)
131125

132126
const config = {
133127
actions: {
@@ -150,12 +144,11 @@ describe.only('OPERATORS', () => {
150144

151145
test('filter - truthy', () => {
152146
expect.assertions(1)
153-
const operator: Operator<string> = pipe(
147+
const test: Operator<string> = pipe(
154148
filter(({ value }) => value === 'foo'),
155149
map(({ value }) => value.toUpperCase()),
156150
action(({ value, state }) => (state.foo = value))
157151
)
158-
const test: Action<string> = fromOperator(operator)
159152

160153
const state = {
161154
foo: 'bar',
@@ -181,12 +174,11 @@ describe.only('OPERATORS', () => {
181174
})
182175

183176
test('filter - falsy', () => {
184-
const operator: Operator<string> = pipe(
177+
const test: Operator<string> = pipe(
185178
filter(({ value }) => value === 'bar'),
186179
map(({ value }) => value.toUpperCase()),
187180
action(({ value, state }) => (state.foo = value))
188181
)
189-
const test: Action<string> = fromOperator(operator)
190182

191183
const state = {
192184
foo: 'bar',
@@ -213,15 +205,14 @@ describe.only('OPERATORS', () => {
213205

214206
test('fork', () => {
215207
expect.assertions(1)
216-
const operator: Operator<string> = pipe(
208+
const test: Operator<string> = pipe(
217209
fork(() => 'foo', {
218210
foo: pipe(
219211
map(({ value }) => value.toUpperCase()),
220212
action(({ value, state }) => (state.foo = value))
221213
),
222214
})
223215
)
224-
const test: Action<string> = fromOperator(operator)
225216

226217
const state = {
227218
foo: 'bar',
@@ -248,7 +239,7 @@ describe.only('OPERATORS', () => {
248239

249240
test('when', () => {
250241
expect.assertions(1)
251-
const operator: Operator<string, string | number> = pipe(
242+
const test: Operator<string, string | number> = pipe(
252243
when(() => true, {
253244
true: pipe(
254245
map(({ value }) => value.toUpperCase()),
@@ -260,7 +251,6 @@ describe.only('OPERATORS', () => {
260251
),
261252
})
262253
)
263-
const test: Action<string> = fromOperator(operator)
264254

265255
const state = {
266256
foo: 'bar',
@@ -289,8 +279,7 @@ describe.only('OPERATORS', () => {
289279
test('wait', () => {
290280
expect.assertions(1)
291281
const runTime = Date.now()
292-
const operator: Operator = wait(500)
293-
const test: Action = fromOperator(operator)
282+
const test: Operator = wait(500)
294283

295284
const config = {
296285
actions: {
@@ -313,11 +302,10 @@ describe.only('OPERATORS', () => {
313302

314303
test('debounce', () => {
315304
expect.assertions(1)
316-
const operator: Operator = pipe(
305+
const test: Operator = pipe(
317306
debounce(100),
318307
action(({ state }) => state.runCount++)
319308
)
320-
const test: Action = fromOperator(operator)
321309
const state = {
322310
runCount: 0,
323311
}

packages/overmind-website/examples/api/operators.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ export default (ts) =>
55
code: `
66
import { Operator, action } from 'overmind'
77
8-
export const changeFoo: Action = fromOperator(
9-
action(({ state }) => {
10-
state.foo = 'bar'
11-
})
12-
)
8+
export const changeFoo: Operator = action(({ state }) => {
9+
state.foo = 'bar'
10+
})
1311
`,
1412
},
1513
]

packages/overmind-website/examples/api/operators_pipe.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,38 @@ export default (ts) =>
33
? [
44
{
55
code: `
6-
import { Action, Operator, fromOperator, pipe, debounce } from 'overmind'
6+
import { Action, Operator, pipe, debounce } from 'overmind'
77
import { QueryResult } from './state'
88
import {
99
setQuery,
1010
filterValidQuery,
1111
queryResult
1212
} from './operators'
1313
14-
export const search: Action<string> = fromOperator(
15-
pipe(
16-
setQuery,
17-
filterValidQuery,
18-
debounce(200),
19-
queryResult
20-
)
14+
export const search: Action<string> = pipe(
15+
setQuery,
16+
filterValidQuery,
17+
debounce(200),
18+
queryResult
2119
)
2220
`,
2321
},
2422
]
2523
: [
2624
{
2725
code: `
28-
import { fromOperator, pipe, debounce } from 'overmind'
26+
import { pipe, debounce } from 'overmind'
2927
import {
3028
setQuery,
3129
filterValidQuery,
3230
queryResult
3331
} from './operators'
3432
35-
export const search = fromOperator(
36-
pipe(
37-
setQuery,
38-
filterValidQuery,
39-
debounce(200),
40-
queryResult
41-
)
33+
export const search = pipe(
34+
setQuery,
35+
filterValidQuery,
36+
debounce(200),
37+
queryResult
4238
)
4339
`,
4440
},

packages/overmind-website/examples/guide/goingfunctional/actionoperator.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,31 @@ export default (ts) =>
44
{
55
fileName: 'overmind/actions.ts',
66
code: `
7-
import { Action, fromOperator, action } from 'overmind'
7+
import { Action, Operator, action } from 'overmind'
88
99
export const plainAction: Action = ({ value, state }) => {
1010
1111
}
1212
13-
export const functionlAction: Action = fromOperator(
14-
action(({ value, state }) => {
13+
export const functionlAction: Operator = action(({ value, state }) => {
1514
16-
})
17-
)
15+
})
1816
`,
1917
},
2018
]
2119
: [
2220
{
2321
fileName: 'overmind/actions.js',
2422
code: `
25-
import { fromOperator, action } from 'overmind'
23+
import { action } from 'overmind'
2624
2725
export const plainAction = ({ value, state }) => {
2826
2927
}
3028
31-
export const functionlAction = fromOperator(
32-
action(({ value, state }) => {
29+
export const functionlAction = action(({ value, state }) => {
3330
34-
})
35-
)
31+
})
3632
`,
3733
},
3834
]

0 commit comments

Comments
 (0)