1- import { deserialize , serialize } from 'v8'
21import { ChildProcess } from 'child_process'
3- import InterceptedArray from './array .js'
2+ import initChild from './init-child .js'
43import IPOSMessaging from './messaging.js'
4+ import { deserialize , serialize } from './serialize.js'
5+ import intercept from './intercept.js'
56
67export default class IPOS {
7- private fields : Map < string , object >
8+ private readonly fields : Map < string , object >
89 private fieldsReverseMap : Map < object , string >
9- private processes : Set < ChildProcess >
1010 private processMessagingMap : Map < ChildProcess , IPOSMessaging >
1111 private readonly proxy
12- private messaging ?: IPOSMessaging ;
12+ protected messaging ?: IPOSMessaging
1313
14- static new ( ) : IPOS {
15- return new IPOS ( )
14+ static new ( ) : IPOS | Promise < IPOS > {
15+ const ipos = new IPOS ( )
16+ // was called on child process
17+ if ( process . send ) {
18+ return new Promise ( async resolve => {
19+ await initChild . call ( ipos )
20+ resolve ( ipos )
21+ } )
22+ }
23+ return ipos
1624 }
1725
1826 constructor ( ) {
1927 this . fields = new Map ( )
2028 this . fieldsReverseMap = new Map ( )
21- this . processes = new Set ( )
2229 this . processMessagingMap = new Map ( )
2330
2431 // proxy makes all "target.fields" available as "actual" fields
@@ -31,39 +38,21 @@ export default class IPOS {
3138 }
3239 }
3340 } )
34- // was called on child process
35- if ( process . send ) {
36- this . messaging ?. listenForType ( 'sync' , message => {
37- console . log ( message )
38- if ( message . fields )
39- this . fields = deserialize ( message . fields )
40- } )
41-
42- // register with parent process
43- this . messaging = new IPOSMessaging ( process )
44- this . messaging . send ( 'register' )
45- }
4641 return this . proxy
4742 }
4843
44+ public get ( key : string ) : any {
45+ return this . fields . get ( key )
46+ }
47+
48+ // todo: also accept and update non object values
4949 public create ( key : string , value : object ) : void {
50- if ( Array . isArray ( value ) ) {
51- value = new InterceptedArray ( ( object , method , ...args ) =>
50+ // console.log('create', key)
51+ if ( typeof value === 'object' )
52+ value = intercept ( value , ( object , method , ...args ) =>
5253 this . sendMethodCall ( object , method , ...args )
5354 )
54- }
55- /*Object.getOwnPropertyNames(Object.getPrototypeOf(value))
56- .filter(methodName => !(methodName.startsWith('__') && methodName.endsWith('__')))
57- .forEach(methodName => {
58- if (!value[methodName]) return
59- const method = value[methodName]
60- value[methodName] = function (...args: any) {
61- // interception
62- console.log(methodName)
63- method.call(value, ...args)
64- }
65- })
66- */
55+
6756 this . fields . set ( key , value )
6857 this . fieldsReverseMap . set ( value , key )
6958 // todo: send update message
@@ -84,16 +73,14 @@ export default class IPOS {
8473 if ( registered ) return
8574 registered = true
8675
87- this . processes . add ( process )
8876 this . processMessagingMap . set ( process , messaging )
8977 this . syncProcess ( process )
9078 } )
9179 }
9280
9381 private syncProcess ( process : ChildProcess ) {
94- console . log ( 'sending sync' )
9582 this . processMessagingMap . get ( process ) ?. send ( 'sync' , {
96- field : serialize ( this . fields )
83+ fields : JSON . stringify ( IPOS . serialize ( this . fields ) )
9784 } )
9885 }
9986
@@ -107,30 +94,14 @@ export default class IPOS {
10794 } )
10895 }
10996
110- // serializes types, that "JSON.stringify()" doesn't properly handle
111- /*private static serialize(value: any): any | void {
112- // todo: handle other builtins
113- if (['string', 'number'].includes(typeof value)) {
114- return value
115- } else if (typeof value === 'function') {
116- return value.toString()
117- } else if (Array.isArray(value)) {
118- return value.map(serialize)
119- } else if (value.constructor === {}.constructor) {
120- return Object.fromEntries(
121- Array.from(
122- Object.entries(value)
123- .map(([key, value]) =>
124- [key, this.serialize(value)]
125- )
126- )
127- )
128- } else {
129- if (!value.stringify && !value.serialize)
130- throw new Error(
131- `Class: \`${value.constructor.name}\` must have methods to serialize and deserialize objects. (\`.stringify()\`, \`.serialize()\`)`
132- )
133- // return value.toString()
134- }
135- }*/
97+ /**
98+ * Serializes types that "JSON.stringify()" doesn't properly handle
99+ */
100+ public static serialize ( value : any ) : any | void {
101+ return serialize ( value )
102+ }
103+
104+ public static deserialize ( value : string | number | Array < any > | { $$iposType ?: string , data : any } ) : any | void {
105+ return deserialize ( value )
106+ }
136107}
0 commit comments