Skip to content

Commit 12426bc

Browse files
feat(overmind): allow async mutate operator
1 parent d7c855b commit 12426bc

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

packages/node_modules/overmind-devtools/src/components/ActionOperator/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const ActionOperator: SFC<Props> = ({
2525
const { state, actions } = useOvermind()
2626
const isExpanded = state.expandAllActionDetails || !operator.isCollapsed
2727

28+
console.log(operator.mutations)
2829
return (
2930
<div>
3031
{value === undefined ? null : (

packages/node_modules/overmind/src/index.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export class Overmind<Config extends Configuration> implements Configuration {
176176
operatorId: -1,
177177
path: [],
178178
emit: this.eventHub.emit.bind(this.eventHub),
179+
send: this.devtools ? this.devtools.send.bind(this.devtools) : () => {},
179180
}
180181
}
181182
private createContext(value, execution, tree) {
@@ -742,13 +743,27 @@ export function mutate<Input, Config extends Configuration = TheConfig>(
742743
if (err) next(err)
743744
else {
744745
startDebugOperator('mutate', operation, context)
745-
const mutationsTree = context.proxyStateTree.getMutationTree()
746-
operation({ ...context, state: mutationsTree.state })
747-
const newContext = createContext(context, context.value)
748-
context.execution.emit(EventType.MUTATIONS, {
749-
...newContext.execution,
750-
mutations: mutationsTree.mutations,
746+
const mutationTree = context.proxyStateTree.getMutationTree()
747+
if (!IS_PRODUCTION) {
748+
mutationTree.onMutation((mutation) => {
749+
context.execution.emit(EventType.MUTATIONS, {
750+
...context.execution,
751+
operatorId: context.execution.operatorId + 1,
752+
mutations: [mutation],
753+
})
754+
})
755+
}
756+
const maybePromise: any = operation({
757+
...context,
758+
state: mutationTree.state,
751759
})
760+
const newContext = createContext(
761+
context,
762+
maybePromise instanceof Promise
763+
? maybePromise.then(() => context.value)
764+
: context.value
765+
)
766+
752767
stopDebugOperator(newContext)
753768
next(null, newContext)
754769
}

packages/node_modules/overmind/src/internalTypes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IMutation, ProxyStateTree } from 'proxy-state-tree'
2-
import { Configuration, TAction, TOperator, StateObject } from './types'
2+
import { Configuration, TAction, TOperator, TStateObject } from './types'
33

44
export type SubType<Base, Condition> = Pick<
55
Base,
@@ -115,14 +115,14 @@ export type TBaseContext<Config extends Configuration> = Config['effects'] & {
115115

116116
type Derived = (parent: any, config: any) => any
117117

118-
export type ResolveState<State extends StateObject> = State extends undefined
118+
export type ResolveState<State extends TStateObject> = State extends undefined
119119
? {}
120120
: {
121121
[P in keyof State]: State[P] extends Derived
122122
? ReturnType<State[P]>
123123
: State[P] extends Array<any>
124124
? State[P]
125-
: State[P] extends StateObject
125+
: State[P] extends TStateObject
126126
? ResolveState<State[P]>
127127
: State[P]
128128
}

packages/overmind-website/src/app/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Action, Operator, debounce, pipe } from 'overmind'
2-
import { Page, RouteContext, GuideParams, VideoParams, Guide } from './types'
2+
import { Page, RouteContext, GuideParams, VideoParams } from './types'
33
import * as o from './operators'
44

55
export const openHome: Action<RouteContext> = async ({ state, request }) => {

packages/overmind-website/src/app/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import state from './state'
33
import onInitialize from './onInitialize'
44
import * as actions from './actions'
55
import * as effects from './effects'
6-
import { createHook, createConnect, TConnect } from 'overmind-react'
6+
import { createHook } from 'overmind-react'
77

88
const config = {
99
onInitialize,

packages/overmind-website/src/app/state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type State = {
2525
isLoadingSearchResult: boolean
2626
searchResult: SearchResult[]
2727
showSearchResult: boolean
28+
test: string
2829
}
2930

3031
const state: State = {
@@ -43,6 +44,7 @@ const state: State = {
4344
isLoadingSearchResult: false,
4445
searchResult: [],
4546
showSearchResult: false,
47+
test: '',
4648
}
4749

4850
export default state

0 commit comments

Comments
 (0)