Skip to content

Commit 8245226

Browse files
committed
make the example work fully
1 parent ed728c6 commit 8245226

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

example/main-process.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,3 @@ const subProcess = child_process.spawn('node', ['sub-process.js'], {
1212
await sharedObject.addProcess(subProcess)
1313
sharedObject.create('exampleObject', {})
1414
sharedObject.exampleObject.from = 'the other side'
15-
16-
console.log(sharedObject.exampleArray, sharedObject.exampleObject)

example/sub-process.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import IPOS from '../lib/main.js'
22

33
const sharedObject = await IPOS.new()
4-
console.log('sharedObject.exampleArray', sharedObject.exampleArray)
4+
// wait for the main process to set the values for "exampleObject"
55
setTimeout(() =>
6-
console.log(sharedObject.exampleArray[0], /*...*/sharedObject.exampleObject/*.entries()*/),
6+
console.log(sharedObject.exampleArray[0], ...Object.entries(sharedObject.exampleObject)[0]),
77
10)

src/intercept.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function intercept(value: object, interceptCallback: (object: object, method: string, ...args: any) => void): object {
1+
export default function intercept(value: object, key: string, interceptCallback: (key: string, method: string, ...args: any) => void): object {
22
const arrayMutatingMethods = ['copyWithin', 'fill', 'pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift']
33
const objectMutatingMethods: string[] = []
44
const mapMutatingMethods = ['clear', 'delete', 'set']
@@ -18,12 +18,18 @@ export default function intercept(value: object, interceptCallback: (object: obj
1818
if (Reflect.has(target, name) && mutatingMethods.get(value.constructor).includes(name)) {
1919
const method = Reflect.get(target, name)
2020
return (...args: any) => {
21-
interceptCallback(value, name, ...args)
21+
interceptCallback(key, name, ...args)
2222
method.call(value, ...args)
2323
}
2424
} else {
2525
return Reflect.get(target, name)
2626
}
27-
}
27+
},
28+
set(target, name: string, value: any): boolean {
29+
// @ts-ignore
30+
const result = (target[name] = value)
31+
interceptCallback(key, '$$iposDefine', name, value)
32+
return !!result
33+
},
2834
})
2935
}

src/main.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import IPOSMessaging, {iposMessagingMessage, iposMessagingType} from './messagin
44
import intercept from './intercept.js'
55

66
export default class IPOS {
7-
private readonly fields: Map<string, object>
8-
private fieldsReverseMap: Map<object, string>
7+
private readonly fields: Map<string, ProxyHandler<any>>
8+
private readonly fieldsRaw: Map<string, any>
9+
private fieldsReverseMap: Map<ProxyHandler<any>, string>
910
private processMessagingMap: Map<ChildProcess, IPOSMessaging>
1011
private readonly proxy
1112
protected messaging?: IPOSMessaging
@@ -24,6 +25,7 @@ export default class IPOS {
2425

2526
constructor() {
2627
this.fields = new Map()
28+
this.fieldsRaw = new Map()
2729
this.fieldsReverseMap = new Map()
2830
this.processMessagingMap = new Map()
2931

@@ -72,16 +74,21 @@ export default class IPOS {
7274
return this.fields.get(key)
7375
}
7476

77+
private getRaw(key: string): any {
78+
return this.fields.get(key)
79+
}
80+
7581
/******************** CREATE ********************/
7682
public create(key: string, value: any): void {
7783
this.createStealthy(key, value)
7884
this.sendToAll('set', {key, value})
7985
}
8086

8187
protected createStealthy(key: string, value: object): void {
88+
this.fieldsRaw.set(key, value)
8289
if (typeof value === 'object')
83-
value = intercept(value, (object, method, ...args) =>
84-
this.sendMethodCall(object, method, ...args)
90+
value = intercept(value, key, (key, method, ...args) =>
91+
this.sendMethodCall(key, method, ...args)
8592
)
8693

8794
this.fields.set(key, value)
@@ -96,13 +103,18 @@ export default class IPOS {
96103
/******************** UPDATE ********************/
97104
protected performUpdate(message: iposMessagingMessage) {
98105
if (!message.do || !message.on) return
99-
this.get(message.on)[message.do](...(message.with ?? []))
106+
if (message.do === '$$iposDefine') {
107+
if (!message.with) return
108+
this.fieldsRaw.get(message.on)[message.with[0]] = message.with[1]
109+
} else {
110+
this.fieldsRaw.get(message.on)[message.do](...(message.with ?? []))
111+
}
100112
}
101113

102-
private sendMethodCall(object: object, method: string, ...args: any) {
114+
private sendMethodCall(key: string, method: string, ...args: any) {
103115
this.sendToAll('update', {
104116
do: method,
105-
on: this.fieldsReverseMap.get(object),
117+
on: key,
106118
with: Array.from(args)
107119
})
108120
}

src/serialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export function serialize(value: any): any | void {
22
// todo: handle other builtins
3-
if (['string', 'number'].includes(typeof value)) {
3+
if (['string', 'number', 'boolean'].includes(typeof value) || !value) {
44
return value
55
} else if (typeof value === 'function') {
66
return {

0 commit comments

Comments
 (0)