wenay-common2 1.0.46 → 1.0.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/lib/Common/Color.d.ts +3 -0
  2. package/lib/Common/Color.js +4 -0
  3. package/lib/Common/ObserveAll2/reactive2.d.ts +32 -0
  4. package/lib/Common/ObserveAll2/reactive2.js +249 -0
  5. package/lib/Common/Time.d.ts +8 -0
  6. package/lib/Common/Time.js +41 -14
  7. package/lib/Common/async/PromiseArrayListen.d.ts +3 -3
  8. package/lib/Common/async/PromiseArrayListen.js +10 -13
  9. package/lib/Common/async/createIterableObject.js +1 -1
  10. package/lib/Common/async/waitRun.d.ts +11 -4
  11. package/lib/Common/async/waitRun.js +74 -44
  12. package/lib/Common/core/Decorator.d.ts +3 -1
  13. package/lib/Common/core/Decorator.js +5 -2
  14. package/lib/Common/core/common.d.ts +25 -7
  15. package/lib/Common/core/common.js +114 -34
  16. package/lib/Common/data/ByteStream.d.ts +1 -1
  17. package/lib/Common/data/ByteStream.js +21 -8
  18. package/lib/Common/data/List.d.ts +5 -0
  19. package/lib/Common/data/List.js +13 -4
  20. package/lib/Common/data/ListNodeAnd.js +2 -2
  21. package/lib/Common/data/objectPath.d.ts +4 -0
  22. package/lib/Common/data/objectPath.js +8 -3
  23. package/lib/Common/events/Listen.d.ts +38 -5
  24. package/lib/Common/events/Listen.js +89 -54
  25. package/lib/Common/events/SocketBuffer.d.ts +9 -4
  26. package/lib/Common/events/SocketServerHook.d.ts +20 -7
  27. package/lib/Common/events/SocketServerHook.js +2 -1
  28. package/lib/Common/events/UseListenTransform.d.ts +8 -3
  29. package/lib/Common/events/event.d.ts +12 -0
  30. package/lib/Common/events/event.js +10 -6
  31. package/lib/Common/events/joinListens.d.ts +2 -0
  32. package/lib/Common/events/joinListens.js +13 -12
  33. package/lib/Common/funcTimeWait.d.ts +28 -0
  34. package/lib/Common/funcTimeWait.js +18 -1
  35. package/lib/Common/isProxy.d.ts +1 -0
  36. package/lib/Common/isProxy.js +2 -0
  37. package/lib/Common/node_console.d.ts +2 -0
  38. package/lib/Common/node_console.js +3 -76
  39. package/lib/Common/rcp/createRpcServerAutoWithProtocolDetection.d.ts +2 -0
  40. package/lib/Common/rcp/createRpcServerAutoWithProtocolDetection.js +63 -30
  41. package/lib/Common/rcp/listen-deep.d.ts +13 -5
  42. package/lib/Common/rcp/listen-socket.d.ts +21 -3
  43. package/lib/Common/rcp/listen-socket.js +97 -6
  44. package/lib/Common/rcp/oldCommonsServer.d.ts +1 -1
  45. package/lib/Common/rcp/oldCommonsServer.js +15 -1
  46. package/lib/Common/rcp/old/320/241ommonsServerMini.js +1 -0
  47. package/lib/Common/rcp/rpc-caps.d.ts +10 -0
  48. package/lib/Common/rcp/rpc-caps.js +16 -0
  49. package/lib/Common/rcp/rpc-client.d.ts +23 -1
  50. package/lib/Common/rcp/rpc-client.js +306 -25
  51. package/lib/Common/rcp/rpc-clientHub.d.ts +7 -1
  52. package/lib/Common/rcp/rpc-clientHub.js +25 -2
  53. package/lib/Common/rcp/rpc-index.d.ts +3 -0
  54. package/lib/Common/rcp/rpc-index.js +3 -0
  55. package/lib/Common/rcp/rpc-inproc.d.ts +17 -0
  56. package/lib/Common/rcp/rpc-inproc.js +30 -0
  57. package/lib/Common/rcp/rpc-off.d.ts +10 -0
  58. package/lib/Common/rcp/rpc-off.js +22 -0
  59. package/lib/Common/rcp/rpc-protocol.d.ts +6 -0
  60. package/lib/Common/rcp/rpc-protocol.js +4 -2
  61. package/lib/Common/rcp/rpc-server-auto.d.ts +14 -3
  62. package/lib/Common/rcp/rpc-server-auto.js +81 -6
  63. package/lib/Common/rcp/rpc-server.d.ts +17 -2
  64. package/lib/Common/rcp/rpc-server.js +127 -31
  65. package/lib/Common/rcp/rpc-shape.d.ts +17 -0
  66. package/lib/Common/rcp/rpc-shape.js +42 -0
  67. package/lib/Common/rcp/rpc-walk.js +29 -11
  68. package/lib/Exchange/Bars.d.ts +3 -0
  69. package/lib/Exchange/Bars.js +4 -1
  70. package/lib/Exchange/CParams.d.ts +2 -0
  71. package/lib/Exchange/CParams.js +21 -14
  72. package/lib/Exchange/MarketData.d.ts +6 -0
  73. package/lib/Exchange/MarketData.js +4 -0
  74. package/lib/index.d.ts +1 -0
  75. package/lib/index.js +2 -1
  76. package/lib/server/WebHook3.js +22 -9
  77. package/package.json +2 -1
