Skip to content

Commit 6e7912c

Browse files
authored
Merge pull request #134 from cerebral/proxyObjectValue
feat(overmind): allow proxies in object value passed to action
2 parents f8b97aa + efa5a20 commit 6e7912c

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Overmind, TAction, TApp, EventType } from './'
1+
import { EventType, Overmind, TAction, TApp } from './'
22
import { modules } from './modules'
33

44
function toJSON(obj) {
@@ -30,12 +30,21 @@ function createDefaultApp() {
3030
const changeValue: Action<{ isAwesome: boolean }> = (context) => {
3131
context.value.isAwesome = !context.value.isAwesome
3232
}
33+
const changeFormValue: Action<{
34+
key: string
35+
form: { [key: string]: any }
36+
value: any
37+
}> = (context) => {
38+
const { form, key, value } = context.value
39+
form[key] = value
40+
}
3341
const actions = {
42+
asyncChangeFoo,
43+
changeFormValue,
3444
changeFoo,
3545
changeFooWithEffect,
36-
waitAndChangeFoo,
37-
asyncChangeFoo,
3846
changeValue,
47+
waitAndChangeFoo,
3948
}
4049
const effects = {
4150
hello() {
@@ -184,7 +193,7 @@ describe('Overmind', () => {
184193
const app = createDefaultApp()
185194
app.eventHub.once(EventType.MUTATIONS, (data) => {
186195
expect(toJSON(data)).toEqual({
187-
actionId: 0,
196+
actionId: 2,
188197
actionName: 'changeFoo',
189198
mutations: [
190199
{
@@ -205,7 +214,7 @@ describe('Overmind', () => {
205214
const app = createDefaultApp()
206215
app.eventHub.on(EventType.MUTATIONS, (data) => {
207216
expect(toJSON(data)).toEqual({
208-
actionId: 2,
217+
actionId: 5,
209218
actionName: 'waitAndChangeFoo',
210219
mutations: [
211220
{
@@ -226,7 +235,7 @@ describe('Overmind', () => {
226235
const app = createDefaultApp()
227236
app.eventHub.on(EventType.MUTATIONS, (data) => {
228237
expect(toJSON(data)).toEqual({
229-
actionId: 3,
238+
actionId: 0,
230239
actionName: 'asyncChangeFoo',
231240
mutations: [
232241
{
@@ -305,4 +314,16 @@ describe('Overmind', () => {
305314
expect(() => app.actions.changeValue(app.state.item)).not.toThrow()
306315
expect(app.state.item.isAwesome).toBe(false)
307316
})
317+
test('should allow mutations on passed values in object', () => {
318+
expect.assertions(2)
319+
const app = createDefaultApp()
320+
expect(() =>
321+
app.actions.changeFormValue({
322+
form: app.state.item,
323+
key: 'isAwesome',
324+
value: false,
325+
})
326+
).not.toThrow()
327+
expect(app.state.item.isAwesome).toBe(false)
328+
})
308329
})

packages/node_modules/overmind/src/index.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { EventEmitter } from 'betsy'
2-
import { ProxyStateTree } from 'proxy-state-tree'
2+
import { IS_PROXY, ProxyStateTree } from 'proxy-state-tree'
3+
34
import { Derived } from './derived'
45
import { Devtools, Message, safeValue } from './Devtools'
56
import {
@@ -9,17 +10,17 @@ import {
910
ResolveActions,
1011
ResolveState,
1112
} from './internalTypes'
13+
import { proxifyEffects } from './proxyfyEffects'
1214
import { Reaction } from './reaction'
1315
import {
1416
BaseApp,
1517
Configuration,
16-
TDerive,
17-
TReaction,
1818
TAction,
1919
TContext,
20+
TDerive,
2021
TOperator,
22+
TReaction,
2123
} from './types'
22-
import { proxifyEffects } from './proxyfyEffects'
2324

2425
export * from './types'
2526

@@ -179,6 +180,24 @@ export class Overmind<Config extends Configuration> implements BaseApp {
179180
this.trackEffects(this.effects, execution)
180181
)
181182
}
183+
private scopeValue(scopedTree: ProxyStateTree, value: any) {
184+
if (!value) {
185+
return value
186+
}
187+
if (value[IS_PROXY]) {
188+
return scopedTree.scope(value)
189+
} else if (isPlainObject(value)) {
190+
return Object.assign(
191+
{},
192+
...Object.keys(value).map((key) => ({
193+
[key]: scopedTree.scope(value[key]),
194+
}))
195+
)
196+
} else {
197+
return value
198+
}
199+
}
200+
182201
private createAction(name, action) {
183202
this.actionReferences.push(action)
184203
return (value?) => {
@@ -212,7 +231,7 @@ export class Overmind<Config extends Configuration> implements BaseApp {
212231
scopedTree.startMutationTracking()
213232
const result = action(
214233
this.createContext(
215-
scopedTree.scope(value),
234+
this.scopeValue(scopedTree, value),
216235
execution,
217236
scopedTree.get()
218237
)

0 commit comments

Comments
 (0)