@pisell/core 0.0.2
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/README.md +1 -0
- package/es/app/app.d.ts +101 -0
- package/es/app/app.js +177 -0
- package/es/app/const.d.ts +4 -0
- package/es/app/const.js +6 -0
- package/es/app/index.d.ts +14 -0
- package/es/app/index.js +52 -0
- package/es/applicationManager/application.d.ts +197 -0
- package/es/applicationManager/application.js +469 -0
- package/es/applicationManager/index.d.ts +19 -0
- package/es/applicationManager/index.js +206 -0
- package/es/aws/index.d.ts +16 -0
- package/es/aws/index.js +76 -0
- package/es/cmd/const.d.ts +11 -0
- package/es/cmd/const.js +13 -0
- package/es/cmd/index.d.ts +13 -0
- package/es/cmd/index.js +91 -0
- package/es/cmd/type.d.ts +5 -0
- package/es/cmd/type.js +1 -0
- package/es/communicationManager/index.d.ts +59 -0
- package/es/communicationManager/index.js +174 -0
- package/es/config.d.ts +3 -0
- package/es/config.js +14 -0
- package/es/cookie/index.d.ts +13 -0
- package/es/cookie/index.js +47 -0
- package/es/css/global.less +73 -0
- package/es/css/index.less +31 -0
- package/es/css/variables.css +84 -0
- package/es/data/index.d.ts +8 -0
- package/es/data/index.js +16 -0
- package/es/history/config.d.ts +24 -0
- package/es/history/config.js +43 -0
- package/es/history/index.d.ts +26 -0
- package/es/history/index.js +80 -0
- package/es/history/type.d.ts +2 -0
- package/es/history/type.js +1 -0
- package/es/hooks/index.d.ts +12 -0
- package/es/hooks/index.js +13 -0
- package/es/hooks/useDelayedValue/index.d.ts +2 -0
- package/es/hooks/useDelayedValue/index.js +26 -0
- package/es/hooks/useDispatch/index.d.ts +2 -0
- package/es/hooks/useDispatch/index.js +2 -0
- package/es/hooks/useLowCode/index.d.ts +13 -0
- package/es/hooks/useLowCode/index.js +74 -0
- package/es/hooks/useStore/index.d.ts +6 -0
- package/es/hooks/useStore/index.js +11 -0
- package/es/index.d.ts +8 -0
- package/es/index.js +10 -0
- package/es/indexDB/index.d.ts +296 -0
- package/es/indexDB/index.js +1619 -0
- package/es/locales/en.d.ts +3 -0
- package/es/locales/en.js +5 -0
- package/es/locales/index.d.ts +39 -0
- package/es/locales/index.js +199 -0
- package/es/locales/original.d.ts +3 -0
- package/es/locales/original.js +7 -0
- package/es/locales/type.d.ts +19 -0
- package/es/locales/type.js +1 -0
- package/es/locales/zh-CN.d.ts +3 -0
- package/es/locales/zh-CN.js +5 -0
- package/es/locales/zh-HK.d.ts +3 -0
- package/es/locales/zh-HK.js +5 -0
- package/es/logger/feishu.d.ts +11 -0
- package/es/logger/feishu.js +50 -0
- package/es/logger/index.d.ts +135 -0
- package/es/logger/index.js +870 -0
- package/es/menuManager/hooks.d.ts +17 -0
- package/es/menuManager/hooks.js +136 -0
- package/es/menuManager/index.d.ts +28 -0
- package/es/menuManager/index.js +165 -0
- package/es/models/global.d.ts +32 -0
- package/es/models/global.js +66 -0
- package/es/models/index.d.ts +45 -0
- package/es/models/index.js +65 -0
- package/es/models/type.d.ts +2 -0
- package/es/models/type.js +1 -0
- package/es/plugin/index.d.ts +0 -0
- package/es/plugin/index.js +0 -0
- package/es/pubsub/example.d.ts +5 -0
- package/es/pubsub/example.js +92 -0
- package/es/pubsub/index.d.ts +73 -0
- package/es/pubsub/index.js +166 -0
- package/es/render/index.d.ts +0 -0
- package/es/render/index.js +0 -0
- package/es/request/cache.d.ts +46 -0
- package/es/request/cache.js +372 -0
- package/es/request/cancelToken.d.ts +38 -0
- package/es/request/cancelToken.js +59 -0
- package/es/request/config.d.ts +3 -0
- package/es/request/config.js +63 -0
- package/es/request/constants.d.ts +2 -0
- package/es/request/constants.js +6 -0
- package/es/request/index.d.ts +24 -0
- package/es/request/index.js +267 -0
- package/es/request/pisell2Request.d.ts +6 -0
- package/es/request/pisell2Request.js +62 -0
- package/es/request/type.d.ts +57 -0
- package/es/request/type.js +10 -0
- package/es/request/utils.d.ts +46 -0
- package/es/request/utils.js +147 -0
- package/es/routes/config.d.ts +7 -0
- package/es/routes/config.js +17 -0
- package/es/routes/index.d.ts +28 -0
- package/es/routes/index.js +151 -0
- package/es/socket/components/SocketMonitorPage.d.ts +6 -0
- package/es/socket/components/SocketMonitorPage.js +485 -0
- package/es/socket/components/index.d.ts +2 -0
- package/es/socket/components/index.js +2 -0
- package/es/socket/constants.d.ts +33 -0
- package/es/socket/constants.js +39 -0
- package/es/socket/events.d.ts +31 -0
- package/es/socket/events.js +19 -0
- package/es/socket/heartbeat.d.ts +66 -0
- package/es/socket/heartbeat.js +184 -0
- package/es/socket/index.d.ts +61 -0
- package/es/socket/index.js +246 -0
- package/es/socket/monitor.d.ts +169 -0
- package/es/socket/monitor.js +448 -0
- package/es/socket/reconnect.d.ts +61 -0
- package/es/socket/reconnect.js +199 -0
- package/es/socket/socket.d.ts +130 -0
- package/es/socket/socket.js +613 -0
- package/es/socket/types.d.ts +85 -0
- package/es/socket/types.js +35 -0
- package/es/storage/index.d.ts +17 -0
- package/es/storage/index.js +46 -0
- package/es/tasks/index.d.ts +127 -0
- package/es/tasks/index.js +1119 -0
- package/es/tasks/scheduledTasksExample.d.ts +61 -0
- package/es/tasks/scheduledTasksExample.js +351 -0
- package/es/tasks/type.d.ts +100 -0
- package/es/tasks/type.js +1 -0
- package/es/tasks/useTasks.d.ts +5 -0
- package/es/tasks/useTasks.js +25 -0
- package/es/type.d.ts +2 -0
- package/es/type.js +1 -0
- package/es/utils/adaptiveThrottle/index.d.ts +36 -0
- package/es/utils/adaptiveThrottle/index.js +136 -0
- package/es/variables/VariablesProvider.d.ts +7 -0
- package/es/variables/VariablesProvider.js +12 -0
- package/es/variables/config.d.ts +3 -0
- package/es/variables/config.js +16 -0
- package/es/variables/index.d.ts +6 -0
- package/es/variables/index.js +5 -0
- package/es/variables/type.d.ts +2 -0
- package/es/variables/type.js +1 -0
- package/es/website/index.d.ts +6 -0
- package/es/website/index.js +65 -0
- package/lib/app/app.d.ts +101 -0
- package/lib/app/app.js +171 -0
- package/lib/app/const.d.ts +4 -0
- package/lib/app/const.js +33 -0
- package/lib/app/index.d.ts +14 -0
- package/lib/app/index.js +76 -0
- package/lib/applicationManager/application.d.ts +197 -0
- package/lib/applicationManager/application.js +280 -0
- package/lib/applicationManager/index.d.ts +19 -0
- package/lib/applicationManager/index.js +88 -0
- package/lib/aws/index.d.ts +16 -0
- package/lib/aws/index.js +67 -0
- package/lib/cmd/const.d.ts +11 -0
- package/lib/cmd/const.js +39 -0
- package/lib/cmd/index.d.ts +13 -0
- package/lib/cmd/index.js +96 -0
- package/lib/cmd/type.d.ts +5 -0
- package/lib/cmd/type.js +17 -0
- package/lib/communicationManager/index.d.ts +59 -0
- package/lib/communicationManager/index.js +102 -0
- package/lib/config.d.ts +3 -0
- package/lib/config.js +36 -0
- package/lib/cookie/index.d.ts +13 -0
- package/lib/cookie/index.js +62 -0
- package/lib/css/global.less +73 -0
- package/lib/css/index.less +31 -0
- package/lib/css/variables.css +84 -0
- package/lib/data/index.d.ts +8 -0
- package/lib/data/index.js +38 -0
- package/lib/history/config.d.ts +24 -0
- package/lib/history/config.js +41 -0
- package/lib/history/index.d.ts +26 -0
- package/lib/history/index.js +99 -0
- package/lib/history/type.d.ts +2 -0
- package/lib/history/type.js +17 -0
- package/lib/hooks/index.d.ts +12 -0
- package/lib/hooks/index.js +44 -0
- package/lib/hooks/useDelayedValue/index.d.ts +2 -0
- package/lib/hooks/useDelayedValue/index.js +36 -0
- package/lib/hooks/useDispatch/index.d.ts +2 -0
- package/lib/hooks/useDispatch/index.js +26 -0
- package/lib/hooks/useLowCode/index.d.ts +13 -0
- package/lib/hooks/useLowCode/index.js +75 -0
- package/lib/hooks/useStore/index.d.ts +6 -0
- package/lib/hooks/useStore/index.js +33 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.js +58 -0
- package/lib/indexDB/index.d.ts +296 -0
- package/lib/indexDB/index.js +767 -0
- package/lib/locales/en.d.ts +3 -0
- package/lib/locales/en.js +29 -0
- package/lib/locales/index.d.ts +39 -0
- package/lib/locales/index.js +159 -0
- package/lib/locales/original.d.ts +3 -0
- package/lib/locales/original.js +31 -0
- package/lib/locales/type.d.ts +19 -0
- package/lib/locales/type.js +17 -0
- package/lib/locales/zh-CN.d.ts +3 -0
- package/lib/locales/zh-CN.js +29 -0
- package/lib/locales/zh-HK.d.ts +3 -0
- package/lib/locales/zh-HK.js +29 -0
- package/lib/logger/feishu.d.ts +11 -0
- package/lib/logger/feishu.js +52 -0
- package/lib/logger/index.d.ts +135 -0
- package/lib/logger/index.js +476 -0
- package/lib/menuManager/hooks.d.ts +17 -0
- package/lib/menuManager/hooks.js +135 -0
- package/lib/menuManager/index.d.ts +28 -0
- package/lib/menuManager/index.js +125 -0
- package/lib/models/global.d.ts +32 -0
- package/lib/models/global.js +62 -0
- package/lib/models/index.d.ts +45 -0
- package/lib/models/index.js +69 -0
- package/lib/models/type.d.ts +2 -0
- package/lib/models/type.js +17 -0
- package/lib/plugin/index.d.ts +0 -0
- package/lib/plugin/index.js +0 -0
- package/lib/pubsub/example.d.ts +5 -0
- package/lib/pubsub/example.js +61 -0
- package/lib/pubsub/index.d.ts +73 -0
- package/lib/pubsub/index.js +141 -0
- package/lib/render/index.d.ts +0 -0
- package/lib/render/index.js +0 -0
- package/lib/request/cache.d.ts +46 -0
- package/lib/request/cache.js +195 -0
- package/lib/request/cancelToken.d.ts +38 -0
- package/lib/request/cancelToken.js +59 -0
- package/lib/request/config.d.ts +3 -0
- package/lib/request/config.js +69 -0
- package/lib/request/constants.d.ts +2 -0
- package/lib/request/constants.js +34 -0
- package/lib/request/index.d.ts +24 -0
- package/lib/request/index.js +210 -0
- package/lib/request/pisell2Request.d.ts +6 -0
- package/lib/request/pisell2Request.js +75 -0
- package/lib/request/type.d.ts +57 -0
- package/lib/request/type.js +36 -0
- package/lib/request/utils.d.ts +46 -0
- package/lib/request/utils.js +113 -0
- package/lib/routes/config.d.ts +7 -0
- package/lib/routes/config.js +50 -0
- package/lib/routes/index.d.ts +28 -0
- package/lib/routes/index.js +121 -0
- package/lib/socket/components/SocketMonitorPage.d.ts +6 -0
- package/lib/socket/components/SocketMonitorPage.js +340 -0
- package/lib/socket/components/index.d.ts +2 -0
- package/lib/socket/components/index.js +39 -0
- package/lib/socket/constants.d.ts +33 -0
- package/lib/socket/constants.js +62 -0
- package/lib/socket/events.d.ts +31 -0
- package/lib/socket/events.js +44 -0
- package/lib/socket/heartbeat.d.ts +66 -0
- package/lib/socket/heartbeat.js +157 -0
- package/lib/socket/index.d.ts +61 -0
- package/lib/socket/index.js +198 -0
- package/lib/socket/monitor.d.ts +169 -0
- package/lib/socket/monitor.js +350 -0
- package/lib/socket/reconnect.d.ts +61 -0
- package/lib/socket/reconnect.js +157 -0
- package/lib/socket/socket.d.ts +130 -0
- package/lib/socket/socket.js +426 -0
- package/lib/socket/types.d.ts +85 -0
- package/lib/socket/types.js +36 -0
- package/lib/storage/index.d.ts +17 -0
- package/lib/storage/index.js +64 -0
- package/lib/tasks/index.d.ts +127 -0
- package/lib/tasks/index.js +710 -0
- package/lib/tasks/scheduledTasksExample.d.ts +61 -0
- package/lib/tasks/scheduledTasksExample.js +267 -0
- package/lib/tasks/type.d.ts +100 -0
- package/lib/tasks/type.js +17 -0
- package/lib/tasks/useTasks.d.ts +5 -0
- package/lib/tasks/useTasks.js +37 -0
- package/lib/type.d.ts +2 -0
- package/lib/type.js +17 -0
- package/lib/utils/adaptiveThrottle/index.d.ts +36 -0
- package/lib/utils/adaptiveThrottle/index.js +121 -0
- package/lib/variables/VariablesProvider.d.ts +7 -0
- package/lib/variables/VariablesProvider.js +51 -0
- package/lib/variables/config.d.ts +3 -0
- package/lib/variables/config.js +38 -0
- package/lib/variables/index.d.ts +6 -0
- package/lib/variables/index.js +29 -0
- package/lib/variables/type.d.ts +2 -0
- package/lib/variables/type.js +17 -0
- package/lib/website/index.d.ts +6 -0
- package/lib/website/index.js +75 -0
- package/package.json +49 -0
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/socket/socket.ts
|
|
20
|
+
var socket_exports = {};
|
|
21
|
+
__export(socket_exports, {
|
|
22
|
+
Socket: () => Socket
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(socket_exports);
|
|
25
|
+
var import_types = require("./types");
|
|
26
|
+
var import_constants = require("./constants");
|
|
27
|
+
var import_events = require("./events");
|
|
28
|
+
var import_heartbeat = require("./heartbeat");
|
|
29
|
+
var import_reconnect = require("./reconnect");
|
|
30
|
+
var import_monitor = require("./monitor");
|
|
31
|
+
var import_app = require("../app");
|
|
32
|
+
var Socket = class {
|
|
33
|
+
ws = null;
|
|
34
|
+
url;
|
|
35
|
+
options;
|
|
36
|
+
status = import_types.SocketStatus.CLOSED;
|
|
37
|
+
eventHandlers = {};
|
|
38
|
+
// 心跳和重连管理器
|
|
39
|
+
heartbeatManager = null;
|
|
40
|
+
reconnectManager = null;
|
|
41
|
+
// 监控器
|
|
42
|
+
monitor = null;
|
|
43
|
+
// 消息ID计数器
|
|
44
|
+
messageIdCounter = 0;
|
|
45
|
+
/**
|
|
46
|
+
* 创建Socket实例
|
|
47
|
+
* @param options Socket配置选项
|
|
48
|
+
*/
|
|
49
|
+
constructor(options) {
|
|
50
|
+
this.url = options.url;
|
|
51
|
+
this.options = {
|
|
52
|
+
...import_constants.DEFAULT_SOCKET_OPTIONS,
|
|
53
|
+
...options
|
|
54
|
+
};
|
|
55
|
+
if (this.options.autoConnect) {
|
|
56
|
+
this.connect().catch((error) => {
|
|
57
|
+
console.warn("Socket connection failed:", error);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* 建立WebSocket连接
|
|
63
|
+
*/
|
|
64
|
+
async connect() {
|
|
65
|
+
const app = (0, import_app.getApp)();
|
|
66
|
+
const isConnected = this.isConnected();
|
|
67
|
+
if (isConnected || this.status === import_types.SocketStatus.CONNECTING) {
|
|
68
|
+
app.logger.addLog({
|
|
69
|
+
type: "info",
|
|
70
|
+
title: "socket已连接或连接中",
|
|
71
|
+
metadata: {
|
|
72
|
+
isConnected,
|
|
73
|
+
status: this.status
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
this.setStatus(import_types.SocketStatus.CONNECTING);
|
|
79
|
+
try {
|
|
80
|
+
this.ws = new WebSocket(this.url, this.options.protocols);
|
|
81
|
+
this.setupWebSocketListeners();
|
|
82
|
+
await new Promise((resolve, reject) => {
|
|
83
|
+
const timeoutId = setTimeout(() => {
|
|
84
|
+
reject(new Error("Connection timeout"));
|
|
85
|
+
}, this.options.timeout);
|
|
86
|
+
const onOpen = () => {
|
|
87
|
+
clearTimeout(timeoutId);
|
|
88
|
+
resolve();
|
|
89
|
+
};
|
|
90
|
+
const onError = (error) => {
|
|
91
|
+
clearTimeout(timeoutId);
|
|
92
|
+
reject(error);
|
|
93
|
+
};
|
|
94
|
+
this.ws.addEventListener("open", onOpen, { once: true });
|
|
95
|
+
this.ws.addEventListener("error", onError, { once: true });
|
|
96
|
+
});
|
|
97
|
+
if (this.options.heartbeat) {
|
|
98
|
+
this.initHeartbeat();
|
|
99
|
+
}
|
|
100
|
+
if (this.options.reconnection) {
|
|
101
|
+
this.initReconnect();
|
|
102
|
+
}
|
|
103
|
+
} catch (error) {
|
|
104
|
+
this.handleConnectionFailure(error);
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 关闭WebSocket连接
|
|
110
|
+
* @param isManual 是否手动关闭
|
|
111
|
+
*/
|
|
112
|
+
disconnect(isManual = false) {
|
|
113
|
+
if (!this.ws || this.status === import_types.SocketStatus.CLOSED || this.status === import_types.SocketStatus.CLOSING) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
this.setStatus(import_types.SocketStatus.CLOSING);
|
|
117
|
+
if (this.heartbeatManager) {
|
|
118
|
+
this.heartbeatManager.stop();
|
|
119
|
+
}
|
|
120
|
+
if (this.reconnectManager) {
|
|
121
|
+
this.reconnectManager.stopReconnect();
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
this.ws.close();
|
|
125
|
+
} catch (error) {
|
|
126
|
+
console.error("WebSocket close error:", error);
|
|
127
|
+
}
|
|
128
|
+
this.setStatus(import_types.SocketStatus.CLOSED);
|
|
129
|
+
this.ws = null;
|
|
130
|
+
this.emit(import_events.SocketEvents.DISCONNECT, { isManual });
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* 发送消息到服务器
|
|
134
|
+
* @param event 事件名称
|
|
135
|
+
* @param data 消息数据
|
|
136
|
+
* @param id 消息ID(可选,默认自动生成)
|
|
137
|
+
*/
|
|
138
|
+
send(type, data, id) {
|
|
139
|
+
if (!this.isConnected()) {
|
|
140
|
+
throw new Error("WebSocket is not connected");
|
|
141
|
+
}
|
|
142
|
+
const message = {
|
|
143
|
+
type,
|
|
144
|
+
data,
|
|
145
|
+
id: id || this.generateMessageId()
|
|
146
|
+
};
|
|
147
|
+
this.sendRaw(message);
|
|
148
|
+
this.emit(import_events.SocketEvents.SEND, message);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* 发送原始消息
|
|
152
|
+
* @param message 消息对象
|
|
153
|
+
*/
|
|
154
|
+
sendRaw(message) {
|
|
155
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
this.ws.send(JSON.stringify(message));
|
|
160
|
+
} catch (error) {
|
|
161
|
+
console.error("WebSocket send error:", error);
|
|
162
|
+
this.emit(import_events.SocketEvents.ERROR, error);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* 注册事件监听器
|
|
167
|
+
* @param event 事件名称
|
|
168
|
+
* @param handler 事件处理函数
|
|
169
|
+
*/
|
|
170
|
+
on(event, handler) {
|
|
171
|
+
if (!this.eventHandlers[event]) {
|
|
172
|
+
this.eventHandlers[event] = [];
|
|
173
|
+
}
|
|
174
|
+
this.eventHandlers[event].push(handler);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* 移除事件监听器
|
|
178
|
+
* @param event 事件名称
|
|
179
|
+
* @param handler 事件处理函数(可选,不提供则移除所有该事件的处理函数)
|
|
180
|
+
*/
|
|
181
|
+
off(event, handler) {
|
|
182
|
+
if (!this.eventHandlers[event]) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (!handler) {
|
|
186
|
+
delete this.eventHandlers[event];
|
|
187
|
+
} else {
|
|
188
|
+
this.eventHandlers[event] = this.eventHandlers[event].filter((h) => h !== handler);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* 触发事件
|
|
193
|
+
* @param event 事件名称
|
|
194
|
+
* @param args 事件参数
|
|
195
|
+
*/
|
|
196
|
+
emit(event, ...args) {
|
|
197
|
+
const handlers = this.eventHandlers[event];
|
|
198
|
+
if (handlers) {
|
|
199
|
+
handlers.forEach((handler) => {
|
|
200
|
+
try {
|
|
201
|
+
handler(...args);
|
|
202
|
+
} catch (error) {
|
|
203
|
+
console.error(`Error in event handler for ${event}:`, error);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* 获取当前连接状态
|
|
210
|
+
*/
|
|
211
|
+
getStatus() {
|
|
212
|
+
return this.status;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* 获取当前状态描述
|
|
216
|
+
*/
|
|
217
|
+
getStatusText() {
|
|
218
|
+
return import_constants.WS_STATUS_MAP[this.status] || "未知状态";
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* 检查是否已连接
|
|
222
|
+
*/
|
|
223
|
+
isConnected() {
|
|
224
|
+
return this.ws !== null && this.ws.readyState === WebSocket.OPEN;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* 设置连接状态
|
|
228
|
+
*/
|
|
229
|
+
setStatus(status) {
|
|
230
|
+
this.status = status;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* 设置WebSocket事件监听器
|
|
234
|
+
*/
|
|
235
|
+
setupWebSocketListeners() {
|
|
236
|
+
if (!this.ws) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
this.ws.addEventListener("open", () => {
|
|
240
|
+
this.setStatus(import_types.SocketStatus.OPEN);
|
|
241
|
+
this.emit(import_events.SocketEvents.CONNECT);
|
|
242
|
+
});
|
|
243
|
+
this.ws.addEventListener("close", (event) => {
|
|
244
|
+
if (this.status !== import_types.SocketStatus.CLOSING && this.status !== import_types.SocketStatus.CLOSED) {
|
|
245
|
+
this.handleConnectionClosed(event);
|
|
246
|
+
} else {
|
|
247
|
+
this.setStatus(import_types.SocketStatus.CLOSED);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
this.ws.addEventListener("error", (event) => {
|
|
251
|
+
this.emit(import_events.SocketEvents.ERROR, event);
|
|
252
|
+
});
|
|
253
|
+
this.ws.addEventListener("message", (event) => {
|
|
254
|
+
this.handleMessage(event);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* 处理接收到的消息
|
|
259
|
+
*/
|
|
260
|
+
handleMessage(event) {
|
|
261
|
+
try {
|
|
262
|
+
const message = JSON.parse(event.data);
|
|
263
|
+
if (message.type === import_constants.DEFAULT_HEARTBEAT_RESPONSE) {
|
|
264
|
+
this.handleHeartbeatResponse();
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
this.emit(import_events.SocketEvents.MESSAGE, message);
|
|
268
|
+
this.emit(message.type, message.data);
|
|
269
|
+
} catch (error) {
|
|
270
|
+
console.error("Error parsing message:", error, event.data);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* 处理连接关闭
|
|
275
|
+
*/
|
|
276
|
+
handleConnectionClosed(event) {
|
|
277
|
+
if (this.heartbeatManager) {
|
|
278
|
+
this.heartbeatManager.stop();
|
|
279
|
+
}
|
|
280
|
+
this.setStatus(import_types.SocketStatus.CLOSED);
|
|
281
|
+
this.ws = null;
|
|
282
|
+
this.emit(import_events.SocketEvents.DISCONNECT, event);
|
|
283
|
+
this.startReconnection();
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* 处理连接失败
|
|
287
|
+
*/
|
|
288
|
+
handleConnectionFailure(error) {
|
|
289
|
+
this.setStatus(import_types.SocketStatus.CLOSED);
|
|
290
|
+
this.ws = null;
|
|
291
|
+
this.emit(import_events.SocketEvents.ERROR, error);
|
|
292
|
+
this.startReconnection();
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* 处理心跳响应
|
|
296
|
+
*/
|
|
297
|
+
handleHeartbeatResponse() {
|
|
298
|
+
if (this.heartbeatManager) {
|
|
299
|
+
this.heartbeatManager.receiveHeartbeatResponse();
|
|
300
|
+
this.emit(import_events.SocketEvents.HEARTBEAT_RESPONSE);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* 初始化心跳检测
|
|
305
|
+
*/
|
|
306
|
+
initHeartbeat() {
|
|
307
|
+
if (!this.options.heartbeat) {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
const heartbeatOptions = {
|
|
311
|
+
interval: this.options.heartbeatInterval,
|
|
312
|
+
timeout: this.options.heartbeatTimeout,
|
|
313
|
+
message: import_constants.DEFAULT_HEARTBEAT_MESSAGE
|
|
314
|
+
};
|
|
315
|
+
this.heartbeatManager = new import_heartbeat.HeartbeatManager(heartbeatOptions, (msg) => this.sendRaw(msg), {
|
|
316
|
+
onTimeout: () => {
|
|
317
|
+
this.emit(import_events.SocketEvents.HEARTBEAT_TIMEOUT);
|
|
318
|
+
this.disconnect();
|
|
319
|
+
this.startReconnection();
|
|
320
|
+
},
|
|
321
|
+
onResponse: () => {
|
|
322
|
+
this.emit(import_events.SocketEvents.HEARTBEAT_RESPONSE);
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
this.heartbeatManager.start();
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* 初始化重连管理
|
|
329
|
+
*/
|
|
330
|
+
initReconnect() {
|
|
331
|
+
if (!this.options.reconnection) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const reconnectOptions = {
|
|
335
|
+
enabled: true,
|
|
336
|
+
attempts: this.options.reconnectionAttempts,
|
|
337
|
+
delay: this.options.reconnectionDelay,
|
|
338
|
+
delayMax: this.options.reconnectionDelayMax,
|
|
339
|
+
jitter: false
|
|
340
|
+
};
|
|
341
|
+
this.reconnectManager = new import_reconnect.ReconnectManager(
|
|
342
|
+
reconnectOptions,
|
|
343
|
+
async () => {
|
|
344
|
+
try {
|
|
345
|
+
await this.connect();
|
|
346
|
+
return true;
|
|
347
|
+
} catch (error) {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
{
|
|
352
|
+
onAttempt: (attempt) => {
|
|
353
|
+
this.emit(import_events.SocketEvents.RECONNECT_ATTEMPT, attempt);
|
|
354
|
+
},
|
|
355
|
+
onSuccess: () => {
|
|
356
|
+
this.emit(import_events.SocketEvents.RECONNECT);
|
|
357
|
+
},
|
|
358
|
+
onFail: (error) => {
|
|
359
|
+
this.emit(import_events.SocketEvents.RECONNECT_ERROR, error);
|
|
360
|
+
},
|
|
361
|
+
onMaxAttemptsExceeded: () => {
|
|
362
|
+
this.emit(import_events.SocketEvents.RECONNECT_FAILED);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* 开始重连过程
|
|
369
|
+
*/
|
|
370
|
+
startReconnection() {
|
|
371
|
+
if (this.reconnectManager && this.options.reconnection) {
|
|
372
|
+
this.setStatus(import_types.SocketStatus.RECONNECTING);
|
|
373
|
+
this.reconnectManager.startReconnect();
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* 生成消息ID
|
|
378
|
+
*/
|
|
379
|
+
generateMessageId() {
|
|
380
|
+
this.messageIdCounter++;
|
|
381
|
+
return `msg_${Date.now()}_${this.messageIdCounter}`;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* 启用监控功能
|
|
385
|
+
* @param options 监控配置选项
|
|
386
|
+
*/
|
|
387
|
+
enableMonitoring(options) {
|
|
388
|
+
if (!this.monitor) {
|
|
389
|
+
this.monitor = new import_monitor.SocketMonitor(this, options);
|
|
390
|
+
} else if (!this.monitor.isActive()) {
|
|
391
|
+
this.monitor.start();
|
|
392
|
+
}
|
|
393
|
+
return this.monitor;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* 禁用监控功能
|
|
397
|
+
*/
|
|
398
|
+
disableMonitoring() {
|
|
399
|
+
if (this.monitor) {
|
|
400
|
+
this.monitor.stop();
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* 获取监控器实例
|
|
405
|
+
*/
|
|
406
|
+
getMonitor() {
|
|
407
|
+
return this.monitor;
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* 释放资源
|
|
411
|
+
*/
|
|
412
|
+
destroy() {
|
|
413
|
+
this.disconnect(true);
|
|
414
|
+
if (this.monitor) {
|
|
415
|
+
this.monitor.destroy();
|
|
416
|
+
this.monitor = null;
|
|
417
|
+
}
|
|
418
|
+
this.eventHandlers = {};
|
|
419
|
+
this.heartbeatManager = null;
|
|
420
|
+
this.reconnectManager = null;
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
424
|
+
0 && (module.exports = {
|
|
425
|
+
Socket
|
|
426
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Socket配置选项
|
|
3
|
+
*/
|
|
4
|
+
export interface SocketOptions {
|
|
5
|
+
/** 是否自动连接 */
|
|
6
|
+
autoConnect?: boolean;
|
|
7
|
+
/** 是否启用重连功能 */
|
|
8
|
+
reconnection?: boolean;
|
|
9
|
+
/** 最大重连尝试次数,默认无限 */
|
|
10
|
+
reconnectionAttempts?: number;
|
|
11
|
+
/** 重连延迟时间(毫秒) */
|
|
12
|
+
reconnectionDelay?: number;
|
|
13
|
+
/** 最大重连延迟时间(毫秒) */
|
|
14
|
+
reconnectionDelayMax?: number;
|
|
15
|
+
/** 是否启用心跳检测 */
|
|
16
|
+
heartbeat?: boolean;
|
|
17
|
+
/** 心跳间隔时间(毫秒) */
|
|
18
|
+
heartbeatInterval?: number;
|
|
19
|
+
/** 请求超时时间(毫秒) */
|
|
20
|
+
timeout?: number;
|
|
21
|
+
/** 心跳超时时间(毫秒) */
|
|
22
|
+
heartbeatTimeout?: number;
|
|
23
|
+
/** 自定义协议 */
|
|
24
|
+
protocols?: string | string[];
|
|
25
|
+
/** 连接URL */
|
|
26
|
+
url: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Socket连接状态
|
|
30
|
+
*/
|
|
31
|
+
export declare enum SocketStatus {
|
|
32
|
+
CONNECTING = 0,
|
|
33
|
+
OPEN = 1,
|
|
34
|
+
CLOSING = 2,
|
|
35
|
+
CLOSED = 3,
|
|
36
|
+
RECONNECTING = 4
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Socket消息格式
|
|
40
|
+
*/
|
|
41
|
+
export interface SocketMessage {
|
|
42
|
+
/** 事件类型 */
|
|
43
|
+
type: string;
|
|
44
|
+
/** 消息数据 */
|
|
45
|
+
data?: any;
|
|
46
|
+
/** 消息ID */
|
|
47
|
+
id?: string;
|
|
48
|
+
event?: any;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 心跳配置选项
|
|
52
|
+
*/
|
|
53
|
+
export interface HeartbeatOptions {
|
|
54
|
+
/** 心跳间隔时间(毫秒) */
|
|
55
|
+
interval: number;
|
|
56
|
+
/** 心跳超时时间(毫秒) */
|
|
57
|
+
timeout: number;
|
|
58
|
+
/** 心跳探测消息 */
|
|
59
|
+
message?: SocketMessage;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* 重连配置选项
|
|
63
|
+
*/
|
|
64
|
+
export interface ReconnectOptions {
|
|
65
|
+
/** 是否启用重连 */
|
|
66
|
+
enabled: boolean;
|
|
67
|
+
/** 最大重连尝试次数 */
|
|
68
|
+
attempts?: number;
|
|
69
|
+
/** 重连延迟时间(毫秒) */
|
|
70
|
+
delay: number;
|
|
71
|
+
/** 最大重连延迟时间(毫秒) */
|
|
72
|
+
delayMax?: number;
|
|
73
|
+
/** 是否使用指数退避算法 */
|
|
74
|
+
jitter?: boolean;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* 事件处理函数类型
|
|
78
|
+
*/
|
|
79
|
+
export declare type EventHandler = (...args: any[]) => void;
|
|
80
|
+
/**
|
|
81
|
+
* 事件处理函数映射
|
|
82
|
+
*/
|
|
83
|
+
export interface EventHandlerMap {
|
|
84
|
+
[event: string]: EventHandler[];
|
|
85
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/socket/types.ts
|
|
20
|
+
var types_exports = {};
|
|
21
|
+
__export(types_exports, {
|
|
22
|
+
SocketStatus: () => SocketStatus
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(types_exports);
|
|
25
|
+
var SocketStatus = /* @__PURE__ */ ((SocketStatus2) => {
|
|
26
|
+
SocketStatus2[SocketStatus2["CONNECTING"] = 0] = "CONNECTING";
|
|
27
|
+
SocketStatus2[SocketStatus2["OPEN"] = 1] = "OPEN";
|
|
28
|
+
SocketStatus2[SocketStatus2["CLOSING"] = 2] = "CLOSING";
|
|
29
|
+
SocketStatus2[SocketStatus2["CLOSED"] = 3] = "CLOSED";
|
|
30
|
+
SocketStatus2[SocketStatus2["RECONNECTING"] = 4] = "RECONNECTING";
|
|
31
|
+
return SocketStatus2;
|
|
32
|
+
})(SocketStatus || {});
|
|
33
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
34
|
+
0 && (module.exports = {
|
|
35
|
+
SocketStatus
|
|
36
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import App from '../app';
|
|
2
|
+
export interface StorageOptions {
|
|
3
|
+
storageKey?: string;
|
|
4
|
+
removeStorageAfter?: (key: string) => void;
|
|
5
|
+
setStorageAfter?: (key: string, value: string) => void;
|
|
6
|
+
}
|
|
7
|
+
export declare class Storage {
|
|
8
|
+
private storageKey;
|
|
9
|
+
private app?;
|
|
10
|
+
private options;
|
|
11
|
+
constructor(app?: App, options?: StorageOptions);
|
|
12
|
+
createKey: (key: string) => string;
|
|
13
|
+
setStorage: (key: string, value: string) => void;
|
|
14
|
+
getStorage: (key: string) => string | null;
|
|
15
|
+
removeStorage: (key: string) => void;
|
|
16
|
+
clear: () => void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/storage/index.ts
|
|
20
|
+
var storage_exports = {};
|
|
21
|
+
__export(storage_exports, {
|
|
22
|
+
Storage: () => Storage
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(storage_exports);
|
|
25
|
+
var Storage = class {
|
|
26
|
+
storageKey = "CapacitorStorage.native_";
|
|
27
|
+
app;
|
|
28
|
+
options = {};
|
|
29
|
+
constructor(app, options) {
|
|
30
|
+
this.app = app;
|
|
31
|
+
if (options) {
|
|
32
|
+
this.options = options;
|
|
33
|
+
}
|
|
34
|
+
if (options == null ? void 0 : options.storageKey) {
|
|
35
|
+
this.storageKey = options.storageKey;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
createKey = (key) => {
|
|
39
|
+
if (key.indexOf(this.storageKey) !== -1) {
|
|
40
|
+
return key;
|
|
41
|
+
}
|
|
42
|
+
return this.storageKey + key;
|
|
43
|
+
};
|
|
44
|
+
setStorage = (key, value) => {
|
|
45
|
+
var _a, _b;
|
|
46
|
+
localStorage.setItem(this.createKey(key), value);
|
|
47
|
+
(_b = (_a = this.options) == null ? void 0 : _a.setStorageAfter) == null ? void 0 : _b.call(_a, key, value);
|
|
48
|
+
};
|
|
49
|
+
getStorage = (key) => {
|
|
50
|
+
return localStorage.getItem(this.createKey(key));
|
|
51
|
+
};
|
|
52
|
+
removeStorage = (key) => {
|
|
53
|
+
var _a, _b;
|
|
54
|
+
localStorage.removeItem(this.createKey(key));
|
|
55
|
+
(_b = (_a = this.options) == null ? void 0 : _a.removeStorageAfter) == null ? void 0 : _b.call(_a, key);
|
|
56
|
+
};
|
|
57
|
+
clear = () => {
|
|
58
|
+
localStorage.clear();
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
62
|
+
0 && (module.exports = {
|
|
63
|
+
Storage
|
|
64
|
+
});
|