wenay-common2 1.0.42 → 1.0.44
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.
- package/lib/Common/events/Listen.d.ts +26 -14
- package/lib/Common/events/Listen.js +12 -11
- package/lib/Common/events/SocketBuffer.d.ts +10 -4
- package/lib/Common/events/SocketServerHook.d.ts +12 -6
- package/lib/Common/events/UseListenTransform.d.ts +6 -3
- package/lib/Common/funcTimeWait.d.ts +10 -7
- package/lib/Common/funcTimeWait.js +54 -56
- package/lib/Common/rcp/listen-socket.js +1 -1
- package/lib/Exchange/LoadBase.d.ts +1 -2
- package/lib/Exchange/LoadBase.js +116 -59
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type Listener<T extends any[]> = (...r: T) => void;
|
|
2
2
|
export type NormalizeTuple<T> = T extends any[] ? T : [T];
|
|
3
|
-
export declare function funcListenCallbackBase<T>(b: (e: Listener<NormalizeTuple<T>>) => (void | (() => void)), data?: {
|
|
3
|
+
export declare function funcListenCallbackBase<T, T2 = any>(b: (e: Listener<NormalizeTuple<T>>) => (void | (() => void)), data?: {
|
|
4
4
|
event?: (type: "add" | "remove", count: number, api: ReturnType<typeof funcListenCallbackBase<T>>) => void;
|
|
5
5
|
fast?: boolean;
|
|
6
6
|
addListenClose?: ReturnType<typeof funcListenCallbackBase<any>>;
|
|
@@ -10,11 +10,14 @@ export declare function funcListenCallbackBase<T>(b: (e: Listener<NormalizeTuple
|
|
|
10
10
|
run: () => void;
|
|
11
11
|
close: () => void;
|
|
12
12
|
eventClose: (cb: () => void) => () => void;
|
|
13
|
-
removeEventClose: (cb: () => void) => void;
|
|
14
|
-
addListen: (cb: Listener<NormalizeTuple<T>>, cbClose
|
|
15
|
-
|
|
13
|
+
removeEventClose: (cb: (() => void)) => void;
|
|
14
|
+
addListen: (cb: Listener<NormalizeTuple<T>>, { cbClose, key }?: {
|
|
15
|
+
cbClose?: () => void;
|
|
16
|
+
key?: T2;
|
|
17
|
+
}) => () => void;
|
|
18
|
+
removeListen: (k: Listener<NormalizeTuple<T>> | null | T2) => void;
|
|
16
19
|
count: () => number;
|
|
17
|
-
readonly getAllKeys: Listener<NormalizeTuple<T>>[];
|
|
20
|
+
readonly getAllKeys: (T2 | Listener<NormalizeTuple<T>>)[];
|
|
18
21
|
};
|
|
19
22
|
export declare function funcListenCallbackFast<T>(a: (e: (Listener<NormalizeTuple<T>> | null)) => (void | (() => void))): {
|
|
20
23
|
func: Listener<NormalizeTuple<T>>;
|
|
@@ -23,10 +26,13 @@ export declare function funcListenCallbackFast<T>(a: (e: (Listener<NormalizeTupl
|
|
|
23
26
|
close: () => void;
|
|
24
27
|
eventClose: (cb: () => void) => () => void;
|
|
25
28
|
removeEventClose: (cb: () => void) => void;
|
|
26
|
-
addListen: (cb: Listener<NormalizeTuple<T>>, cbClose
|
|
27
|
-
|
|
29
|
+
addListen: (cb: Listener<NormalizeTuple<T>>, { cbClose, key }?: {
|
|
30
|
+
cbClose?: (() => void) | undefined;
|
|
31
|
+
key?: any;
|
|
32
|
+
}) => () => void;
|
|
33
|
+
removeListen: (k: any) => void;
|
|
28
34
|
count: () => number;
|
|
29
|
-
readonly getAllKeys: Listener<NormalizeTuple<
|
|
35
|
+
readonly getAllKeys: (T2 | Listener<NormalizeTuple<T_1>>)[];
|
|
30
36
|
};
|
|
31
37
|
export declare function funcListenCallback<T>(a: (e: (Listener<NormalizeTuple<T>> | null)) => (void | (() => void)), event?: (type: "add" | "remove", count: number, api: ReturnType<typeof funcListenCallbackBase<T>>) => void, fast?: boolean): {
|
|
32
38
|
func: Listener<NormalizeTuple<T>>;
|
|
@@ -35,10 +41,13 @@ export declare function funcListenCallback<T>(a: (e: (Listener<NormalizeTuple<T>
|
|
|
35
41
|
close: () => void;
|
|
36
42
|
eventClose: (cb: () => void) => () => void;
|
|
37
43
|
removeEventClose: (cb: () => void) => void;
|
|
38
|
-
addListen: (cb: Listener<NormalizeTuple<T>>, cbClose
|
|
39
|
-
|
|
44
|
+
addListen: (cb: Listener<NormalizeTuple<T>>, { cbClose, key }?: {
|
|
45
|
+
cbClose?: (() => void) | undefined;
|
|
46
|
+
key?: any;
|
|
47
|
+
}) => () => void;
|
|
48
|
+
removeListen: (k: any) => void;
|
|
40
49
|
count: () => number;
|
|
41
|
-
readonly getAllKeys: Listener<NormalizeTuple<
|
|
50
|
+
readonly getAllKeys: (T2 | Listener<NormalizeTuple<T_1>>)[];
|
|
42
51
|
};
|
|
43
52
|
export declare function UseListen<T>(data?: Parameters<typeof funcListenCallbackBase<T>>[1]): readonly [(...a: NormalizeTuple<T>) => void, {
|
|
44
53
|
func: Listener<NormalizeTuple<T>>;
|
|
@@ -47,9 +56,12 @@ export declare function UseListen<T>(data?: Parameters<typeof funcListenCallback
|
|
|
47
56
|
close: () => void;
|
|
48
57
|
eventClose: (cb: () => void) => () => void;
|
|
49
58
|
removeEventClose: (cb: () => void) => void;
|
|
50
|
-
addListen: (cb: Listener<NormalizeTuple<T>>, cbClose
|
|
51
|
-
|
|
59
|
+
addListen: (cb: Listener<NormalizeTuple<T>>, { cbClose, key }?: {
|
|
60
|
+
cbClose?: (() => void) | undefined;
|
|
61
|
+
key?: any;
|
|
62
|
+
}) => () => void;
|
|
63
|
+
removeListen: (k: any) => void;
|
|
52
64
|
count: () => number;
|
|
53
|
-
readonly getAllKeys: Listener<NormalizeTuple<
|
|
65
|
+
readonly getAllKeys: (T2 | Listener<NormalizeTuple<T_1>>)[];
|
|
54
66
|
}];
|
|
55
67
|
export declare function isListenCallback(obj: any): obj is ReturnType<typeof funcListenCallbackBase>;
|
|
@@ -75,31 +75,32 @@ function funcListenCallbackBase(b, data) {
|
|
|
75
75
|
sinh.delete(cb);
|
|
76
76
|
evClose.delete(cb);
|
|
77
77
|
},
|
|
78
|
-
addListen: (cb, cbClose) => {
|
|
79
|
-
|
|
78
|
+
addListen: (cb, { cbClose, key } = {}) => {
|
|
79
|
+
const k = key ?? cb;
|
|
80
|
+
obj.set(k, cb);
|
|
80
81
|
if (cbClose) {
|
|
81
|
-
if (evClose.has(
|
|
82
|
-
const r = evClose.get(
|
|
82
|
+
if (evClose.has(k)) {
|
|
83
|
+
const r = evClose.get(k);
|
|
83
84
|
if (r !== cbClose) {
|
|
84
85
|
evClose.delete(r);
|
|
85
|
-
evClose.delete(
|
|
86
|
+
evClose.delete(k);
|
|
86
87
|
sinh.delete(r);
|
|
87
88
|
}
|
|
88
89
|
}
|
|
89
|
-
evClose.set(
|
|
90
|
+
evClose.set(k, cbClose);
|
|
90
91
|
sinh.set(cbClose, cb);
|
|
91
92
|
}
|
|
92
93
|
if (fast)
|
|
93
94
|
rebuild();
|
|
94
95
|
event?.("add", obj.size, api);
|
|
95
|
-
return () => api.removeListen(
|
|
96
|
+
return () => api.removeListen(k);
|
|
96
97
|
},
|
|
97
|
-
removeListen: (
|
|
98
|
-
obj.delete(
|
|
99
|
-
const e = evClose.get(
|
|
98
|
+
removeListen: (k) => {
|
|
99
|
+
obj.delete(k);
|
|
100
|
+
const e = evClose.get(k);
|
|
100
101
|
if (fast)
|
|
101
102
|
rebuild();
|
|
102
|
-
evClose.delete(
|
|
103
|
+
evClose.delete(k);
|
|
103
104
|
if (e) {
|
|
104
105
|
evClose.delete(e);
|
|
105
106
|
sinh.delete(e);
|
|
@@ -18,7 +18,10 @@ export declare function funcListenCallbackSnapshot<T extends realSocket2<any | a
|
|
|
18
18
|
memo: T4;
|
|
19
19
|
snapshot?: (memo: T4) => T3;
|
|
20
20
|
}): {
|
|
21
|
-
run: (cb: import("./Listen").Listener<[data: getTypeCallback<T>, memo: T3]>,
|
|
21
|
+
run: (cb: import("./Listen").Listener<[data: getTypeCallback<T>, memo: T3]>, params_1?: {
|
|
22
|
+
cbClose?: (() => void) | undefined;
|
|
23
|
+
key?: any;
|
|
24
|
+
} | undefined) => () => void;
|
|
22
25
|
snapshot: () => T3 | undefined;
|
|
23
26
|
memo: T4;
|
|
24
27
|
listenA: {
|
|
@@ -28,10 +31,13 @@ export declare function funcListenCallbackSnapshot<T extends realSocket2<any | a
|
|
|
28
31
|
close: () => void;
|
|
29
32
|
eventClose: (cb: () => void) => () => void;
|
|
30
33
|
removeEventClose: (cb: () => void) => void;
|
|
31
|
-
addListen: (cb: import("./Listen").Listener<[data: getTypeCallback<T>, memo: T3]>, cbClose
|
|
32
|
-
|
|
34
|
+
addListen: (cb: import("./Listen").Listener<[data: getTypeCallback<T>, memo: T3]>, { cbClose, key }?: {
|
|
35
|
+
cbClose?: (() => void) | undefined;
|
|
36
|
+
key?: any;
|
|
37
|
+
}) => () => void;
|
|
38
|
+
removeListen: (k: any) => void;
|
|
33
39
|
count: () => number;
|
|
34
|
-
readonly getAllKeys: import("./Listen").Listener<
|
|
40
|
+
readonly getAllKeys: (T2_1 | import("./Listen").Listener<import("./Listen").NormalizeTuple<T_1>>)[];
|
|
35
41
|
};
|
|
36
42
|
connect: () => void;
|
|
37
43
|
readonly disconnect: ((a: Omit<Parameters<T>[0], "callback"> & {
|
|
@@ -11,10 +11,13 @@ export declare function SocketServerHook(opt?: {
|
|
|
11
11
|
close: () => void;
|
|
12
12
|
eventClose: (cb: () => void) => () => void;
|
|
13
13
|
removeEventClose: (cb: () => void) => void;
|
|
14
|
-
addListen: (cb: import("./Listen").Listener<[unknown]>, cbClose
|
|
15
|
-
|
|
14
|
+
addListen: (cb: import("./Listen").Listener<[unknown]>, { cbClose, key }?: {
|
|
15
|
+
cbClose?: (() => void) | undefined;
|
|
16
|
+
key?: any;
|
|
17
|
+
}) => () => void;
|
|
18
|
+
removeListen: (k: any) => void;
|
|
16
19
|
count: () => number;
|
|
17
|
-
readonly getAllKeys: import("./Listen").Listener<[
|
|
20
|
+
readonly getAllKeys: (T2 | import("./Listen").Listener<import("./Listen").NormalizeTuple<T>>)[];
|
|
18
21
|
}];
|
|
19
22
|
};
|
|
20
23
|
get(tag: string): readonly [(a_0: unknown) => void, {
|
|
@@ -24,10 +27,13 @@ export declare function SocketServerHook(opt?: {
|
|
|
24
27
|
close: () => void;
|
|
25
28
|
eventClose: (cb: () => void) => () => void;
|
|
26
29
|
removeEventClose: (cb: () => void) => void;
|
|
27
|
-
addListen: (cb: import("./Listen").Listener<[unknown]>, cbClose
|
|
28
|
-
|
|
30
|
+
addListen: (cb: import("./Listen").Listener<[unknown]>, { cbClose, key }?: {
|
|
31
|
+
cbClose?: (() => void) | undefined;
|
|
32
|
+
key?: any;
|
|
33
|
+
}) => () => void;
|
|
34
|
+
removeListen: (k: any) => void;
|
|
29
35
|
count: () => number;
|
|
30
|
-
readonly getAllKeys: import("./Listen").Listener<[
|
|
36
|
+
readonly getAllKeys: (T2 | import("./Listen").Listener<import("./Listen").NormalizeTuple<T>>)[];
|
|
31
37
|
}];
|
|
32
38
|
provider: (tag: string, data: any) => void;
|
|
33
39
|
};
|
|
@@ -8,8 +8,11 @@ export declare function UseListenTransform<TSource extends any[], TTarget extend
|
|
|
8
8
|
close: () => void;
|
|
9
9
|
eventClose: (cb: () => void) => () => void;
|
|
10
10
|
removeEventClose: (cb: () => void) => void;
|
|
11
|
-
addListen: (cb: Listener<NormalizeTuple<TTarget>>, cbClose
|
|
12
|
-
|
|
11
|
+
addListen: (cb: Listener<NormalizeTuple<TTarget>>, { cbClose, key }?: {
|
|
12
|
+
cbClose?: (() => void) | undefined;
|
|
13
|
+
key?: any;
|
|
14
|
+
}) => () => void;
|
|
15
|
+
removeListen: (k: any) => void;
|
|
13
16
|
count: () => number;
|
|
14
|
-
readonly getAllKeys: Listener<NormalizeTuple<
|
|
17
|
+
readonly getAllKeys: (T2 | Listener<NormalizeTuple<T>>)[];
|
|
15
18
|
}];
|
|
@@ -1,29 +1,32 @@
|
|
|
1
1
|
export type tApiKey = string;
|
|
2
|
-
type tType = "UID" | "IP" |
|
|
2
|
+
type tType = "UID" | "IP" | tApiKey;
|
|
3
3
|
type tFunc = {
|
|
4
4
|
timeStamp?: number;
|
|
5
5
|
type: tType;
|
|
6
6
|
weight: number;
|
|
7
7
|
};
|
|
8
|
-
export declare function funcTimeW(
|
|
8
|
+
export declare function funcTimeW(): {
|
|
9
9
|
dStatic: {
|
|
10
10
|
[key: string]: [number, number][];
|
|
11
11
|
};
|
|
12
|
+
data: any[];
|
|
12
13
|
add(item: tFunc): void;
|
|
13
14
|
cleanByTime(type: tType, ms?: number): void;
|
|
14
15
|
weight(type: tType, ms?: number): number;
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
weightNow(type: tType, ms?: number): number;
|
|
17
|
+
byWeight(type: tType, weight?: number): number;
|
|
18
|
+
byWeightTimeNow(type: tType, timeNow?: number, weight?: number): number;
|
|
17
19
|
};
|
|
18
20
|
export declare const FuncTimeWait: {
|
|
19
21
|
dStatic: {
|
|
20
22
|
[key: string]: [number, number][];
|
|
21
23
|
};
|
|
24
|
+
data: any[];
|
|
22
25
|
add(item: tFunc): void;
|
|
23
26
|
cleanByTime(type: tType, ms?: number): void;
|
|
24
27
|
weight(type: tType, ms?: number): number;
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
weightNow(type: tType, ms?: number): number;
|
|
29
|
+
byWeight(type: tType, weight?: number): number;
|
|
30
|
+
byWeightTimeNow(type: tType, timeNow?: number, weight?: number): number;
|
|
27
31
|
};
|
|
28
|
-
export declare function testFuncTimeW(): void;
|
|
29
32
|
export {};
|
|
@@ -2,74 +2,89 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FuncTimeWait = void 0;
|
|
4
4
|
exports.funcTimeW = funcTimeW;
|
|
5
|
-
|
|
6
|
-
const common_1 = require("./core/common");
|
|
7
|
-
function funcTimeW(maxHistoryElements = 800) {
|
|
5
|
+
function funcTimeW() {
|
|
8
6
|
const dStatic = {};
|
|
9
|
-
|
|
10
|
-
if (arr.length === 0)
|
|
11
|
-
return 0;
|
|
12
|
-
if (timeStamp >= arr[arr.length - 1][0])
|
|
13
|
-
return arr.length;
|
|
14
|
-
if (timeStamp <= arr[0][0])
|
|
15
|
-
return 0;
|
|
16
|
-
const index = (0, common_1.BSearch)(arr, timeStamp, (a, b) => a[0] - b, "greatOrEqual", "ascend");
|
|
17
|
-
return index === -1 ? arr.length : index;
|
|
18
|
-
}
|
|
19
|
-
function cleanupEmptyKey(type) {
|
|
20
|
-
if (dStatic[type] && dStatic[type].length === 0) {
|
|
21
|
-
delete dStatic[type];
|
|
22
|
-
}
|
|
23
|
-
}
|
|
7
|
+
const data = [];
|
|
24
8
|
return {
|
|
25
9
|
dStatic,
|
|
10
|
+
data,
|
|
26
11
|
add(item) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const insertIndex = getInsertIndex(arr, timeStamp);
|
|
30
|
-
if (insertIndex === arr.length) {
|
|
31
|
-
arr.push([timeStamp, item.weight]);
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
arr.splice(insertIndex, 0, [timeStamp, item.weight]);
|
|
12
|
+
if (!dStatic[item.type]) {
|
|
13
|
+
dStatic[item.type] = [];
|
|
35
14
|
}
|
|
15
|
+
dStatic[item.type].push([item.timeStamp ?? Date.now(), item.weight]);
|
|
36
16
|
},
|
|
37
17
|
cleanByTime(type, ms = 60 * 1000) {
|
|
38
18
|
const arr = dStatic[type];
|
|
39
19
|
if (!arr || arr.length === 0)
|
|
40
20
|
return;
|
|
41
|
-
const
|
|
42
|
-
if (arr[0][0] >
|
|
21
|
+
const timeStamp = Date.now();
|
|
22
|
+
if (arr[0][0] > timeStamp - ms)
|
|
43
23
|
return;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
24
|
+
let cutIndex = 0;
|
|
25
|
+
while (cutIndex < arr.length && arr[cutIndex][0] < timeStamp - ms) {
|
|
26
|
+
cutIndex++;
|
|
47
27
|
}
|
|
48
|
-
|
|
28
|
+
if (cutIndex > 0) {
|
|
49
29
|
arr.splice(0, cutIndex);
|
|
50
30
|
}
|
|
51
|
-
cleanupEmptyKey(type);
|
|
52
31
|
},
|
|
53
32
|
weight(type, ms = 60 * 1000) {
|
|
54
33
|
const arr = dStatic[type];
|
|
55
34
|
if (!arr || arr.length === 0)
|
|
56
35
|
return 0;
|
|
57
|
-
const
|
|
36
|
+
const timeStamp = Date.now();
|
|
58
37
|
let sum = 0;
|
|
59
38
|
let i = arr.length - 1;
|
|
60
39
|
for (; i >= 0; i--) {
|
|
61
40
|
const [_time, _weight] = arr[i];
|
|
62
|
-
if (_time <
|
|
41
|
+
if (_time < timeStamp - ms)
|
|
63
42
|
break;
|
|
64
43
|
sum += _weight;
|
|
65
44
|
}
|
|
66
45
|
if (i >= 0) {
|
|
67
46
|
arr.splice(0, i + 1);
|
|
68
|
-
cleanupEmptyKey(type);
|
|
69
47
|
}
|
|
70
48
|
return sum;
|
|
71
49
|
},
|
|
72
|
-
|
|
50
|
+
weightNow(type, ms = 60 * 1000) {
|
|
51
|
+
const arr = dStatic[type];
|
|
52
|
+
if (!arr || arr.length === 0)
|
|
53
|
+
return 0;
|
|
54
|
+
const timeStamp = Date.now();
|
|
55
|
+
let sum = 0;
|
|
56
|
+
let i = arr.length - 1;
|
|
57
|
+
for (; i >= 0; i--) {
|
|
58
|
+
const [_time, _weight] = arr[i];
|
|
59
|
+
if (_time < timeStamp - ms)
|
|
60
|
+
break;
|
|
61
|
+
sum += _weight;
|
|
62
|
+
}
|
|
63
|
+
if (i >= 0) {
|
|
64
|
+
arr.splice(0, i + 1);
|
|
65
|
+
}
|
|
66
|
+
return sum;
|
|
67
|
+
},
|
|
68
|
+
byWeight(type, weight = 50000) {
|
|
69
|
+
const arr = dStatic[type];
|
|
70
|
+
if (!arr || arr.length === 0)
|
|
71
|
+
return 0;
|
|
72
|
+
let sum = 0;
|
|
73
|
+
let i = arr.length - 1;
|
|
74
|
+
let result = 0;
|
|
75
|
+
for (; i >= 0; i--) {
|
|
76
|
+
sum += arr[i][1];
|
|
77
|
+
if (sum > weight) {
|
|
78
|
+
result = arr[i + 1]?.[0] ?? 0;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (i > 800) {
|
|
83
|
+
arr.splice(0, i - 800);
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
},
|
|
87
|
+
byWeightTimeNow(type, timeNow = Date.now(), weight = 50000) {
|
|
73
88
|
const arr = dStatic[type];
|
|
74
89
|
if (!arr || arr.length === 0)
|
|
75
90
|
return 0;
|
|
@@ -89,28 +104,11 @@ function funcTimeW(maxHistoryElements = 800) {
|
|
|
89
104
|
break;
|
|
90
105
|
}
|
|
91
106
|
}
|
|
92
|
-
if (i >
|
|
93
|
-
arr.splice(0, i -
|
|
94
|
-
cleanupEmptyKey(type);
|
|
107
|
+
if (i > 800) {
|
|
108
|
+
arr.splice(0, i - 800);
|
|
95
109
|
}
|
|
96
110
|
return result;
|
|
97
111
|
},
|
|
98
|
-
removeKey(type) {
|
|
99
|
-
delete dStatic[type];
|
|
100
|
-
}
|
|
101
112
|
};
|
|
102
113
|
}
|
|
103
114
|
exports.FuncTimeWait = funcTimeW();
|
|
104
|
-
function testFuncTimeW() {
|
|
105
|
-
const tracker = funcTimeW();
|
|
106
|
-
const type = "UID";
|
|
107
|
-
const now = Date.now();
|
|
108
|
-
tracker.add({ type, weight: 1, timeStamp: now });
|
|
109
|
-
tracker.add({ type, weight: 2, timeStamp: now - 500 });
|
|
110
|
-
tracker.add({ type, weight: 3, timeStamp: now + 500 });
|
|
111
|
-
console.log("funcTimeW order", tracker.dStatic);
|
|
112
|
-
tracker.cleanByTime(type, 200);
|
|
113
|
-
console.log("funcTimeW cleanByTime", tracker.dStatic);
|
|
114
|
-
const w = tracker.weight(type, 1000);
|
|
115
|
-
console.log("funcTimeW weight 1s", w, tracker.dStatic);
|
|
116
|
-
}
|
|
@@ -71,7 +71,7 @@ function listenSocket(e, d) {
|
|
|
71
71
|
}
|
|
72
72
|
inner(...a);
|
|
73
73
|
};
|
|
74
|
-
addListen(active, removeCallback);
|
|
74
|
+
addListen(active, { cbClose: removeCallback });
|
|
75
75
|
addListenClose?.addListen(removeCallback);
|
|
76
76
|
return new Promise((resolve) => {
|
|
77
77
|
resolveWait = () => { resolve(); };
|
package/lib/Exchange/LoadBase.js
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LoadQuoteBase = LoadQuoteBase;
|
|
4
|
+
const wenay_common2_1 = require("wenay-common2");
|
|
4
5
|
const funcTimeWait_1 = require("../Common/funcTimeWait");
|
|
5
|
-
const common_1 = require("../Common/core/common");
|
|
6
6
|
function LoadQuoteBase(setting, data) {
|
|
7
|
-
const { base, maxLoadBars, intervalToName } = setting;
|
|
7
|
+
const { base, maxLoadBars, countConnect, intervalToName } = setting;
|
|
8
|
+
const maxLoadBars2 = setting.maxLoadBars2 ?? maxLoadBars;
|
|
8
9
|
const startMap = new Map();
|
|
9
10
|
const keyName = setting.nameKey ?? "loadKey";
|
|
10
11
|
const time = setting.time ?? 60000;
|
|
11
12
|
const other = data;
|
|
12
13
|
const getDataEl = (a) => setting.controlTimeToNumber?.(a);
|
|
13
14
|
async function waitLimit(weight = 1) {
|
|
14
|
-
const
|
|
15
|
-
const t1 = oldestTs > 0 ? oldestTs - Date.now() + time + 1 : 0;
|
|
15
|
+
const t1 = funcTimeWait_1.FuncTimeWait.byWeight(keyName, setting.countConnect) - (Date.now() - time) + 1;
|
|
16
16
|
if (t1 > 0) {
|
|
17
|
-
|
|
17
|
+
funcTimeWait_1.FuncTimeWait.add({ type: keyName, weight: weight, timeStamp: Date.now() + t1 });
|
|
18
|
+
await (0, wenay_common2_1.sleepAsync)(t1);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
funcTimeWait_1.FuncTimeWait.add({ type: keyName, weight: weight, timeStamp: Date.now() });
|
|
18
22
|
}
|
|
19
|
-
funcTimeWait_1.FuncTimeWait.add({ type: keyName, weight: weight, timeStamp: Date.now() });
|
|
20
23
|
}
|
|
21
24
|
const mapTimeToName = new Map(intervalToName.map((e) => [e.time.sec, e]));
|
|
22
25
|
const _fetch = other?.fetch ?? fetch;
|
|
@@ -26,82 +29,136 @@ function LoadQuoteBase(setting, data) {
|
|
|
26
29
|
throw "_fetch - не определен";
|
|
27
30
|
if (!infoTF)
|
|
28
31
|
throw "нет такого таймфрейма";
|
|
32
|
+
let lastTime;
|
|
29
33
|
const nameForMap = info.exchangeName + info.symbol + infoTF.name;
|
|
30
34
|
let leftTime = startMap.get(nameForMap);
|
|
31
35
|
if (!leftTime) {
|
|
32
36
|
await waitLimit();
|
|
33
|
-
|
|
34
|
-
symbol: info.symbol, baseURL: base, interval: infoTF.name,
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
37
|
+
try {
|
|
38
|
+
leftTime = await setting.funcFistTime({ symbol: info.symbol, baseURL: base, interval: infoTF.name, fetch: _fetch, intervalTF: info.tf, waitLimit });
|
|
39
|
+
startMap.set(nameForMap, leftTime);
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
if (data?.error == true)
|
|
43
|
+
throw e;
|
|
44
|
+
else
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
38
47
|
}
|
|
39
|
-
const time1 = Math.max(info.time1.valueOf(), leftTime.valueOf());
|
|
40
|
-
const time2 = info.time2.valueOf();
|
|
48
|
+
const [time1, time2] = [Math.max(info.time1.valueOf(), leftTime.valueOf()), info.time2.valueOf()];
|
|
41
49
|
if (time2 <= time1) {
|
|
42
50
|
return [];
|
|
43
51
|
}
|
|
44
|
-
const [
|
|
52
|
+
const [t1, t2] = info.right ? [time1, time2] : [time2, time1];
|
|
45
53
|
const arr = [];
|
|
46
54
|
const interval = infoTF.time.valueOf();
|
|
55
|
+
const map = [];
|
|
47
56
|
if (maxLoadBars instanceof Date) {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
57
|
+
const [step1] = [
|
|
58
|
+
maxLoadBars.valueOf()
|
|
59
|
+
];
|
|
60
|
+
arr.push(lastTime = t1);
|
|
61
|
+
let barsTime = (t1 - t2);
|
|
62
|
+
if (barsTime <= maxLoadBars.valueOf())
|
|
63
|
+
arr.push(t2);
|
|
64
|
+
else {
|
|
65
|
+
barsTime -= maxLoadBars.valueOf();
|
|
66
|
+
arr.push(lastTime = lastTime - step1);
|
|
67
|
+
for (; barsTime > 0; barsTime -= maxLoadBars.valueOf())
|
|
68
|
+
arr.push(lastTime = lastTime - step1);
|
|
69
|
+
if (barsTime < 0)
|
|
70
|
+
arr.push(t2);
|
|
54
71
|
}
|
|
55
|
-
if (arr[arr.length - 1] > tOlder)
|
|
56
|
-
arr.push(tOlder);
|
|
57
72
|
}
|
|
58
|
-
else if (typeof maxLoadBars
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
73
|
+
else if (typeof maxLoadBars == "number") {
|
|
74
|
+
const [step1] = [
|
|
75
|
+
maxLoadBars * interval,
|
|
76
|
+
];
|
|
77
|
+
arr.push(lastTime = t1);
|
|
78
|
+
let bars = (t1 - t2) / interval;
|
|
79
|
+
if (bars <= maxLoadBars)
|
|
80
|
+
arr.push(t2);
|
|
81
|
+
else {
|
|
64
82
|
bars -= maxLoadBars;
|
|
83
|
+
arr.push(lastTime = lastTime - step1);
|
|
84
|
+
for (; bars > 0; bars -= maxLoadBars)
|
|
85
|
+
arr.push(lastTime = lastTime - step1);
|
|
86
|
+
arr.push(t2);
|
|
65
87
|
}
|
|
66
|
-
if (arr[arr.length - 1] > tOlder)
|
|
67
|
-
arr.push(tOlder);
|
|
68
88
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const endTime = arr[i];
|
|
72
|
-
const startTime = arr[i + 1];
|
|
73
|
-
if (startTime >= endTime)
|
|
89
|
+
for (let i = 1; i < arr.length; i++) {
|
|
90
|
+
if (arr[i].valueOf() >= arr[i - 1].valueOf())
|
|
74
91
|
continue;
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
let res = await setting.funcLoad(
|
|
90
|
-
if (setting.controlTimeToNumber && res.length
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
const loader = async () => {
|
|
93
|
+
const data = {
|
|
94
|
+
maxLoadBars: maxLoadBars,
|
|
95
|
+
fetch: _fetch,
|
|
96
|
+
baseURL: base,
|
|
97
|
+
symbol: info.symbol,
|
|
98
|
+
interval: infoTF.name,
|
|
99
|
+
startTime: new Date(arr[i]),
|
|
100
|
+
endTime: new Date(arr[i - 1]),
|
|
101
|
+
limit: maxLoadBars,
|
|
102
|
+
intervalTF: info.tf,
|
|
103
|
+
waitLimit
|
|
104
|
+
};
|
|
105
|
+
await waitLimit();
|
|
106
|
+
let res = await setting.funcLoad(data);
|
|
107
|
+
if (setting.controlTimeToNumber && res.length) {
|
|
108
|
+
let [t1, t2] = [
|
|
109
|
+
getDataEl(res[0]),
|
|
110
|
+
getDataEl(res.at(-1))
|
|
111
|
+
];
|
|
112
|
+
if (t1 && t2 && t1 > t2) {
|
|
94
113
|
res = res.reverse();
|
|
95
114
|
}
|
|
96
115
|
}
|
|
97
|
-
|
|
116
|
+
return res;
|
|
117
|
+
};
|
|
118
|
+
map.push(loader());
|
|
119
|
+
}
|
|
120
|
+
const resulI = await Promise.allSettled(map);
|
|
121
|
+
const result = [];
|
|
122
|
+
for (let i = resulI.length - 1; i >= 0; i--) {
|
|
123
|
+
const el = resulI[i];
|
|
124
|
+
if (el.status == "fulfilled") {
|
|
125
|
+
result.push(...el.value);
|
|
98
126
|
}
|
|
99
|
-
|
|
100
|
-
console.error(
|
|
101
|
-
if (data?.error
|
|
102
|
-
throw
|
|
127
|
+
if (el.status == "rejected") {
|
|
128
|
+
console.error(el.reason);
|
|
129
|
+
if (data?.error == true)
|
|
130
|
+
throw el.reason;
|
|
103
131
|
}
|
|
104
132
|
}
|
|
105
133
|
return result;
|
|
106
134
|
};
|
|
107
135
|
}
|
|
136
|
+
async function test() {
|
|
137
|
+
const arr = [];
|
|
138
|
+
for (let i = 0; i < 10000; i++) {
|
|
139
|
+
arr[i] = { time: Date.now() - i * wenay_common2_1.TF.H1.msec, price: i };
|
|
140
|
+
}
|
|
141
|
+
let ress = [];
|
|
142
|
+
const tt = LoadQuoteBase({
|
|
143
|
+
base: "",
|
|
144
|
+
countConnect: 2,
|
|
145
|
+
funcFistTime: async ({}) => {
|
|
146
|
+
const time = new Date(arr.at(-1).time);
|
|
147
|
+
console.log("funcFistTime: ", time);
|
|
148
|
+
return time;
|
|
149
|
+
},
|
|
150
|
+
nameKey: "cd",
|
|
151
|
+
maxLoadBars: 100,
|
|
152
|
+
time: 50,
|
|
153
|
+
intervalToName: [{ name: "1", time: wenay_common2_1.TF.H1 }],
|
|
154
|
+
funcLoad: (data) => {
|
|
155
|
+
ress.push(data.startTime);
|
|
156
|
+
ress.push(data.endTime);
|
|
157
|
+
return (async () => [{ time: 5 }])();
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
const res = await tt({ symbol: "s", time2: new Date(), tf: wenay_common2_1.TF.H1, time1: new Date(2015) });
|
|
161
|
+
ress.sort((a, b) => a.valueOf() - b.valueOf());
|
|
162
|
+
console.log(ress);
|
|
163
|
+
console.log(res);
|
|
164
|
+
}
|