@lightsoft/js-sdk 1.2.2 → 1.2.3

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/dist/index.d.ts CHANGED
@@ -259,7 +259,7 @@ declare class Tour {
259
259
  close(): void;
260
260
  }
261
261
 
262
- type WebSocketState = 'CONNECTING' | 'OPEN' | 'CLOSING' | 'CLOSED';
262
+ type WebSocketState = "CONNECTING" | "OPEN" | "CLOSING" | "CLOSED";
263
263
  interface WebSocketConfig {
264
264
  heartbeat?: boolean;
265
265
  heartbeatTime?: number;
@@ -287,6 +287,25 @@ declare function addURLParams(params: Record<string, any>, url?: string): TAddUR
287
287
  declare function getURLParams(url?: string): Record<string, any>;
288
288
  declare function deleteURLParams(keys: string[], url?: string): URL;
289
289
 
290
+ interface StreamRequestConfig extends RequestInit {
291
+ parseResponse?: boolean;
292
+ }
293
+ declare class StreamRequest {
294
+ private url;
295
+ private options;
296
+ private beforeCallback?;
297
+ private messageCallback?;
298
+ private errorCallback?;
299
+ private parseResponse?;
300
+ private beforeResponse?;
301
+ constructor(url: string, config?: StreamRequestConfig);
302
+ private onBefore;
303
+ private send;
304
+ onBeforeResponse(beforeResponse: () => void): void;
305
+ private onMessage;
306
+ private onError;
307
+ }
308
+
290
309
  /**
291
310
  * 匹配中文字母数字
292
311
  */
@@ -312,5 +331,5 @@ declare const email: RegExp;
312
331
  */
313
332
  declare const url: RegExp;
314
333
 
315
- export { HAS_FEILD, InputLegalityValidate, InputLengthValidate, LENGTH_LIMIT, LOGIN_TYPE, Mask, RecordType, Request, RequestChain, Tour, addURLParams, countUp, createWebSocket, dataHandler, debounce, deepMerge, deleteURLParams, download, email, findTreeDataNode, findTreeDataNodeName, getState, getURLParams, hasRepeatArray, idCard, ip, isMobileDevice, isNull, objectArrayToString, phone, throttle, uniqueByProperty, url, useClose, useCountDown, useDownload, useOnMessage, useSendMessage, zhLetterNumber };
334
+ export { HAS_FEILD, InputLegalityValidate, InputLengthValidate, LENGTH_LIMIT, LOGIN_TYPE, Mask, RecordType, Request, RequestChain, StreamRequest, Tour, addURLParams, countUp, createWebSocket, dataHandler, debounce, deepMerge, deleteURLParams, download, email, findTreeDataNode, findTreeDataNodeName, getState, getURLParams, hasRepeatArray, idCard, ip, isMobileDevice, isNull, objectArrayToString, phone, throttle, uniqueByProperty, url, useClose, useCountDown, useDownload, useOnMessage, useSendMessage, zhLetterNumber };
316
335
  export type { ISectoralTable, Tag, typeIsNull };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { _ as __assign, a as __awaiter, b as __generator } from './ws-DGDUMiR9.js';
2
- export { c as createWebSocket, g as getState, e as useClose, d as useOnMessage, u as useSendMessage } from './ws-DGDUMiR9.js';
1
+ import { _ as __assign, a as __awaiter, b as __generator, c as __rest } from './ws-Dvvmk4pM.js';
2
+ export { d as createWebSocket, g as getState, f as useClose, e as useOnMessage, u as useSendMessage } from './ws-Dvvmk4pM.js';
3
3
 
4
4
  var READY_STATE;
5
5
  (function (READY_STATE) {
@@ -2866,9 +2866,8 @@ function hasRepeatArray(arr, keys) {
2866
2866
  var map = new Map();
2867
2867
  var _loop_1 = function (item) {
2868
2868
  var val = (keys === null || keys === void 0 ? void 0 : keys.map(function (key) { return item[key]; }).join(',')) || item;
2869
- if (map.has(val)) {
2869
+ if (map.has(val))
2870
2870
  return { value: [true, val.split(',')] };
2871
- }
2872
2871
  map.set(val, item);
2873
2872
  };
2874
2873
  for (var _i = 0, arr_1 = arr; _i < arr_1.length; _i++) {
@@ -2905,6 +2904,79 @@ function deleteURLParams(keys, url) {
2905
2904
  return newUrl;
2906
2905
  }
2907
2906
 
2907
+ var StreamRequest = /** @class */ (function () {
2908
+ function StreamRequest(url, config) {
2909
+ var _a = config, parseResponse = _a.parseResponse, options = __rest(_a, ["parseResponse"]);
2910
+ this.url = url;
2911
+ this.options = options || {};
2912
+ this.parseResponse = parseResponse;
2913
+ }
2914
+ StreamRequest.prototype.onBefore = function (callback) {
2915
+ this.beforeCallback = callback;
2916
+ };
2917
+ StreamRequest.prototype.send = function (params) {
2918
+ return __awaiter(this, void 0, void 0, function () {
2919
+ var _a, url, options, parseResponse, res, reader, _b, done, value, data, newData, error_1;
2920
+ var _this = this;
2921
+ var _c, _d;
2922
+ return __generator(this, function (_e) {
2923
+ switch (_e.label) {
2924
+ case 0:
2925
+ _a = this, url = _a.url, options = _a.options, parseResponse = _a.parseResponse;
2926
+ _e.label = 1;
2927
+ case 1:
2928
+ _e.trys.push([1, 6, , 7]);
2929
+ if (typeof this.beforeCallback === 'function')
2930
+ this.beforeCallback(params);
2931
+ return [4 /*yield*/, window.fetch(url, __assign(__assign({}, options), { body: JSON.stringify(params) }))];
2932
+ case 2:
2933
+ res = _e.sent();
2934
+ reader = (_c = res.body) === null || _c === void 0 ? void 0 : _c.getReader();
2935
+ if (!reader)
2936
+ return [2 /*return*/];
2937
+ _e.label = 3;
2938
+ case 3:
2939
+ return [4 /*yield*/, reader.read()];
2940
+ case 4:
2941
+ _b = _e.sent(), done = _b.done, value = _b.value;
2942
+ if (done)
2943
+ return [3 /*break*/, 5];
2944
+ data = new TextDecoder().decode(value);
2945
+ newData = data.split('\n');
2946
+ if (typeof this.beforeResponse === 'function') {
2947
+ this.beforeResponse(data, this.messageCallback);
2948
+ }
2949
+ else {
2950
+ newData.forEach(function (message) {
2951
+ var _a;
2952
+ if (message.trim()) {
2953
+ (_a = _this.messageCallback) === null || _a === void 0 ? void 0 : _a.call(_this, parseResponse ? JSON.parse(message || '{}') : message);
2954
+ }
2955
+ });
2956
+ }
2957
+ return [3 /*break*/, 3];
2958
+ case 5: return [3 /*break*/, 7];
2959
+ case 6:
2960
+ error_1 = _e.sent();
2961
+ (_d = this.errorCallback) === null || _d === void 0 ? void 0 : _d.call(this, error_1);
2962
+ return [3 /*break*/, 7];
2963
+ case 7: return [2 /*return*/];
2964
+ }
2965
+ });
2966
+ });
2967
+ };
2968
+ StreamRequest.prototype.onBeforeResponse = function (beforeResponse) {
2969
+ this.beforeResponse = beforeResponse;
2970
+ };
2971
+ StreamRequest.prototype.onMessage = function (callback) {
2972
+ this.messageCallback = callback;
2973
+ };
2974
+ StreamRequest.prototype.onError = function (callback) {
2975
+ this.errorCallback = callback;
2976
+ };
2977
+ return StreamRequest;
2978
+ }());
2979
+
2908
2980
  /**
2909
2981
  * 匹配中文字母数字
2910
2982
  */
@@ -2930,4 +3002,4 @@ var email = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$
2930
3002
  */
2931
3003
  var url = /^https?:\/\/[^\s/$.?#].[^\s]*$/;
2932
3004
 
2933
- export { HAS_FEILD, InputLegalityValidate, InputLengthValidate, LENGTH_LIMIT, LOGIN_TYPE, Mask, RecordType, Request, RequestChain, Tour, addURLParams, countUp, dataHandler, debounce, deepMerge, deleteURLParams, download, email, findTreeDataNode, findTreeDataNodeName, getURLParams, hasRepeatArray, idCard, ip, isMobileDevice, isNull, objectArrayToString, phone, throttle, uniqueByProperty, url, useCountDown, useDownload, zhLetterNumber };
3005
+ export { HAS_FEILD, InputLegalityValidate, InputLengthValidate, LENGTH_LIMIT, LOGIN_TYPE, Mask, RecordType, Request, RequestChain, StreamRequest, Tour, addURLParams, countUp, dataHandler, debounce, deepMerge, deleteURLParams, download, email, findTreeDataNode, findTreeDataNodeName, getURLParams, hasRepeatArray, idCard, ip, isMobileDevice, isNull, objectArrayToString, phone, throttle, uniqueByProperty, url, useCountDown, useDownload, zhLetterNumber };
package/dist/index.umd.js CHANGED
@@ -32,6 +32,18 @@
32
32
  return __assign.apply(this, arguments);
33
33
  };
34
34
 
35
+ function __rest(s, e) {
36
+ var t = {};
37
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
38
+ t[p] = s[p];
39
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
40
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
41
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
42
+ t[p[i]] = s[p[i]];
43
+ }
44
+ return t;
45
+ }
46
+
35
47
  function __awaiter(thisArg, _arguments, P, generator) {
36
48
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
49
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -947,8 +959,9 @@
947
959
  var reconnectNumber = 0;
948
960
  var open = function (ws, config, callback) {
949
961
  var reconnectTime = config.reconnectTime, _a = config.maxReconnectNumber, maxReconnectNumber = _a === void 0 ? 1 : _a;
950
- var isOpen = getState(ws, 'OPEN');
951
- if (isOpen || (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
962
+ var isOpen = getState(ws, "OPEN");
963
+ if (isOpen ||
964
+ (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
952
965
  clearTimeout(reconnectTimeout);
953
966
  reconnectTimeout = null;
954
967
  reconnectNumber = null;
@@ -966,12 +979,12 @@
966
979
  var heartbeatTime = config.heartbeatTime;
967
980
  var timer = null;
968
981
  timer = setInterval(function () {
969
- var isOpen = getState(ws, 'OPEN');
970
- var isConnected = getState(ws, 'CONNECTING');
982
+ var isOpen = getState(ws, "OPEN");
983
+ var isConnected = getState(ws, "CONNECTING");
971
984
  if (isOpen) {
972
985
  useSendMessage(ws, {
973
- type: 'heartbeat',
974
- timestamp: Date.now()
986
+ type: "heartbeat",
987
+ timestamp: Date.now(),
975
988
  });
976
989
  }
977
990
  else if (!isConnected) {
@@ -984,7 +997,7 @@
984
997
  }, heartbeatTime);
985
998
  }
986
999
  function useSendMessage(ws, data) {
987
- var isOpen = getState(ws, 'OPEN');
1000
+ var isOpen = getState(ws, "OPEN");
988
1001
  if (isOpen)
989
1002
  ws.send(JSON.stringify(data));
990
1003
  }
@@ -1001,7 +1014,7 @@
1001
1014
  }
1002
1015
  function useClose(ws, code, message) {
1003
1016
  code = code || 1000;
1004
- message = message || 'connection closed by client';
1017
+ message = message || "connection closed by client";
1005
1018
  ws.close(code, message);
1006
1019
  }
1007
1020
 
@@ -1060,9 +1073,8 @@
1060
1073
  var map = new Map();
1061
1074
  var _loop_1 = function (item) {
1062
1075
  var val = (keys === null || keys === void 0 ? void 0 : keys.map(function (key) { return item[key]; }).join(',')) || item;
1063
- if (map.has(val)) {
1076
+ if (map.has(val))
1064
1077
  return { value: [true, val.split(',')] };
1065
- }
1066
1078
  map.set(val, item);
1067
1079
  };
1068
1080
  for (var _i = 0, arr_1 = arr; _i < arr_1.length; _i++) {
@@ -1099,6 +1111,79 @@
1099
1111
  return newUrl;
1100
1112
  }
1101
1113
 
1114
+ var StreamRequest = /** @class */ (function () {
1115
+ function StreamRequest(url, config) {
1116
+ var _a = config, parseResponse = _a.parseResponse, options = __rest(_a, ["parseResponse"]);
1117
+ this.url = url;
1118
+ this.options = options || {};
1119
+ this.parseResponse = parseResponse;
1120
+ }
1121
+ StreamRequest.prototype.onBefore = function (callback) {
1122
+ this.beforeCallback = callback;
1123
+ };
1124
+ StreamRequest.prototype.send = function (params) {
1125
+ return __awaiter(this, void 0, void 0, function () {
1126
+ var _a, url, options, parseResponse, res, reader, _b, done, value, data, newData, error_1;
1127
+ var _this = this;
1128
+ var _c, _d;
1129
+ return __generator(this, function (_e) {
1130
+ switch (_e.label) {
1131
+ case 0:
1132
+ _a = this, url = _a.url, options = _a.options, parseResponse = _a.parseResponse;
1133
+ _e.label = 1;
1134
+ case 1:
1135
+ _e.trys.push([1, 6, , 7]);
1136
+ if (typeof this.beforeCallback === 'function')
1137
+ this.beforeCallback(params);
1138
+ return [4 /*yield*/, window.fetch(url, __assign(__assign({}, options), { body: JSON.stringify(params) }))];
1139
+ case 2:
1140
+ res = _e.sent();
1141
+ reader = (_c = res.body) === null || _c === void 0 ? void 0 : _c.getReader();
1142
+ if (!reader)
1143
+ return [2 /*return*/];
1144
+ _e.label = 3;
1145
+ case 3:
1146
+ return [4 /*yield*/, reader.read()];
1147
+ case 4:
1148
+ _b = _e.sent(), done = _b.done, value = _b.value;
1149
+ if (done)
1150
+ return [3 /*break*/, 5];
1151
+ data = new TextDecoder().decode(value);
1152
+ newData = data.split('\n');
1153
+ if (typeof this.beforeResponse === 'function') {
1154
+ this.beforeResponse(data, this.messageCallback);
1155
+ }
1156
+ else {
1157
+ newData.forEach(function (message) {
1158
+ var _a;
1159
+ if (message.trim()) {
1160
+ (_a = _this.messageCallback) === null || _a === void 0 ? void 0 : _a.call(_this, parseResponse ? JSON.parse(message || '{}') : message);
1161
+ }
1162
+ });
1163
+ }
1164
+ return [3 /*break*/, 3];
1165
+ case 5: return [3 /*break*/, 7];
1166
+ case 6:
1167
+ error_1 = _e.sent();
1168
+ (_d = this.errorCallback) === null || _d === void 0 ? void 0 : _d.call(this, error_1);
1169
+ return [3 /*break*/, 7];
1170
+ case 7: return [2 /*return*/];
1171
+ }
1172
+ });
1173
+ });
1174
+ };
1175
+ StreamRequest.prototype.onBeforeResponse = function (beforeResponse) {
1176
+ this.beforeResponse = beforeResponse;
1177
+ };
1178
+ StreamRequest.prototype.onMessage = function (callback) {
1179
+ this.messageCallback = callback;
1180
+ };
1181
+ StreamRequest.prototype.onError = function (callback) {
1182
+ this.errorCallback = callback;
1183
+ };
1184
+ return StreamRequest;
1185
+ }());
1186
+
1102
1187
  /**
1103
1188
  * 匹配中文字母数字
1104
1189
  */
@@ -1129,6 +1214,7 @@
1129
1214
  exports.Mask = Mask;
1130
1215
  exports.Request = Request;
1131
1216
  exports.RequestChain = RequestChain;
1217
+ exports.StreamRequest = StreamRequest;
1132
1218
  exports.Tour = Tour;
1133
1219
  exports.addURLParams = addURLParams;
1134
1220
  exports.countUp = countUp;
@@ -0,0 +1,20 @@
1
+ interface AudioRecorderOptions {
2
+ channelCount: number;
3
+ sampleRate: number;
4
+ echoCancellation: boolean;
5
+ mimeType: string;
6
+ }
7
+ declare class AudioRecorder {
8
+ private options;
9
+ private stream;
10
+ private mediaRecorder;
11
+ private audioChunks;
12
+ constructor(options: AudioRecorderOptions);
13
+ start(): Promise<void>;
14
+ stop(): void;
15
+ reset(): void;
16
+ getAudioBlob(): Blob;
17
+ getAudioUrl(): string;
18
+ getAudioBase64(): string;
19
+ uploadAudio(): Promise<void>;
20
+ }
@@ -8,3 +8,4 @@ export * from './download';
8
8
  export * from './number';
9
9
  export * from './array';
10
10
  export * from './url';
11
+ export * from './sse';
@@ -0,0 +1,19 @@
1
+ interface StreamRequestConfig extends RequestInit {
2
+ parseResponse?: boolean;
3
+ }
4
+ export declare class StreamRequest {
5
+ private url;
6
+ private options;
7
+ private beforeCallback?;
8
+ private messageCallback?;
9
+ private errorCallback?;
10
+ private parseResponse?;
11
+ private beforeResponse?;
12
+ constructor(url: string, config?: StreamRequestConfig);
13
+ private onBefore;
14
+ private send;
15
+ onBeforeResponse(beforeResponse: () => void): void;
16
+ private onMessage;
17
+ private onError;
18
+ }
19
+ export {};
@@ -1,4 +1,4 @@
1
- type WebSocketState = 'CONNECTING' | 'OPEN' | 'CLOSING' | 'CLOSED';
1
+ type WebSocketState = "CONNECTING" | "OPEN" | "CLOSING" | "CLOSED";
2
2
  interface WebSocketConfig {
3
3
  heartbeat?: boolean;
4
4
  heartbeatTime?: number;
@@ -0,0 +1,181 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
16
+
17
+
18
+ var __assign = function() {
19
+ __assign = Object.assign || function __assign(t) {
20
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
21
+ s = arguments[i];
22
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+
29
+ function __rest(s, e) {
30
+ var t = {};
31
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
32
+ t[p] = s[p];
33
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
34
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
35
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
36
+ t[p[i]] = s[p[i]];
37
+ }
38
+ return t;
39
+ }
40
+
41
+ function __awaiter(thisArg, _arguments, P, generator) {
42
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
43
+ return new (P || (P = Promise))(function (resolve, reject) {
44
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
45
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
46
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
47
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
48
+ });
49
+ }
50
+
51
+ function __generator(thisArg, body) {
52
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
53
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
54
+ function verb(n) { return function (v) { return step([n, v]); }; }
55
+ function step(op) {
56
+ if (f) throw new TypeError("Generator is already executing.");
57
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
58
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
59
+ if (y = 0, t) op = [op[0] & 2, t.value];
60
+ switch (op[0]) {
61
+ case 0: case 1: t = op; break;
62
+ case 4: _.label++; return { value: op[1], done: false };
63
+ case 5: _.label++; y = op[1]; op = [0]; continue;
64
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
65
+ default:
66
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
67
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
68
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
69
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
70
+ if (t[2]) _.ops.pop();
71
+ _.trys.pop(); continue;
72
+ }
73
+ op = body.call(thisArg, _);
74
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
75
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
76
+ }
77
+ }
78
+
79
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
80
+ var e = new Error(message);
81
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
82
+ };
83
+
84
+ var defaultWebSocketConfig = {
85
+ heartbeat: false,
86
+ heartbeatTime: 15000,
87
+ reconnect: false,
88
+ reconnectTime: 1000,
89
+ maxReconnectNumber: 3,
90
+ };
91
+ function createWebSocket(url, config, onOpen) {
92
+ config = __assign(__assign({}, defaultWebSocketConfig), config);
93
+ var open = reconnectWebSocket();
94
+ var init = function () {
95
+ var ws = new WebSocket(url);
96
+ ws.onopen = function () {
97
+ if (config === null || config === void 0 ? void 0 : config.heartbeat)
98
+ heartbeat(ws, config);
99
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
100
+ };
101
+ ws.onerror = function () {
102
+ if ((config === null || config === void 0 ? void 0 : config.reconnect) && open) {
103
+ open(ws, config, init);
104
+ }
105
+ else {
106
+ open = null;
107
+ config = null;
108
+ }
109
+ };
110
+ return ws;
111
+ };
112
+ return init();
113
+ }
114
+ function getState(ws, type) {
115
+ return ws.readyState === WebSocket[type];
116
+ }
117
+ function reconnectWebSocket() {
118
+ var reconnectTimeout;
119
+ var reconnectNumber = 0;
120
+ var open = function (ws, config, callback) {
121
+ var reconnectTime = config.reconnectTime, _a = config.maxReconnectNumber, maxReconnectNumber = _a === void 0 ? 1 : _a;
122
+ var isOpen = getState(ws, "OPEN");
123
+ if (isOpen ||
124
+ (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
125
+ clearTimeout(reconnectTimeout);
126
+ reconnectTimeout = null;
127
+ reconnectNumber = null;
128
+ config.reconnect = false;
129
+ return;
130
+ }
131
+ reconnectTimeout = setTimeout(function () {
132
+ reconnectNumber += 1;
133
+ callback();
134
+ }, reconnectTime);
135
+ };
136
+ return open;
137
+ }
138
+ function heartbeat(ws, config) {
139
+ var heartbeatTime = config.heartbeatTime;
140
+ var timer = null;
141
+ timer = setInterval(function () {
142
+ var isOpen = getState(ws, "OPEN");
143
+ var isConnected = getState(ws, "CONNECTING");
144
+ if (isOpen) {
145
+ useSendMessage(ws, {
146
+ type: "heartbeat",
147
+ timestamp: Date.now(),
148
+ });
149
+ }
150
+ else if (!isConnected) {
151
+ var open_1 = reconnectWebSocket();
152
+ open_1(ws, config, function () {
153
+ timer && clearInterval(timer);
154
+ heartbeat(ws, config);
155
+ });
156
+ }
157
+ }, heartbeatTime);
158
+ }
159
+ function useSendMessage(ws, data) {
160
+ var isOpen = getState(ws, "OPEN");
161
+ if (isOpen)
162
+ ws.send(JSON.stringify(data));
163
+ }
164
+ function useOnMessage(ws, callback) {
165
+ try {
166
+ ws.onmessage = function (e) {
167
+ var data = JSON.parse(e.data);
168
+ callback(data, e);
169
+ };
170
+ }
171
+ catch (error) {
172
+ console.error(error);
173
+ }
174
+ }
175
+ function useClose(ws, code, message) {
176
+ code = code || 1000;
177
+ message = message || "connection closed by client";
178
+ ws.close(code, message);
179
+ }
180
+
181
+ export { __assign as _, __awaiter as a, __generator as b, __rest as c, createWebSocket as d, useOnMessage as e, useClose as f, getState as g, useSendMessage as u };
package/dist/ws.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- type WebSocketState = 'CONNECTING' | 'OPEN' | 'CLOSING' | 'CLOSED';
1
+ type WebSocketState = "CONNECTING" | "OPEN" | "CLOSING" | "CLOSED";
2
2
  interface WebSocketConfig {
3
3
  heartbeat?: boolean;
4
4
  heartbeatTime?: number;
package/dist/ws.js CHANGED
@@ -1 +1 @@
1
- export { c as createWebSocket, g as getState, e as useClose, d as useOnMessage, u as useSendMessage } from './ws-DGDUMiR9.js';
1
+ export { d as createWebSocket, g as getState, f as useClose, e as useOnMessage, u as useSendMessage } from './ws-Dvvmk4pM.js';
package/dist/ws.umd.js CHANGED
@@ -75,8 +75,9 @@
75
75
  var reconnectNumber = 0;
76
76
  var open = function (ws, config, callback) {
77
77
  var reconnectTime = config.reconnectTime, _a = config.maxReconnectNumber, maxReconnectNumber = _a === void 0 ? 1 : _a;
78
- var isOpen = getState(ws, 'OPEN');
79
- if (isOpen || (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
78
+ var isOpen = getState(ws, "OPEN");
79
+ if (isOpen ||
80
+ (reconnectTimeout && reconnectNumber >= maxReconnectNumber)) {
80
81
  clearTimeout(reconnectTimeout);
81
82
  reconnectTimeout = null;
82
83
  reconnectNumber = null;
@@ -94,12 +95,12 @@
94
95
  var heartbeatTime = config.heartbeatTime;
95
96
  var timer = null;
96
97
  timer = setInterval(function () {
97
- var isOpen = getState(ws, 'OPEN');
98
- var isConnected = getState(ws, 'CONNECTING');
98
+ var isOpen = getState(ws, "OPEN");
99
+ var isConnected = getState(ws, "CONNECTING");
99
100
  if (isOpen) {
100
101
  useSendMessage(ws, {
101
- type: 'heartbeat',
102
- timestamp: Date.now()
102
+ type: "heartbeat",
103
+ timestamp: Date.now(),
103
104
  });
104
105
  }
105
106
  else if (!isConnected) {
@@ -112,7 +113,7 @@
112
113
  }, heartbeatTime);
113
114
  }
114
115
  function useSendMessage(ws, data) {
115
- var isOpen = getState(ws, 'OPEN');
116
+ var isOpen = getState(ws, "OPEN");
116
117
  if (isOpen)
117
118
  ws.send(JSON.stringify(data));
118
119
  }
@@ -129,7 +130,7 @@
129
130
  }
130
131
  function useClose(ws, code, message) {
131
132
  code = code || 1000;
132
- message = message || 'connection closed by client';
133
+ message = message || "connection closed by client";
133
134
  ws.close(code, message);
134
135
  }
135
136
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightsoft/js-sdk",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "",
5
5
  "main": "dist/index.umd.js",
6
6
  "module": "dist/index.js",