@pisell/core 1.0.6 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/es/app/app.d.ts +7 -7
  2. package/es/app/index.d.ts +1 -1
  3. package/es/applicationManager/application.d.ts +2 -2
  4. package/es/hooks/useStore/index.d.ts +1 -1
  5. package/es/locales/type.d.ts +3 -3
  6. package/es/logger/index.d.ts +1 -1
  7. package/es/models/index.d.ts +4 -4
  8. package/es/pubsub/index.d.ts +1 -1
  9. package/es/request/cache.d.ts +1 -1
  10. package/es/request/pisell2Request.d.ts +1 -1
  11. package/es/routes/index.d.ts +2 -2
  12. package/es/socket/types.d.ts +1 -1
  13. package/es/tasks/index.js +7 -6
  14. package/es/tasks/type.d.ts +4 -4
  15. package/es/variables/index.d.ts +3 -3
  16. package/lib/app/app.d.ts +7 -7
  17. package/lib/app/app.js +45 -21
  18. package/lib/app/index.d.ts +1 -1
  19. package/lib/applicationManager/application.d.ts +2 -2
  20. package/lib/applicationManager/application.js +9 -6
  21. package/lib/applicationManager/index.js +1 -1
  22. package/lib/cookie/index.js +2 -4
  23. package/lib/data/index.js +3 -0
  24. package/lib/history/index.js +24 -22
  25. package/lib/hooks/useStore/index.d.ts +1 -1
  26. package/lib/indexDB/index.js +16 -11
  27. package/lib/locales/index.js +95 -94
  28. package/lib/locales/type.d.ts +3 -3
  29. package/lib/logger/index.d.ts +1 -1
  30. package/lib/logger/index.js +17 -7
  31. package/lib/menuManager/index.js +9 -12
  32. package/lib/models/index.d.ts +4 -4
  33. package/lib/pubsub/index.d.ts +1 -1
  34. package/lib/pubsub/index.js +1 -3
  35. package/lib/request/cache.d.ts +1 -1
  36. package/lib/request/pisell2Request.d.ts +1 -1
  37. package/lib/routes/index.d.ts +2 -2
  38. package/lib/routes/index.js +3 -1
  39. package/lib/socket/components/SocketMonitorPage.js +6 -12
  40. package/lib/socket/heartbeat.js +10 -5
  41. package/lib/socket/index.js +1 -3
  42. package/lib/socket/monitor.js +26 -24
  43. package/lib/socket/reconnect.js +10 -3
  44. package/lib/socket/socket.js +12 -10
  45. package/lib/socket/types.d.ts +1 -1
  46. package/lib/storage/index.js +25 -24
  47. package/lib/tasks/index.js +163 -157
  48. package/lib/tasks/type.d.ts +4 -4
  49. package/lib/variables/index.d.ts +3 -3
  50. package/package.json +2 -2