@@ -22,23 +22,26 @@ class CObjectEventsArr {
22
22
  AddStart(data) { this.data.unshift(data); this.setup(this.data[0]); }
23
23
  AddEnd(data) { this.setup(this.data[this.data.push(data) - 1]); }
24
24
  Add(data) { this.setup(this.data[this.data.push(data) - 1]); }
25
+ add(data, opts = {}) { opts.at == 'start' ? this.AddStart(data) : this.AddEnd(data); }
25
26
  OnEvent(data) { [...this.data].forEach((e) => { e.func?.(data); if (e.func2) {
26
27
  e.func2(data);
27
28
  e.del?.();
28
29
  } }); }
30
+ emit(data) { this.OnEvent(data); }
29
31
  OnSpecEvent(f) { [...this.data].forEach((e) => { const l = e.func?.(); l && f(l); if (e.func2) {
30
32
  e.func2();
31
33
  e.del?.();
32
34
  } }); }
33
35
  Clean() {
34
36
  const a = [...this.data];
35
- this.data = [];
36
37
  for (let i = a.length - 1; i >= 0; i--) {
37
38
  a[i].del?.();
38
39
  }
39
40
  }
41
+ clear() { this.Clean(); }
40
42
  count() { return this.data.length; }
41
43
  get length() { return this.count(); }
44
+ get size() { return this.count(); }
42
45
  }
43
46
  exports.CObjectEventsArr = CObjectEventsArr;
44
47
  class CObjectEventsList {
@@ -67,22 +70,23 @@ class CObjectEventsList {
67
70
  AddStart(data) { this.setup(this.data.AddStart(data)); }
68
71
  AddEnd(data) { this.setup(this.data.AddEnd(data)); }
69
72
  Add(data) { this.setup(this.data.AddEnd(data)); }
73
+ add(data, opts = {}) { opts.at == 'start' ? this.AddStart(data) : this.AddEnd(data); }
70
74
  OnEvent(data) { const a = []; this.data.forEach(e => a.push(e)); a.forEach(e => { e.func?.(data); if (e.func2) {
71
75
  e.func2(data);
72
76
  e.del?.();
73
77
  } }); }
78
+ emit(data) { this.OnEvent(data); }
74
79
  OnSpecEvent(f) { const a = []; this.data.forEach(e => a.push(e)); a.forEach((e) => { const l = e.func?.(); if (l) {
75
80
  f(l);
76
81
  } if (e.func2) {
77
82
  e.func2();
78
83
  e.del?.();
79
84
  } }); }
80
- Clean() { let r = this.data.First(); while (r) {
81
- const buf = r;
82
- r = r?.Next();
83
- buf.DeleteLink();
84
- } }
85
+ Clean() { const a = []; this.data.forEach(e => a.push(e)); for (let i = a.length - 1; i >= 0; i--)
86
+ a[i].del?.(); }
87
+ clear() { this.Clean(); }
85
88
  count() { return this.data.countRef(); }
86
89
  get length() { return this.count(); }
90
+ get size() { return this.count(); }
87
91
  }
88
92
  exports.CObjectEventsList = CObjectEventsList;
@@ -11,6 +11,8 @@ type JoinResult<R> = {
11
11
  listen: listen<[R, string]>[1];
12
12
  pending: Map<string, Map<string, any>>;
13
13
  clear: (tid?: string) => void;
14
+ destroy: () => void;
15
+ add: (port: listen<any>[1], key?: string) => void;
14
16
  addListen: (listener: listen<any>[1], key?: string) => void;
15
17
  };
16
18
  export declare function joinListens<T extends Record<string, any[]>>(listens: ListenMap<T>, keyExtractor?: KeyExtractor<any>): JoinResult<CollectedResult<T>>;
@@ -18,7 +18,7 @@ function joinListens(listens, keyExtractor) {
18
18
  const result = isArray
19
19
  ? keys.map(k => bucket.get(k))
20
20
  : Object.fromEntries(bucket);
21
- bucket.clear();
21
+ buckets.delete(tid);
22
22
  set(result, tid);
23
23
  };
24
24
  const unsubs = [];
@@ -30,31 +30,32 @@ function joinListens(listens, keyExtractor) {
30
30
  buckets.get(tid).set(portId, data.length <= 1 ? data[0] : data);
31
31
  tryFire(tid);
32
32
  };
33
- listener.addListen(cb);
34
- unsubs.push(() => listener.removeListen(cb));
33
+ unsubs.push(listener.addListen(cb));
35
34
  };
