Skip to content

Commit 8a14634

Browse files
feat(overmind): allow immediate on reaction and change to nested option
1 parent 52961f7 commit 8a14634

File tree

1 file changed

+22
-7
lines changed
  • packages/node_modules/overmind/src

1 file changed

+22
-7
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,9 @@ export class Overmind<ThisConfig extends IConfiguration>
574574
this.eventHub.emit(EventType.OPERATOR_ASYNC, execution)
575575
result.then(() => {
576576
execution.isRunning = false
577-
mutationTree.dispose()
577+
if (!boundExecution) {
578+
mutationTree.dispose()
579+
}
578580
this.eventHub.emit(EventType.OPERATOR_END, {
579581
...execution,
580582
isAsync: true,
@@ -584,7 +586,9 @@ export class Overmind<ThisConfig extends IConfiguration>
584586
})
585587
} else {
586588
execution.isRunning = false
587-
mutationTree.dispose()
589+
if (!boundExecution) {
590+
mutationTree.dispose()
591+
}
588592
this.eventHub.emit(EventType.OPERATOR_END, {
589593
...execution,
590594
isAsync: false,
@@ -829,20 +833,25 @@ export class Overmind<ThisConfig extends IConfiguration>
829833
reaction = (
830834
stateCallback: (state: ThisConfig['state']) => any,
831835
updateCallback: (state: ThisConfig['state']) => void,
832-
deep: boolean = false
836+
options: {
837+
nested?: boolean
838+
immediate?: boolean
839+
} = {}
833840
) => {
834-
if (deep) {
841+
let disposer
842+
843+
if (options.nested) {
835844
const value = stateCallback(this.state)
836845

837846
if (!value || !value[IS_PROXY]) {
838847
throw new Error(
839-
'You have to return an object or array when using a "deep" reaction'
848+
'You have to return an object or array when using a "nested" reaction'
840849
)
841850
}
842851

843852
const path = value[PATH]
844853

845-
return this.addFlushListener((mutations) => {
854+
disposer = this.addFlushListener((mutations) => {
846855
mutations.forEach((mutation) => {
847856
if (mutation.path.startsWith(path)) {
848857
updateCallback(this.state)
@@ -856,10 +865,16 @@ export class Overmind<ThisConfig extends IConfiguration>
856865
() => updateCallback(tree.state)
857866
)
858867

859-
return () => {
868+
disposer = () => {
860869
tree.dispose()
861870
}
862871
}
872+
873+
if (options.immediate) {
874+
updateCallback(this.state)
875+
}
876+
877+
return disposer
863878
}
864879
addMutationListener = (cb: IMutationCallback) => {
865880
return this.proxyStateTree.onMutation(cb)

0 commit comments

Comments
 (0)