Skip to content

Commit bd0b9c8

Browse files
fix(overmind): new approach to serializing using symbol
1 parent e220a6b commit bd0b9c8

File tree

7 files changed

+38
-20
lines changed

7 files changed

+38
-20
lines changed

packages/node_modules/overmind/src/Devtools.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IS_JSON } from './utils'
1+
import { SERIALIZE } from './utils'
22

33
export type Message = {
44
type: string
@@ -82,7 +82,7 @@ export class Devtools {
8282
return value
8383
}
8484

85-
if (value && value[IS_JSON] && value.constructor.name !== 'Object') {
85+
if (value && value[SERIALIZE]) {
8686
return {
8787
__CLASS__: true,
8888
name: value.constructor.name,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { EventType, IAction, Overmind, createOvermindMock, rehydrate } from './'
1+
import { EventType, IAction, Overmind, SERIALIZE, createOvermindMock, rehydrate } from './'
22
import { namespaced } from './config'
33

44
function toJSON(obj) {
55
return JSON.parse(JSON.stringify(obj))
66
}
77

88
class StateValue {
9+
[SERIALIZE]
910
value = 'foo'
1011
toJSON() {
1112
return {
@@ -57,7 +58,7 @@ function createDefaultOvermind() {
5758
}
5859
const rehydrateAction: Action<any> = ({ state }, newState) => {
5960
rehydrate(state, newState, {
60-
value: (json) => StateValue.fromJSON(json)
61+
value: StateValue.fromJSON
6162
})
6263
}
6364
const actions = {

packages/node_modules/overmind/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
ResolveActions,
2626
ResolveState,
2727
SSRMode,
28+
Serializable,
2829
TestMode
2930
} from './internalTypes'
3031
import {
@@ -66,9 +67,9 @@ import {
6667

6768
export * from './types'
6869

69-
export { createOperator, createMutationOperator, ResolveState, ResolveActions }
70+
export { createOperator, createMutationOperator, ResolveState, ResolveActions, Serializable, }
7071

71-
export { MODE_DEFAULT, MODE_TEST, MODE_SSR } from './utils'
72+
export { MODE_DEFAULT, MODE_TEST, MODE_SSR, SERIALIZE } from './utils'
7273

7374
/** This type can be overwriten by app developers if they want to avoid
7475
* typing and then they can import `Action`, `Operation` etc. directly from

packages/node_modules/overmind/src/internalTypes.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from 'proxy-state-tree'
77

88
import { IAction, IOperator, IState } from './types'
9+
import { SERIALIZE } from './utils'
910

1011
export type SubType<Base, Condition> = Pick<
1112
Base,
@@ -16,13 +17,17 @@ export type NestedPartial<T> = T extends Function
1617
? T
1718
: Partial<{ [P in keyof T]: NestedPartial<T[P]> }>
1819

20+
export interface Serializable {
21+
[SERIALIZE]: boolean
22+
}
23+
1924
export type RehydrateClasses<T extends IState> = Pick<{
20-
[P in keyof T]: T[P] extends { toJSON: () => infer U } ? (data: U) => T[P] :
21-
T[P] extends Array<{ toJSON: () => infer U }> ? (data: U) => T[P][keyof T[P]] :
22-
T[P] extends { [key: string]: { toJSON: () => infer U } } ? (data: U) => T[P][keyof T[P]] :
25+
[P in keyof T]: T[P] extends Serializable ? (data: any) => T[P] :
26+
T[P] extends Array<Serializable> ? (data: any) => T[P][keyof T[P]] :
27+
T[P] extends { [key: string]: Serializable } ? (data: any) => T[P][keyof T[P]] :
2328
T[P] extends IState ? RehydrateClasses<T[P]> :
2429
never
25-
},{ [Key in keyof T]: T[Key] extends { toJSON: () => any } | Array<{ toJSON: () => any }> | { [key: string]: { toJSON: () => any } } ? Key : never }[keyof T]>
30+
},{ [Key in keyof T]: T[Key] extends Serializable | Array<Serializable> | { [key: string]: Serializable } ? Key : never }[keyof T]>
2631

2732
export type Options = {
2833
name?: string

packages/node_modules/overmind/src/utils.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const EXECUTION = Symbol('execution')
1313
export const MODE_DEFAULT = Symbol('MODE_DEFAULT')
1414
export const MODE_TEST = Symbol('MODE_TEST')
1515
export const MODE_SSR = Symbol('MODE_SSR')
16-
export const IS_JSON = Symbol('IS_JSON')
16+
export const SERIALIZE = Symbol('SERIALIZE')
1717

1818
export class MockedEventEmitter {
1919
emit() {}
@@ -23,10 +23,8 @@ export class MockedEventEmitter {
2323
addListener() {}
2424
}
2525

26-
export const json = (obj: any) => {
27-
const result = deepCopy(obj && obj[IS_PROXY] ? obj[VALUE] : obj)
28-
result[IS_JSON] = obj
29-
return result
26+
export const json = <T>(obj: T): T => {
27+
return deepCopy(obj && obj[IS_PROXY] ? obj[VALUE] : obj)
3028
}
3129

3230

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
filter,
77
mutate,
88
pipe,
9+
rehydrate,
910
} from 'overmind'
1011

1112
import { GuideParams, Page, RouteContext, VideoParams } from './types'
@@ -123,3 +124,15 @@ export const changeQuery: Operator<string> = pipe(
123124
export const viewHelpGotIt: Action = ({ state }) => {
124125
state.showViewHelp = false
125126
}
127+
128+
export const test: Action = ({ state }) => {
129+
rehydrate(
130+
state,
131+
{
132+
user: {
133+
name: 'Bob',
134+
},
135+
},
136+
{}
137+
)
138+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Derive } from 'overmind'
1+
import { Derive, SERIALIZE } from 'overmind'
22

33
import {
44
Api,
@@ -37,11 +37,11 @@ type State = {
3737
}
3838

3939
class User {
40+
[SERIALIZE]
4041
name = 'Bob'
41-
toJSON() {
42-
return {
43-
name: 'ARNE',
44-
}
42+
toJSON() {}
43+
static fromJSON(json) {
44+
return Object.assign(new User(), json)
4545
}
4646
}
4747

0 commit comments

Comments
 (0)