36
35
  for (const portId of keys) {
37
36
  bindPort(portId, map[portId]);
38
37
  }
38
+ function add(listener, key) {
39
+ const portId = isArray ? String(keys.length) : (key ?? String(keys.length));
40
+ if (map[portId])
41
+ return;
42
+ map[portId] = listener;
43
+ keys.push(portId);
44
+ bindPort(portId, listener);
45
+ }
39
46
  return {
40
47
  listen: out,
41
48
  pending: buckets,
42
49
  clear: (tid) => {
43
50
  tid ? buckets.delete(tid) : buckets.clear();
44
51
  },
45
- destroy: (_tid) => {
52
+ destroy: () => {
46
53
  for (const u of unsubs)
47
54
  u();
48
55
  unsubs.length = 0;
49
56
  buckets.clear();
50
57
  },
51
- addListen: (listener, key) => {
52
- const portId = isArray ? String(keys.length) : (key ?? String(keys.length));
53
- if (map[portId])
54
- return;
55
- map[portId] = listener;
56
- keys.push(portId);
57
- bindPort(portId, listener);
58
- }
58
+ add,
59
+ addListen: (listener, key) => add(listener, key)
59
60
  };
60
61
  }
@@ -27,4 +27,32 @@ export declare const FuncTimeWait: {
27
27
  byWeight(type: tType, weight?: number): number;
28
28
  byWeightTimeNow(type: tType, timeNow?: number, weight?: number): number;
29
29
  };
30
+ export declare function createRateWindow(): {
31
+ prune: (type: tType, ms?: number) => void;
32
+ sumWeight: (type: tType, ms?: number) => number;
33
+ readyAt: (type: tType, weight?: number) => number;
34
+ dStatic: {
35
+ [key: string]: [number, number][];
36
+ };
37
+ data: any[];
38
+ add(item: tFunc): void;
39
+ cleanByTime(type: tType, ms?: number): void;
40
+ weight(type: tType, ms?: number): number;
41
+ byWeight(type: tType, weight?: number): number;
42
+ byWeightTimeNow(type: tType, timeNow?: number, weight?: number): number;
43
+ };
44
+ export declare const rateWindow: {
45
+ prune: (type: tType, ms?: number) => void;
46
+ sumWeight: (type: tType, ms?: number) => number;
47
+ readyAt: (type: tType, weight?: number) => number;
48
+ dStatic: {
49
+ [key: string]: [number, number][];
50
+ };
51
+ data: any[];
52
+ add(item: tFunc): void;
53
+ cleanByTime(type: tType, ms?: number): void;
54
+ weight(type: tType, ms?: number): number;
55
+ byWeight(type: tType, weight?: number): number;
56
+ byWeightTimeNow(type: tType, timeNow?: number, weight?: number): number;
57
+ };
30
58
  export {};
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FuncTimeWait = void 0;
3
+ exports.rateWindow = exports.FuncTimeWait = void 0;
4
4
  exports.funcTimeW = funcTimeW;
