Skip to content

Commit 0f2aedf

Browse files
fix(overmind): fix typing issue with rehydrate
1 parent e1f09c8 commit 0f2aedf

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

packages/node_modules/overmind-devtools-client/DevtoolBackend.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class DevtoolBackend {
2222

2323
this.devtoolServer.on('connection', this.onConnection)
2424
this.devtoolServer.on('error', reject)
25+
this.devtoolServer.on('close', (reason) => {
26+
console.log("Devtools backend closed", reason)
27+
})
2528
this.devtoolServer.on('listening', resolve)
2629
})
2730
}

packages/node_modules/overmind-devtools-client/src/BackendConnector.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class WebsocketConnector {
3838
})
3939
this.messagesBeforeConnected.length = 0
4040
}
41+
this.socket.onclose = (reason) => {
42+
console.log("Socket closed", reason)
43+
}
4144
})
4245
}
4346
emit(event: string, data?: any) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { rehydrate } from './'
1+
import { SERIALIZE, rehydrate } from './'
22

33
describe('REHYDRATE', () => {
44
test('should allow rehydration', () => {
@@ -35,6 +35,12 @@ describe('REHYDRATE', () => {
3535
expect.assertions(1)
3636
class User {
3737
name = 'Bob'
38+
toJSON() {
39+
return {
40+
[SERIALIZE]: true,
41+
name: this.name
42+
}
43+
}
3844
static fromJSON(json) {
3945
return Object.assign(new User(), json)
4046
}
@@ -71,6 +77,7 @@ describe('REHYDRATE', () => {
7177
test('should allow rehydration of array', () => {
7278
expect.assertions(2)
7379
class User {
80+
[SERIALIZE]
7481
name = 'Bob'
7582
static fromJSON(json) {
7683
return Object.assign(new User(), json)
@@ -98,6 +105,7 @@ describe('REHYDRATE', () => {
98105
test('should allow rehydration of dictionary', () => {
99106
expect.assertions(2)
100107
class User {
108+
[SERIALIZE]
101109
name = 'Bob'
102110
static fromJSON(json) {
103111
return Object.assign(new User(), json)

packages/node_modules/overmind/src/rehydrate.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { IMutation } from 'proxy-state-tree'
22

3-
import { IState } from './types'
4-
53
export function rehydrateState(target: any, source: any, classes: any = {}) {
64
if (!target || !source) {
75
throw new Error(`You have to pass a "target" and "source" object to rehydrate`)
@@ -23,7 +21,7 @@ export function rehydrateState(target: any, source: any, classes: any = {}) {
2321
target[key] = classInstance(source[key])
2422
} else if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
2523
if (!target[key]) target[key] = {}
26-
rehydrateState(target[key] as IState, source[key] as IState, classes[key])
24+
rehydrateState(target[key], source[key], classes[key])
2725
} else {
2826
target[key] = source[key]
2927
}
@@ -42,16 +40,18 @@ export type Serializable = Serialize | {
4240
}
4341
}
4442

45-
export type RehydrateClasses<T extends IState> = Pick<{
43+
type StateNode = { [key: string]: any }
44+
45+
export type RehydrateClasses<T extends StateNode> = Pick<{
4646
[P in keyof T]: T[P] extends Serializable ? (data: any) => T[P] :
4747
T[P] extends Array<Serializable> ? (data: any) => T[P][keyof T[P]] :
4848
T[P] extends { [key: string]: Serializable } ? (data: any) => T[P][keyof T[P]] :
49-
T[P] extends IState ? RehydrateClasses<T[P]> :
49+
T[P] extends StateNode ? RehydrateClasses<T[P]> :
5050
never
5151
},{ [Key in keyof T]: T[Key] extends Serializable | Array<Serializable> | { [key: string]: Serializable } ? Key : never }[keyof T]>
5252

5353

54-
export const rehydrate = <T extends IState>(state: T, source: IMutation[] | IState, classes: RehydrateClasses<T> = {} as any) => {
54+
export const rehydrate = <T extends { [key: string]: any }>(state: T, source: IMutation[] | StateNode, classes: RehydrateClasses<T> = {} as any) => {
5555
if (Array.isArray(source)) {
5656
const mutations = source as IMutation[]
5757
mutations.forEach((mutation) => {

0 commit comments

Comments
 (0)