Skip to content

Commit 0bb0eec

Browse files
committed
add updating field values
1 parent 086a755 commit 0bb0eec

File tree

6 files changed

+32
-11
lines changed

6 files changed

+32
-11
lines changed

example/main-process.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const subProcess = child_process.spawn('node', ['sub-process.js'], {
99
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
1010
})
1111

12-
sharedObject.addProcess(subProcess)
12+
await sharedObject.addProcess(subProcess)
1313
sharedObject.create('exampleObject', {})
1414
sharedObject.exampleObject.from = 'the other side'
1515

src/init-child.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ export default function initChild(this: IPOS) {
66
const promise = new Promise(res => resolve = res)
77

88
this.messaging = new IPOSMessaging(process)
9-
this.messaging?.listenForType('sync', message => {
9+
this.messaging.listenForType('sync', message => {
1010
if (!message.fields) return
1111
Object.entries(
1212
IPOS.deserialize(JSON.parse(message.fields))
1313
)
1414
.map(([key, value]: [string, any]) => {
15-
this.create(key, value.constructor())
16-
if (Array.isArray(value)) {
17-
this.get(key)?.push(...value)
18-
}
15+
this.createStealthy(key, value)
1916
})
2017
resolve()
2118
})
2219
// register with parent process
2320
this.messaging.send('register')
21+
this.messaging.listenForType('update', message => {
22+
if (!message.do || !message.on) return
23+
this.get(message.on)[message.do](...(message.with ?? []))
24+
})
2425

2526
return promise
2627
}

src/intercept.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import InterceptedArray from './intercept/array.js'
22

33
export default function intercept(value: object, interceptCallback: (object: object, method: string, ...args: any) => void): object {
44
if (Array.isArray(value)) {
5-
value = new InterceptedArray(interceptCallback)
5+
value = InterceptedArray.new(value, interceptCallback)
66
}
77
/*Object.getOwnPropertyNames(Object.getPrototypeOf(value))
88
.filter(methodName => !(methodName.startsWith('__') && methodName.endsWith('__')))

src/intercept/array.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ export default class InterceptedArray<T> extends Array {
77
this.callback = callback
88
}
99

10+
static new<T>(arrayLike: ArrayLike<T>, callback: (object: object, method: string, ...args: any) => void): InterceptedArray<T> {
11+
const interceptedArray: InterceptedArray<T> =
12+
new InterceptedArray(callback)
13+
for (let i = 0; i < arrayLike.length; i++) {
14+
interceptedArray[i] = arrayLike[i]
15+
}
16+
return interceptedArray
17+
}
18+
1019
copyWithin(target: number, start: number, end?: number): this {
1120
this.callback(this, 'copyWithin', target, start, end)
1221
return super.copyWithin(target, start, end)

src/main.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ export default class IPOS {
4545
return this.fields.get(key)
4646
}
4747

48-
// todo: also accept and update non object values
48+
// todo: also accept and update non-object values
4949
public create(key: string, value: object): void {
50+
this.createStealthy(key, value)
51+
// todo: send update message
52+
}
53+
54+
protected createStealthy(key: string, value: object): void {
5055
// console.log('create', key)
5156
if (typeof value === 'object')
5257
value = intercept(value, (object, method, ...args) =>
@@ -63,19 +68,22 @@ export default class IPOS {
6368
// todo: send update message
6469
}
6570

66-
public addProcess(process: ChildProcess) {
71+
public addProcess(process: ChildProcess): Promise<void> {
6772
if (!process.send)
6873
throw new Error(`Process must have an ipc channel. Activate by passing "stdio: [<stdin>, <stdout>, <stderr>, 'ipc']" as an option.`)
6974
const messaging = new IPOSMessaging(process)
7075

71-
let registered = false
76+
let registered = false, resolve: Function
77+
const promise: Promise<void> = new Promise(res => resolve = res)
7278
messaging.listenForType('register', () => {
7379
if (registered) return
7480
registered = true
7581

7682
this.processMessagingMap.set(process, messaging)
7783
this.syncProcess(process)
84+
resolve()
7885
})
86+
return promise
7987
}
8088

8189
private syncProcess(process: ChildProcess) {

src/messaging.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ type iposMessagingCallback = (message: iposMessagingMessage) => (any | void)
55
type iposMessagingMessage = {
66
protocol: 'ipos',
77
type: iposMessagingType,
8-
fields?: string
8+
fields?: string,
9+
do?: string,
10+
on?: string,
11+
with?: Array<any>,
912
}
1013

1114
const mustHaveSendError = new Error(`Process must have a \`.send()\` method.`)

0 commit comments

Comments
 (0)