@zk-tech/bedrock 0.1.3 → 0.1.4
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/services/service-extension/index.cjs +823 -0
- package/dist/services/service-extension/index.cjs.map +1 -0
- package/dist/services/service-extension/index.d.cts +49 -0
- package/dist/services/service-extension/index.d.ts +49 -0
- package/dist/services/service-extension/index.js +820 -0
- package/dist/services/service-extension/index.js.map +1 -0
- package/package.json +6 -1
|
@@ -0,0 +1,820 @@
|
|
|
1
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
2
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
3
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
4
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
5
|
+
if (decorator = decorators[i])
|
|
6
|
+
result = (decorator(result)) || result;
|
|
7
|
+
return result;
|
|
8
|
+
};
|
|
9
|
+
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
10
|
+
|
|
11
|
+
// src/services/service-extension/descriptor.ts
|
|
12
|
+
var InstantiationPhase = /* @__PURE__ */ ((InstantiationPhase2) => {
|
|
13
|
+
InstantiationPhase2[InstantiationPhase2["Eager"] = 0] = "Eager";
|
|
14
|
+
InstantiationPhase2[InstantiationPhase2["Idle"] = 1] = "Idle";
|
|
15
|
+
return InstantiationPhase2;
|
|
16
|
+
})(InstantiationPhase || {});
|
|
17
|
+
|
|
18
|
+
// src/di/base.ts
|
|
19
|
+
var serviceIds = /* @__PURE__ */ new Map();
|
|
20
|
+
var DI_TARGET = "$di$target";
|
|
21
|
+
var DI_DEPENDENCIES = "$di$dependencies";
|
|
22
|
+
function setServiceDependency(id, ctor, index) {
|
|
23
|
+
if (ctor[DI_TARGET] === ctor) {
|
|
24
|
+
ctor[DI_DEPENDENCIES].push({ id, index });
|
|
25
|
+
} else {
|
|
26
|
+
ctor[DI_DEPENDENCIES] = [{ id, index }];
|
|
27
|
+
ctor[DI_TARGET] = ctor;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function createDecorator(serviceId) {
|
|
31
|
+
if (serviceIds.has(serviceId)) {
|
|
32
|
+
return serviceIds.get(serviceId);
|
|
33
|
+
}
|
|
34
|
+
const id = function(target, key, index) {
|
|
35
|
+
if (arguments.length !== 3) {
|
|
36
|
+
throw new Error("@IServiceName-decorator can only be used to decorate a parameter");
|
|
37
|
+
}
|
|
38
|
+
setServiceDependency(id, target, index);
|
|
39
|
+
};
|
|
40
|
+
id.toString = () => serviceId;
|
|
41
|
+
serviceIds.set(serviceId, id);
|
|
42
|
+
return id;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/di/instantiation-service.interface.ts
|
|
46
|
+
var IInstantiationService = createDecorator("instantiation");
|
|
47
|
+
|
|
48
|
+
// src/_internal/logger.ts
|
|
49
|
+
var Logger = {
|
|
50
|
+
error(...data) {
|
|
51
|
+
if (typeof console.error === "function") {
|
|
52
|
+
console.error(...data);
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
info(...data) {
|
|
56
|
+
if (typeof console.info === "function") {
|
|
57
|
+
console.info(...data);
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
log(...data) {
|
|
61
|
+
if (typeof console.log === "function") {
|
|
62
|
+
console.log(...data);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
time(label) {
|
|
66
|
+
if (typeof console.time === "function") {
|
|
67
|
+
console.time(label);
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
timeEnd(label) {
|
|
71
|
+
if (typeof console.timeEnd === "function") {
|
|
72
|
+
console.timeEnd(label);
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
timeLog(label, ...data) {
|
|
76
|
+
if (typeof console.timeLog === "function") {
|
|
77
|
+
console.timeLog(label, ...data);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
timeStamp(label) {
|
|
81
|
+
if (typeof console.timeStamp === "function") {
|
|
82
|
+
console.timeStamp(label);
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
trace(...data) {
|
|
86
|
+
if (typeof console.trace === "function") {
|
|
87
|
+
console.trace(...data);
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
warn(...data) {
|
|
91
|
+
if (typeof console.warn === "function") {
|
|
92
|
+
console.warn(...data);
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
profile(label) {
|
|
96
|
+
if (typeof console.profile === "function") {
|
|
97
|
+
console.profile(label);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
profileEnd(label) {
|
|
101
|
+
if (typeof console.profileEnd === "function") {
|
|
102
|
+
console.profileEnd(label);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
function BRANCH_DISPOSE(from, to) {
|
|
107
|
+
}
|
|
108
|
+
function SET_PARENT_OF_DISPOSABLE(child, parent) {
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// src/dispose/disposable-store.ts
|
|
112
|
+
var _DisposableStore = class _DisposableStore {
|
|
113
|
+
constructor() {
|
|
114
|
+
this._toDispose = /* @__PURE__ */ new Set();
|
|
115
|
+
this._isDisposed = false;
|
|
116
|
+
}
|
|
117
|
+
get isDisposed() {
|
|
118
|
+
return this._isDisposed;
|
|
119
|
+
}
|
|
120
|
+
dispose() {
|
|
121
|
+
if (this._isDisposed) {
|
|
122
|
+
Logger.warn(new Error("DisposableStore has disposed.").stack);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
this._isDisposed = true;
|
|
126
|
+
this.clear();
|
|
127
|
+
}
|
|
128
|
+
clear() {
|
|
129
|
+
if (this._toDispose.size === 0) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
for (const disposable of this._toDispose) {
|
|
133
|
+
BRANCH_DISPOSE(this.constructor.name, disposable.constructor.name);
|
|
134
|
+
}
|
|
135
|
+
const errors = [];
|
|
136
|
+
for (const disposable of this._toDispose) {
|
|
137
|
+
try {
|
|
138
|
+
disposable.dispose();
|
|
139
|
+
} catch (e) {
|
|
140
|
+
errors.push(e);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
this._toDispose.clear();
|
|
144
|
+
if (errors.length > 0) {
|
|
145
|
+
throw new AggregateError(errors, "find error when dispose store.");
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
add(o) {
|
|
149
|
+
if (!o) {
|
|
150
|
+
return o;
|
|
151
|
+
}
|
|
152
|
+
if (o === this) {
|
|
153
|
+
throw new Error("Cannot register a disposable on itself.");
|
|
154
|
+
}
|
|
155
|
+
if (this._isDisposed) {
|
|
156
|
+
if (!_DisposableStore.DISABLE_DISPOSED_WARNING) {
|
|
157
|
+
Logger.warn(
|
|
158
|
+
new Error(
|
|
159
|
+
"Trying to add a disposable to a DisposableStore that has already been disposed of. The added object will be leaked!"
|
|
160
|
+
).stack
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
this._toDispose.add(o);
|
|
165
|
+
}
|
|
166
|
+
return o;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
_DisposableStore.DISABLE_DISPOSED_WARNING = false;
|
|
170
|
+
var DisposableStore = _DisposableStore;
|
|
171
|
+
|
|
172
|
+
// src/assert/assert.ts
|
|
173
|
+
function abort(reason) {
|
|
174
|
+
throw new Error(`lvAssert(${reason})`);
|
|
175
|
+
}
|
|
176
|
+
function lvAssert(expr, reason) {
|
|
177
|
+
if (!expr) {
|
|
178
|
+
abort(reason ?? "#expr is false");
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/dispose/disposable-t.ts
|
|
183
|
+
var Disposable = class {
|
|
184
|
+
constructor() {
|
|
185
|
+
this._store = new DisposableStore();
|
|
186
|
+
SET_PARENT_OF_DISPOSABLE(this._store);
|
|
187
|
+
}
|
|
188
|
+
// 销毁该节点和所有的子节点
|
|
189
|
+
dispose() {
|
|
190
|
+
BRANCH_DISPOSE(this.constructor.name, this._store.constructor.name);
|
|
191
|
+
this._store.dispose();
|
|
192
|
+
}
|
|
193
|
+
// 挂载子节点
|
|
194
|
+
_register(o) {
|
|
195
|
+
if (o === this) {
|
|
196
|
+
throw new Error("Cannot register a disposable on itself!");
|
|
197
|
+
}
|
|
198
|
+
return this._store.add(o);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
// src/scheduler/core/task.ts
|
|
203
|
+
var Task = class {
|
|
204
|
+
constructor(callback, _startTime, _expirationTime) {
|
|
205
|
+
this._startTime = _startTime;
|
|
206
|
+
this._expirationTime = _expirationTime;
|
|
207
|
+
this._sortIndex = -1;
|
|
208
|
+
this._callback = callback;
|
|
209
|
+
}
|
|
210
|
+
getCallback() {
|
|
211
|
+
return this._callback;
|
|
212
|
+
}
|
|
213
|
+
setCallback(callback) {
|
|
214
|
+
lvAssert(this._callback === void 0, "cant overlay callback.");
|
|
215
|
+
this._callback = callback;
|
|
216
|
+
}
|
|
217
|
+
clearCallback() {
|
|
218
|
+
this._callback = void 0;
|
|
219
|
+
}
|
|
220
|
+
getStartTime() {
|
|
221
|
+
return this._startTime;
|
|
222
|
+
}
|
|
223
|
+
setStartTime(startTime) {
|
|
224
|
+
this._startTime = startTime;
|
|
225
|
+
}
|
|
226
|
+
getExpirationTime() {
|
|
227
|
+
return this._expirationTime;
|
|
228
|
+
}
|
|
229
|
+
setExpirationTime(expirationTime) {
|
|
230
|
+
this._expirationTime = expirationTime;
|
|
231
|
+
}
|
|
232
|
+
setSortIndex(index) {
|
|
233
|
+
this._sortIndex = index;
|
|
234
|
+
}
|
|
235
|
+
getSortIndex() {
|
|
236
|
+
return this._sortIndex;
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
// src/scheduler/core/utils.ts
|
|
241
|
+
var maxSigned31BitInt = 1073741823;
|
|
242
|
+
var IMMEDIATE_PRIORITY_TIMEOUT = -1;
|
|
243
|
+
var USER_BLOCKING_PRIORITY_TIMEOUT = 250;
|
|
244
|
+
var NORMAL_PRIORITY_TIMEOUT = 5e3;
|
|
245
|
+
var LOW_PRIORITY_TIMEOUT = 1e4;
|
|
246
|
+
var IDLE_PRIORITY_TIMEOUT = maxSigned31BitInt;
|
|
247
|
+
function getCurrentTime() {
|
|
248
|
+
return Date.now();
|
|
249
|
+
}
|
|
250
|
+
function getTimeout(priorityLevel = 2 /* NormalPriority */) {
|
|
251
|
+
switch (priorityLevel) {
|
|
252
|
+
case 0 /* ImmediatePriority */:
|
|
253
|
+
return IMMEDIATE_PRIORITY_TIMEOUT;
|
|
254
|
+
case 1 /* UserBlockingPriority */:
|
|
255
|
+
return USER_BLOCKING_PRIORITY_TIMEOUT;
|
|
256
|
+
case 4 /* IdlePriority */:
|
|
257
|
+
return IDLE_PRIORITY_TIMEOUT;
|
|
258
|
+
case 3 /* LowPriority */:
|
|
259
|
+
return LOW_PRIORITY_TIMEOUT;
|
|
260
|
+
case 2 /* NormalPriority */:
|
|
261
|
+
default:
|
|
262
|
+
return NORMAL_PRIORITY_TIMEOUT;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function makeTask(callback, options = {}) {
|
|
266
|
+
const currentTime = getCurrentTime();
|
|
267
|
+
const delay = options.delay ?? 0;
|
|
268
|
+
const timeout = getTimeout(options.priorityLevel);
|
|
269
|
+
const startTime = delay + currentTime;
|
|
270
|
+
const expirationTime = startTime + timeout;
|
|
271
|
+
const newTask = new Task(callback, startTime, expirationTime);
|
|
272
|
+
return newTask;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// src/scheduler/core/chunk-scheduler.ts
|
|
276
|
+
var ChunkScheduler = class {
|
|
277
|
+
constructor(_task, _scheduler) {
|
|
278
|
+
this._task = _task;
|
|
279
|
+
this._scheduler = _scheduler;
|
|
280
|
+
this._needContinue = false;
|
|
281
|
+
}
|
|
282
|
+
get needContinue() {
|
|
283
|
+
return this._needContinue;
|
|
284
|
+
}
|
|
285
|
+
continueExecute(callback) {
|
|
286
|
+
this._needContinue = true;
|
|
287
|
+
this._task.setCallback(callback);
|
|
288
|
+
}
|
|
289
|
+
execute(callback, options = {}) {
|
|
290
|
+
const newTask = makeTask(callback, options);
|
|
291
|
+
this._scheduler.addTask(newTask);
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
// src/scheduler/core/actuator.ts
|
|
296
|
+
var Actuator = class {
|
|
297
|
+
constructor(_taskQueue, _scheduler) {
|
|
298
|
+
this._taskQueue = _taskQueue;
|
|
299
|
+
this._scheduler = _scheduler;
|
|
300
|
+
}
|
|
301
|
+
workLoop(hasTimeRemaining, initialTime, deadline) {
|
|
302
|
+
let currentTime = initialTime;
|
|
303
|
+
this._taskQueue.advance(currentTime);
|
|
304
|
+
let currentTask = this._taskQueue.waitingTasks.peek();
|
|
305
|
+
while (currentTask !== null) {
|
|
306
|
+
if (currentTask.getExpirationTime() > currentTime && (!hasTimeRemaining || this._scheduler.shouldYieldToHost(deadline))) {
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
if (currentTask.getCallback() === void 0) {
|
|
310
|
+
this._taskQueue.waitingTasks.remove();
|
|
311
|
+
} else {
|
|
312
|
+
const callback = currentTask.getCallback();
|
|
313
|
+
const didUserCallbackTimeout = currentTask.getExpirationTime() <= currentTime;
|
|
314
|
+
const remainingTime = deadline - currentTime;
|
|
315
|
+
currentTask.clearCallback();
|
|
316
|
+
const chunkInvoker = new ChunkScheduler(currentTask, this._scheduler);
|
|
317
|
+
callback(chunkInvoker, didUserCallbackTimeout, remainingTime);
|
|
318
|
+
if (!chunkInvoker.needContinue) {
|
|
319
|
+
this._taskQueue.waitingTasks.remove();
|
|
320
|
+
}
|
|
321
|
+
currentTime = getCurrentTime();
|
|
322
|
+
this._taskQueue.advance(currentTime);
|
|
323
|
+
}
|
|
324
|
+
currentTask = this._taskQueue.waitingTasks.peek();
|
|
325
|
+
}
|
|
326
|
+
if (currentTask !== null) {
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
this._scheduler.requestHostTimeout(currentTime);
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
// src/structure/min-heap.ts
|
|
335
|
+
var MinHeap = class {
|
|
336
|
+
constructor(compareFunction) {
|
|
337
|
+
this._heap = [];
|
|
338
|
+
if (compareFunction) {
|
|
339
|
+
this._compare = compareFunction;
|
|
340
|
+
} else {
|
|
341
|
+
this._compare = (a, b) => {
|
|
342
|
+
if (a < b) return -1;
|
|
343
|
+
if (a > b) return 1;
|
|
344
|
+
return 0;
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
insert(value) {
|
|
349
|
+
this._heap.push(value);
|
|
350
|
+
this._siftUp();
|
|
351
|
+
}
|
|
352
|
+
peek() {
|
|
353
|
+
return this._heap.length > 0 ? this._heap[0] : null;
|
|
354
|
+
}
|
|
355
|
+
remove() {
|
|
356
|
+
if (this._heap.length === 0) return null;
|
|
357
|
+
if (this._heap.length === 1) return this._heap.pop();
|
|
358
|
+
const item = this._heap[0];
|
|
359
|
+
this._heap[0] = this._heap.pop();
|
|
360
|
+
this._siftDown();
|
|
361
|
+
return item;
|
|
362
|
+
}
|
|
363
|
+
size() {
|
|
364
|
+
return this._heap.length;
|
|
365
|
+
}
|
|
366
|
+
clear() {
|
|
367
|
+
this._heap.length = 0;
|
|
368
|
+
}
|
|
369
|
+
_getLeftChildIndex(parentIndex) {
|
|
370
|
+
return 2 * parentIndex + 1;
|
|
371
|
+
}
|
|
372
|
+
_getRightChildIndex(parentIndex) {
|
|
373
|
+
return 2 * parentIndex + 2;
|
|
374
|
+
}
|
|
375
|
+
_getParentIndex(childIndex) {
|
|
376
|
+
return Math.floor((childIndex - 1) / 2);
|
|
377
|
+
}
|
|
378
|
+
_swap(indexOne, indexTwo) {
|
|
379
|
+
[this._heap[indexOne], this._heap[indexTwo]] = [this._heap[indexTwo], this._heap[indexOne]];
|
|
380
|
+
}
|
|
381
|
+
_siftUp() {
|
|
382
|
+
let index = this._heap.length - 1;
|
|
383
|
+
while (index > 0 && this._compare(this._heap[this._getParentIndex(index)], this._heap[index]) > 0) {
|
|
384
|
+
const parentIndex = this._getParentIndex(index);
|
|
385
|
+
this._swap(index, parentIndex);
|
|
386
|
+
index = parentIndex;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
_siftDown() {
|
|
390
|
+
let index = 0;
|
|
391
|
+
let smallerChildIndex = this._getLeftChildIndex(index);
|
|
392
|
+
while (smallerChildIndex < this._heap.length) {
|
|
393
|
+
const rightChildIndex = this._getRightChildIndex(index);
|
|
394
|
+
if (rightChildIndex < this._heap.length && this._compare(this._heap[rightChildIndex], this._heap[smallerChildIndex]) < 0) {
|
|
395
|
+
smallerChildIndex = rightChildIndex;
|
|
396
|
+
}
|
|
397
|
+
if (this._compare(this._heap[index], this._heap[smallerChildIndex]) <= 0) {
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
this._swap(index, smallerChildIndex);
|
|
401
|
+
index = smallerChildIndex;
|
|
402
|
+
smallerChildIndex = this._getLeftChildIndex(index);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
// src/scheduler/core/task-queue.ts
|
|
408
|
+
function compare(lhs, rhs) {
|
|
409
|
+
return lhs.getSortIndex() - rhs.getSortIndex();
|
|
410
|
+
}
|
|
411
|
+
var TaskQueue = class {
|
|
412
|
+
constructor() {
|
|
413
|
+
// 延时任务
|
|
414
|
+
this._timerTasks = new MinHeap(compare);
|
|
415
|
+
// 等待执行的任务
|
|
416
|
+
this._waitingTasks = new MinHeap(compare);
|
|
417
|
+
}
|
|
418
|
+
get timerTasks() {
|
|
419
|
+
return this._timerTasks;
|
|
420
|
+
}
|
|
421
|
+
get waitingTasks() {
|
|
422
|
+
return this._waitingTasks;
|
|
423
|
+
}
|
|
424
|
+
advance(currentTime) {
|
|
425
|
+
let task = this._timerTasks.peek();
|
|
426
|
+
while (task !== null) {
|
|
427
|
+
if (task.getCallback() === null) {
|
|
428
|
+
this._timerTasks.remove();
|
|
429
|
+
} else if (task.getStartTime() <= currentTime) {
|
|
430
|
+
this._timerTasks.remove();
|
|
431
|
+
task.setSortIndex(task.getExpirationTime());
|
|
432
|
+
this._waitingTasks.insert(task);
|
|
433
|
+
} else {
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
task = this._timerTasks.peek();
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
// src/scheduler/executor/abstract-executor.ts
|
|
442
|
+
var AbstractExecutor = class {
|
|
443
|
+
constructor() {
|
|
444
|
+
this._timeoutId = -1;
|
|
445
|
+
this._deadline = 0;
|
|
446
|
+
this._yieldInterval = 16;
|
|
447
|
+
}
|
|
448
|
+
get deadline() {
|
|
449
|
+
return this._deadline;
|
|
450
|
+
}
|
|
451
|
+
setFrameRate(fps) {
|
|
452
|
+
lvAssert(fps > 0 && fps <= 125);
|
|
453
|
+
this._yieldInterval = Math.floor(1e3 / fps);
|
|
454
|
+
}
|
|
455
|
+
resetFrameRate() {
|
|
456
|
+
this._yieldInterval = 16;
|
|
457
|
+
}
|
|
458
|
+
requestHostTimeout(fn, delayMs) {
|
|
459
|
+
lvAssert(this._timeoutId === -1, "has request host timeout.");
|
|
460
|
+
clearTimeout(this._timeoutId);
|
|
461
|
+
this._timeoutId = setTimeout(() => {
|
|
462
|
+
this._timeoutId = -1;
|
|
463
|
+
fn();
|
|
464
|
+
}, delayMs);
|
|
465
|
+
}
|
|
466
|
+
cancelHostTimeout() {
|
|
467
|
+
clearTimeout(this._timeoutId);
|
|
468
|
+
this._timeoutId = -1;
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
// src/scheduler/executor/idle-callback-executor.ts
|
|
473
|
+
var IdleCallbackExecutor = class extends AbstractExecutor {
|
|
474
|
+
constructor() {
|
|
475
|
+
super();
|
|
476
|
+
this._flushCallback = () => {
|
|
477
|
+
if (!this._scheduledCallback) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
const currentTime = getCurrentTime();
|
|
481
|
+
const deadline = currentTime + this._yieldInterval;
|
|
482
|
+
try {
|
|
483
|
+
const hasMoreWork = this._scheduledCallback(true, currentTime, deadline);
|
|
484
|
+
if (!hasMoreWork) {
|
|
485
|
+
this._scheduledCallback = void 0;
|
|
486
|
+
this._disposable = void 0;
|
|
487
|
+
} else {
|
|
488
|
+
this._disposable = this._runWhenIdle(this._flushCallback);
|
|
489
|
+
}
|
|
490
|
+
} catch (err) {
|
|
491
|
+
this._disposable = this._runWhenIdle(this._flushCallback);
|
|
492
|
+
throw err;
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
if (typeof requestIdleCallback !== "function" || typeof cancelIdleCallback !== "function") {
|
|
496
|
+
this._runWhenIdle = (runner) => {
|
|
497
|
+
let disposed = false;
|
|
498
|
+
setTimeout(() => {
|
|
499
|
+
if (disposed) {
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
const end = Date.now() + 15;
|
|
503
|
+
runner(
|
|
504
|
+
Object.freeze({
|
|
505
|
+
didTimeout: true,
|
|
506
|
+
timeRemaining() {
|
|
507
|
+
return Math.max(0, end - Date.now());
|
|
508
|
+
}
|
|
509
|
+
})
|
|
510
|
+
);
|
|
511
|
+
});
|
|
512
|
+
return {
|
|
513
|
+
dispose() {
|
|
514
|
+
if (disposed) {
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
disposed = true;
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
};
|
|
521
|
+
} else {
|
|
522
|
+
this._runWhenIdle = (runner, timeout) => {
|
|
523
|
+
const handle = requestIdleCallback(
|
|
524
|
+
runner,
|
|
525
|
+
typeof timeout === "number" ? { timeout } : void 0
|
|
526
|
+
);
|
|
527
|
+
let disposed = false;
|
|
528
|
+
return {
|
|
529
|
+
dispose() {
|
|
530
|
+
if (disposed) {
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
disposed = true;
|
|
534
|
+
cancelIdleCallback(handle);
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
requestHostCallback(fn) {
|
|
541
|
+
this._scheduledCallback = fn;
|
|
542
|
+
if (!this._disposable) {
|
|
543
|
+
this._disposable = this._runWhenIdle(this._flushCallback);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
cancelHostCallback() {
|
|
547
|
+
this._disposable?.dispose();
|
|
548
|
+
this._scheduledCallback = void 0;
|
|
549
|
+
this._disposable = void 0;
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
// src/scheduler/executor/post-message-executor.ts
|
|
554
|
+
var PostMessageExecutor = class extends AbstractExecutor {
|
|
555
|
+
constructor() {
|
|
556
|
+
super();
|
|
557
|
+
this._isMessageLoopRunning = false;
|
|
558
|
+
this._channel = new MessageChannel();
|
|
559
|
+
this._flushCallback = () => {
|
|
560
|
+
if (!this._scheduledCallback) {
|
|
561
|
+
this._isMessageLoopRunning = false;
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
const currentTime = getCurrentTime();
|
|
565
|
+
const deadline = currentTime + this._yieldInterval;
|
|
566
|
+
try {
|
|
567
|
+
const hasMoreWork = this._scheduledCallback(true, currentTime, deadline);
|
|
568
|
+
if (!hasMoreWork) {
|
|
569
|
+
this._isMessageLoopRunning = false;
|
|
570
|
+
this._scheduledCallback = void 0;
|
|
571
|
+
} else {
|
|
572
|
+
this._channel.port2.postMessage(null);
|
|
573
|
+
}
|
|
574
|
+
} catch (err) {
|
|
575
|
+
this._channel.port2.postMessage(null);
|
|
576
|
+
throw err;
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
this._channel.port1.onmessage = () => {
|
|
580
|
+
this._flushCallback();
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
requestHostCallback(fn) {
|
|
584
|
+
this._scheduledCallback = fn;
|
|
585
|
+
if (!this._isMessageLoopRunning) {
|
|
586
|
+
this._isMessageLoopRunning = true;
|
|
587
|
+
this._channel.port2.postMessage(null);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
cancelHostCallback() {
|
|
591
|
+
this._scheduledCallback = void 0;
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
// src/scheduler/executor/make-executor.ts
|
|
596
|
+
function makeExecutor() {
|
|
597
|
+
try {
|
|
598
|
+
if (global.window) {
|
|
599
|
+
return new PostMessageExecutor();
|
|
600
|
+
}
|
|
601
|
+
} catch (e) {
|
|
602
|
+
}
|
|
603
|
+
return new IdleCallbackExecutor();
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// src/scheduler/core/scheduler.ts
|
|
607
|
+
var Scheduler = class {
|
|
608
|
+
constructor() {
|
|
609
|
+
this._isHostTimeoutScheduled = false;
|
|
610
|
+
this._isHostCallbackScheduled = false;
|
|
611
|
+
this._isWorking = false;
|
|
612
|
+
this._enableInputPending = true;
|
|
613
|
+
this._executor = makeExecutor();
|
|
614
|
+
this._taskQueue = new TaskQueue();
|
|
615
|
+
this._actuator = new Actuator(this._taskQueue, this);
|
|
616
|
+
/**
|
|
617
|
+
* 是否需要让出事件给宿主
|
|
618
|
+
*/
|
|
619
|
+
this.shouldYieldToHost = (() => {
|
|
620
|
+
try {
|
|
621
|
+
if (global.navigator?.scheduling?.isInputPending !== void 0) {
|
|
622
|
+
const { scheduling } = global.navigator;
|
|
623
|
+
return (deadline) => {
|
|
624
|
+
if (this._enableInputPending && scheduling.isInputPending()) {
|
|
625
|
+
return true;
|
|
626
|
+
}
|
|
627
|
+
return getCurrentTime() >= deadline;
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
} catch (e) {
|
|
631
|
+
}
|
|
632
|
+
return (deadline) => {
|
|
633
|
+
return getCurrentTime() >= deadline;
|
|
634
|
+
};
|
|
635
|
+
})();
|
|
636
|
+
this._handleHostTimeout = () => {
|
|
637
|
+
this._isHostTimeoutScheduled = false;
|
|
638
|
+
const currentTime = getCurrentTime();
|
|
639
|
+
this._taskQueue.advance(currentTime);
|
|
640
|
+
if (this._isHostCallbackScheduled) {
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
if (this._taskQueue.waitingTasks.peek() !== null) {
|
|
644
|
+
this._isHostCallbackScheduled = true;
|
|
645
|
+
this._executor.requestHostCallback(this._handleHostCallback);
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
this._requestHostTimeout(currentTime);
|
|
649
|
+
};
|
|
650
|
+
this._handleHostCallback = (hasTimeRemaining, initialTime, deadline) => {
|
|
651
|
+
this._isHostCallbackScheduled = false;
|
|
652
|
+
if (this._isHostTimeoutScheduled) {
|
|
653
|
+
this._isHostTimeoutScheduled = false;
|
|
654
|
+
this._executor.cancelHostTimeout();
|
|
655
|
+
}
|
|
656
|
+
this._isWorking = true;
|
|
657
|
+
try {
|
|
658
|
+
return this._actuator.workLoop(hasTimeRemaining, initialTime, deadline);
|
|
659
|
+
} finally {
|
|
660
|
+
this._isWorking = false;
|
|
661
|
+
}
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
get taskQueue() {
|
|
665
|
+
return this._taskQueue;
|
|
666
|
+
}
|
|
667
|
+
get executor() {
|
|
668
|
+
return this._executor;
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* 设置是否开启InputPending
|
|
672
|
+
*/
|
|
673
|
+
setEnableInputPending(val) {
|
|
674
|
+
this._enableInputPending = val;
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* 调度器中添加任务
|
|
678
|
+
*/
|
|
679
|
+
addTask(task) {
|
|
680
|
+
const currentTime = getCurrentTime();
|
|
681
|
+
if (task.getStartTime() > currentTime) {
|
|
682
|
+
task.setSortIndex(task.getStartTime());
|
|
683
|
+
this._taskQueue.timerTasks.insert(task);
|
|
684
|
+
if (this._taskQueue.waitingTasks.peek() === null && task === this._taskQueue.timerTasks.peek()) {
|
|
685
|
+
if (this._isHostTimeoutScheduled) {
|
|
686
|
+
this._executor.cancelHostTimeout();
|
|
687
|
+
} else {
|
|
688
|
+
this._isHostTimeoutScheduled = true;
|
|
689
|
+
}
|
|
690
|
+
this._executor.requestHostTimeout(this._handleHostTimeout, task.getStartTime() - currentTime);
|
|
691
|
+
}
|
|
692
|
+
} else {
|
|
693
|
+
task.setSortIndex(task.getExpirationTime());
|
|
694
|
+
this._taskQueue.waitingTasks.insert(task);
|
|
695
|
+
if (!this._isHostCallbackScheduled && !this._isWorking) {
|
|
696
|
+
this._isHostCallbackScheduled = true;
|
|
697
|
+
this._executor.requestHostCallback(this._handleHostCallback);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* 尝试启动异步任务调度
|
|
703
|
+
*/
|
|
704
|
+
requestHostTimeout(currentTime) {
|
|
705
|
+
this._requestHostTimeout(currentTime);
|
|
706
|
+
}
|
|
707
|
+
_requestHostTimeout(currentTime) {
|
|
708
|
+
lvAssert(!this._isHostCallbackScheduled);
|
|
709
|
+
lvAssert(!this._isHostTimeoutScheduled);
|
|
710
|
+
const firstTimerTask = this._taskQueue.timerTasks.peek();
|
|
711
|
+
if (firstTimerTask !== null) {
|
|
712
|
+
this._isHostTimeoutScheduled = true;
|
|
713
|
+
this._executor.requestHostTimeout(this._handleHostTimeout, firstTimerTask.getStartTime() - currentTime);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
var scheduler;
|
|
718
|
+
function getScheduler() {
|
|
719
|
+
if (!scheduler) {
|
|
720
|
+
scheduler = new Scheduler();
|
|
721
|
+
}
|
|
722
|
+
return scheduler;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
// src/scheduler/callback-token.ts
|
|
726
|
+
var CallbackToken = class {
|
|
727
|
+
constructor(_task) {
|
|
728
|
+
this._task = _task;
|
|
729
|
+
}
|
|
730
|
+
dispose() {
|
|
731
|
+
this._task.clearCallback();
|
|
732
|
+
}
|
|
733
|
+
updatePriorityLevel(priorityLevel) {
|
|
734
|
+
const callback = this._task.getCallback();
|
|
735
|
+
if (!callback) {
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
this._task.clearCallback();
|
|
739
|
+
const newTask = makeTask(callback, {
|
|
740
|
+
priorityLevel
|
|
741
|
+
});
|
|
742
|
+
const startTime = this._task.getStartTime();
|
|
743
|
+
const timeout = getTimeout(priorityLevel);
|
|
744
|
+
const expirationTime = startTime + timeout;
|
|
745
|
+
newTask.setStartTime(startTime);
|
|
746
|
+
newTask.setExpirationTime(expirationTime);
|
|
747
|
+
getScheduler().addTask(newTask);
|
|
748
|
+
this._task = newTask;
|
|
749
|
+
}
|
|
750
|
+
};
|
|
751
|
+
|
|
752
|
+
// src/scheduler/lv-scheduler-callback.ts
|
|
753
|
+
function lvSchedulerCallback(callback, options = {}) {
|
|
754
|
+
const newTask = makeTask(callback, options);
|
|
755
|
+
getScheduler().addTask(newTask);
|
|
756
|
+
return new CallbackToken(newTask);
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
// src/services/service-extension/extension-service.interface.ts
|
|
760
|
+
var IExtensionService = createDecorator("extension");
|
|
761
|
+
|
|
762
|
+
// src/services/service-extension/extension-service.ts
|
|
763
|
+
var ExtensionService = class extends Disposable {
|
|
764
|
+
constructor(_instantiationService) {
|
|
765
|
+
super();
|
|
766
|
+
this._instantiationService = _instantiationService;
|
|
767
|
+
// TODO(niurouwan): 增加disposeMap
|
|
768
|
+
this._instances = /* @__PURE__ */ new Map();
|
|
769
|
+
this._pending = /* @__PURE__ */ new Map();
|
|
770
|
+
}
|
|
771
|
+
register(descriptor, onContributionReady) {
|
|
772
|
+
if (descriptor.instantiationPhase === 0 /* Eager */) {
|
|
773
|
+
this._safeCreate(descriptor);
|
|
774
|
+
onContributionReady?.(this._instances.get(descriptor.id));
|
|
775
|
+
}
|
|
776
|
+
if (descriptor.instantiationPhase === 1 /* Idle */) {
|
|
777
|
+
const disposable = lvSchedulerCallback(() => {
|
|
778
|
+
this._safeCreate(descriptor);
|
|
779
|
+
this._pending.delete(descriptor.id);
|
|
780
|
+
onContributionReady?.(this._instances.get(descriptor.id));
|
|
781
|
+
});
|
|
782
|
+
this._pending.set(descriptor.id, disposable);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
registerInstance(id, instance) {
|
|
786
|
+
if (this._instances.has(id)) {
|
|
787
|
+
console.error("duplicate register contribution: ", id);
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
this._instances.set(id, instance);
|
|
791
|
+
}
|
|
792
|
+
dispose() {
|
|
793
|
+
for (const [, v] of this._instances) {
|
|
794
|
+
v.dispose();
|
|
795
|
+
}
|
|
796
|
+
for (const [, v] of this._pending) {
|
|
797
|
+
v.dispose();
|
|
798
|
+
}
|
|
799
|
+
super.dispose();
|
|
800
|
+
}
|
|
801
|
+
_safeCreate(descriptor) {
|
|
802
|
+
if (this._instances.has(descriptor.id)) {
|
|
803
|
+
console.error("duplicate register contribution: ", descriptor.id);
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
const staticArguments = descriptor.staticArguments ?? [];
|
|
807
|
+
this._createInstance(descriptor, staticArguments);
|
|
808
|
+
}
|
|
809
|
+
_createInstance(descriptor, staticArguments) {
|
|
810
|
+
const instance = this._instantiationService.createInstance(descriptor.ctor, ...staticArguments);
|
|
811
|
+
this._instances.set(descriptor.id, instance);
|
|
812
|
+
}
|
|
813
|
+
};
|
|
814
|
+
ExtensionService = __decorateClass([
|
|
815
|
+
__decorateParam(0, IInstantiationService)
|
|
816
|
+
], ExtensionService);
|
|
817
|
+
|
|
818
|
+
export { ExtensionService, IExtensionService, InstantiationPhase };
|
|
819
|
+
//# sourceMappingURL=index.js.map
|
|
820
|
+
//# sourceMappingURL=index.js.map
|