Skip to content

Commit 8e5edd8

Browse files
fix(overmind): prevent using proxifier on SSR
1 parent b9923b4 commit 8e5edd8

File tree

6 files changed

+128
-64
lines changed

6 files changed

+128
-64
lines changed

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
"trailingComma": "es5",
33
"singleQuote": true,
44
"semi": false,
5+
56
"arrowParens": "always",
67
"overrides": [
8+
{
9+
"files": "packages/node_modules/**/*"
10+
},
711
{
812
"files": "*.md",
913
"options": {

packages/node_modules/overmind/src/index.ts

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
Options,
2626
ResolveActions,
2727
SSRMode,
28-
TestMode
28+
TestMode,
2929
} from './internalTypes'
3030
import {
3131
createContext,
@@ -59,7 +59,7 @@ import {
5959
getChangeMutations,
6060
getFunctionName,
6161
isPromise,
62-
processState
62+
processState,
6363
} from './utils'
6464

6565
export * from './types'
@@ -72,7 +72,9 @@ export { SERIALIZE, rehydrate } from './rehydrate'
7272

7373
export { Statemachine, statemachine } from './statemachine'
7474

75-
export const derived = <S extends object, R extends object, O>(cb: (state: S, rootState: R) => O): O => {
75+
export const derived = <S extends object, R extends object, O>(
76+
cb: (state: S, rootState: R) => O
77+
): O => {
7678
cb[IS_DERIVED_CONSTRUCTOR] = true
7779
return cb as any
7880
}
@@ -85,7 +87,7 @@ export interface Config {}
8587

8688
export interface Context extends IContext<Config> {}
8789

88-
export type RootState = Context["state"]
90+
export type RootState = Context['state']
8991

9092
export interface Action<Value = void, ReturnValue = void>
9193
extends IAction<Config, Value, ReturnValue> {}
@@ -250,12 +252,11 @@ export class Overmind<ThisConfig extends IConfiguration>
250252
this.eventHub = eventHub as EventEmitter<Events>
251253
this.mode = mode
252254

253-
254255
/*
255256
Expose the created actions
256257
*/
257258
this.actions = this.getActions(configuration.actions)
258-
259+
259260
if (mode.mode === MODE_SSR) {
260261
return
261262
}
@@ -333,9 +334,14 @@ export class Overmind<ThisConfig extends IConfiguration>
333334
nextTick = setTimeout(flushTree, 0)
334335
})
335336
} else if (mode.mode === MODE_DEFAULT || mode.mode === MODE_TEST) {
336-
if (process.env.NODE_ENV === 'test' || (this.devtools && options.hotReloading !== false)) {
337+
if (
338+
process.env.NODE_ENV === 'test' ||
339+
(this.devtools && options.hotReloading !== false)
340+
) {
337341
eventHub.on(EventType.MUTATIONS, (execution) => {
338-
this.reydrateMutationsForHotReloading = this.reydrateMutationsForHotReloading.concat(execution.mutations)
342+
this.reydrateMutationsForHotReloading = this.reydrateMutationsForHotReloading.concat(
343+
execution.mutations
344+
)
339345
})
340346
}
341347
eventHub.on(EventType.OPERATOR_ASYNC, (execution) => {
@@ -395,6 +401,7 @@ export class Overmind<ThisConfig extends IConfiguration>
395401
this.getState(configuration) as any,
396402
{
397403
devmode,
404+
ssr: this.mode.mode === MODE_SSR,
398405
delimiter: this.delimiter,
399406
onSetFunction: (tree, path, target, prop, func) => {
400407
if (func[IS_DERIVED_CONSTRUCTOR]) {
@@ -407,23 +414,33 @@ export class Overmind<ThisConfig extends IConfiguration>
407414
let func = target[prop]
408415

409416
if (func[IS_DERIVED]) {
410-
return func(eventHub, tree, proxyStateTree, path.split(this.delimiter))
417+
return func(
418+
eventHub,
419+
tree,
420+
proxyStateTree,
421+
path.split(this.delimiter)
422+
)
411423
}
412424

413425
if (func[IS_DERIVED_CONSTRUCTOR]) {
414426
const derived = new Derived(func) as any
415427
target[prop] = derived
416428

417-
return derived(eventHub, tree, proxyStateTree, path.split(this.delimiter))
429+
return derived(
430+
eventHub,
431+
tree,
432+
proxyStateTree,
433+
path.split(this.delimiter)
434+
)
418435
}
419436

420437
return func
421-
},
438+
},
422439
onGetter: devmode
423440
? (path, value) => {
424441
this.eventHub.emitAsync(EventType.GETTER, {
425442
path,
426-
value
443+
value,
427444
})
428445
}
429446
: undefined,
@@ -542,7 +559,7 @@ export class Overmind<ThisConfig extends IConfiguration>
542559
const execution = this.createExecution(name, action, boundExecution)
543560
this.eventHub.emit(EventType.ACTION_START, {
544561
...execution,
545-
value
562+
value,
546563
})
547564

548565
if (action[IS_OPERATOR]) {
@@ -593,7 +610,7 @@ export class Overmind<ThisConfig extends IConfiguration>
593610
}
594611
this.eventHub.emit(EventType.ACTION_START, {
595612
...execution,
596-
value
613+
value,
597614
})
598615
this.eventHub.emit(EventType.OPERATOR_START, execution)
599616

@@ -703,7 +720,6 @@ export class Overmind<ThisConfig extends IConfiguration>
703720
return actionFunc
704721
}
705722
private trackEffects(effects = {}, execution) {
706-
707723
if (process.env.NODE_ENV === 'production') {
708724
return effects
709725
}
@@ -836,7 +852,9 @@ export class Overmind<ThisConfig extends IConfiguration>
836852
// We want to trigger property access when setting objects and arrays, as any derived set would
837853
// then trigger and update the devtools
838854
data.mutations.forEach((mutation) => {
839-
const value = mutation.path.split(this.delimiter).reduce((aggr, key) => aggr[key], this.proxyStateTree.state)
855+
const value = mutation.path
856+
.split(this.delimiter)
857+
.reduce((aggr, key) => aggr[key], this.proxyStateTree.state)
840858
if (isPlainObject(value)) {
841859
Object.keys(value).forEach((key) => value[key])
842860
} else if (Array.isArray(value)) {
@@ -848,11 +866,13 @@ export class Overmind<ThisConfig extends IConfiguration>
848866
}
849867
})
850868
}
851-
869+
852870
// Access the derived which will trigger calculation and devtools
853871
if (eventType === EventType.DERIVED_DIRTY) {
854-
data.derivedPath
855-
.reduce((aggr, key) => aggr[key], this.proxyStateTree.state)
872+
data.derivedPath.reduce(
873+
(aggr, key) => aggr[key],
874+
this.proxyStateTree.state
875+
)
856876
}
857877
})(EventType[type])
858878
)
@@ -862,17 +882,15 @@ export class Overmind<ThisConfig extends IConfiguration>
862882
data: {
863883
state: this.proxyStateTree.state,
864884
actions: getActionPaths(actions),
865-
delimiter: this.delimiter
885+
delimiter: this.delimiter,
866886
},
867887
})
868888
this.devtools = devtools
869889
}
870890
private getState(configuration: IConfiguration) {
871891
let state = {}
872892
if (configuration.state) {
873-
state = processState(
874-
configuration.state,
875-
)
893+
state = processState(configuration.state)
876894
}
877895

878896
return state
@@ -915,11 +933,8 @@ export class Overmind<ThisConfig extends IConfiguration>
915933

916934
return aggr[key]
917935
}, this.actions)
918-
target[name] = this.createAction(
919-
actionName,
920-
actions[name]
921-
) as any
922-
936+
target[name] = this.createAction(actionName, actions[name]) as any
937+
923938
target[name].displayName = path.concat(name).join('.')
924939
}
925940
} else {
@@ -955,7 +970,11 @@ export class Overmind<ThisConfig extends IConfiguration>
955970
mutations.forEach((mutation) => {
956971
if (mutation.path.startsWith(path)) {
957972
updateCallback(
958-
path ? path.split(this.delimiter).reduce((aggr, key) => aggr[key], this.state) : this.state
973+
path
974+
? path
975+
.split(this.delimiter)
976+
.reduce((aggr, key) => aggr[key], this.state)
977+
: this.state
959978
)
960979
}
961980
})
@@ -993,7 +1012,10 @@ export class Overmind<ThisConfig extends IConfiguration>
9931012
return this.proxyStateTree.onFlush(cb)
9941013
}
9951014
reconfigure(configuration: IConfiguration) {
996-
const changeMutations = getChangeMutations(this.originalConfiguration.state, configuration.state || {})
1015+
const changeMutations = getChangeMutations(
1016+
this.originalConfiguration.state,
1017+
configuration.state || {}
1018+
)
9971019

9981020
this.updateActions(configuration.actions)
9991021
this.effects = configuration.effects || {}

packages/node_modules/proxy-state-tree/src/Proxyfier.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ const arrayMutations = new Set([
2121
const getValue = (proxyOrValue) =>
2222
proxyOrValue && proxyOrValue[IS_PROXY] ? proxyOrValue[VALUE] : proxyOrValue
2323

24-
const isClass = (value) => typeof value === 'object' && value !== null && !Array.isArray(value) && value.constructor.name !== 'Object' && Object.isExtensible(value)
24+
const isClass = (value) =>
25+
typeof value === 'object' &&
26+
value !== null &&
27+
!Array.isArray(value) &&
28+
value.constructor.name !== 'Object' &&
29+
Object.isExtensible(value)
2530

2631
export class Proxifier {
2732
CACHED_PROXY = Symbol('CACHED_PROXY')
@@ -133,7 +138,8 @@ export class Proxifier {
133138
prop === 'length' ||
134139
(typeof target[prop] === 'function' &&
135140
!arrayMutations.has(String(prop))) ||
136-
typeof prop === 'symbol' || target[prop] instanceof Date
141+
typeof prop === 'symbol' ||
142+
target[prop] instanceof Date
137143
) {
138144
return target[prop]
139145
}
@@ -154,9 +160,8 @@ export class Proxifier {
154160

155161
let result
156162

157-
158163
if (process.env.NODE_ENV === 'production') {
159-
result = target[prop](...args)
164+
result = target[prop](...args)
160165
} else {
161166
result = target[prop](
162167
...args.map((arg) =>
@@ -230,11 +235,20 @@ export class Proxifier {
230235
if (prop === VALUE) return object
231236
if (prop === PROXY_TREE) return proxifier.tree
232237

233-
if (typeof prop === 'symbol' || prop in Object.prototype || target[prop] instanceof Date)
238+
if (
239+
typeof prop === 'symbol' ||
240+
prop in Object.prototype ||
241+
target[prop] instanceof Date
242+
)
234243
return target[prop]
235244

236-
237-
const descriptor = Object.getOwnPropertyDescriptor(target, prop) || (Object.getPrototypeOf(target) && Object.getOwnPropertyDescriptor(Object.getPrototypeOf(target), prop))
245+
const descriptor =
246+
Object.getOwnPropertyDescriptor(target, prop) ||
247+
(Object.getPrototypeOf(target) &&
248+
Object.getOwnPropertyDescriptor(
249+
Object.getPrototypeOf(target),
250+
prop
251+
))
238252

239253
if (descriptor && 'get' in descriptor) {
240254
const value = descriptor.get.call(proxy)
@@ -266,7 +280,9 @@ export class Proxifier {
266280
prop
267281
)
268282
}
269-
return isClass(target) ? targetValue : targetValue.call(target, proxifier.tree, nestedPath)
283+
return isClass(target)
284+
? targetValue
285+
: targetValue.call(target, proxifier.tree, nestedPath)
270286
} else {
271287
currentTree.trackPathListeners.forEach((cb) => cb(nestedPath))
272288
trackingTree && trackingTree.proxifier.trackPath(nestedPath)
@@ -295,7 +311,10 @@ export class Proxifier {
295311
const mutationTree = proxifier.getMutationTree()
296312
const existingValue = target[prop]
297313

298-
if (typeof value === 'function' && proxifier.tree.master.options.onSetFunction) {
314+
if (
315+
typeof value === 'function' &&
316+
proxifier.tree.master.options.onSetFunction
317+
) {
299318
value = proxifier.tree.master.options.onSetFunction(
300319
proxifier.getTrackingTree() || proxifier.tree,
301320
nestedPath,
@@ -314,7 +333,7 @@ export class Proxifier {
314333
path: nestedPath,
315334
args: [value],
316335
delimiter: proxifier.delimiter,
317-
hasChangedValue
336+
hasChangedValue,
318337
},
319338
objectChangePath
320339
)
@@ -372,7 +391,7 @@ export class Proxifier {
372391
return this.createArrayProxy(value, path)
373392
} else if (isPlainObject(value) || isClass(value)) {
374393
return this.createObjectProxy(value, path)
375-
}
394+
}
376395
}
377396

378397
return value

0 commit comments

Comments
 (0)