Skip to content

Commit af8d284

Browse files
committed
fix a bug where method calls of class instances wouldn't synchronise
1 parent 3d09b1b commit af8d284

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/__test__/runCreateFieldsTest.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ export default function createFieldsTest(
3030
}
3131
},
3232
(ipos_for_probing) => {
33+
// get originals of if is proxy
34+
const fieldByProperty = (ipos_for_probing.myField as any).__original ?? ipos_for_probing.myField
35+
const fieldByFunction = (ipos_for_probing.get('myField') as any).__original ?? ipos_for_probing.get('myField')
36+
3337
// lodash equal to compare maps and sets
34-
expect(_.isEqualWith(ipos_for_probing.myField, value, customizer)).toEqual(true)
35-
expect(_.isEqualWith(ipos_for_probing.get('myField'), value, customizer)).toEqual(true)
38+
expect(_.isEqualWith(fieldByProperty, value, customizer)).toEqual(true)
39+
expect(_.isEqualWith(fieldByFunction, value, customizer)).toEqual(true)
3640
}
3741
)
3842
})

src/intercept.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {classes as registeredClassesMap} from './serialize.js'
2+
13
export default function intercept<V>(value: V, key: string, interceptCallback: (key: string, method: string, ...args: any) => void): V {
24
const arrayMutatingMethods = ['copyWithin', 'fill', 'pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift']
35
const objectMutatingMethods: string[] = []
@@ -11,12 +13,18 @@ export default function intercept<V>(value: V, key: string, interceptCallback: (
1113
mutatingMethods.set(Set, setMutatingMethods)
1214
mutatingMethods.set(Function, functionMutatingMethods)
1315

14-
if (!value || typeof value !== 'object' || !mutatingMethods.has(value.constructor))
16+
const registeredClasses = Array.from(registeredClassesMap.values())
17+
18+
if (!value || typeof value !== 'object' || !(mutatingMethods.has(value.constructor) || registeredClasses.includes(value.constructor)))
1519
return value
1620

1721
return new Proxy(value, {
1822
get(target, name: string) {
19-
if (Reflect.has(target, name) && mutatingMethods.get(value.constructor).includes(name)) {
23+
if (name === '__proxy')
24+
return true
25+
if (name === '__original')
26+
return value
27+
if (Reflect.has(target, name) && (mutatingMethods.get(value.constructor)?.includes(name) || registeredClasses.includes(value.constructor))) {
2028
const method = Reflect.get(target, name)
2129
return (...args: any) => {
2230
interceptCallback(key, name, ...args)

src/serialize.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function serialize(value: any): any | void {
3232
return value
3333
} else if (Array.isArray(value)) {
3434
return value.map(v => serialize(v))
35-
} else if (value.constructor === {}.constructor || value.valueOf().constructor === {}.constructor) {
35+
} else if (value.constructor === {}.constructor || value.valueOf()?.constructor === {}.constructor) {
3636
return Object.fromEntries(
3737
Array.from(Object.entries(value))
3838
.map(([key, value]) => [key, serialize(value)])
@@ -59,6 +59,7 @@ export function serialize(value: any): any | void {
5959
} else if (isNativeObject(value)) {
6060
throw new Error(`Could not serialise: \`${value.constructor.name}\`.`)
6161
} else {
62+
value = value.__original ?? value
6263
const serializeMethod = deSerialize.get(value.constructor)?.serialize ?? value.stringify ?? value.serialize
6364
if (!serializeMethod)
6465
throw new Error(

0 commit comments

Comments
 (0)