5
+ exports.createRateWindow = createRateWindow;
5
6
  function funcTimeW() {
6
7
  const dStatic = {};
7
8
  const data = [];
9
+ const sortByTime = (arr) => arr.sort((a, b) => a[0] - b[0]);
8
10
  return {
9
11
  dStatic,
10
12
  data,
@@ -13,11 +15,13 @@ function funcTimeW() {
13
15
  dStatic[item.type] = [];
14
16
  }
15
17
  dStatic[item.type].push([item.timeStamp ?? Date.now(), item.weight]);
18
+ sortByTime(dStatic[item.type]);
16
19
  },
17
20
  cleanByTime(type, ms = 60 * 1000) {
18
21
  const arr = dStatic[type];
19
22
  if (!arr || arr.length === 0)
20
23
  return;
24
+ sortByTime(arr);
21
25
  const timeStamp = Date.now();
22
26
  if (arr[0][0] > timeStamp - ms)
23
27
  return;
@@ -33,6 +37,7 @@ function funcTimeW() {
33
37
  const arr = dStatic[type];
34
38
  if (!arr || arr.length === 0)
35
39
  return 0;
40
+ sortByTime(arr);
36
41
  const timeStamp = Date.now();
37
42
  let sum = 0;
38
43
  let i = arr.length - 1;
@@ -51,6 +56,7 @@ function funcTimeW() {
51
56
  const arr = dStatic[type];
52
57
  if (!arr || arr.length === 0)
53
58
  return 0;
59
+ sortByTime(arr);
54
60
  let sum = 0;
55
61
  let i = arr.length - 1;
56
62
  let result = 0;
@@ -70,6 +76,7 @@ function funcTimeW() {
70
76
  const arr = dStatic[type];
71
77
  if (!arr || arr.length === 0)
72
78
  return 0;
79
+ sortByTime(arr);
73
80
  let sum = 0;
74
81
  let i = arr.length - 1;
75
82
  for (; i >= 0; i--) {
@@ -94,3 +101,13 @@ function funcTimeW() {
94
101
  };
95
102
  }
96
103
  exports.FuncTimeWait = funcTimeW();
104
+ function createRateWindow() {
105
+ const w = funcTimeW();
106
+ return {
107
+ ...w,
108
+ prune: w.cleanByTime,
109
+ sumWeight: w.weight,
110
+ readyAt: w.byWeight,
111
+ };
112
+ }
113
+ exports.rateWindow = createRateWindow();
@@ -1,2 +1,3 @@
1
1
  export declare function isProxyInit(): void;
2
+ export declare const installProxyTracking: typeof isProxyInit;
2
3
  export declare function isProxy(a: any): boolean;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.installProxyTracking = void 0;
3
4
  exports.isProxyInit = isProxyInit;
4
5
  exports.isProxy = isProxy;
5
6
  const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
@@ -31,6 +32,7 @@ function isProxyInit() {
31
32
  init = true;
32
33
  set();
33
34
  }
35
+ exports.installProxyTracking = isProxyInit;
34
36
  function isProxy(a) {
35
37
  if (nodeIsProxy)
36
38
  return nodeIsProxy(a);
@@ -4,3 +4,5 @@ export declare function disable(): void;
4
4
  export declare function __LineFile(lvl?: number): string;
5
5
  export declare function __LineFile2(lvl?: number): string;
6
6
  export declare function __LineFiles(lvlStart?: number, lvlEnd?: number | undefined): string[];
7
+ export declare const callerLine: typeof __LineFile2;
8
+ export declare const callerLines: typeof __LineFiles;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.callerLines = exports.callerLine = void 0;
3
4
  exports.enable = enable;
4
5
  exports.disable = disable;
5
6
  exports.__LineFile = __LineFile;
@@ -9,78 +10,6 @@ const { self, window } = globalThis;
9
10
  let _enabled = false;
10
11
  function enable(flag = true) { _enabled = flag; }
11
12
  function disable() { _enabled = false; }
12
- let wrapCallSite;
13
- function setupLogs2() {
14
- if (typeof self != 'object' && typeof window != "object") {
15
- function moduleName(name) { return name; }
16
- let inspector = require(moduleName('inspector'));
17
- if (inspector.url() != undefined)
18
- return;
19
- try {
20
- let module = require(moduleName('source-map-support'));
21
- module.install();
22
- wrapCallSite = module.wrapCallSite;
23
- }
24
- catch (e) {
25
- console.warn(e);
26
- return;
27
- }
28
- _enabled = true;
29
- const origLogMethod = console.log;
30
- const origErrorMethod = console.error;
31
- let _callee;
32
- for (let methodName of [
33
- 'debug', 'info', 'log', 'warn', 'error', 'group', 'groupCollapsed', 'table', 'timeLog', 'timeEnd',
34
- 'count', 'assert', 'dir', 'dirxml'
35
- ]) {
36
- const origMethod = console[methodName];
37
- console[methodName] = ((...args) => {
38
- if (!_enabled)
39
- return origMethod(...args);
40
- const originalPrepareStackTrace = Error.prepareStackTrace;
41
- Error.prepareStackTrace = (_, stack) => stack;
42
- let callee = new Error().stack[1];
43
- Error.prepareStackTrace = originalPrepareStackTrace;
44
- if (!callee) {
45
- origErrorMethod("сallee is not found in node_console");
46
- _enabled = false;
47
- return origMethod(...args);
48
- }
49
- if (!methodName.match(/debug|info|log|warn|error|dirxml/)) {
50
- _callee ??= callee;
51
- return origMethod(...args);
52
- }
53
- if (_callee) {
54
- callee = _callee;
55
- _callee = undefined;
56
- }
57
- if (wrapCallSite)
58
- callee = wrapCallSite(callee);
59
- const fileName = callee.getFileName();
60
- if (fileName?.includes("source-map-support")) {
61
- origMethod(...args);
62
- return;
63
- }
64
- let fileAndLine = `${fileName}:${callee.getLineNumber()}:${callee.getColumnNumber()} ` + callee.getFunctionName();
65
- fileAndLine = fileAndLine.replaceAll("\\", "/");
66
- fileAndLine = fileAndLine.replace("webpack:///", "");
67
- fileAndLine = fileAndLine.replace("?", "");
68
- if (!fileAndLine.startsWith("./"))
69
- if (!fileAndLine.toLowerCase().startsWith("file:///"))
70
- fileAndLine = "file:///" + fileAndLine;
71
- let [firstArg, ...otherArgs] = args;
72
- if (1)
73
- origMethod(...args, "", fileAndLine);
74
- else if (typeof firstArg === 'string') {
75
- origMethod(fileAndLine + ' ' + firstArg, ...otherArgs);
76
- }
77
- else {
78
- origMethod(fileAndLine, ...args);
79
- }
80
- });
81
- }
82
- }
83
- }
84
13
  function setupLogs() {
85
14
  if (typeof self != 'object' && typeof window != "object") {
86
15
  function moduleName(name) { return name; }
@@ -153,8 +82,6 @@ function __LineFile2(lvl = 0) {
153
82
  const originalPrepareStackTrace = Error.prepareStackTrace;
154
83
  Error.prepareStackTrace = (_, stack) => stack;
155
84
  let e = new Error().stack[lvl + 1];
156
- if (wrapCallSite)
157
- e = wrapCallSite(e);
158
85
  Error.prepareStackTrace = originalPrepareStackTrace;
159
86
  return `${e.getFileName()}:${e.getLineNumber()}:${e.getColumnNumber()} ` + e.getFunctionName();
160
87
  }
@@ -162,9 +89,9 @@ function __LineFiles(lvlStart = 0, lvlEnd = 5) {
162
89
  const originalPrepareStackTrace = Error.prepareStackTrace;
163
90
  Error.prepareStackTrace = (_, stack) => stack;
164
91
  let e = new Error().stack.slice(lvlStart + 1, lvlEnd);
165
- if (wrapCallSite)
166
- e = e.map(e => wrapCallSite(e));
167
92
  const msgs = e.map(e => `${e.getFileName()}:${e.getLineNumber()}:${e.getColumnNumber()} ` + e.getFunctionName());
168
93
  Error.prepareStackTrace = originalPrepareStackTrace;
169
94
  return msgs;
170
95
  }
96
+ exports.callerLine = __LineFile2;
97
+ exports.callerLines = __LineFiles;
@@ -17,5 +17,7 @@ export declare function createRpcServerAuto2<T extends object>({ socket, object:
17
17
  getProtocol: () => ClientProtocol;
18
18
  getLegacySchema: () => any;
19
19
  getResolved: () => any;
20
+ dispose: (reason?: string) => void;
21
+ reset: () => void;
20
22
  };
21
23
  export {};
@@ -8,42 +8,58 @@ const rpc_protocol_1 = require("./rpc-protocol");
8
8
  const old_ommonsServerMini_1 = require("./old\u0421ommonsServerMini");
9
9
  const rpc_dynamic_1 = require("./rpc-dynamic");
10
10
  const rpc_limits_1 = require("./rpc-limits");
11
- function createRpcServerAuto({ socket, object: target, socketKey: key, debug, hooks, disconnectListen, limits }) {
12
- const cache = new WeakMap();
13
- function getListenSocket(parent, disconnectListen) {
14
- let result = cache.get(parent);
15
- if (!result) {
16
- result = (0, listen_socket_1.listenSocket)(parent, { addListenClose: disconnectListen });
17
- cache.set(parent, result);
18
- }
19
- return result;
20
- }
21
- (0, rpc_server_1.createRpcServer)({
22
- socket, object: target, socketKey: key, debug, limits,
23
- hooks: {
24
- ...hooks,
25
- resolveTransform: (obj) => {
26
- if (!(0, Listen_1.isListenCallback)(obj))
27
- return obj;
28
- return getListenSocket(obj, disconnectListen);
29
- },
30
- },
31
- });
32
- }
33
11
  function createRpcServerAuto2({ socket, object: target, socketKey: key, debug = false, hooks, disconnectListen, limits, onProtocolDetect, }) {
34
12
  const cache = new WeakMap();
13
+ const listenSockets = new Set();
14
+ function unsubscribeAllActive() {
15
+ for (const w of [...listenSockets])
16
+ w.removeCallback();
17
+ }
35
18
  function getListenSocket(parent) {
36
19
  let result = cache.get(parent);
20
+ if (result)
21
+ listenSockets.add(result);
37
22
  if (!result) {
38
- result = (0, listen_socket_1.listenSocket)(parent, { addListenClose: disconnectListen });
23
+ const subs = new Map();
24
+ function subscribe(z) {
25
+ if (typeof z !== "function")
26
+ return Promise.reject(new TypeError("Listen callback expects a function"));
27
+ subs.get(z)?.removeCallback();
28
+ const w = (0, listen_socket_1.listenSocket)(parent, { addListenClose: disconnectListen });
29
+ subs.set(z, w);
30
+ const done = w.callback(z);
31
+ done.then(() => { if (subs.get(z) == w)
32
+ subs.delete(z); });
33
+ return done;
34
+ }
35
+ function subscribeOnce(z) {
36
+ if (typeof z !== "function")
37
+ return Promise.reject(new TypeError("Listen once expects a function"));
38
+ subs.get(z)?.removeCallback();
39
+ const w = (0, listen_socket_1.listenSocket)(parent, { addListenClose: disconnectListen });
40
+ subs.set(z, w);
41
+ const done = w.once(z);
42
+ done.then(() => { if (subs.get(z) == w)
43
+ subs.delete(z); });
44
+ return done;
45
+ }
46
+ function unsubscribeAll() {
47
+ subs.forEach(w => w.removeCallback());
48
+ subs.clear();
49
+ return true;
50
+ }
51
+ result = { callback: subscribe, removeCallback: unsubscribeAll, on: subscribe, once: subscribeOnce, close: () => parent.close?.() };
52
+ listenSockets.add(result);
39
53
  cache.set(parent, result);
40
54
  }
41
55
  return result;
42
56
  }
43
57
  function resolveTransform(obj) {
44
- if (!(0, Listen_1.isListenCallback)(obj))
45
- return obj;
46
- return getListenSocket(obj);
58
+ if ((0, Listen_1.isListenCallback)(obj))
59
+ return getListenSocket(obj);
60
+ if ((0, Listen_1.isListenOn)(obj))
61
+ return getListenSocket((0, Listen_1.getListenByOn)(obj));
62
+ return obj;
47
63
  }
48
64
  function transformTree(obj) {
49
65
  let current = obj;
@@ -61,7 +77,7 @@ function createRpcServerAuto2({ socket, object: target, socketKey: key, debug =
61
77
  out[k] = v;
62
78
  continue;
63
79
  }
64
- out[k] = typeof v === 'function' ? v : (v != null && typeof v === 'object') ? transformTree(v) : v;
80
+ out[k] = typeof v === 'function' ? resolveTransform(v) : (v != null && typeof v === 'object') ? transformTree(v) : v;
65
81
  }
66
82
  return out;
67
83
  }
@@ -96,6 +112,8 @@ function createRpcServerAuto2({ socket, object: target, socketKey: key, debug =
96
112
  let protocol = null;
97
113
  let v2Handler = null;
98
114
  let legacyHandler = null;
115
+ let disposed = false;
116
+ let activeHandler = null;
99
117
  function isLegacyStrictRequest(msg) {
100
118
  return msg === '___STRICTLY';
101
119
  }
@@ -108,7 +126,7 @@ function createRpcServerAuto2({ socket, object: target, socketKey: key, debug =
108
126
  function isV2Message(msg) {
109
127
  if (msg === rpc_protocol_1.Pkt.STRICT)
110
128
  return true;
111
- if (Array.isArray(msg) && (msg[0] === rpc_protocol_1.Pkt.CALL || msg[0] === rpc_protocol_1.Pkt.PIPE))
129
+ if (Array.isArray(msg) && (msg[0] === rpc_protocol_1.Pkt.CALL || msg[0] === rpc_protocol_1.Pkt.PIPE || msg[0] === rpc_protocol_1.Pkt.HELLO))
112
130
  return true;
113
131
  return false;
114
132
  }
@@ -143,6 +161,7 @@ function createRpcServerAuto2({ socket, object: target, socketKey: key, debug =
143
161
  limits,
144
162
  hooks: {
145
163
  ...hooks,
164
+ onDispose: () => { unsubscribeAllActive(); hooks?.onDispose?.(); },
146
165
  resolveTransform,
147
166
  },
148
167
  });
@@ -152,7 +171,7 @@ function createRpcServerAuto2({ socket, object: target, socketKey: key, debug =
152
171
  onMsgCb(msg);
153
172
  };
154
173
  }
155
- socket.on(key, (msg) => {
174
+ function handleMessage(msg) {
156
175
  if (debug) {
157
176
  console.log('[RPC-AUTO2 IN]', typeof msg === 'object' ? JSON.stringify(msg) : msg);
158
177
  }
@@ -197,10 +216,24 @@ function createRpcServerAuto2({ socket, object: target, socketKey: key, debug =
197
216
  }
198
217
  if (debug)
199
218
  console.warn('[RPC-AUTO2] Unknown message format, ignoring:', msg);
200
- });
219
+ }
220
+ activeHandler = handleMessage;
221
+ socket.on(key, (msg) => activeHandler?.(msg));
222
+ function reset() { unsubscribeAllActive(); protocol = null; legacyHandler = null; v2Handler = null; }
223
+ function dispose(reason) {
224
+ if (disposed)
225
+ return;
226
+ disposed = true;
227
+ activeHandler = null;
228
+ reset();
229
+ if (debug)
230
+ console.log('[RPC-AUTO2] disposed', reason ?? '');
231
+ }
201
232
  return {
202
233
  getProtocol: () => protocol,
203
234
  getLegacySchema: () => legacySchema,
204
235
  getResolved: () => resolved,
236
+ dispose,
237
+ reset,
205
238
  };
206
239
  }
@@ -1,4 +1,12 @@
1
- import { listenSocket, listenSocketFirst, listenSocketAll, listenSocketSmart } from "./listen-socket";
1
+ import { type ListenOn } from "../events/Listen";
2
+ import { listenSocket, listenSocketFirst, listenSocketAll, listenSocketSmart, type tSubHandle } from "./listen-socket";
3
+ type WithSubHandle<R> = R extends {
4
+ callback: (...a: infer A) => any;
5
+ } ? Omit<R, 'callback' | 'on' | 'once'> & {
6
+ callback: (...a: A) => tSubHandle & Promise<void>;
7
+ on: (...a: A) => tSubHandle & Promise<void>;
8
+ once: (...a: A) => tSubHandle & Promise<void>;
9
+ } : R;
2
10
  type Obj = Record<string, any>;
3
11
  type InferArgs<T> = T extends {
4
12
  addListen: (cb: (...args: infer R) => void, ...rest: any[]) => any;
@@ -6,22 +14,22 @@ type InferArgs<T> = T extends {
6
14
  export type DeepSocketListen<T> = {
7
15
  [K in keyof T]: T[K] extends {
8
16
  addListen: Function;
9
- } ? ReturnType<typeof listenSocket<InferArgs<T[K]>>> : T[K] extends (...a: any[]) => any ? T[K] : T[K] extends typeof Promise ? T[K] : T[K] extends object ? DeepSocketListen<T[K]> : T[K];
17
+ } ? WithSubHandle<ReturnType<typeof listenSocket<InferArgs<T[K]>>>> : T[K] extends ListenOn<infer Z> ? WithSubHandle<ReturnType<typeof listenSocket<Z>>> : T[K] extends (...a: any[]) => any ? T[K] : T[K] extends Promise<any> ? T[K] : T[K] extends typeof Promise ? T[K] : T[K] extends object ? DeepSocketListen<T[K]> : T[K];
10
18
  };
11
19
  export type DeepSocketListenFirst<T> = {
12
20
  [K in keyof T]: T[K] extends {
13
21
  addListen: Function;
14
- } ? ReturnType<typeof listenSocketFirst<InferArgs<T[K]>>> : T[K] extends (...a: any[]) => any ? T[K] : T[K] extends typeof Promise ? T[K] : T[K] extends object ? DeepSocketListenFirst<T[K]> : T[K];
22
+ } ? ReturnType<typeof listenSocketFirst<InferArgs<T[K]>>> : T[K] extends ListenOn<infer Z> ? ReturnType<typeof listenSocketFirst<Z>> : T[K] extends (...a: any[]) => any ? T[K] : T[K] extends Promise<any> ? T[K] : T[K] extends typeof Promise ? T[K] : T[K] extends object ? DeepSocketListenFirst<T[K]> : T[K];
15
23
  };
16
24
  export type DeepSocketListenAll<T> = {
17
25
  [K in keyof T]: T[K] extends {
18
26
  addListen: Function;
19
- } ? ReturnType<typeof listenSocketAll<InferArgs<T[K]>>> : T[K] extends (...a: any[]) => any ? T[K] : T[K] extends typeof Promise ? T[K] : T[K] extends object ? DeepSocketListenAll<T[K]> : T[K];
27
+ } ? ReturnType<typeof listenSocketAll<InferArgs<T[K]>>> : T[K] extends ListenOn<infer Z> ? ReturnType<typeof listenSocketAll<Z>> : T[K] extends (...a: any[]) => any ? T[K] : T[K] extends Promise<any> ? T[K] : T[K] extends typeof Promise ? T[K] : T[K] extends object ? DeepSocketListenAll<T[K]> : T[K];
20
28
  };
21
29
  export type DeepSocketListenSmart<T> = {
22
30
  [K in keyof T]: NonNullable<T[K]> extends {
23
31
  addListen: Function;
24
- } ? ReturnType<typeof listenSocketSmart<InferArgs<NonNullable<T[K]>>>> | Extract<T[K], undefined | null> : NonNullable<T[K]> extends (...a: any[]) => any ? T[K] : NonNullable<T[K]> extends typeof Promise ? T[K] : NonNullable<T[K]> extends object ? DeepSocketListenSmart<T[K]> : T[K];
32
+ } ? ReturnType<typeof listenSocketSmart<InferArgs<NonNullable<T[K]>>>> | Extract<T[K], undefined | null> : NonNullable<T[K]> extends ListenOn<infer Z> ? ReturnType<typeof listenSocketSmart<Z>> : NonNullable<T[K]> extends (...a: any[]) => any ? T[K] : NonNullable<T[K]> extends Promise<any> ? T[K] : NonNullable<T[K]> extends typeof Promise ? T[K] : NonNullable<T[K]> extends object ? DeepSocketListenSmart<T[K]> : T[K];
25
33
  };
26
34
  export declare function matchKeys<T extends Obj, T2 extends Obj>(obj1: T, obj2: T2): boolean;
27
35
  export declare function matchKeysList<T extends Obj>(obj1: T, keys: string[]): boolean;
@@ -1,25 +1,43 @@
1
1
  import { funcListenCallbackBase, type Listener } from "../events/Listen";
2
+ import { type Off } from "./rpc-off";
2
3
  type ListenCallbackResult<T extends any[] = any[]> = ReturnType<typeof funcListenCallbackBase<T>>;
4
+ export type tSubHandle = Off<void, {
5
+ unsubscribe: () => void;
6
+ removeCallback: () => void;
7
+ }>;
3
8
  export declare function listenSocket<Z extends any[] = any[]>(e: ListenCallbackResult<Z>, d?: {
4
9
  readonly status?: () => boolean;
5
10
  readonly addListenClose?: ListenCallbackResult<any>;
6
11
  readonly stop?: (x: Listener<Z>) => any;
7
12
  readonly paramsModify?: (...e: Z) => any[];
13
+ readonly throttle?: number;
8
14
  }): {
9
15
  callback: (z: Listener<Z>) => Promise<void>;
10
16
  removeCallback: () => boolean;
17
+ on: (z: Listener<Z>) => Promise<void>;
18
+ once: (z: Listener<Z>) => Promise<void>;
19
+ close: () => void;
11
20
  };
12
21
  export declare function listenSocketFirst<Z extends any[] = any[]>(e: ListenCallbackResult<Z>, options?: Omit<Parameters<typeof listenSocket>[1], "paramsModify">): {
13
- callback: (z: (a: Z[0]) => void) => void;
22
+ callback: (z: (a: Z[0]) => void) => tSubHandle;
23
+ on: (z: (a: Z[0]) => void) => tSubHandle;
24
+ once: (z: (a: Z[0]) => void) => tSubHandle;
25
+ close: () => void;
14
26
  removeCallback: () => boolean;
15
27
  };
16
28
  export declare function listenSocketAll<Z extends any[] = any[]>(e: ListenCallbackResult<Z>, options?: Omit<Parameters<typeof listenSocket>[1], "paramsModify">): {
17
- callback: (z: (...args: Z) => void) => void;
29
+ callback: (z: (...args: Z) => void) => tSubHandle;
30
+ on: (z: (...args: Z) => void) => tSubHandle;
31
+ once: (z: (...args: Z) => void) => tSubHandle;
32
+ close: () => void;
18
33
  removeCallback: () => boolean;
19
34
  };
20
35
  type SmartCallback<Z extends any[]> = Z extends [infer Single] ? (a: Single) => void : (...args: Z) => void;
21
36
  export declare function listenSocketSmart<Z extends any[] = any[]>(e: ListenCallbackResult<Z>, options?: Omit<Parameters<typeof listenSocket>[1], "paramsModify">): {
22
- callback: (z: SmartCallback<Z>) => void;
37
+ callback: (z: SmartCallback<Z>) => tSubHandle;
38
+ on: (z: SmartCallback<Z>) => tSubHandle;
39
+ once: (z: SmartCallback<Z>) => tSubHandle;
40
+ close: () => void;
23
41
  removeCallback: () => boolean;
24
42
  };
25
43
  export {};