Skip to content

Commit e1dbf16

Browse files
feat(overmind): make rehydrate plain function
1 parent a03661f commit e1dbf16

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ export const MODE_SSR = Symbol('MODE_SSR')
7979
export const json = (obj: any) =>
8080
deepCopy(obj && obj[IS_PROXY] ? obj[VALUE] : obj)
8181

82+
export const rehydrate = (state: object, mutations: IMutation[]) => {
83+
mutations.forEach((mutation) => {
84+
const pathArray = mutation.path.split('.')
85+
const key = pathArray.pop()
86+
const target = pathArray.reduce((aggr, key) => aggr[key], state)
87+
88+
if (mutation.method === 'set') {
89+
target[key] = mutation.args[0]
90+
} else if (mutation.method === 'unset') {
91+
delete target[key]
92+
} else {
93+
target[key][mutation.method](...mutation.args)
94+
}
95+
})
96+
}
97+
8298
export interface OvermindSSR<Config extends IConfiguration>
8399
extends Overmind<Config> {
84100
hydrate(): IMutation[]
@@ -716,21 +732,6 @@ export class Overmind<ThisConfig extends IConfiguration>
716732
addFlushListener = (cb: IFlushCallback) => {
717733
return this.proxyStateTree.onFlush(cb)
718734
}
719-
rehydrate(state: object, mutations: IMutation[]) {
720-
mutations.forEach((mutation) => {
721-
const pathArray = mutation.path.split('.')
722-
const key = pathArray.pop()
723-
const target = pathArray.reduce((aggr, key) => aggr[key], state)
724-
725-
if (mutation.method === 'set') {
726-
target[key] = mutation.args[0]
727-
} else if (mutation.method === 'unset') {
728-
delete target[key]
729-
} else {
730-
target[key][mutation.method](...mutation.args)
731-
}
732-
})
733-
}
734735
reconfigure(configuration: IConfiguration) {
735736
const mergedConfiguration = {
736737
...configuration,

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { createOvermindSSR, Overmind, IOnInitialize, IConfig } from './'
1+
import {
2+
createOvermindSSR,
3+
Overmind,
4+
IOnInitialize,
5+
IConfig,
6+
rehydrate,
7+
} from './'
28
import { IMutation } from 'proxy-state-tree'
39

410
describe('Mock', () => {
@@ -53,7 +59,7 @@ describe('Mock', () => {
5359
foo: 'bar',
5460
}
5561
const onInitialize: OnInitialize = ({ state }, overmind) => {
56-
overmind.rehydrate(state, mutations)
62+
rehydrate(state, mutations)
5763
}
5864
const config = {
5965
onInitialize,

packages/overmind-website/examples/guide/serversiderendering/renderonclient.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ export default (ts) =>
44
{
55
fileName: 'overmind/onInitialize.ts',
66
code: `
7-
import { OnInitialize } from 'overmind'
7+
import { OnInitialize, rehydrate } from 'overmind'
88
9-
export const onInitialize: OnInitialize = ({ state }, overmind) => {
9+
export const onInitialize: OnInitialize = ({ state }) => {
1010
const mutations = window.__OVERMIND_MUTATIONS
1111
12-
overmind.rehydrate(state, mutations)
12+
rehydrate(state, mutations)
1313
}
1414
`,
1515
},
@@ -18,10 +18,12 @@ export const onInitialize: OnInitialize = ({ state }, overmind) => {
1818
{
1919
fileName: 'overmind/onInitialize.js',
2020
code: `
21-
export const onInitialize = ({ state }, overmind) => {
21+
import { rehydrate } from 'overmind'
22+
23+
export const onInitialize = ({ state }) => {
2224
const mutations = window.__OVERMIND_MUTATIONS
2325
24-
overmind.rehydrate(state, mutations)
26+
rehydrate(state, mutations)
2527
}
2628
`,
2729
},

0 commit comments

Comments
 (0)