@@ -23,31 +23,10 @@ __export(storage_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(storage_exports);
25
25
  var Storage = class {
26
+ storageKey = "CapacitorStorage.native_";
27
+ app;
28
+ options = {};
26
29
  constructor(app, options) {
27
- this.storageKey = "CapacitorStorage.native_";
28
- this.options = {};
29
- this.createKey = (key) => {
30
- if (key.indexOf(this.storageKey) !== -1) {
31
- return key;
32
- }
33
- return this.storageKey + key;
34
- };
35
- this.setStorage = (key, value) => {
36
- var _a, _b;
37
- localStorage.setItem(this.createKey(key), value);
38
- (_b = (_a = this.options) == null ? void 0 : _a.setStorageAfter) == null ? void 0 : _b.call(_a, key, value);
39
- };
40
- this.getStorage = (key) => {
41
- return localStorage.getItem(this.createKey(key));
42
- };
43
- this.removeStorage = (key) => {
44
- var _a, _b;
45
- localStorage.removeItem(this.createKey(key));
46
- (_b = (_a = this.options) == null ? void 0 : _a.removeStorageAfter) == null ? void 0 : _b.call(_a, key);
47
- };
48
- this.clear = () => {
49
- localStorage.clear();
50
- };
51
30
  this.app = app;
52
31
  if (options) {
53
32
  this.options = options;
@@ -56,6 +35,28 @@ var Storage = class {
56
35
  this.storageKey = options.storageKey;
57
36
  }
58
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
+ };
59
60
  };
60
61
  // Annotate the CommonJS export names for ESM import in node:
61
62
  0 && (module.exports = {
@@ -35,161 +35,16 @@ module.exports = __toCommonJS(tasks_exports);
35
35
  var import_dayjs = __toESM(require("dayjs"));
36
36
  var import_utils = require("@pisell/utils");
37
37
  var import_useTasks = __toESM(require("./useTasks"));
38
- var TasksManager = class {
38
+ var TasksManager = class _TasksManager {
39
+ static instance;
40
+ taskFunctions;
41
+ tasks;
42
+ app;
43
+ db;
44
+ useTasks = import_useTasks.default;
45
+ watchTaskCallback = () => {
46
+ };
39
47
  constructor(app) {
40
- this.useTasks = import_useTasks.default;
41
- this.watchTaskCallback = () => {
42
- };
43
- // 将任务队列状态同步到 local
44
- this.saveTaskQueueToLocal = async (state) => {
45
- var _a;
46
- console.log("saveTaskQueueToLocal", state);
47
- let tasksData = await this.db.get("tasks", "tasks");
48
- const newData = { id: "tasks", ...state };
49
- if (!tasksData) {
50
- await this.db.add("tasks", newData);
51
- } else {
52
- await this.db.update("tasks", newData);
53
- }
54
- (_a = this.watchTaskCallback) == null ? void 0 : _a.call(this, this.tasks);
55
- };
56
- // 从 local 加载任务队列状态
57
- this.loadTaskQueueFromLocal = async () => {
58
- var _a, _b;
59
- try {
60
- const tasks = await this.db.get("tasks", "tasks");
61
- for (let module2 in tasks) {
62
- for (let queueId in tasks[module2]) {
63
- for (let task of ((_b = (_a = tasks == null ? void 0 : tasks[module2]) == null ? void 0 : _a[queueId]) == null ? void 0 : _b.tasks) || []) {
64
- if (task.destroy) {
65
- delete tasks[module2][queueId].tasks;
66
- }
67
- }
68
- }
69
- }
70
- return tasks;
71
- } catch (err) {
72
- console.log("loadTaskQueueFromLocal", err);
73
- return null;
74
- }
75
- };
76
- /**
77
- * @title: 执行任务
78
- * @description:
79
- * @param {Task} task
80
- * @return {*}
81
- * @Author: zhiwei.Wang
82
- * @Date: 2024-09-26 13:53
83
- */
84
- this.runTask = async (task) => {
85
- const beforeAction = this.getTaskFunction(task.beforeAction || "");
86
- const afterAction = this.getTaskFunction(task.afterAction || "");
87
- const action = this.getTaskFunction(task.action || "");
88
- let beforeActionRes;
89
- let afterActionRes;
90
- let actionRes = { status: "success" };
91
- try {
92
- if (beforeAction) {
93
- beforeActionRes = await beforeAction({ task });
94
- }
95
- if (action) {
96
- actionRes = await action({ task, beforeActionRes });
97
- }
98
- if (afterAction) {
99
- afterActionRes = await afterAction({ task, actionRes, afterActionRes });
100
- }
101
- } catch (error) {
102
- this.app.logger.addLog({
103
- type: "error",
104
- title: `任务执行失败-${task.id}`,
105
- metadata: { error, taskPayload: task == null ? void 0 : task.payload }
106
- });
107
- console.log("任务执行失败", error);
108
- actionRes = { status: "failure" };
109
- }
110
- console.log("Tasks--->", `任务执行成功: ${task.id}`);
111
- return actionRes;
112
- };
113
- /**
114
- * @title: 启动轮询
115
- * @description: 根据轮询间隔定期执行任务
116
- * @param {Task} task
117
- * @return {*}
118
- */
119
- this.startPolling = (task) => {
120
- if (!task.polling || !task.polling.interval) {
121
- console.log("不是轮询任务");
122
- return;
123
- }
124
- const timerId = setTimeout(async () => {
125
- var _a;
126
- try {
127
- await this.runTask(task);
128
- console.log("轮询任务", task);
129
- let _task = { ...task };
130
- _task.pollingResult = {
131
- count: (((_a = _task.pollingResult) == null ? void 0 : _a.count) || 0) + 1,
132
- timerId
133
- };
134
- const newTask = this.updateTask({
135
- module: task.module,
136
- queueId: task.queueId,
137
- taskId: task.id,
138
- other: _task
139
- });
140
- if (!newTask) {
141
- return;
142
- }
143
- this.startPolling(newTask);
144
- } catch (error) {
145
- clearTimeout(timerId);
146
- console.error("轮询任务异常", error);
147
- }
148
- }, task.polling.interval);
149
- };
150
- /**
151
- * @title: 创建任务数据
152
- * @description:
153
- * @param {Partial} payload
154
- * @return {*}
155
- * @Author: zhiwei.Wang
156
- * @Date: 2024-09-26 13:54
157
- */
158
- this.createTaskData = (payload) => {
159
- return {
160
- id: (0, import_utils.getUniqueId)("task_"),
161
- create_at: (0, import_dayjs.default)().format("YYYY-MM-DD HH:mm:ss"),
162
- type: "local",
163
- retries: 0,
164
- maxRetries: 3,
165
- status: "pending",
166
- // 执行函数
167
- action: "",
168
- // 执行参数
169
- payload: {},
170
- // 执行前的钩子
171
- beforeAction: "",
172
- // 执行前的参数
173
- beforePayload: {},
174
- // 执行后的钩子
175
- afterAction: "",
176
- // 执行后的参数
177
- afterPayload: {},
178
- destroy: true,
179
- ...payload
180
- };
181
- };
182
- this.getTaskQueue = (payload) => {
183
- const { queueId, module: module2 } = payload;
184
- return this.tasks[module2][queueId].tasks;
185
- };
186
- this.timeout = (ms) => {
187
- return new Promise((resolve) => {
188
- setTimeout(() => {
189
- resolve(true);
190
- }, ms || 5e3);
191
- });
192
- };
193
48
  if (!app) {
194
49
  throw new Error("app is required");
195
50
  }
@@ -200,10 +55,10 @@ var TasksManager = class {
200
55
  }
201
56
  // 单例模式
202
57
  static getInstance(app) {
203
- if (!TasksManager.instance && app) {
204
- TasksManager.instance = new TasksManager(app);
58
+ if (!_TasksManager.instance && app) {
59
+ _TasksManager.instance = new _TasksManager(app);
205
60
  }
206
- return TasksManager.instance;
61
+ return _TasksManager.instance;
207
62
  }
208
63
  // 注册任务函数
209
64
  addTaskFunction(name, fun) {
@@ -233,6 +88,157 @@ var TasksManager = class {
233
88
  console.log("initTasks", tasks);
234
89
  }
235
90
  }
91
+ // 将任务队列状态同步到 local
92
+ saveTaskQueueToLocal = async (state) => {
93
+ var _a;
94
+ console.log("saveTaskQueueToLocal", state);
95
+ let tasksData = await this.db.get("tasks", "tasks");
96
+ const newData = { id: "tasks", ...state };
97
+ if (!tasksData) {
98
+ await this.db.add("tasks", newData);
99
+ } else {
100
+ await this.db.update("tasks", newData);
101
+ }
102
+ (_a = this.watchTaskCallback) == null ? void 0 : _a.call(this, this.tasks);
103
+ };
104
+ // 从 local 加载任务队列状态
105
+ loadTaskQueueFromLocal = async () => {
106
+ var _a, _b;
107
+ try {
108
+ const tasks = await this.db.get("tasks", "tasks");
109
+ for (let module2 in tasks) {
110
+ for (let queueId in tasks[module2]) {
111
+ for (let task of ((_b = (_a = tasks == null ? void 0 : tasks[module2]) == null ? void 0 : _a[queueId]) == null ? void 0 : _b.tasks) || []) {
112
+ if (task.destroy) {
113
+ delete tasks[module2][queueId].tasks;
114
+ }
115
+ }
116
+ }
117
+ }
118
+ return tasks;
119
+ } catch (err) {
120
+ console.log("loadTaskQueueFromLocal", err);
121
+ return null;
122
+ }
123
+ };
124
+ /**
125
+ * @title: 执行任务
126
+ * @description:
127
+ * @param {Task} task
128
+ * @return {*}
129
+ * @Author: zhiwei.Wang
130
+ * @Date: 2024-09-26 13:53
131
+ */
132
+ runTask = async (task) => {
133
+ const beforeAction = this.getTaskFunction(task.beforeAction || "");
134
+ const afterAction = this.getTaskFunction(task.afterAction || "");
135
+ const action = this.getTaskFunction(task.action || "");
136
+ let beforeActionRes;
137
+ let afterActionRes;
138
+ let actionRes = { status: "success" };
139
+ try {
140
+ if (beforeAction) {
141
+ beforeActionRes = await beforeAction({ task });
142
+ }
143
+ if (action) {
144
+ actionRes = await action({ task, beforeActionRes });
145
+ }
146
+ if (afterAction) {
147
+ afterActionRes = await afterAction({ task, actionRes, afterActionRes });
148
+ }
149
+ } catch (error) {
150
+ this.app.logger.addLog({
151
+ type: "error",
152
+ title: `任务执行失败-${task.id}`,
153
+ metadata: { error, taskPayload: task == null ? void 0 : task.payload }
154
+ });
155
+ console.log("任务执行失败", error);
156
+ actionRes = { status: "failure" };
157
+ }
158
+ console.log("Tasks--->", `任务执行成功: ${task.id}`);
159
+ return actionRes;
160
+ };
161
+ /**
162
+ * @title: 启动轮询
163
+ * @description: 根据轮询间隔定期执行任务
164
+ * @param {Task} task
165
+ * @return {*}
166
+ */
167
+ startPolling = (task) => {
168
+ if (!task.polling || !task.polling.interval) {
169
+ console.log("不是轮询任务");
170
+ return;
171
+ }
172
+ const timerId = setTimeout(async () => {
173
+ var _a;
174
+ try {
175
+ await this.runTask(task);
176
+ console.log("轮询任务", task);
177
+ let _task = { ...task };
178
+ _task.pollingResult = {
179
+ count: (((_a = _task.pollingResult) == null ? void 0 : _a.count) || 0) + 1,
180
+ timerId
181
+ };
182
+ const newTask = this.updateTask({
183
+ module: task.module,
184
+ queueId: task.queueId,
185
+ taskId: task.id,
186
+ other: _task
187
+ });
188
+ if (!newTask) {
189
+ return;
190
+ }
191
+ this.startPolling(newTask);
192
+ } catch (error) {
193
+ clearTimeout(timerId);
194
+ console.error("轮询任务异常", error);
195
+ }
196
+ }, task.polling.interval);
197
+ };
198
+ /**
199
+ * @title: 创建任务数据
200
+ * @description:
201
+ * @param {Partial} payload
202
+ * @return {*}
203
+ * @Author: zhiwei.Wang
204
+ * @Date: 2024-09-26 13:54
205
+ */
206
+ createTaskData = (payload) => {
207
+ return {
208
+ id: (0, import_utils.getUniqueId)("task_"),
209
+ create_at: (0, import_dayjs.default)().format("YYYY-MM-DD HH:mm:ss"),
210
+ type: "local",
211
+ retries: 0,
212
+ maxRetries: 3,
213
+ status: "pending",
214
+ // 执行函数
215
+ action: "",
216
+ // 执行参数
217
+ payload: {},
218
+ // 执行前的钩子
219
+ beforeAction: "",
220
+ // 执行前的参数
221
+ beforePayload: {},
222
+ // 执行后的钩子
223
+ afterAction: "",
224
+ // 执行后的参数
225
+ afterPayload: {},
226
+ destroy: true,
227
+ ...payload
228
+ };
229
+ };
230
+ getTaskQueue = (payload) => {
231
+ var _a, _b;
232
+ const { queueId, module: module2 } = payload;
233
+ return (_b = (_a = this.tasks[module2]) == null ? void 0 : _a[queueId]) == null ? void 0 : _b.tasks;
234
+ };
235
+ timeout = (ms) => {
236
+ return new Promise((resolve) => {
237
+ setTimeout(() => {
238
+ resolve(true);
239
+ }, ms || 5e3);
240
+ });
241
+ };
236
242
  /**
237
243
  * @title: 执行任务队列
238
244
  * @description:
@@ -1,4 +1,4 @@
1
- export type TaskRunStatus = "pending" | "in-progress" | "success" | "failure";
1
+ export declare type TaskRunStatus = "pending" | "in-progress" | "success" | "failure";
2
2
  export interface Task {
3
3
  id?: string;
4
4
  type?: "local" | "cloud";
@@ -25,9 +25,9 @@ export interface Task {
25
25
  export interface TaskConfig {
26
26
  tasks: Task[];
27
27
  }
28
- type TaskModuleName = string;
29
- type TaskQueueName = string;
30
- type TaskStatus = "uncompleted" | "completed";
28
+ declare type TaskModuleName = string;
29
+ declare type TaskQueueName = string;
30
+ declare type TaskStatus = "uncompleted" | "completed";
31
31
  export interface TaskQueue {
32
32
  status: TaskStatus;
33
33
  tasks: Task[];
@@ -1,6 +1,6 @@
1
- export type VariableMap = Record<string, string>;
1
+ export declare type VariableMap = Record<string, string>;
2
2
  declare const _default: {
3
- setConfig: (newConfig: Partial<import("@").VariablesConfig>) => void;
4
- getConfig: () => import("@").VariablesConfig;
3
+ setConfig: (newConfig: Partial<import("./type").VariablesConfig>) => void;
4
+ getConfig: () => import("./type").VariablesConfig;
5
5
  };
6
6
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pisell/core",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "sideEffects": false,
5
5
  "main": "./lib/index.js",
6
6
  "module": "./es/index.js",
@@ -27,7 +27,7 @@
27
27
  "dva-core": "^2.0.4",
28
28
  "js-md5": "^0.8.3",
29
29
  "axios": "^1.7.2",
30
- "@pisell/utils": "1.0.42"
30
+ "@pisell/utils": "3.0.0"
31
31
  },
32
32
  "files": [
33
33
  "es",