wenay-common 1.0.238 → 1.0.239

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.
@@ -13,10 +13,18 @@ export declare function funcListenBySocket3<Z extends any[] = any[]>(e: ListenCa
13
13
  callback: (z: (a: Z[0]) => void) => void;
14
14
  removeCallback: () => boolean;
15
15
  };
16
+ export declare function funcListenBySocket4<Z extends any[] = any[]>(e: ListenCallbackResult<Z>, options?: Omit<Parameters<typeof funcListenBySocket2>[1], "paramsModify">): {
17
+ callback: (z: (...args: Z) => void) => void;
18
+ removeCallback: () => boolean;
19
+ };
16
20
  export declare function funcListenBySocket<Z extends any[] = any[]>(e: ReturnType<typeof funcListenCallback<Z>>, status: () => boolean, onStop?: ReturnType<typeof funcListenCallback<any>>): {
17
21
  callback: (z: (a: Z[0]) => void) => void;
18
22
  removeCallback: () => boolean;
19
23
  };
24
+ export declare function funcListenBySocketFull<Z extends any[] = any[]>(e: ReturnType<typeof funcListenCallback<Z>>, status: () => boolean, onStop?: ReturnType<typeof funcListenCallback<any>>): {
25
+ callback: (z: (...args: Z) => void) => void;
26
+ removeCallback: () => boolean;
27
+ };
20
28
  export declare const funcListenBySocket1: typeof funcListenBySocket;
21
29
  type Obj = Record<string, any>;
22
30
  export declare function CompareKeys<T extends Obj, T2 extends Obj>(obj1: T, obj2: T2): boolean;
