@a2ui-vue3-elementplus/scheduler 0.1.0
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.cjs +133 -0
- package/dist/index.d.cts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +106 -0
- package/package.json +40 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createScheduler: () => createScheduler
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/scheduler.ts
|
|
28
|
+
var defaultDispatch = () => void 0;
|
|
29
|
+
function messageType(message) {
|
|
30
|
+
if ("createSurface" in message) return "createSurface";
|
|
31
|
+
if ("updateComponents" in message) return "updateComponents";
|
|
32
|
+
if ("updateDataModel" in message) return "updateDataModel";
|
|
33
|
+
if ("deleteSurface" in message) return "deleteSurface";
|
|
34
|
+
return message.type ?? message.kind ?? message.action;
|
|
35
|
+
}
|
|
36
|
+
function payloadOf(message) {
|
|
37
|
+
if ("createSurface" in message) return message.createSurface;
|
|
38
|
+
if ("updateComponents" in message) return message.updateComponents;
|
|
39
|
+
if ("updateDataModel" in message) return message.updateDataModel;
|
|
40
|
+
if ("deleteSurface" in message) return message.deleteSurface;
|
|
41
|
+
return message.payload ?? message;
|
|
42
|
+
}
|
|
43
|
+
function updateDataModelMergeKey(message) {
|
|
44
|
+
if (messageType(message) !== "updateDataModel") return void 0;
|
|
45
|
+
const payload = payloadOf(message);
|
|
46
|
+
const surfaceId = String(payload.surfaceId ?? payload.id ?? "main");
|
|
47
|
+
const path = String(payload.path ?? "");
|
|
48
|
+
return `${surfaceId}\0${path}`;
|
|
49
|
+
}
|
|
50
|
+
function sortQueue(queue, strategy) {
|
|
51
|
+
if (strategy === "fifo") return [...queue].sort((a, b) => a.sequence - b.sequence);
|
|
52
|
+
const priority = (message) => {
|
|
53
|
+
switch (messageType(message)) {
|
|
54
|
+
case "deleteSurface":
|
|
55
|
+
return 0;
|
|
56
|
+
case "createSurface":
|
|
57
|
+
return 1;
|
|
58
|
+
case "updateComponents":
|
|
59
|
+
return 2;
|
|
60
|
+
case "updateDataModel":
|
|
61
|
+
return 3;
|
|
62
|
+
default:
|
|
63
|
+
return 4;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
return [...queue].sort((a, b) => priority(a.message) - priority(b.message) || a.sequence - b.sequence);
|
|
67
|
+
}
|
|
68
|
+
function createScheduler(options = {}) {
|
|
69
|
+
const strategy = options.strategy ?? "balanced";
|
|
70
|
+
const dispatch = options.dispatch ?? defaultDispatch;
|
|
71
|
+
const queue = [];
|
|
72
|
+
const dataModelIndexes = /* @__PURE__ */ new Map();
|
|
73
|
+
let sequence = 0;
|
|
74
|
+
let paused = false;
|
|
75
|
+
function rebuildMergeIndex() {
|
|
76
|
+
dataModelIndexes.clear();
|
|
77
|
+
queue.forEach((item, index) => {
|
|
78
|
+
const key = updateDataModelMergeKey(item.message);
|
|
79
|
+
if (key) dataModelIndexes.set(key, index);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
async function flush() {
|
|
83
|
+
if (paused || queue.length === 0) return [];
|
|
84
|
+
const batch = sortQueue(queue.splice(0), strategy);
|
|
85
|
+
dataModelIndexes.clear();
|
|
86
|
+
const results = [];
|
|
87
|
+
for (const item of batch) {
|
|
88
|
+
results.push(await dispatch(item.message));
|
|
89
|
+
}
|
|
90
|
+
return results;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
enqueue(message) {
|
|
94
|
+
const mergeKey = updateDataModelMergeKey(message);
|
|
95
|
+
if (mergeKey) {
|
|
96
|
+
const existingIndex = dataModelIndexes.get(mergeKey);
|
|
97
|
+
if (existingIndex !== void 0) {
|
|
98
|
+
queue[existingIndex] = { ...queue[existingIndex], message };
|
|
99
|
+
return queue.length;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
queue.push({ message, sequence: sequence++ });
|
|
103
|
+
if (mergeKey) dataModelIndexes.set(mergeKey, queue.length - 1);
|
|
104
|
+
return queue.length;
|
|
105
|
+
},
|
|
106
|
+
flush,
|
|
107
|
+
pause() {
|
|
108
|
+
paused = true;
|
|
109
|
+
},
|
|
110
|
+
resume(options2 = {}) {
|
|
111
|
+
paused = false;
|
|
112
|
+
if (options2.flush) return flush();
|
|
113
|
+
return void 0;
|
|
114
|
+
},
|
|
115
|
+
clear() {
|
|
116
|
+
queue.splice(0);
|
|
117
|
+
rebuildMergeIndex();
|
|
118
|
+
},
|
|
119
|
+
size() {
|
|
120
|
+
return queue.length;
|
|
121
|
+
},
|
|
122
|
+
isPaused() {
|
|
123
|
+
return paused;
|
|
124
|
+
},
|
|
125
|
+
peek() {
|
|
126
|
+
return sortQueue(queue, strategy).map((item) => item.message);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
131
|
+
0 && (module.exports = {
|
|
132
|
+
createScheduler
|
|
133
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { A2RuntimeMessage } from '@a2ui-vue3-elementplus/runtime-core';
|
|
2
|
+
|
|
3
|
+
type SchedulerStrategy = 'balanced' | 'fifo';
|
|
4
|
+
type SchedulerDispatch = (message: A2RuntimeMessage) => unknown | Promise<unknown>;
|
|
5
|
+
interface SchedulerOptions {
|
|
6
|
+
strategy?: SchedulerStrategy;
|
|
7
|
+
dispatch?: SchedulerDispatch;
|
|
8
|
+
}
|
|
9
|
+
interface Scheduler {
|
|
10
|
+
enqueue(message: A2RuntimeMessage): number;
|
|
11
|
+
flush(): Promise<unknown[]>;
|
|
12
|
+
pause(): void;
|
|
13
|
+
resume(options?: {
|
|
14
|
+
flush?: boolean;
|
|
15
|
+
}): Promise<unknown[]> | void;
|
|
16
|
+
clear(): void;
|
|
17
|
+
size(): number;
|
|
18
|
+
isPaused(): boolean;
|
|
19
|
+
peek(): A2RuntimeMessage[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare function createScheduler(options?: SchedulerOptions): Scheduler;
|
|
23
|
+
|
|
24
|
+
export { type Scheduler, type SchedulerDispatch, type SchedulerOptions, type SchedulerStrategy, createScheduler };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { A2RuntimeMessage } from '@a2ui-vue3-elementplus/runtime-core';
|
|
2
|
+
|
|
3
|
+
type SchedulerStrategy = 'balanced' | 'fifo';
|
|
4
|
+
type SchedulerDispatch = (message: A2RuntimeMessage) => unknown | Promise<unknown>;
|
|
5
|
+
interface SchedulerOptions {
|
|
6
|
+
strategy?: SchedulerStrategy;
|
|
7
|
+
dispatch?: SchedulerDispatch;
|
|
8
|
+
}
|
|
9
|
+
interface Scheduler {
|
|
10
|
+
enqueue(message: A2RuntimeMessage): number;
|
|
11
|
+
flush(): Promise<unknown[]>;
|
|
12
|
+
pause(): void;
|
|
13
|
+
resume(options?: {
|
|
14
|
+
flush?: boolean;
|
|
15
|
+
}): Promise<unknown[]> | void;
|
|
16
|
+
clear(): void;
|
|
17
|
+
size(): number;
|
|
18
|
+
isPaused(): boolean;
|
|
19
|
+
peek(): A2RuntimeMessage[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare function createScheduler(options?: SchedulerOptions): Scheduler;
|
|
23
|
+
|
|
24
|
+
export { type Scheduler, type SchedulerDispatch, type SchedulerOptions, type SchedulerStrategy, createScheduler };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// src/scheduler.ts
|
|
2
|
+
var defaultDispatch = () => void 0;
|
|
3
|
+
function messageType(message) {
|
|
4
|
+
if ("createSurface" in message) return "createSurface";
|
|
5
|
+
if ("updateComponents" in message) return "updateComponents";
|
|
6
|
+
if ("updateDataModel" in message) return "updateDataModel";
|
|
7
|
+
if ("deleteSurface" in message) return "deleteSurface";
|
|
8
|
+
return message.type ?? message.kind ?? message.action;
|
|
9
|
+
}
|
|
10
|
+
function payloadOf(message) {
|
|
11
|
+
if ("createSurface" in message) return message.createSurface;
|
|
12
|
+
if ("updateComponents" in message) return message.updateComponents;
|
|
13
|
+
if ("updateDataModel" in message) return message.updateDataModel;
|
|
14
|
+
if ("deleteSurface" in message) return message.deleteSurface;
|
|
15
|
+
return message.payload ?? message;
|
|
16
|
+
}
|
|
17
|
+
function updateDataModelMergeKey(message) {
|
|
18
|
+
if (messageType(message) !== "updateDataModel") return void 0;
|
|
19
|
+
const payload = payloadOf(message);
|
|
20
|
+
const surfaceId = String(payload.surfaceId ?? payload.id ?? "main");
|
|
21
|
+
const path = String(payload.path ?? "");
|
|
22
|
+
return `${surfaceId}\0${path}`;
|
|
23
|
+
}
|
|
24
|
+
function sortQueue(queue, strategy) {
|
|
25
|
+
if (strategy === "fifo") return [...queue].sort((a, b) => a.sequence - b.sequence);
|
|
26
|
+
const priority = (message) => {
|
|
27
|
+
switch (messageType(message)) {
|
|
28
|
+
case "deleteSurface":
|
|
29
|
+
return 0;
|
|
30
|
+
case "createSurface":
|
|
31
|
+
return 1;
|
|
32
|
+
case "updateComponents":
|
|
33
|
+
return 2;
|
|
34
|
+
case "updateDataModel":
|
|
35
|
+
return 3;
|
|
36
|
+
default:
|
|
37
|
+
return 4;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
return [...queue].sort((a, b) => priority(a.message) - priority(b.message) || a.sequence - b.sequence);
|
|
41
|
+
}
|
|
42
|
+
function createScheduler(options = {}) {
|
|
43
|
+
const strategy = options.strategy ?? "balanced";
|
|
44
|
+
const dispatch = options.dispatch ?? defaultDispatch;
|
|
45
|
+
const queue = [];
|
|
46
|
+
const dataModelIndexes = /* @__PURE__ */ new Map();
|
|
47
|
+
let sequence = 0;
|
|
48
|
+
let paused = false;
|
|
49
|
+
function rebuildMergeIndex() {
|
|
50
|
+
dataModelIndexes.clear();
|
|
51
|
+
queue.forEach((item, index) => {
|
|
52
|
+
const key = updateDataModelMergeKey(item.message);
|
|
53
|
+
if (key) dataModelIndexes.set(key, index);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
async function flush() {
|
|
57
|
+
if (paused || queue.length === 0) return [];
|
|
58
|
+
const batch = sortQueue(queue.splice(0), strategy);
|
|
59
|
+
dataModelIndexes.clear();
|
|
60
|
+
const results = [];
|
|
61
|
+
for (const item of batch) {
|
|
62
|
+
results.push(await dispatch(item.message));
|
|
63
|
+
}
|
|
64
|
+
return results;
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
enqueue(message) {
|
|
68
|
+
const mergeKey = updateDataModelMergeKey(message);
|
|
69
|
+
if (mergeKey) {
|
|
70
|
+
const existingIndex = dataModelIndexes.get(mergeKey);
|
|
71
|
+
if (existingIndex !== void 0) {
|
|
72
|
+
queue[existingIndex] = { ...queue[existingIndex], message };
|
|
73
|
+
return queue.length;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
queue.push({ message, sequence: sequence++ });
|
|
77
|
+
if (mergeKey) dataModelIndexes.set(mergeKey, queue.length - 1);
|
|
78
|
+
return queue.length;
|
|
79
|
+
},
|
|
80
|
+
flush,
|
|
81
|
+
pause() {
|
|
82
|
+
paused = true;
|
|
83
|
+
},
|
|
84
|
+
resume(options2 = {}) {
|
|
85
|
+
paused = false;
|
|
86
|
+
if (options2.flush) return flush();
|
|
87
|
+
return void 0;
|
|
88
|
+
},
|
|
89
|
+
clear() {
|
|
90
|
+
queue.splice(0);
|
|
91
|
+
rebuildMergeIndex();
|
|
92
|
+
},
|
|
93
|
+
size() {
|
|
94
|
+
return queue.length;
|
|
95
|
+
},
|
|
96
|
+
isPaused() {
|
|
97
|
+
return paused;
|
|
98
|
+
},
|
|
99
|
+
peek() {
|
|
100
|
+
return sortQueue(queue, strategy).map((item) => item.message);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
export {
|
|
105
|
+
createScheduler
|
|
106
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@a2ui-vue3-elementplus/scheduler",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/EricLee823/a2ui-vue3-elementplus.git",
|
|
8
|
+
"directory": "packages/scheduler"
|
|
9
|
+
},
|
|
10
|
+
"main": "./dist/index.cjs",
|
|
11
|
+
"module": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"require": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public",
|
|
25
|
+
"registry": "https://registry.npmjs.org/"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@a2ui-vue3-elementplus/runtime-core": "0.1.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"tsup": "^8.3.5",
|
|
32
|
+
"typescript": "^5.8.0",
|
|
33
|
+
"vitest": "^3.0.0"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"typecheck": "tsc --noEmit"
|
|
39
|
+
}
|
|
40
|
+
}
|