@zk-tech/bedrock 0.0.1 → 0.0.2

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 (203) hide show
  1. package/package.json +1 -2
  2. package/src/_internal/logger.ts +0 -59
  3. package/src/array/array.test.ts +0 -35
  4. package/src/array/array.ts +0 -25
  5. package/src/array/index.ts +0 -1
  6. package/src/assert/assert.test.ts +0 -86
  7. package/src/assert/assert.ts +0 -42
  8. package/src/assert/index.ts +0 -2
  9. package/src/async/barrier.test.ts +0 -90
  10. package/src/async/barrier.ts +0 -58
  11. package/src/async/cancellation.test.ts +0 -85
  12. package/src/async/cancellation.ts +0 -193
  13. package/src/async/index.ts +0 -18
  14. package/src/async/queue/queue.test.ts +0 -70
  15. package/src/async/queue/queue.ts +0 -56
  16. package/src/async/queue/task.test.ts +0 -155
  17. package/src/async/queue/task.ts +0 -67
  18. package/src/async/utils.test.ts +0 -28
  19. package/src/async/utils.ts +0 -8
  20. package/src/async/wait.ts +0 -9
  21. package/src/byte/format.test.ts +0 -64
  22. package/src/byte/format.ts +0 -44
  23. package/src/byte/index.ts +0 -2
  24. package/src/byte/node_modules/.vitest/results.json +0 -1
  25. package/src/byte/var.ts +0 -11
  26. package/src/cache/index.ts +0 -2
  27. package/src/cache/lru-with-timeout.test.ts +0 -88
  28. package/src/cache/lru-with-timeout.ts +0 -85
  29. package/src/cache/lru.test.ts +0 -56
  30. package/src/cache/lru.ts +0 -59
  31. package/src/context/context.test.ts +0 -17
  32. package/src/context/context.ts +0 -60
  33. package/src/context/index.ts +0 -8
  34. package/src/di/base.ts +0 -73
  35. package/src/di/container-service.test.ts +0 -179
  36. package/src/di/context.web.tsx +0 -41
  37. package/src/di/descriptor.ts +0 -31
  38. package/src/di/idle-value.test.ts +0 -73
  39. package/src/di/idle-value.ts +0 -63
  40. package/src/di/index.common.ts +0 -32
  41. package/src/di/index.ts +0 -2
  42. package/src/di/instantiation-service.interface.ts +0 -46
  43. package/src/di/instantiation-service.test.ts +0 -337
  44. package/src/di/instantiation-service.ts +0 -468
  45. package/src/di/lazy/foo.mock.ts +0 -28
  46. package/src/di/lazy/idle-load.ts +0 -39
  47. package/src/di/lazy/index.ts +0 -4
  48. package/src/di/lazy/lazy-service.test.ts +0 -65
  49. package/src/di/lazy/lazy-service.ts +0 -71
  50. package/src/di/lazy/type.ts +0 -5
  51. package/src/di/node_modules/.vitest/results.json +0 -1
  52. package/src/di/proxy-builder.test.ts +0 -45
  53. package/src/di/proxy-builder.ts +0 -38
  54. package/src/di/service-collection.test.ts +0 -27
  55. package/src/di/service-collection.ts +0 -46
  56. package/src/di/service-ownership-collection.test.ts +0 -39
  57. package/src/di/service-ownership-collection.ts +0 -38
  58. package/src/di/service-registry.test.ts +0 -66
  59. package/src/di/service-registry.ts +0 -99
  60. package/src/di/trace.ts +0 -85
  61. package/src/dispose/disposable-store.test.ts +0 -57
  62. package/src/dispose/disposable-store.ts +0 -80
  63. package/src/dispose/disposable-t.test.ts +0 -123
  64. package/src/dispose/disposable-t.ts +0 -238
  65. package/src/dispose/disposable-utils.test.ts +0 -15
  66. package/src/dispose/disposable-utils.ts +0 -28
  67. package/src/dispose/dispose-base.ts +0 -9
  68. package/src/dispose/index.ts +0 -34
  69. package/src/dispose/logger.test.ts +0 -65
  70. package/src/dispose/logger.ts +0 -39
  71. package/src/dispose/timer.test.ts +0 -30
  72. package/src/dispose/timer.ts +0 -16
  73. package/src/dispose/tracker.test.ts +0 -51
  74. package/src/dispose/tracker.ts +0 -105
  75. package/src/error/error-base.ts +0 -45
  76. package/src/error/error-code.ts +0 -39
  77. package/src/error/error-const.test.ts +0 -30
  78. package/src/error/error-const.ts +0 -16
  79. package/src/error/error-or.test.ts +0 -44
  80. package/src/error/error-or.ts +0 -2
  81. package/src/error/error-t.test.ts +0 -116
  82. package/src/error/error-t.ts +0 -100
  83. package/src/error/index.ts +0 -24
  84. package/src/error/node_modules/.vitest/results.json +0 -1
  85. package/src/event/disposable-linked-list.ts +0 -29
  86. package/src/event/emitter.test.ts +0 -191
  87. package/src/event/emitter.ts +0 -162
  88. package/src/event/error-handler.ts +0 -22
  89. package/src/event/index.ts +0 -34
  90. package/src/event/once.ts +0 -29
  91. package/src/event/phase-emitter.test.ts +0 -212
  92. package/src/event/phase-emitter.ts +0 -209
  93. package/src/event/shortcut-event-utils.ts +0 -33
  94. package/src/event/utils.ts +0 -6
  95. package/src/event/when.ts +0 -40
  96. package/src/function/debounce.test.ts +0 -274
  97. package/src/function/debounce.ts +0 -168
  98. package/src/function/index.ts +0 -2
  99. package/src/function/node_modules/.vitest/results.json +0 -1
  100. package/src/function/throttle.test.ts +0 -179
  101. package/src/function/throttle.ts +0 -26
  102. package/src/hash/hash-t.test.ts +0 -100
  103. package/src/hash/hash-t.ts +0 -51
  104. package/src/hash/index.ts +0 -3
  105. package/src/json/index.ts +0 -1
  106. package/src/json/node_modules/.vitest/results.json +0 -1
  107. package/src/json/parse.ts +0 -19
  108. package/src/launch/abstract-job.ts +0 -45
  109. package/src/launch/cost-recorder.ts +0 -22
  110. package/src/launch/index.ts +0 -2
  111. package/src/launch/job-scheduler.test.ts +0 -122
  112. package/src/launch/job-scheduler.ts +0 -118
  113. package/src/launch/node_modules/.vitest/deps/_metadata.json +0 -8
  114. package/src/launch/node_modules/.vitest/deps/package.json +0 -3
  115. package/src/launch/node_modules/.vitest/results.json +0 -1
  116. package/src/lock/README.md +0 -11
  117. package/src/lock/capability.test.ts +0 -110
  118. package/src/lock/capability.ts +0 -89
  119. package/src/lock/index.ts +0 -15
  120. package/src/lock/node_modules/.vitest/results.json +0 -1
  121. package/src/lock/semaphore.ts +0 -21
  122. package/src/lock/shared-mutex.test.ts +0 -537
  123. package/src/lock/shared-mutex.ts +0 -242
  124. package/src/lock/utils.test.ts +0 -165
  125. package/src/lock/utils.ts +0 -135
  126. package/src/lodash-es/index.ts +0 -1
  127. package/src/math/degree.ts +0 -16
  128. package/src/math/index.ts +0 -7
  129. package/src/math/math.test.ts +0 -40
  130. package/src/math/math.ts +0 -64
  131. package/src/math/node_modules/.vitest/results.json +0 -1
  132. package/src/math/vector.test.ts +0 -73
  133. package/src/math/vector.ts +0 -114
  134. package/src/network/client.interface.ts +0 -104
  135. package/src/network/client.web.ts +0 -24
  136. package/src/network/index.common.ts +0 -10
  137. package/src/network/index.ts +0 -2
  138. package/src/network/plugins/retry.ts +0 -98
  139. package/src/objects/deep-clone.test.ts +0 -40
  140. package/src/objects/deep-clone.ts +0 -13
  141. package/src/objects/deep-equal.test.ts +0 -86
  142. package/src/objects/deep-equal.ts +0 -60
  143. package/src/objects/index.ts +0 -4
  144. package/src/platform/index.ts +0 -64
  145. package/src/promise/index.ts +0 -16
  146. package/src/promise/promise.test.ts +0 -254
  147. package/src/promise/promise.ts +0 -212
  148. package/src/scheduler/callback-token.ts +0 -31
  149. package/src/scheduler/core/actuator-args.test.ts +0 -47
  150. package/src/scheduler/core/actuator.test.ts +0 -82
  151. package/src/scheduler/core/actuator.ts +0 -58
  152. package/src/scheduler/core/chunk-scheduler.test.ts +0 -54
  153. package/src/scheduler/core/chunk-scheduler.ts +0 -28
  154. package/src/scheduler/core/node_modules/.vitest/results.json +0 -1
  155. package/src/scheduler/core/scheduler.test.ts +0 -328
  156. package/src/scheduler/core/scheduler.ts +0 -172
  157. package/src/scheduler/core/task-queue.test.ts +0 -78
  158. package/src/scheduler/core/task-queue.ts +0 -44
  159. package/src/scheduler/core/task.test.ts +0 -34
  160. package/src/scheduler/core/task.ts +0 -52
  161. package/src/scheduler/core/utils.ts +0 -48
  162. package/src/scheduler/executor/abstract-executor.test.ts +0 -44
  163. package/src/scheduler/executor/abstract-executor.ts +0 -38
  164. package/src/scheduler/executor/executor.interface.ts +0 -39
  165. package/src/scheduler/executor/idle-callback-executor.test.ts +0 -70
  166. package/src/scheduler/executor/idle-callback-executor.ts +0 -98
  167. package/src/scheduler/executor/make-executor.ts +0 -18
  168. package/src/scheduler/executor/post-message-executor.test.ts +0 -66
  169. package/src/scheduler/executor/post-message-executor.ts +0 -52
  170. package/src/scheduler/index.ts +0 -15
  171. package/src/scheduler/lv-scheduler-callback.ts +0 -19
  172. package/src/scheduler/lv-scheduler-config.ts +0 -17
  173. package/src/scheduler/type.ts +0 -48
  174. package/src/sprintf/index.ts +0 -2
  175. package/src/sprintf/sprintf.test.ts +0 -95
  176. package/src/sprintf/sprintf.ts +0 -97
  177. package/src/structure/graph.test.ts +0 -181
  178. package/src/structure/graph.ts +0 -105
  179. package/src/structure/index.ts +0 -8
  180. package/src/structure/linked-list.test.ts +0 -74
  181. package/src/structure/linked-list.ts +0 -145
  182. package/src/structure/min-heap.test.ts +0 -71
  183. package/src/structure/min-heap.ts +0 -91
  184. package/src/type/REAME.md +0 -2
  185. package/src/type/distributive-omit.interface.ts +0 -4
  186. package/src/type/index.ts +0 -3
  187. package/src/type/object-key-paths.interface.ts +0 -40
  188. package/src/undo-redo-stack/README.md +0 -61
  189. package/src/undo-redo-stack/action-stack.test.ts +0 -330
  190. package/src/undo-redo-stack/action-stack.ts +0 -150
  191. package/src/undo-redo-stack/element.ts +0 -4
  192. package/src/undo-redo-stack/index.ts +0 -7
  193. package/src/undo-redo-stack/state-stack.test.ts +0 -118
  194. package/src/undo-redo-stack/state-stack.ts +0 -133
  195. package/src/uuid/index.ts +0 -7
  196. package/src/uuid/uuid.ts +0 -86
  197. package/src/worker/cors-worker.ts +0 -38
  198. package/src/worker/index.ts +0 -4
  199. package/src/worker/node_modules/.vitest/results.json +0 -1
  200. package/src/worker/promise-worker-main-thread.test.ts +0 -91
  201. package/src/worker/promise-worker-main-thread.ts +0 -76
  202. package/src/worker/promise-worker-worker-thread.ts +0 -64
  203. package/src/worker/promise-worker.interface.ts +0 -15