@@ -31,6 +39,7 @@ type DeepSocket<T> = {
31
39
  export declare function DeepCompareKeys<T, T2 extends Obj, T3>(obj1: T, obj2: T2, func: (a: T2) => T3): T3 | T | null;
32
40
  export declare function deepModifyByListenSocket<T>(obj: T, status: () => boolean): DeepSocket<T>;
33
41
  export declare function deepModifyByListenSocket2<T>(obj: T, data?: Parameters<typeof funcListenBySocket2>[1]): DeepSocket<T>;
34
- export declare function deepModifyByListenSocket3<T>(obj: T, data?: Parameters<typeof funcListenBySocket3>[1]): DeepSocket<T>;
42
+ export declare function deepModifyByListenSocket3<T>(obj: T, data?: Parameters<typeof funcListenBySocket4>[1]): DeepSocket<T>;
43
+ export declare function deepModifyByListenSocket4<T>(obj: T, status: () => boolean): DeepSocket<T>;
35
44
  export declare const funcListenBySocketObj: typeof deepModifyByListenSocket;
36
45
  export {};
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.funcListenBySocketObj = exports.funcListenBySocket1 = void 0;
4
4
  exports.funcListenBySocket2 = funcListenBySocket2;
5
5
  exports.funcListenBySocket3 = funcListenBySocket3;
6
+ exports.funcListenBySocket4 = funcListenBySocket4;
6
7
  exports.funcListenBySocket = funcListenBySocket;
8
+ exports.funcListenBySocketFull = funcListenBySocketFull;
7
9
  exports.CompareKeys = CompareKeys;
8
10
  exports.CompareKeys2 = CompareKeys2;
9
11
  exports.DeepCompareKeys2 = DeepCompareKeys2;
@@ -11,6 +13,7 @@ exports.DeepCompareKeys = DeepCompareKeys;
11
13
  exports.deepModifyByListenSocket = deepModifyByListenSocket;
12
14
  exports.deepModifyByListenSocket2 = deepModifyByListenSocket2;
13
15
  exports.deepModifyByListenSocket3 = deepModifyByListenSocket3;
16
+ exports.deepModifyByListenSocket4 = deepModifyByListenSocket4;
14
17
  const isProxy_1 = require("./isProxy");
15
18
  const Listen_1 = require("./Listen");
16
19
  function isLeafValue(value) {
@@ -73,6 +76,15 @@ function funcListenBySocket3(e, options) {
73
76
  removeCallback: r.removeCallback,
74
77
  };
75
78
  }
79
+ function funcListenBySocket4(e, options) {
80
+ const r = funcListenBySocket2(e, {
81
+ ...options,
82
+ });
83
+ return {
84
+ callback: r.callback,
85
+ removeCallback: r.removeCallback,
86
+ };
87
+ }
76
88
  function funcListenBySocket(e, status, onStop) {
77
89
  const r = funcListenBySocket3(e, {
78
90
  status,
@@ -81,6 +93,14 @@ function funcListenBySocket(e, status, onStop) {
81
93
  });
82
94
  return r;
83
95
  }
96
+ function funcListenBySocketFull(e, status, onStop) {
97
+ const r = funcListenBySocket4(e, {
98
+ status,
99
+ addListenClose: onStop,
100
+ stop: (x) => x?.("___STOP"),
101
+ });
102
+ return r;
103
+ }
84
104
  exports.funcListenBySocket1 = funcListenBySocket;
85
105
  function CompareKeys(obj1, obj2) {
86
106
  return CompareKeys2(obj1, Object.keys(obj2));
@@ -112,6 +132,9 @@ function deepModifyByListenSocket2(obj, data) {
112
132
  return DeepCompareKeys(obj, NOOP_LISTEN, (e) => funcListenBySocket2(e, data));
113
133
  }
114
134
  function deepModifyByListenSocket3(obj, data) {
115
- return DeepCompareKeys(obj, NOOP_LISTEN, (e) => funcListenBySocket3(e, data));
135
+ return DeepCompareKeys(obj, NOOP_LISTEN, (e) => funcListenBySocket4(e, data));
136
+ }
137
+ function deepModifyByListenSocket4(obj, status) {
138
+ return DeepCompareKeys(obj, NOOP_LISTEN, (e) => funcListenBySocketFull(e, status));
116
139
  }
117
140
  exports.funcListenBySocketObj = deepModifyByListenSocket;
@@ -3,6 +3,10 @@ type Socket = {
3
3
  on: (e: string, cb: (d: any) => void) => void;
4
4
  };
5
5
  type Func = (...args: any[]) => any;
6
+ export declare const createIdPool: () => {
7
+ next: () => number;
8
+ release(id: number): void;
9
+ };
6
10
  export declare function rpcEndCallback(fn: Function): void;
7
11
  type PromiseServerHooks<T> = {
8
12
  onRequest?: (ctx: {
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createIdPool = void 0;
3
4
  exports.rpcEndCallback = rpcEndCallback;
4
5
  exports.CreatAPIFacadeServer2 = createAPIFacadeServer;
5
6
  exports.CreatAPIFacadeClient2 = createAPIFacadeClient;
@@ -8,13 +9,15 @@ const FN_MARKER = "$_f";
8
9
  const BANNED_KEYS = new Set(["__proto__", "constructor", "prototype"]);
9
10
  const isSafeKey = (k) => !BANNED_KEYS.has(k);
10
11
  const hasOwn = (obj, k) => Object.prototype.hasOwnProperty.call(obj, k);
11
- class IdPool {
12
- s = [];
13
- p = 0;
14
- n = 0;
15
- next() { return this.p > 0 ? this.s[--this.p] : ++this.n; }
16
- release(id) { this.s[this.p++] = id; }
17
- }
12
+ const createIdPool = () => {
13
+ const s = [];
14
+ let n = 0;
15
+ return {
16
+ next: () => s.length > 0 ? s.pop() : ++n,
17
+ release(id) { s.push(id); }
18
+ };
19
+ };
20
+ exports.createIdPool = createIdPool;
18
21
  function walk(val, onLeaf) {
19
22
  if (val == null || typeof val !== "object")
20
23
  return onLeaf(val);
@@ -190,7 +193,7 @@ function createServer(socket, key, target, hooks) {
190
193
  }
191
194
  function createClient(socket, key, opts) {
192
195
  const limit = opts?.limit ?? 10000;
193
- const pool = new IdPool();
196
+ const pool = (0, exports.createIdPool)();
194
197
  const pending = new Map();
195
198
  const callbacks = new Map();
196
199
  const routeCache = {};
@@ -0,0 +1,17 @@
1
+ import { UseListen } from "./Listen";
2
+ type listen<T extends any[] = any[]> = ReturnType<typeof UseListen<T>>;
3
+ type KeyExtractor<D> = (data: D) => string | undefined;
4
+ type ListenMap<T extends Record<string, any>> = {
5
+ [K in keyof T]: listen<T[K]>[1];
6
+ };
7
+ type CollectedResult<T extends Record<string, any>> = {
8
+ [K in keyof T]: T[K];
9
+ };
10
+ type JoinResult<R> = {
11
+ listen: listen<[R, string]>[1];
12
+ pending: Map<string, Map<string, any>>;
13
+ clear: (tid?: string) => void;
14
+ };
15
+ export declare function joinListens<T extends Record<string, any[]>>(listens: ListenMap<T>, keyExtractor?: KeyExtractor<any>): JoinResult<CollectedResult<T>>;
16
+ export declare function joinListens<D extends any[] = any[]>(listens: listen<D>[1][], keyExtractor?: KeyExtractor<any>): JoinResult<D[][]>;
17
+ export {};
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.joinListens = joinListens;
4
+ const Listen_1 = require("./Listen");
5
+ function joinListens(listens, keyExtractor) {
6
+ const isArray = Array.isArray(listens);
7
+ const map = isArray
8
+ ? Object.fromEntries(listens.map((l, i) => [String(i), l]))
9
+ : listens;
10
+ const [set, out] = (0, Listen_1.UseListen)();
11
+ const keys = Object.keys(map);
12
+ const buckets = new Map();
13
+ const getKey = (data) => keyExtractor?.(data) ?? "_";
14
+ const tryFire = (tid) => {
15
+ const bucket = buckets.get(tid);
16
+ if (bucket.size < keys.length)
17
+ return;
18
+ const result = isArray
19
+ ? keys.map(k => bucket.get(k))
20
+ : Object.fromEntries(bucket);
21
+ buckets.delete(tid);
22
+ set(result, tid);
23
+ };
24
+ for (const portId of keys) {
25
+ map[portId].addListen((...data) => {
26
+ const tid = getKey(data[0]);
27
+ if (!buckets.has(tid))
28
+ buckets.set(tid, new Map());
29
+ buckets.get(tid).set(portId, data);
30
+ tryFire(tid);
31
+ });
32
+ }
33
+ return {
34
+ listen: out,
35
+ pending: buckets,
36
+ clear: (tid) => {
37
+ tid ? buckets.delete(tid) : buckets.clear();
38
+ }
39
+ };
40
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wenay-common",
3
- "version": "1.0.238",
3
+ "version": "1.0.239",
4
4
  "description": "Common library",
5
5
  "strict": true,
6
6
  "main": "lib/index.js",