@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
package/src/dispose/tracker.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { Logger } from '@/_internal/logger';
|
|
2
|
-
import type { IDisposable } from './dispose-base';
|
|
3
|
-
import { EmptyDispose } from './dispose-base';
|
|
4
|
-
|
|
5
|
-
export interface IDisposableTracker {
|
|
6
|
-
trackDisposable: (disposable: IDisposable) => void;
|
|
7
|
-
|
|
8
|
-
setParent: (child: IDisposable, parent: IDisposable | null) => void;
|
|
9
|
-
|
|
10
|
-
markAsDisposed: (disposable: IDisposable) => void;
|
|
11
|
-
|
|
12
|
-
markAsLeaked: (disposable: IDisposable) => void;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
let disposableTracker: IDisposableTracker | null = null;
|
|
16
|
-
|
|
17
|
-
function makeDefaultTracker() {
|
|
18
|
-
// reactHooks useEffect中出现disposable,我们默认为不会泄漏
|
|
19
|
-
// 默认使用者一定会在useEffect的返回值中进行dispose
|
|
20
|
-
const ignorePattern = /commitHookEffectList/i;
|
|
21
|
-
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
23
|
-
const __is_disposable_tracked__ = '__is_disposable_tracked__';
|
|
24
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
25
|
-
const __is_leak_marked__ = '__is_leak_marked__';
|
|
26
|
-
return new (class implements IDisposableTracker {
|
|
27
|
-
trackDisposable(x: IDisposable): void {
|
|
28
|
-
const stack = new Error('Potentially leaked disposable').stack!;
|
|
29
|
-
setTimeout(() => {
|
|
30
|
-
if (stack.match(ignorePattern)) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
if ((x as any)[__is_leak_marked__]) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
if (!(x as any)[__is_disposable_tracked__]) {
|
|
37
|
-
Logger.log(stack);
|
|
38
|
-
}
|
|
39
|
-
}, 3000);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
setParent(child: IDisposable, parent: IDisposable | null): void {
|
|
43
|
-
if (child && child !== EmptyDispose) {
|
|
44
|
-
try {
|
|
45
|
-
(child as any)[__is_disposable_tracked__] = true;
|
|
46
|
-
} catch {
|
|
47
|
-
// noop
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
markAsDisposed(disposable: IDisposable): void {
|
|
53
|
-
if (disposable && disposable !== EmptyDispose) {
|
|
54
|
-
try {
|
|
55
|
-
(disposable as any)[__is_disposable_tracked__] = true;
|
|
56
|
-
} catch {
|
|
57
|
-
// noop
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
62
|
-
markAsLeaked(disposable: IDisposable): void {
|
|
63
|
-
if (disposable && disposable !== EmptyDispose) {
|
|
64
|
-
try {
|
|
65
|
-
(disposable as any)[__is_leak_marked__] = true;
|
|
66
|
-
} catch {
|
|
67
|
-
// noop
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
})();
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function setDisposableTracker(tracker: IDisposableTracker | null): void {
|
|
75
|
-
disposableTracker = tracker;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function enableTrack(tracker: IDisposableTracker = makeDefaultTracker()) {
|
|
79
|
-
setDisposableTracker(tracker);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export function disableTrack() {
|
|
83
|
-
setDisposableTracker(null);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// 辅助能力 跟踪disposable
|
|
87
|
-
export function TRACK_DISPOSABLE<T extends IDisposable>(x: T): T {
|
|
88
|
-
disposableTracker?.trackDisposable(x);
|
|
89
|
-
return x;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// 辅助能力 标记disposable成功
|
|
93
|
-
export function MARK_AS_DISPOSED<T extends IDisposable>(x: T): void {
|
|
94
|
-
disposableTracker?.markAsDisposed(x);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// 辅助能力 标记disposable关系
|
|
98
|
-
export function SET_PARENT_OF_DISPOSABLE(child: IDisposable, parent: IDisposable | null): void {
|
|
99
|
-
disposableTracker?.setParent(child, parent);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// 辅助能力 主动标记泄漏
|
|
103
|
-
export function MARK_AS_LEAKED<T extends IDisposable>(x: T) {
|
|
104
|
-
disposableTracker?.markAsLeaked(x);
|
|
105
|
-
}
|
package/src/error/error-base.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// lv项目中使用的错误类型
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
export type ILvErrorRef<K = unknown> = ILvErrorOr<never, K>;
|
|
6
|
-
|
|
7
|
-
export interface ILvRealErrorRef<K = unknown> {
|
|
8
|
-
readonly ok: false;
|
|
9
|
-
readonly code: number;
|
|
10
|
-
readonly msg: string;
|
|
11
|
-
readonly cause?: ILvErrorRef | Error;
|
|
12
|
-
readonly toString: () => string;
|
|
13
|
-
readonly pair: () => [ILvRealErrorRef<K>, null];
|
|
14
|
-
readonly errorInfo?: K;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @deprecated 使用findJsError获取到jsError后再拿stack
|
|
18
|
-
*/
|
|
19
|
-
readonly stack?: string;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 尽可能找到lvError对应的js错误
|
|
23
|
-
*/
|
|
24
|
-
readonly findJsError: () => Error;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
//
|
|
28
|
-
// 没有错误时
|
|
29
|
-
//
|
|
30
|
-
type ILvValueRef<T> = {
|
|
31
|
-
readonly ok: true;
|
|
32
|
-
readonly code: 0;
|
|
33
|
-
readonly msg: '';
|
|
34
|
-
readonly cause?: undefined;
|
|
35
|
-
readonly value: T;
|
|
36
|
-
readonly toString: () => string;
|
|
37
|
-
readonly pair: () => [null, T];
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
//
|
|
41
|
-
// lv项目中使用的携带值可能错误类型
|
|
42
|
-
//
|
|
43
|
-
export type ILvErrorOr<T, K = unknown> = (ILvRealErrorRef<K> | ILvValueRef<T>) & {
|
|
44
|
-
readonly pair: () => [null, T] | [ILvRealErrorRef<K>, null];
|
|
45
|
-
};
|
package/src/error/error-code.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { lvErrorConst } from './error-const';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 提供了通用的错误码(+1至+256)
|
|
5
|
-
*
|
|
6
|
-
* 注意:这里只是提供了通用的错误码,方便服务使用,但并不是要求服务一定使用如下的错误码来表明某种错误
|
|
7
|
-
*/
|
|
8
|
-
export enum GenericError {
|
|
9
|
-
Ok = 0,
|
|
10
|
-
Cancelled = 1, // 操作被取消
|
|
11
|
-
TimedOut = 2, // 操作超时
|
|
12
|
-
PermissionDenied = 3, // 无权限
|
|
13
|
-
AlreadyExists = 4, // 已经存在(文件/记录等)
|
|
14
|
-
NotSupported = 5, // 操作不支持
|
|
15
|
-
ResourceUnavailable = 6, // 资源不可用
|
|
16
|
-
OutOfRange = 7, // (参数/结果等)发生越界
|
|
17
|
-
InvalidArgument = 8, // 无效参数
|
|
18
|
-
NetworkFailed = 9, // 网络失败
|
|
19
|
-
Interrupted = 10, // 操作被中断(捕获异常转为错误)
|
|
20
|
-
ResultNil = 11, // 结果不存在(null or undefined转为错误)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 通用错误码所对应的编译时常量对象(ErrorConst)
|
|
25
|
-
*/
|
|
26
|
-
export const cancelledError = lvErrorConst(GenericError.Cancelled, 'operation(s) cancelled.');
|
|
27
|
-
export const timeoutError = lvErrorConst(GenericError.TimedOut, 'operation(s) timed out.');
|
|
28
|
-
export const permissionDeniedError = lvErrorConst(GenericError.PermissionDenied, 'permission denied.');
|
|
29
|
-
export const alreadyExistsError = lvErrorConst(GenericError.AlreadyExists, 'already exists.');
|
|
30
|
-
export const notSupportedError = lvErrorConst(GenericError.NotSupported, 'operation(s) not supported.');
|
|
31
|
-
export const resourceUnavailableError = lvErrorConst(
|
|
32
|
-
GenericError.ResourceUnavailable,
|
|
33
|
-
'resource is unavailable.',
|
|
34
|
-
);
|
|
35
|
-
export const outOfRangeError = lvErrorConst(GenericError.OutOfRange, 'out of range.');
|
|
36
|
-
export const invalidArgumentError = lvErrorConst(GenericError.InvalidArgument, 'invalid arguments.');
|
|
37
|
-
export const networkFailedError = lvErrorConst(GenericError.NetworkFailed, 'network failed.');
|
|
38
|
-
export const interruptedError = lvErrorConst(GenericError.Interrupted, 'interrupted.');
|
|
39
|
-
export const resultNilError = lvErrorConst(GenericError.ResultNil, 'result is nil.');
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { lvErrorConst } from './error-const';
|
|
2
|
-
|
|
3
|
-
const NETWORK_CODE = 404;
|
|
4
|
-
const NETWORK_MSG = 'network error';
|
|
5
|
-
|
|
6
|
-
const networkError = lvErrorConst(NETWORK_CODE, NETWORK_MSG);
|
|
7
|
-
|
|
8
|
-
describe('LvErrorConst', () => {
|
|
9
|
-
test('creates runtime error', () => {
|
|
10
|
-
const err = networkError();
|
|
11
|
-
expect(err.ok).toBe(false);
|
|
12
|
-
expect(err.code).toBe(NETWORK_CODE);
|
|
13
|
-
expect(err.msg).toBe(NETWORK_MSG);
|
|
14
|
-
if (!err.ok) {
|
|
15
|
-
expect(err.code).toBe(NETWORK_CODE);
|
|
16
|
-
expect(err.msg).toBe(NETWORK_MSG);
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test('creates runtime error with overridden message', () => {
|
|
21
|
-
const err = networkError('new error msg');
|
|
22
|
-
expect(err.ok).toBe(false);
|
|
23
|
-
expect(err.code).toBe(NETWORK_CODE);
|
|
24
|
-
expect(err.msg).toBe('new error msg');
|
|
25
|
-
if (!err.ok) {
|
|
26
|
-
expect(err.code).toBe(NETWORK_CODE);
|
|
27
|
-
expect(err.msg).toBe('new error msg');
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
});
|
package/src/error/error-const.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { makeError, makeErrorBy } from './error-t';
|
|
2
|
-
|
|
3
|
-
//
|
|
4
|
-
// Error编译时错误对象
|
|
5
|
-
//
|
|
6
|
-
export function lvErrorConst(code: number, msg: string) {
|
|
7
|
-
return (rewrite?: string | Error) => {
|
|
8
|
-
if (!rewrite) {
|
|
9
|
-
return makeError(code, msg);
|
|
10
|
-
}
|
|
11
|
-
if (typeof rewrite === 'string') {
|
|
12
|
-
return makeError(code, rewrite);
|
|
13
|
-
}
|
|
14
|
-
return makeErrorBy(code, rewrite.message, rewrite);
|
|
15
|
-
};
|
|
16
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import type { ILvErrorOr } from './error-base';
|
|
2
|
-
import { makeOkWith } from './error-or';
|
|
3
|
-
|
|
4
|
-
describe('LvErrorOr', () => {
|
|
5
|
-
test('creates error without error state', () => {
|
|
6
|
-
const result = makeOkWith<number>(3);
|
|
7
|
-
expect(result.ok).toBe(true);
|
|
8
|
-
const [err, value] = result.pair();
|
|
9
|
-
try {
|
|
10
|
-
// @ts-expect-error Should fail at compile time because error data is used without checking state
|
|
11
|
-
expect(err.ok).toBe(true);
|
|
12
|
-
} catch (error) {
|
|
13
|
-
// err is undefined, ignore
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// TODO: Should fail when using success data without checking state, but adding | null subtype is acceptable
|
|
17
|
-
expect(value).toBe(3);
|
|
18
|
-
if (!err) {
|
|
19
|
-
// State is checked as no error, should remove | null subtype here
|
|
20
|
-
value.toString();
|
|
21
|
-
expect(value).toBe(3);
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test('creates error without error state', () => {
|
|
26
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
27
|
-
const result = getData();
|
|
28
|
-
// @ts-expect-error Should fail at compile time, haven't checked if it's ok
|
|
29
|
-
const { value } = result;
|
|
30
|
-
void value;
|
|
31
|
-
if (result.ok) {
|
|
32
|
-
// State is checked as ok, can safely use value
|
|
33
|
-
result.value.toString();
|
|
34
|
-
}
|
|
35
|
-
if (!result.ok) {
|
|
36
|
-
// State is checked as not ok, can safely use error details
|
|
37
|
-
result.code.toString();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function getData(): ILvErrorOr<number> {
|
|
41
|
-
return makeOkWith(42);
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
});
|
package/src/error/error-or.ts
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import type { ILvErrorOr } from './error-base';
|
|
2
|
-
import { makeOkWith } from './error-or';
|
|
3
|
-
import { isLvErrorRef, makeError, makeErrorBy, makeOk } from './error-t';
|
|
4
|
-
|
|
5
|
-
const NETWORK_CODE = 404;
|
|
6
|
-
const NETWORK_MSG = 'network error';
|
|
7
|
-
|
|
8
|
-
describe('LvError', () => {
|
|
9
|
-
test('creates error without error state', () => {
|
|
10
|
-
const err = makeOk();
|
|
11
|
-
if (!err.ok) {
|
|
12
|
-
throw new Error('should be ok');
|
|
13
|
-
}
|
|
14
|
-
expect(err.ok).toBe(true);
|
|
15
|
-
void err.code;
|
|
16
|
-
// expect(err.code).toBe(0);
|
|
17
|
-
// For non-error state, toString content is undefined
|
|
18
|
-
expect(err.toString()).toBe('[object Object]');
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('error without error state supports destructuring', () => {
|
|
22
|
-
const result = makeOk();
|
|
23
|
-
const [err, value] = result.pair();
|
|
24
|
-
if (err) {
|
|
25
|
-
throw new Error('should be ok');
|
|
26
|
-
}
|
|
27
|
-
expect(err).toBe(null);
|
|
28
|
-
expect(value).toBe(null);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('creates error with error state', () => {
|
|
32
|
-
const result = makeError(NETWORK_CODE, NETWORK_MSG);
|
|
33
|
-
expect(result.ok).toBe(false);
|
|
34
|
-
|
|
35
|
-
// Supports destructuring
|
|
36
|
-
const [err, value] = result.pair();
|
|
37
|
-
if (!err) {
|
|
38
|
-
throw new Error('should be error');
|
|
39
|
-
}
|
|
40
|
-
expect(err.code).toBe(NETWORK_CODE);
|
|
41
|
-
expect(err.msg).toBe(NETWORK_MSG);
|
|
42
|
-
expect(value).toBe(null);
|
|
43
|
-
|
|
44
|
-
// Supports getting cause
|
|
45
|
-
expect(result.toString()).toBe('[404]network error.');
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
test('creates error with cause', () => {
|
|
49
|
-
const err0 = new Error('original error');
|
|
50
|
-
const err1 = makeErrorBy(NETWORK_CODE, NETWORK_MSG, err0);
|
|
51
|
-
if (err1.ok) {
|
|
52
|
-
throw new Error('should be error');
|
|
53
|
-
}
|
|
54
|
-
const err2 = makeErrorBy(NETWORK_CODE, NETWORK_MSG, err1);
|
|
55
|
-
expect(err2.ok).toBe(false);
|
|
56
|
-
expect(err2.toString()).toBe(
|
|
57
|
-
'[404]network error.\ncaused by [404]network error\ncaused by [jsError]Error-original error',
|
|
58
|
-
);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
test('type testing', () => {
|
|
62
|
-
const err0 = new Error('original error');
|
|
63
|
-
// eslint-disable-next-line no-unused-vars
|
|
64
|
-
const [err1, value1] = makeErrorBy(NETWORK_CODE, NETWORK_MSG, err0).pair();
|
|
65
|
-
try {
|
|
66
|
-
// @ts-expect-error Cannot use value1 without checking error state
|
|
67
|
-
value1.toString();
|
|
68
|
-
} catch {
|
|
69
|
-
// value1 is null, ignore this runtime error
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (err1) {
|
|
73
|
-
// Can directly convert destructured value to ILvErrorOr<string> for easy propagation
|
|
74
|
-
// eslint-disable-next-line no-unused-vars
|
|
75
|
-
const err2: ILvErrorOr<string> = err1;
|
|
76
|
-
}
|
|
77
|
-
const [err2, value2] = makeOkWith(1).pair();
|
|
78
|
-
if (!err2) {
|
|
79
|
-
// State is checked as OK, can directly use value
|
|
80
|
-
value2.toString();
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test('checks if any object is LvErrorRef', () => {
|
|
85
|
-
expect(isLvErrorRef(makeOk())).toBe(true);
|
|
86
|
-
expect(isLvErrorRef(makeOkWith(1))).toBe(true);
|
|
87
|
-
expect(isLvErrorRef(makeError(1, 'hello'))).toBe(true);
|
|
88
|
-
expect(isLvErrorRef(makeErrorBy(NETWORK_CODE, NETWORK_MSG, makeError(1, 'hello')))).toBe(true);
|
|
89
|
-
const falsyValues = [
|
|
90
|
-
undefined,
|
|
91
|
-
null,
|
|
92
|
-
true,
|
|
93
|
-
false,
|
|
94
|
-
{ ok: true, value: 1 },
|
|
95
|
-
{ ok: false },
|
|
96
|
-
'ok',
|
|
97
|
-
new Error(),
|
|
98
|
-
];
|
|
99
|
-
falsyValues.forEach((val) => {
|
|
100
|
-
expect(isLvErrorRef(val)).toBe(false);
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
test('finds jsError from LvErrorRef if possible', () => {
|
|
105
|
-
const jsError = new Error('jsError1');
|
|
106
|
-
const lvError1 = makeErrorBy(-1, 'hello', jsError);
|
|
107
|
-
expect(lvError1.findJsError()).toBe(jsError);
|
|
108
|
-
|
|
109
|
-
const lvError2 = makeErrorBy(-1, 'hello', lvError1);
|
|
110
|
-
expect(lvError2.findJsError()).toBe(jsError);
|
|
111
|
-
|
|
112
|
-
const lvError3 = makeError(-1, 'world');
|
|
113
|
-
const lvError4 = makeErrorBy(-1, 'hello', lvError3);
|
|
114
|
-
expect(lvError4.findJsError().message).toBe('[-1]hello.\ncaused by [-1]world');
|
|
115
|
-
});
|
|
116
|
-
});
|
package/src/error/error-t.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import type { ILvErrorRef, ILvErrorOr, ILvRealErrorRef } from './error-base';
|
|
2
|
-
|
|
3
|
-
//
|
|
4
|
-
// Error类
|
|
5
|
-
//
|
|
6
|
-
// 用户给函数签名明确错误
|
|
7
|
-
// function foo(): ILvErrorRef
|
|
8
|
-
// return makeOk(); // 如果没有错误
|
|
9
|
-
// return makeError(code, msg); // 指定错误码和携带消息
|
|
10
|
-
//
|
|
11
|
-
// const SomeError = LvErrorConst(code, msg); // 更推荐用LvErrorConst生成
|
|
12
|
-
// return SomeError();
|
|
13
|
-
//
|
|
14
|
-
|
|
15
|
-
const lvErrorRefSymbol = Symbol('lvErrorRef');
|
|
16
|
-
|
|
17
|
-
function findJsError(error: ILvErrorRef | Error) {
|
|
18
|
-
let obj: ILvErrorRef | Error | undefined = error;
|
|
19
|
-
while (obj) {
|
|
20
|
-
if (obj instanceof Error) {
|
|
21
|
-
return obj;
|
|
22
|
-
}
|
|
23
|
-
obj = (obj as ILvErrorRef).cause;
|
|
24
|
-
}
|
|
25
|
-
// 如果没有Error,那就序列化lvError
|
|
26
|
-
return new Error((error as ILvErrorRef).toString());
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function makeOk<K>(): ILvErrorOr<never, K> {
|
|
30
|
-
return {
|
|
31
|
-
ok: true,
|
|
32
|
-
value: null!,
|
|
33
|
-
pair() {
|
|
34
|
-
return [null, null!];
|
|
35
|
-
},
|
|
36
|
-
code: 0,
|
|
37
|
-
msg: '',
|
|
38
|
-
...{ [lvErrorRefSymbol]: true }, // 跳过类型检测
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function makeOkWith<T, K = unknown>(value: T): ILvErrorOr<T, K> {
|
|
43
|
-
return {
|
|
44
|
-
ok: true,
|
|
45
|
-
value,
|
|
46
|
-
pair() {
|
|
47
|
-
return [null, value];
|
|
48
|
-
},
|
|
49
|
-
code: 0,
|
|
50
|
-
msg: '',
|
|
51
|
-
...{ [lvErrorRefSymbol]: true }, // 跳过类型检测
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function printCause(cause: ILvErrorRef | Error | undefined): string {
|
|
56
|
-
if (cause === undefined) {
|
|
57
|
-
return '';
|
|
58
|
-
} else if (cause instanceof Error) {
|
|
59
|
-
return `\ncaused by [jsError]${cause.name}-${cause.message}`;
|
|
60
|
-
} else {
|
|
61
|
-
return `\ncaused by [${cause.code}]${cause.msg}${cause.ok ? '' : printCause(cause.cause)}`;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function internalMakeError<T>(code: number, msg: string, cause?: ILvErrorRef | Error, errorInfo?: T) {
|
|
66
|
-
const errorRef: ILvRealErrorRef<T> = {
|
|
67
|
-
ok: false,
|
|
68
|
-
code,
|
|
69
|
-
msg,
|
|
70
|
-
cause,
|
|
71
|
-
errorInfo,
|
|
72
|
-
toString() {
|
|
73
|
-
return `[${code}]${msg}.${cause ? printCause(cause) : ''}`;
|
|
74
|
-
},
|
|
75
|
-
pair() {
|
|
76
|
-
return [errorRef, null];
|
|
77
|
-
},
|
|
78
|
-
stack: cause instanceof Error ? cause.stack : undefined,
|
|
79
|
-
findJsError: () => findJsError(errorRef),
|
|
80
|
-
...{ [lvErrorRefSymbol]: true }, // 跳过类型检测
|
|
81
|
-
};
|
|
82
|
-
return errorRef;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function makeError<T = unknown>(code: number, msg: string, errorInfo?: T): ILvRealErrorRef<T> {
|
|
86
|
-
return internalMakeError(code, msg, undefined, errorInfo);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export function makeErrorBy<T = unknown>(
|
|
90
|
-
code: number,
|
|
91
|
-
msg: string,
|
|
92
|
-
cause: ILvErrorRef | Error,
|
|
93
|
-
errorInfo?: T,
|
|
94
|
-
): ILvRealErrorRef<T> {
|
|
95
|
-
return internalMakeError(code, msg, cause, errorInfo);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function isLvErrorRef(val: unknown): val is ILvErrorRef {
|
|
99
|
-
return typeof val === 'object' && val !== null && lvErrorRefSymbol in val;
|
|
100
|
-
}
|
package/src/error/index.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export type { ILvErrorRef } from './error-base';
|
|
2
|
-
export type { ILvRealErrorRef } from './error-base';
|
|
3
|
-
export type { ILvErrorOr } from './error-base';
|
|
4
|
-
|
|
5
|
-
export { GenericError } from './error-code';
|
|
6
|
-
export { cancelledError } from './error-code';
|
|
7
|
-
export { timeoutError } from './error-code';
|
|
8
|
-
export { permissionDeniedError } from './error-code';
|
|
9
|
-
export { alreadyExistsError } from './error-code';
|
|
10
|
-
export { notSupportedError } from './error-code';
|
|
11
|
-
export { resourceUnavailableError } from './error-code';
|
|
12
|
-
export { outOfRangeError } from './error-code';
|
|
13
|
-
export { invalidArgumentError } from './error-code';
|
|
14
|
-
export { networkFailedError } from './error-code';
|
|
15
|
-
export { interruptedError } from './error-code';
|
|
16
|
-
export { resultNilError } from './error-code';
|
|
17
|
-
|
|
18
|
-
export { lvErrorConst } from './error-const';
|
|
19
|
-
|
|
20
|
-
export { makeOk } from './error-t';
|
|
21
|
-
export { makeOkWith } from './error-t';
|
|
22
|
-
export { makeError } from './error-t';
|
|
23
|
-
export { makeErrorBy } from './error-t';
|
|
24
|
-
export { isLvErrorRef } from './error-t';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":"1.0.1","results":[[":error-or.test.ts",{"duration":0,"failed":false}],[":error-const.test.ts",{"duration":0,"failed":false}],[":error-t.test.ts",{"duration":1,"failed":false}]]}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { LinkedList } from '@/structure/linked-list';
|
|
2
|
-
|
|
3
|
-
export class DisposableLinkedList<T> extends LinkedList<T> {
|
|
4
|
-
unshiftAndGetDisposableNode(value: T): () => void {
|
|
5
|
-
this.unshift(value);
|
|
6
|
-
|
|
7
|
-
const node = this._head!;
|
|
8
|
-
let hasRemoved = false;
|
|
9
|
-
return (): void => {
|
|
10
|
-
if (!hasRemoved) {
|
|
11
|
-
hasRemoved = true;
|
|
12
|
-
super._remove(node);
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
pushAndGetDisposableNode(value: T): () => void {
|
|
18
|
-
this.push(value);
|
|
19
|
-
|
|
20
|
-
const node = this._tail!;
|
|
21
|
-
let hasRemoved = false;
|
|
22
|
-
return (): void => {
|
|
23
|
-
if (!hasRemoved) {
|
|
24
|
-
hasRemoved = true;
|
|
25
|
-
super._remove(node);
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
}
|