@@ -1,122 +0,0 @@
1
- import { JobScheduler } from './job-scheduler';
2
- import { AbstractJob } from './abstract-job';
3
- import { InstantiationService } from '@/di';
4
-
5
- enum Lifecycle {
6
- A,
7
- B,
8
- C,
9
- }
10
-
11
- class Job1 extends AbstractJob<Lifecycle> {
12
- protected _name = 'job1';
13
-
14
- constructor(private readonly _fn: () => void) {
15
- super();
16
- }
17
-
18
- protected _executePhase(phase: Lifecycle) {
19
- switch (phase) {
20
- case Lifecycle.A:
21
- case Lifecycle.B:
22
- this._fn();
23
- break;
24
- default:
25
- return;
26
- }
27
- }
28
- }
29
-
30
- class Job2 extends AbstractJob<Lifecycle> {
31
- protected _name = 'job2';
32
-
33
- constructor(private readonly _fn: () => void) {
34
- super();
35
- }
36
-
37
- protected _executePhase(phase: Lifecycle) {
38
- switch (phase) {
39
- case Lifecycle.C:
40
- this._fn();
41
- break;
42
- default:
43
- return;
44
- }
45
- }
46
- }
47
-
48
- class Job3 extends AbstractJob<Lifecycle> {
49
- protected _name = 'job3';
50
-
51
- constructor(private readonly _fn: () => void) {
52
- super();
53
- this._fn();
54
- }
55
-
56
- protected _executePhase(phase: Lifecycle) {
57
- // donothing
58
- }
59
- }
60
-
61
- describe('jobscheduler', () => {
62
- it('addJob', async () => {
63
- const fn1 = vi.fn();
64
- const fn2 = vi.fn();
65
- const instantiationService = new InstantiationService();
66
- const jobScheduler = instantiationService.createInstance(JobScheduler, Lifecycle.A);
67
- jobScheduler.addJob(new Job1(fn1));
68
- jobScheduler.addJob(new Job2(fn2));
69
-
70
- jobScheduler.prepare(Lifecycle.A);
71
- await jobScheduler.wait(Lifecycle.A);
72
-
73
- jobScheduler.prepare(Lifecycle.B);
74
- await jobScheduler.wait(Lifecycle.B);
75
-
76
- jobScheduler.prepare(Lifecycle.C);
77
- await jobScheduler.wait(Lifecycle.C);
78
-
79
- expect(fn1).toBeCalledTimes(2);
80
- expect(fn2).toBeCalledTimes(1);
81
- });
82
-
83
- it('registerJob', async () => {
84
- const fn1 = vi.fn();
85
- const fn2 = vi.fn();
86
- const instantiationService = new InstantiationService();
87
- const jobScheduler = instantiationService.createInstance(JobScheduler, Lifecycle.A);
88
- jobScheduler.registerJob(Lifecycle.A, Job1, fn1);
89
- jobScheduler.registerJob(Lifecycle.B, Job2, fn2);
90
-
91
- jobScheduler.prepare(Lifecycle.A);
92
- await jobScheduler.wait(Lifecycle.A);
93
-
94
- jobScheduler.prepare(Lifecycle.B);
95
- await jobScheduler.wait(Lifecycle.B);
96
-
97
- jobScheduler.prepare(Lifecycle.C);
98
- await jobScheduler.wait(Lifecycle.C);
99
-
100
- expect(fn1).toBeCalledTimes(2);
101
- expect(fn2).toBeCalledTimes(1);
102
- });
103
-
104
- it('delayConstructJob', async () => {
105
- const fn = vi.fn();
106
- const instantiationService = new InstantiationService();
107
- const jobScheduler = instantiationService.createInstance(JobScheduler, Lifecycle.A);
108
- jobScheduler.registerJob(Lifecycle.C, Job3, fn);
109
-
110
- jobScheduler.prepare(Lifecycle.A);
111
- await jobScheduler.wait(Lifecycle.A);
112
- expect(fn).not.toBeCalled();
113
-
114
- jobScheduler.prepare(Lifecycle.B);
115
- await jobScheduler.wait(Lifecycle.B);
116
- expect(fn).not.toBeCalled();
117
-
118
- jobScheduler.prepare(Lifecycle.C);
119
- await jobScheduler.wait(Lifecycle.C);
120
- expect(fn).toBeCalled();
121
- });
122
- });
@@ -1,118 +0,0 @@
1
- import { IInstantiationService, type GetLeadingNonServiceArgs } from '@/di';
2
- import { lvAssert, lvAssertNotHere } from '@/assert';
3
- import type { AbstractJob } from './abstract-job';
4
- import { CostRecorder } from './cost-recorder';
5
-
6
- class JobDescriptor<T, K extends T> {
7
- readonly ctor: new (...args: any[]) => AbstractJob<T, K>;
8
- readonly staticArguments: any[];
9
-
10
- constructor(ctor: new (...args: any[]) => AbstractJob<T, K>, args: any[]) {
11
- this.ctor = ctor;
12
- this.staticArguments = args;
13
- }
14
- }
15
-
16
- export class JobScheduler<T, K extends T = T> {
17
- private readonly _jobPools: Map<string, AbstractJob<T, K>> = new Map();
18
- private readonly _costRecorder = new CostRecorder();
19
- private readonly _unconstructedJobs: Map<K, JobDescriptor<AbstractJob<T, K>, any>[]> = new Map();
20
-
21
- constructor(
22
- private _currentPhase: K,
23
- @IInstantiationService private readonly _instantiationService: IInstantiationService,
24
- ) {}
25
-
26
- get currentPhase() {
27
- return this._currentPhase;
28
- }
29
-
30
- /**
31
- * 按需添加一个job,job会在phase时刻才进行实例化
32
- * @param phase job实例化时机
33
- * @param ctor job构造函数
34
- * @param args job构造静态参数
35
- */
36
- registerJob<Ctor extends new (...args: any[]) => AbstractJob<T, K>>(
37
- phase: K,
38
- ctor: Ctor,
39
- ...args: GetLeadingNonServiceArgs<ConstructorParameters<Ctor>>
40
- ) {
41
- if (!this._unconstructedJobs.has(phase)) {
42
- this._unconstructedJobs.set(phase, [new JobDescriptor(ctor, args)]);
43
- } else {
44
- this._unconstructedJobs.get(phase)!.push(new JobDescriptor(ctor, args));
45
- }
46
- }
47
-
48
- /**
49
- * 添加一个构造好的job
50
- * @param job 任务
51
- */
52
- addJob(job: AbstractJob<T, K>) {
53
- lvAssert(!this._jobPools.has(job.name), 'cant duplicate add job.');
54
- this._jobPools.set(job.name, job);
55
- }
56
-
57
- removeJob(jobName: string) {
58
- this._jobPools.delete(jobName);
59
- }
60
-
61
- prepare(phase: K) {
62
- const descriptors = this._unconstructedJobs.get(phase) ?? [];
63
- for (const d of descriptors) {
64
- this.addJob(this._instantiationService.createInstance(d.ctor, ...d.staticArguments));
65
- }
66
- let shouldWait = false;
67
- this._unconstructedJobs.delete(phase);
68
- for (const [, job] of this._jobPools) {
69
- const start = Date.now();
70
- job.prepare(phase);
71
- this._costRecorder.record(job.name, phase as number | string, Date.now() - start);
72
-
73
- if (job.shouldWait(phase)) {
74
- shouldWait = true;
75
- }
76
- }
77
-
78
- return shouldWait;
79
- }
80
-
81
- getCost() {
82
- return this._costRecorder.toString();
83
- }
84
-
85
- /**
86
- * 推进到目标阶段,要求目标阶段没有需要等待的任务
87
- * @param phase 目标阶段
88
- */
89
- advanceToPhase(phase: K) {
90
- for (const [, job] of this._jobPools) {
91
- if (job.shouldWait(phase)) {
92
- lvAssertNotHere(`exists job should wait, job name: ${job.name}, phase: ${phase}`);
93
- }
94
- }
95
-
96
- this._currentPhase = phase;
97
- }
98
-
99
- async wait(phase: K) {
100
- const jobPromises: Promise<void>[] = [];
101
- for (const [, job] of this._jobPools) {
102
- if (!job.shouldWait(phase)) {
103
- continue;
104
- }
105
- jobPromises.push(
106
- (() => {
107
- const start = Date.now();
108
- return job.wait(phase).then(() => {
109
- this._costRecorder.record(job.name, phase as number | string, Date.now() - start);
110
- });
111
- })(),
112
- );
113
- }
114
-
115
- await Promise.all(jobPromises);
116
- this._currentPhase = phase;
117
- }
118
- }
@@ -1,8 +0,0 @@
1
- {
2
- "hash": "ec1fa8f3",
3
- "configHash": "a2dd8394",
4
- "lockfileHash": "d75a6bc5",
5
- "browserHash": "cb29fc16",
6
- "optimized": {},
7
- "chunks": {}
8
- }
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }
@@ -1 +0,0 @@
1
- {"version":"1.0.1","results":[[":job-scheduler.test.ts",{"duration":2,"failed":false}]]}
@@ -1,11 +0,0 @@
1
- # 读写锁
2
- TODO(niurouwan): 补上
3
-
4
- ## 核心
5
- - 写写互斥、读写互斥,读读可重入
6
- - 写者优先于读者
7
- - 写者唤醒顺序按调用顺序来
8
-
9
- ## 实现原理
10
-
11
- ## 使用举例
@@ -1,110 +0,0 @@
1
- import { CapabilityStatus, Capability, SharedCapability } from './capability';
2
-
3
- describe('Capability', () => {
4
- // 初始化
5
- it('init', () => {
6
- const capability = new Capability();
7
- // 初始化完成,默认是没有锁状态
8
- expect(capability.status).toBe(CapabilityStatus.Unlocked);
9
- });
10
-
11
- // 获取控制权成功
12
- it('acquire success', () => {
13
- const capability = new Capability();
14
- expect(capability.status).toBe(CapabilityStatus.Unlocked);
15
- capability.acquire();
16
- expect(capability.status).toBe(CapabilityStatus.Locked);
17
- });
18
-
19
- // 获取控制权失败
20
- it('acquire fail', () => {
21
- const capability = new Capability();
22
- capability.acquire();
23
- expect(() => {
24
- capability.acquire();
25
- }).toThrowError();
26
- });
27
-
28
- // 释放控制权成功
29
- it('release success', () => {
30
- let locked = false;
31
- const capability = new Capability();
32
- capability.onUnlocked(() => {
33
- locked = false;
34
- });
35
- expect(capability.status).toBe(CapabilityStatus.Unlocked);
36
- capability.acquire();
37
- locked = true;
38
- expect(capability.status).toBe(CapabilityStatus.Locked);
39
- expect(locked).toBeTruthy();
40
-
41
- capability.release();
42
- expect(capability.status).toBe(CapabilityStatus.Unlocked);
43
- expect(locked).toBeFalsy();
44
- });
45
-
46
- // 直接释放控制权失败
47
- it('release fail1', () => {
48
- const capability = new Capability();
49
- expect(() => {
50
- capability.release();
51
- }).toThrowError();
52
- });
53
-
54
- // 重复释放控制权失败
55
- it('release fail2', () => {
56
- const capability = new Capability();
57
- capability.acquire();
58
- capability.release();
59
- expect(() => {
60
- capability.release();
61
- }).toThrowError();
62
- });
63
- });
64
-
65
- describe('SharedCapability', () => {
66
- // 初始化
67
- it('init', () => {
68
- const capability = new SharedCapability();
69
- // 初始化完成,默认是没有锁状态
70
- expect(capability.status).toBe(CapabilityStatus.Unlocked);
71
- });
72
-
73
- // 获取控制权成功
74
- it('acquire success', () => {
75
- const capability = new SharedCapability();
76
- capability.acquire();
77
- expect(capability.status).toBe(CapabilityStatus.Locked);
78
- expect(() => {
79
- capability.acquire();
80
- }).not.toThrowError();
81
- });
82
-
83
- // 释放控制权成功
84
- it('release success', () => {
85
- const capability = new SharedCapability();
86
- capability.acquire();
87
- expect(capability.status).toBe(CapabilityStatus.Locked);
88
- capability.acquire();
89
- expect(() => {
90
- capability.release();
91
- capability.release();
92
- }).not.toThrowError();
93
- expect(capability.status).toBe(CapabilityStatus.Unlocked);
94
- });
95
-
96
- // 释放控制权失败
97
- it('release fail', () => {
98
- const capability = new SharedCapability();
99
- capability.acquire();
100
- expect(capability.status).toBe(CapabilityStatus.Locked);
101
- capability.acquire();
102
- expect(() => {
103
- capability.release();
104
- capability.release();
105
- }).not.toThrowError();
106
- expect(() => {
107
- capability.release();
108
- }).toThrowError();
109
- });
110
- });
@@ -1,89 +0,0 @@
1
- import { lvAssert } from '@/assert';
2
- import { Emitter, type Event } from '@/event';
3
-
4
- //
5
- // 资源对应的是标准库中的 unsigned state 以及相关的位运算
6
- // 我们用两个具体的Capability结构实现(Capability命名来源于标准库)
7
- //
8
-
9
- /**
10
- * 资源状态
11
- */
12
- export enum CapabilityStatus {
13
- Unlocked,
14
- Locked,
15
- }
16
-
17
- /**
18
- * 独享的资源
19
- *
20
- * acquire 获取控制权
21
- * release 释放控制权
22
- */
23
- export class Capability {
24
- public onUnlocked: Event<[]>;
25
-
26
- private readonly _onUnlocked = new Emitter<[]>();
27
- private _status = CapabilityStatus.Unlocked;
28
-
29
- constructor() {
30
- this.onUnlocked = this._onUnlocked.event;
31
- }
32
-
33
- get status(): CapabilityStatus {
34
- return this._status;
35
- }
36
-
37
- public acquire(): void {
38
- lvAssert(this._status === CapabilityStatus.Unlocked);
39
- this._status = CapabilityStatus.Locked;
40
- }
41
-
42
- public release(): void {
43
- lvAssert(this._status === CapabilityStatus.Locked);
44
- this._status = CapabilityStatus.Unlocked;
45
- this._onUnlocked.fire();
46
- }
47
- }
48
-
49
- /**
50
- * 共享的资源
51
- *
52
- * acquire 获取控制权
53
- * release 释放控制权
54
- */
55
- export class SharedCapability {
56
- public onUnlocked: Event<[]>;
57
-
58
- private readonly _onUnlocked = new Emitter<[]>();
59
- private _status = CapabilityStatus.Unlocked;
60
- private _counter = 0;
61
-
62
- constructor() {
63
- this.onUnlocked = this._onUnlocked.event;
64
- }
65
-
66
- get status(): CapabilityStatus {
67
- return this._status;
68
- }
69
-
70
- get counter(): number {
71
- return this._counter;
72
- }
73
-
74
- public acquire() {
75
- if (this._status === CapabilityStatus.Unlocked) {
76
- this._status = CapabilityStatus.Locked;
77
- }
78
- this._counter++;
79
- }
80
-
81
- public release() {
82
- lvAssert(this._counter > 0);
83
- this._counter--;
84
- if (this._counter === 0) {
85
- this._status = CapabilityStatus.Unlocked;
86
- this._onUnlocked.fire();
87
- }
88
- }
89
- }
package/src/lock/index.ts DELETED
@@ -1,15 +0,0 @@
1
- // 读写能力的互斥量,实现读写锁的能力
2
- export { SharedMutex } from './shared-mutex';
3
-
4
- // 可解锁的句柄
5
- export type { IUnlockable } from './utils';
6
-
7
- // 转移独享锁控制权
8
- export { transferLock } from './utils';
9
- // 尝试转移独享锁控制权
10
- export { tryTransferLock } from './utils';
11
-
12
- // 转移共享锁控制权
13
- export { transferSharedLock } from './utils';
14
- // 尝试转移共享锁控制权
15
- export { tryTransferSharedLock } from './utils';
@@ -1 +0,0 @@
1
- {"version":"1.0.1","results":[]}
@@ -1,21 +0,0 @@
1
- import type { Event } from '@/event';
2
- import { Emitter } from '@/event';
3
-
4
- /**
5
- * 信号
6
- *
7
- * 用来模拟标准库的condition_variable
8
- * 提供监听某个信号被激活的能力
9
- */
10
- export class Semaphore {
11
- public onActive: Event<[]>;
12
- private readonly _onActive = new Emitter<[]>();
13
-
14
- constructor() {
15
- this.onActive = this._onActive.event;
16
- }
17
-
18
- public notify(): void {
19
- this._onActive.fire();
20
- }
21
- }