Skip to content

Commit 6f26265

Browse files
feat(overmind): use fromOperator function to make operators an action
BREAKING CHANGE: have to use fromOperator function to create operator actions
1 parent b184505 commit 6f26265

File tree

15 files changed

+535
-503
lines changed

15 files changed

+535
-503
lines changed

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

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

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

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

packages/node_modules/overmind/src/index.ts

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export interface Derive<Parent extends TStateObject, Value>
4646

4747
export interface OnInitialize extends IOnInitialize<Config> {}
4848

49+
const IS_NODE =
50+
typeof module !== 'undefined' && (!this || this.module !== module)
4951
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
5052
const IS_DEVELOPMENT = process.env.NODE_ENV === 'development'
5153
const IS_OPERATOR = Symbol('operator')
@@ -199,7 +201,9 @@ export class Overmind<Config extends Configuration> implements Configuration {
199201
'\n\n - You are not running on localhost. You will have to manually define the devtools option to connect'
200202
}
201203

202-
console.warn(warning)
204+
if (!IS_NODE) {
205+
console.warn(warning)
206+
}
203207
}
204208

205209
if (IS_PRODUCTION) {
@@ -336,24 +340,10 @@ export class Overmind<Config extends Configuration> implements Configuration {
336340
this.eventHub.emit(EventType.ACTION_START, execution)
337341

338342
action[IS_OPERATOR]
339-
? action(
340-
null,
341-
{
342-
value,
343-
state: this.proxyStateTree.state,
344-
actions: this.actions,
345-
execution,
346-
effects: this.trackEffects(this.effects, execution),
347-
},
348-
(err, finalContext) => {
349-
finalContext &&
350-
this.eventHub.emit(EventType.ACTION_END, {
351-
...finalContext.execution,
352-
operatorId: finalContext.execution.operatorId - 1,
353-
})
354-
if (err) reject(err)
355-
else resolve(this.options.testMode && finalContext.execution)
356-
}
343+
? resolve(
344+
action(
345+
this.createContext(value, execution, this.proxyStateTree)
346+
)
357347
)
358348
: resolve(
359349
action(
@@ -617,6 +607,28 @@ export type Operator<Input = void, Output = Input> = IOperator<
617607
Output
618608
>
619609

610+
export function fromOperator<Config, Input>(
611+
operator: IOperator<Config, Input, any>
612+
): IAction<Config, Input> {
613+
const func = (context) => {
614+
return new Promise((resolve, reject) => {
615+
operator(null, context, (err, finalContext) => {
616+
finalContext &&
617+
context.execution.emit(EventType.ACTION_END, {
618+
...finalContext.execution,
619+
operatorId: finalContext.execution.operatorId - 1,
620+
})
621+
if (err) reject(err)
622+
else resolve()
623+
})
624+
})
625+
}
626+
627+
func[IS_OPERATOR] = true
628+
629+
return func
630+
}
631+
620632
export function pipe<Config extends Configuration, A, B>(
621633
aOperator: IOperator<Config, A, B>
622634
): IOperator<Config, A, B>
@@ -676,12 +688,12 @@ export function pipe(...operators) {
676688
operators[operatorIndex++](
677689
runErr,
678690
runContext,
679-
runNexIOperator,
691+
runNextOperator,
680692
finalClearingAsync
681693
)
682694
}
683695

684-
const runNexIOperator = (operatorError, operatorContext) => {
696+
const runNextOperator = (operatorError, operatorContext) => {
685697
clearTimeout(asyncTimeout)
686698
if (operatorError) return next(operatorError)
687699
if (operatorIndex >= operators.length)
@@ -706,7 +718,7 @@ export function pipe(...operators) {
706718
}
707719
}
708720

709-
runNexIOperator(null, context)
721+
runNextOperator(null, context)
710722
}
711723
}
712724
instance[IS_OPERATOR] = true

packages/node_modules/overmind/src/internalTypes.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,6 @@ 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>
181177
: Actions[T] extends NestedActions
182178
? ResolveActions<Actions[T]>
183179
: never
@@ -203,10 +199,6 @@ export type ResolveMockActions<
203199
? [TActionValue<Actions[T]>] extends [void]
204200
? () => Promise<MockResult>
205201
: (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>
210202
: Actions[T] extends NestedMockActions
211203
? ResolveMockActions<Actions[T]>
212204
: never

0 commit comments

Comments
 (0)