@zk-tech/bedrock 0.0.1 → 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/async/index.cjs.map +1 -1
- package/dist/async/index.d.cts +2 -2
- package/dist/async/index.d.ts +2 -2
- package/dist/async/index.js.map +1 -1
- package/dist/error/index.cjs.map +1 -1
- package/dist/error/index.d.cts +19 -19
- package/dist/error/index.d.ts +19 -19
- package/dist/error/index.js.map +1 -1
- package/dist/{error-base-B4zaiJ5m.d.cts → error-base-DOFtBFla.d.cts} +8 -8
- package/dist/{error-base-B4zaiJ5m.d.ts → error-base-DOFtBFla.d.ts} +8 -8
- package/dist/promise/index.cjs.map +1 -1
- package/dist/promise/index.d.cts +4 -4
- package/dist/promise/index.d.ts +4 -4
- package/dist/promise/index.js.map +1 -1
- package/dist/worker/index.cjs.map +1 -1
- package/dist/worker/index.d.cts +2 -2
- package/dist/worker/index.d.ts +2 -2
- package/dist/worker/index.js.map +1 -1
- package/package.json +1 -2
- package/src/_internal/logger.ts +0 -59
- package/src/array/array.test.ts +0 -35
- package/src/array/array.ts +0 -25
- package/src/array/index.ts +0 -1
- package/src/assert/assert.test.ts +0 -86
- package/src/assert/assert.ts +0 -42
- package/src/assert/index.ts +0 -2
- package/src/async/barrier.test.ts +0 -90
- package/src/async/barrier.ts +0 -58
- package/src/async/cancellation.test.ts +0 -85
- package/src/async/cancellation.ts +0 -193
- package/src/async/index.ts +0 -18
- package/src/async/queue/queue.test.ts +0 -70
- package/src/async/queue/queue.ts +0 -56
- package/src/async/queue/task.test.ts +0 -155
- package/src/async/queue/task.ts +0 -67
- package/src/async/utils.test.ts +0 -28
- package/src/async/utils.ts +0 -8
- package/src/async/wait.ts +0 -9
- package/src/byte/format.test.ts +0 -64
- package/src/byte/format.ts +0 -44
- package/src/byte/index.ts +0 -2
- package/src/byte/node_modules/.vitest/results.json +0 -1
- package/src/byte/var.ts +0 -11
- package/src/cache/index.ts +0 -2
- package/src/cache/lru-with-timeout.test.ts +0 -88
- package/src/cache/lru-with-timeout.ts +0 -85
- package/src/cache/lru.test.ts +0 -56
- package/src/cache/lru.ts +0 -59
- package/src/context/context.test.ts +0 -17
- package/src/context/context.ts +0 -60
- package/src/context/index.ts +0 -8
- package/src/di/base.ts +0 -73
- package/src/di/container-service.test.ts +0 -179
- package/src/di/context.web.tsx +0 -41
- package/src/di/descriptor.ts +0 -31
- package/src/di/idle-value.test.ts +0 -73
- package/src/di/idle-value.ts +0 -63
- package/src/di/index.common.ts +0 -32
- package/src/di/index.ts +0 -2
- package/src/di/instantiation-service.interface.ts +0 -46
- package/src/di/instantiation-service.test.ts +0 -337
- package/src/di/instantiation-service.ts +0 -468
- package/src/di/lazy/foo.mock.ts +0 -28
- package/src/di/lazy/idle-load.ts +0 -39
- package/src/di/lazy/index.ts +0 -4
- package/src/di/lazy/lazy-service.test.ts +0 -65
- package/src/di/lazy/lazy-service.ts +0 -71
- package/src/di/lazy/type.ts +0 -5
- package/src/di/node_modules/.vitest/results.json +0 -1
- package/src/di/proxy-builder.test.ts +0 -45
- package/src/di/proxy-builder.ts +0 -38
- package/src/di/service-collection.test.ts +0 -27
- package/src/di/service-collection.ts +0 -46
- package/src/di/service-ownership-collection.test.ts +0 -39
- package/src/di/service-ownership-collection.ts +0 -38
- package/src/di/service-registry.test.ts +0 -66
- package/src/di/service-registry.ts +0 -99
- package/src/di/trace.ts +0 -85
- package/src/dispose/disposable-store.test.ts +0 -57
- package/src/dispose/disposable-store.ts +0 -80
- package/src/dispose/disposable-t.test.ts +0 -123
- package/src/dispose/disposable-t.ts +0 -238
- package/src/dispose/disposable-utils.test.ts +0 -15
- package/src/dispose/disposable-utils.ts +0 -28
- package/src/dispose/dispose-base.ts +0 -9
- package/src/dispose/index.ts +0 -34
- package/src/dispose/logger.test.ts +0 -65
- package/src/dispose/logger.ts +0 -39
- package/src/dispose/timer.test.ts +0 -30
- package/src/dispose/timer.ts +0 -16
- package/src/dispose/tracker.test.ts +0 -51
- package/src/dispose/tracker.ts +0 -105
- package/src/error/error-base.ts +0 -45
- package/src/error/error-code.ts +0 -39
- package/src/error/error-const.test.ts +0 -30
- package/src/error/error-const.ts +0 -16
- package/src/error/error-or.test.ts +0 -44
- package/src/error/error-or.ts +0 -2
- package/src/error/error-t.test.ts +0 -116
- package/src/error/error-t.ts +0 -100
- package/src/error/index.ts +0 -24
- package/src/error/node_modules/.vitest/results.json +0 -1
- package/src/event/disposable-linked-list.ts +0 -29
- package/src/event/emitter.test.ts +0 -191
- package/src/event/emitter.ts +0 -162
- package/src/event/error-handler.ts +0 -22
- package/src/event/index.ts +0 -34
- package/src/event/once.ts +0 -29
- package/src/event/phase-emitter.test.ts +0 -212
- package/src/event/phase-emitter.ts +0 -209
- package/src/event/shortcut-event-utils.ts +0 -33
- package/src/event/utils.ts +0 -6
- package/src/event/when.ts +0 -40
- package/src/function/debounce.test.ts +0 -274
- package/src/function/debounce.ts +0 -168
- package/src/function/index.ts +0 -2
- package/src/function/node_modules/.vitest/results.json +0 -1
- package/src/function/throttle.test.ts +0 -179
- package/src/function/throttle.ts +0 -26
- package/src/hash/hash-t.test.ts +0 -100
- package/src/hash/hash-t.ts +0 -51
- package/src/hash/index.ts +0 -3
- package/src/json/index.ts +0 -1
- package/src/json/node_modules/.vitest/results.json +0 -1
- package/src/json/parse.ts +0 -19
- package/src/launch/abstract-job.ts +0 -45
- package/src/launch/cost-recorder.ts +0 -22
- package/src/launch/index.ts +0 -2
- package/src/launch/job-scheduler.test.ts +0 -122
- package/src/launch/job-scheduler.ts +0 -118
- package/src/launch/node_modules/.vitest/deps/_metadata.json +0 -8
- package/src/launch/node_modules/.vitest/deps/package.json +0 -3
- package/src/launch/node_modules/.vitest/results.json +0 -1
- package/src/lock/README.md +0 -11
- package/src/lock/capability.test.ts +0 -110
- package/src/lock/capability.ts +0 -89
- package/src/lock/index.ts +0 -15
- package/src/lock/node_modules/.vitest/results.json +0 -1
- package/src/lock/semaphore.ts +0 -21
- package/src/lock/shared-mutex.test.ts +0 -537
- package/src/lock/shared-mutex.ts +0 -242
- package/src/lock/utils.test.ts +0 -165
- package/src/lock/utils.ts +0 -135
- package/src/lodash-es/index.ts +0 -1
- package/src/math/degree.ts +0 -16
- package/src/math/index.ts +0 -7
- package/src/math/math.test.ts +0 -40
- package/src/math/math.ts +0 -64
- package/src/math/node_modules/.vitest/results.json +0 -1
- package/src/math/vector.test.ts +0 -73
- package/src/math/vector.ts +0 -114
- package/src/network/client.interface.ts +0 -104
- package/src/network/client.web.ts +0 -24
- package/src/network/index.common.ts +0 -10
- package/src/network/index.ts +0 -2
- package/src/network/plugins/retry.ts +0 -98
- package/src/objects/deep-clone.test.ts +0 -40
- package/src/objects/deep-clone.ts +0 -13
- package/src/objects/deep-equal.test.ts +0 -86
- package/src/objects/deep-equal.ts +0 -60
- package/src/objects/index.ts +0 -4
- package/src/platform/index.ts +0 -64
- package/src/promise/index.ts +0 -16
- package/src/promise/promise.test.ts +0 -254
- package/src/promise/promise.ts +0 -212
- package/src/scheduler/callback-token.ts +0 -31
- package/src/scheduler/core/actuator-args.test.ts +0 -47
- package/src/scheduler/core/actuator.test.ts +0 -82
- package/src/scheduler/core/actuator.ts +0 -58
- package/src/scheduler/core/chunk-scheduler.test.ts +0 -54
- package/src/scheduler/core/chunk-scheduler.ts +0 -28
- package/src/scheduler/core/node_modules/.vitest/results.json +0 -1
- package/src/scheduler/core/scheduler.test.ts +0 -328
- package/src/scheduler/core/scheduler.ts +0 -172
- package/src/scheduler/core/task-queue.test.ts +0 -78
- package/src/scheduler/core/task-queue.ts +0 -44
- package/src/scheduler/core/task.test.ts +0 -34
- package/src/scheduler/core/task.ts +0 -52
- package/src/scheduler/core/utils.ts +0 -48
- package/src/scheduler/executor/abstract-executor.test.ts +0 -44
- package/src/scheduler/executor/abstract-executor.ts +0 -38
- package/src/scheduler/executor/executor.interface.ts +0 -39
- package/src/scheduler/executor/idle-callback-executor.test.ts +0 -70
- package/src/scheduler/executor/idle-callback-executor.ts +0 -98
- package/src/scheduler/executor/make-executor.ts +0 -18
- package/src/scheduler/executor/post-message-executor.test.ts +0 -66
- package/src/scheduler/executor/post-message-executor.ts +0 -52
- package/src/scheduler/index.ts +0 -15
- package/src/scheduler/lv-scheduler-callback.ts +0 -19
- package/src/scheduler/lv-scheduler-config.ts +0 -17
- package/src/scheduler/type.ts +0 -48
- package/src/sprintf/index.ts +0 -2
- package/src/sprintf/sprintf.test.ts +0 -95
- package/src/sprintf/sprintf.ts +0 -97
- package/src/structure/graph.test.ts +0 -181
- package/src/structure/graph.ts +0 -105
- package/src/structure/index.ts +0 -8
- package/src/structure/linked-list.test.ts +0 -74
- package/src/structure/linked-list.ts +0 -145
- package/src/structure/min-heap.test.ts +0 -71
- package/src/structure/min-heap.ts +0 -91
- package/src/type/REAME.md +0 -2
- package/src/type/distributive-omit.interface.ts +0 -4
- package/src/type/index.ts +0 -3
- package/src/type/object-key-paths.interface.ts +0 -40
- package/src/undo-redo-stack/README.md +0 -61
- package/src/undo-redo-stack/action-stack.test.ts +0 -330
- package/src/undo-redo-stack/action-stack.ts +0 -150
- package/src/undo-redo-stack/element.ts +0 -4
- package/src/undo-redo-stack/index.ts +0 -7
- package/src/undo-redo-stack/state-stack.test.ts +0 -118
- package/src/undo-redo-stack/state-stack.ts +0 -133
- package/src/uuid/index.ts +0 -7
- package/src/uuid/uuid.ts +0 -86
- package/src/worker/cors-worker.ts +0 -38
- package/src/worker/index.ts +0 -4
- package/src/worker/node_modules/.vitest/results.json +0 -1
- package/src/worker/promise-worker-main-thread.test.ts +0 -91
- package/src/worker/promise-worker-main-thread.ts +0 -76
- package/src/worker/promise-worker-worker-thread.ts +0 -64
- package/src/worker/promise-worker.interface.ts +0 -15
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
class ListNode<T> {
|
|
2
|
-
constructor(
|
|
3
|
-
public value: T,
|
|
4
|
-
public next: ListNode<T> | null = null,
|
|
5
|
-
public prev: ListNode<T> | null = null,
|
|
6
|
-
) {}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export class LinkedList<T> {
|
|
10
|
-
protected _head: ListNode<T> | null = null;
|
|
11
|
-
protected _tail: ListNode<T> | null = null;
|
|
12
|
-
protected _count = 0;
|
|
13
|
-
|
|
14
|
-
public get size(): number {
|
|
15
|
-
return this._count;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public get firstNode(): ListNode<T> | null {
|
|
19
|
-
return this._head;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public isEmpty(): boolean {
|
|
23
|
-
return this._head === null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public clear(): void {
|
|
27
|
-
let current = this._head;
|
|
28
|
-
while (current !== null) {
|
|
29
|
-
const next = current.next;
|
|
30
|
-
current.prev = null;
|
|
31
|
-
current.next = null;
|
|
32
|
-
current = next;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
this._head = null;
|
|
36
|
-
this._tail = null;
|
|
37
|
-
this._count = 0;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public unshift(value: T): LinkedList<T> {
|
|
41
|
-
const node = new ListNode(value);
|
|
42
|
-
|
|
43
|
-
if (this.isEmpty()) {
|
|
44
|
-
this._head = node;
|
|
45
|
-
this._tail = node;
|
|
46
|
-
} else {
|
|
47
|
-
const oldHead = this._head;
|
|
48
|
-
this._head = node;
|
|
49
|
-
node.next = oldHead;
|
|
50
|
-
oldHead!.prev = node;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
this._count++;
|
|
54
|
-
return this;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
public push(value: T): LinkedList<T> {
|
|
58
|
-
const node = new ListNode(value);
|
|
59
|
-
|
|
60
|
-
if (this.isEmpty()) {
|
|
61
|
-
this._head = node;
|
|
62
|
-
this._tail = node;
|
|
63
|
-
} else {
|
|
64
|
-
const oldTail = this._tail;
|
|
65
|
-
this._tail = node;
|
|
66
|
-
node.prev = oldTail;
|
|
67
|
-
oldTail!.next = node;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
this._count++;
|
|
71
|
-
return this;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
public shift(): T | null {
|
|
75
|
-
if (this.isEmpty()) {
|
|
76
|
-
return null;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const node = this._head!;
|
|
80
|
-
const value = node.value;
|
|
81
|
-
this._remove(node);
|
|
82
|
-
return value;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
public pop(): T | null {
|
|
86
|
-
if (this.isEmpty()) {
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const node = this._tail!;
|
|
91
|
-
const value = node.value;
|
|
92
|
-
this._remove(node);
|
|
93
|
-
return value;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
public toArray(): T[] {
|
|
97
|
-
const result: T[] = [];
|
|
98
|
-
for (const value of this) {
|
|
99
|
-
result.push(value);
|
|
100
|
-
}
|
|
101
|
-
return result;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
public *[Symbol.iterator](): Iterator<T> {
|
|
105
|
-
let current = this._head;
|
|
106
|
-
while (current !== null) {
|
|
107
|
-
yield current.value;
|
|
108
|
-
current = current.next;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
protected _remove(node: ListNode<T>): void {
|
|
113
|
-
// 如果节点已经被移除(prev 和 next 都为 null),直接返回
|
|
114
|
-
if (node.prev === null && node.next === null && node !== this._head && node !== this._tail) {
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// 更新链表头尾指针
|
|
119
|
-
if (node === this._head) {
|
|
120
|
-
this._head = node.next;
|
|
121
|
-
if (this._head) {
|
|
122
|
-
this._head.prev = null;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
if (node === this._tail) {
|
|
126
|
-
this._tail = node.prev;
|
|
127
|
-
if (this._tail) {
|
|
128
|
-
this._tail.next = null;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// 更新相邻节点的引用
|
|
133
|
-
if (node.prev) {
|
|
134
|
-
node.prev.next = node.next;
|
|
135
|
-
}
|
|
136
|
-
if (node.next) {
|
|
137
|
-
node.next.prev = node.prev;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// 清理被移除节点的引用
|
|
141
|
-
node.prev = null;
|
|
142
|
-
node.next = null;
|
|
143
|
-
this._count--;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { MinHeap } from './min-heap';
|
|
2
|
-
|
|
3
|
-
describe('MinHeap', () => {
|
|
4
|
-
let heap: MinHeap<number>;
|
|
5
|
-
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
heap = new MinHeap();
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
test('insert adds a value and sifts it up the heap', () => {
|
|
11
|
-
heap.insert(2);
|
|
12
|
-
expect(heap.peek()).toEqual(2);
|
|
13
|
-
|
|
14
|
-
heap.insert(3);
|
|
15
|
-
expect(heap.peek()).toEqual(2);
|
|
16
|
-
|
|
17
|
-
heap.insert(1);
|
|
18
|
-
expect(heap.peek()).toEqual(1);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('peek returns the minimum value without removing it', () => {
|
|
22
|
-
expect(heap.peek()).toBeNull();
|
|
23
|
-
|
|
24
|
-
heap.insert(1);
|
|
25
|
-
expect(heap.peek()).toEqual(1);
|
|
26
|
-
|
|
27
|
-
heap.insert(0);
|
|
28
|
-
expect(heap.peek()).toEqual(0);
|
|
29
|
-
|
|
30
|
-
heap.remove();
|
|
31
|
-
expect(heap.peek()).toEqual(1);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('remove extracts the minimum value and returns it', () => {
|
|
35
|
-
expect(heap.remove()).toBeNull();
|
|
36
|
-
|
|
37
|
-
heap.insert(2);
|
|
38
|
-
heap.insert(3);
|
|
39
|
-
heap.insert(1);
|
|
40
|
-
|
|
41
|
-
expect(heap.remove()).toBe(1);
|
|
42
|
-
expect(heap.remove()).toBe(2);
|
|
43
|
-
expect(heap.remove()).toBe(3);
|
|
44
|
-
expect(heap.remove()).toBeNull();
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test('size returns the number of elements in the heap', () => {
|
|
48
|
-
expect(heap.size()).toBe(0);
|
|
49
|
-
|
|
50
|
-
heap.insert(2);
|
|
51
|
-
heap.insert(3);
|
|
52
|
-
expect(heap.size()).toBe(2);
|
|
53
|
-
|
|
54
|
-
heap.remove();
|
|
55
|
-
expect(heap.size()).toBe(1);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test('heap maintains the heap property on inserts and removes', () => {
|
|
59
|
-
heap.insert(3);
|
|
60
|
-
heap.insert(4);
|
|
61
|
-
heap.insert(5);
|
|
62
|
-
heap.insert(1);
|
|
63
|
-
heap.insert(2);
|
|
64
|
-
|
|
65
|
-
expect(heap.remove()).toEqual(1);
|
|
66
|
-
expect(heap.remove()).toEqual(2);
|
|
67
|
-
expect(heap.remove()).toEqual(3);
|
|
68
|
-
expect(heap.remove()).toEqual(4);
|
|
69
|
-
expect(heap.remove()).toEqual(5);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
export class MinHeap<T> {
|
|
2
|
-
private readonly _heap: T[] = [];
|
|
3
|
-
private readonly _compare: (a: T, b: T) => number;
|
|
4
|
-
|
|
5
|
-
constructor(compareFunction?: (a: T, b: T) => number) {
|
|
6
|
-
if (compareFunction) {
|
|
7
|
-
this._compare = compareFunction;
|
|
8
|
-
} else {
|
|
9
|
-
this._compare = (a, b) => {
|
|
10
|
-
if (a < b) return -1;
|
|
11
|
-
if (a > b) return 1;
|
|
12
|
-
return 0;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public insert(value: T): void {
|
|
18
|
-
this._heap.push(value);
|
|
19
|
-
this._siftUp();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public peek(): T | null {
|
|
23
|
-
return this._heap.length > 0 ? this._heap[0] : null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public remove(): T | null {
|
|
27
|
-
if (this._heap.length === 0) return null;
|
|
28
|
-
if (this._heap.length === 1) return this._heap.pop()!;
|
|
29
|
-
|
|
30
|
-
const item = this._heap[0];
|
|
31
|
-
this._heap[0] = this._heap.pop()!;
|
|
32
|
-
this._siftDown();
|
|
33
|
-
return item;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public size(): number {
|
|
37
|
-
return this._heap.length;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public clear(): void {
|
|
41
|
-
this._heap.length = 0;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
private _getLeftChildIndex(parentIndex: number): number {
|
|
45
|
-
return 2 * parentIndex + 1;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private _getRightChildIndex(parentIndex: number): number {
|
|
49
|
-
return 2 * parentIndex + 2;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
private _getParentIndex(childIndex: number): number {
|
|
53
|
-
return Math.floor((childIndex - 1) / 2);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private _swap(indexOne: number, indexTwo: number): void {
|
|
57
|
-
[this._heap[indexOne], this._heap[indexTwo]] = [this._heap[indexTwo], this._heap[indexOne]];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
private _siftUp(): void {
|
|
61
|
-
let index = this._heap.length - 1;
|
|
62
|
-
while (index > 0 && this._compare(this._heap[this._getParentIndex(index)], this._heap[index]) > 0) {
|
|
63
|
-
const parentIndex = this._getParentIndex(index);
|
|
64
|
-
this._swap(index, parentIndex);
|
|
65
|
-
index = parentIndex;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
private _siftDown(): void {
|
|
70
|
-
let index = 0;
|
|
71
|
-
let smallerChildIndex = this._getLeftChildIndex(index);
|
|
72
|
-
|
|
73
|
-
while (smallerChildIndex < this._heap.length) {
|
|
74
|
-
const rightChildIndex = this._getRightChildIndex(index);
|
|
75
|
-
if (
|
|
76
|
-
rightChildIndex < this._heap.length &&
|
|
77
|
-
this._compare(this._heap[rightChildIndex], this._heap[smallerChildIndex]) < 0
|
|
78
|
-
) {
|
|
79
|
-
smallerChildIndex = rightChildIndex;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (this._compare(this._heap[index], this._heap[smallerChildIndex]) <= 0) {
|
|
83
|
-
break;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
this._swap(index, smallerChildIndex);
|
|
87
|
-
index = smallerChildIndex;
|
|
88
|
-
smallerChildIndex = this._getLeftChildIndex(index);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
package/src/type/REAME.md
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
// Typescript 提供的 Omit 函数会导致联合结构丢失,额外包装实现
|
|
2
|
-
// https://stackoverflow.com/questions/57103834/typescript-omit-a-property-from-all-interfaces-in-a-union-but-keep-the-union-s/57103940#57103940
|
|
3
|
-
|
|
4
|
-
export type DistributiveOmit<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
|
package/src/type/index.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 提取对象的 key 用 . 拼接成 path 形式
|
|
3
|
-
* type T1 = ObjectKeyPaths<{ name: string; age: number }>; // expected to be 'name' | 'age'
|
|
4
|
-
* type T2 = ObjectKeyPaths<{
|
|
5
|
-
* refCount: number;
|
|
6
|
-
* person: { name: string; age: number };
|
|
7
|
-
* }>; // expected to be 'refCount' | 'person' | 'person.name' | 'person.age'
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
type GenNode<K extends string | number, IsRoot extends boolean> = IsRoot extends true
|
|
11
|
-
? `${K}`
|
|
12
|
-
: `.${K}` | (K extends number ? `[${K}]` : never);
|
|
13
|
-
|
|
14
|
-
export type ObjectKeyPaths<
|
|
15
|
-
T extends Record<string, unknown>,
|
|
16
|
-
IsRoot extends boolean = true,
|
|
17
|
-
K extends keyof T = keyof T,
|
|
18
|
-
> = K extends string | number
|
|
19
|
-
?
|
|
20
|
-
| GenNode<K, IsRoot>
|
|
21
|
-
| (T[K] extends Record<string, unknown> ? `${GenNode<K, IsRoot>}${ObjectKeyPaths<T[K], false>}` : never)
|
|
22
|
-
: never;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 获取对象 path 对应的类型
|
|
26
|
-
* type T = ValueAtPath<{ a: { b: number } }, 'a.b'>; // expected to be number
|
|
27
|
-
*/
|
|
28
|
-
export type ValueAtPath<
|
|
29
|
-
T extends Record<string, unknown>, // 泛型类型参数 T 表示对象类型
|
|
30
|
-
P extends ObjectKeyPaths<T>, // 泛型类型参数 P 表示路径字符串,必须是 T 可能存在的所有路径之一
|
|
31
|
-
> = P extends `${infer K}.${infer R}` // 如果 P 包含 "." 分隔符,则将其拆分成两部分 K 和 R
|
|
32
|
-
? K extends keyof T // 如果 K 是 T 的属性之一
|
|
33
|
-
? T[K] extends Record<string, unknown>
|
|
34
|
-
? // @ts-ignore
|
|
35
|
-
ValueAtPath<T[K], R> // 递归地获取 T[K] 对应的子对象中的 R 路径属性的值
|
|
36
|
-
: never // 否则返回 never 类型(表示不存在)
|
|
37
|
-
: never
|
|
38
|
-
: P extends keyof T // 如果 P 不包含 "." 分隔符,并且是 T 的属性之一
|
|
39
|
-
? T[P] // 直接返回 T 中对应属性的值
|
|
40
|
-
: never; // 否则返回 never 类型(表示不存在)
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
# UndoRedo栈
|
|
2
|
-
提供了基础的undoredo栈规范,具体业务方可以直接使用或者继承使用。
|
|
3
|
-
|
|
4
|
-
## StateUndoRedoStack
|
|
5
|
-
state形式undoredo栈的实现,类似Redux这种形式,使用方只需要消费当前的state。
|
|
6
|
-
```
|
|
7
|
-
// 用法举例
|
|
8
|
-
const stack = new StateUndoRedoStack<T>(initialState);
|
|
9
|
-
|
|
10
|
-
stack.onDidRedo((state) => {
|
|
11
|
-
UI.reset(state);
|
|
12
|
-
});
|
|
13
|
-
stack.onDidUndo((state) => {
|
|
14
|
-
UI.reset(state);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
特点:
|
|
20
|
-
- 每次只会有一个state变更,不会undo的时候,连续触发两个state行为。
|
|
21
|
-
- 不建议继承并重写方法,因为这种模式的undoRedoStack,本身就是简单场景使用。
|
|
22
|
-
|
|
23
|
-
## ActionUndoRedoStack
|
|
24
|
-
action形式undoredo栈的实现,相比较state形式会复杂一些,业务方需要明确action存在undo和redo两种行为。
|
|
25
|
-
|
|
26
|
-
### ActionStackElement
|
|
27
|
-
action形式栈中的元素,需要使用方满足接口。
|
|
28
|
-
需要提供`undo`方法和`redo`方法即可。
|
|
29
|
-
下面是一个简单的`IActionStackElement`实现举例:
|
|
30
|
-
```
|
|
31
|
-
class CommandElement<T> implement IUndoRedoElement {
|
|
32
|
-
private _currentCommand: T;
|
|
33
|
-
private _invertCommand: T;
|
|
34
|
-
|
|
35
|
-
undo() {
|
|
36
|
-
this._invertCommand.execute();
|
|
37
|
-
this._reverse();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
redo() {
|
|
41
|
-
this._invertCommand.execute();
|
|
42
|
-
this._reverse();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
private _reverse() {
|
|
46
|
-
swap(this._currentCommand, this._invertCommand);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
特点:
|
|
52
|
-
- 禁止继承时重写`undo`,`redo`,`pushElement`这三个方法,因为重写的话会影响生命周期行为。
|
|
53
|
-
- 提供默认的容量能力控制,可以重写`isOverflow`方法来业务精细化判断容量大小。
|
|
54
|
-
- **可以复写`popPastElements`和`popFutureElements`来实现定制化的出栈逻辑。**
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
## 其他
|
|
58
|
-
- 如果需要其他生命周期,请联系基建侧同学。
|
|
59
|
-
- ActionUndoRedoStack中每一次undo、redo执行的粒度是数组,是因为业务场景中可能有联动出栈的情况。
|
|
60
|
-
- **如果本身是state场景,但是有一些复杂的逻辑,可以使用Action形式栈模拟。**
|
|
61
|
-
- Past、Future命名参考于vscode和redux。
|