@open-pioneer/core 0.1.1 → 0.1.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.
- package/CHANGELOG.md +6 -0
- package/Logger.d.ts +101 -0
- package/error.d.ts +49 -0
- package/events.d.ts +60 -0
- package/index.d.ts +5 -0
- package/package.json +3 -2
- package/resources.d.ts +23 -0
- package/utils.d.ts +12 -0
package/CHANGELOG.md
CHANGED
package/Logger.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates and returns a new logger instance for an application wide standardized logging.
|
|
3
|
+
* For available log methods see {@link Logger} interface.
|
|
4
|
+
* The log level is globally configured (see vite.config.ts).
|
|
5
|
+
*
|
|
6
|
+
* @param prefix Prefix used to prefix all log messages invoked by the created logger instance.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createLogger(prefix: string): Logger;
|
|
9
|
+
/**
|
|
10
|
+
* Allowed log levels.
|
|
11
|
+
* Order: DEBUG < INFO < WARN < ERROR
|
|
12
|
+
*/
|
|
13
|
+
export type LogLevel = "DEBUG" | "INFO" | "WARN" | "ERROR";
|
|
14
|
+
export interface LogMethod {
|
|
15
|
+
/**
|
|
16
|
+
* Logger's log method signature
|
|
17
|
+
*
|
|
18
|
+
* @param message Log message to be logged (attached to the prefix). Can be a string or an Error.
|
|
19
|
+
* @param values Arbitrary amount of additional values to be logged (attached to message).
|
|
20
|
+
*/
|
|
21
|
+
(message: string | Error, ...values: unknown[]): void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Provides a logger for a standardized application wide logging.
|
|
25
|
+
*
|
|
26
|
+
* The logger's log level is configured globally.
|
|
27
|
+
*
|
|
28
|
+
* Use the `createLogger` function to receive a new logger instance for logging.
|
|
29
|
+
*/
|
|
30
|
+
export interface Logger {
|
|
31
|
+
/**
|
|
32
|
+
* Prefix prepended to all logging messages
|
|
33
|
+
*/
|
|
34
|
+
readonly prefix: string;
|
|
35
|
+
/**
|
|
36
|
+
* Returns true if debug log level is enabled.
|
|
37
|
+
*/
|
|
38
|
+
isDebug(): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Logging method for debug log level.
|
|
41
|
+
* Follows normal LogMethod structure but logs in debug level.
|
|
42
|
+
*/
|
|
43
|
+
debug: LogMethod;
|
|
44
|
+
/**
|
|
45
|
+
* Logging method for info log level.
|
|
46
|
+
* Follows normal log method structure but logs in info level.
|
|
47
|
+
*/
|
|
48
|
+
info: LogMethod;
|
|
49
|
+
/**
|
|
50
|
+
* Logging method for warn log level.
|
|
51
|
+
* Follows normal log method structure but logs in warn level.
|
|
52
|
+
*/
|
|
53
|
+
warn: LogMethod;
|
|
54
|
+
/**
|
|
55
|
+
* Logging method for debug error level.
|
|
56
|
+
* Follows normal log method structure but logs in error level.
|
|
57
|
+
*/
|
|
58
|
+
error: LogMethod;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* A class implementing a logger with a global log level.
|
|
62
|
+
*/
|
|
63
|
+
export declare class LoggerImpl implements Logger {
|
|
64
|
+
prefix: string;
|
|
65
|
+
private readonly enabledLogLevelNumber;
|
|
66
|
+
constructor(prefix: string, logLevel: LogLevel);
|
|
67
|
+
isDebug(): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Performs a debug log with the given message and values if DEBUG level is enabled.
|
|
70
|
+
* @param message
|
|
71
|
+
* @param values
|
|
72
|
+
*/
|
|
73
|
+
debug(message: string | Error, ...values: unknown[]): void;
|
|
74
|
+
/**
|
|
75
|
+
* Performs an info log with the given message and values if INFO level is enabled.
|
|
76
|
+
* @param message
|
|
77
|
+
* @param values
|
|
78
|
+
*/
|
|
79
|
+
info(message: string | Error, ...values: unknown[]): void;
|
|
80
|
+
/**
|
|
81
|
+
* Performs a warning log with the given message and values if WARN level is enabled.
|
|
82
|
+
* @param message
|
|
83
|
+
* @param values
|
|
84
|
+
*/
|
|
85
|
+
warn(message: string | Error, ...values: unknown[]): void;
|
|
86
|
+
/**
|
|
87
|
+
* Performs an error log with the given message and values if ERROR level is enabled.
|
|
88
|
+
* @param message
|
|
89
|
+
* @param values
|
|
90
|
+
*/
|
|
91
|
+
error(message: string | Error, ...values: unknown[]): void;
|
|
92
|
+
/**
|
|
93
|
+
* Internal method for performing the log if the specified level is enabled.
|
|
94
|
+
* @param level
|
|
95
|
+
* @param messageOrError
|
|
96
|
+
* @param values
|
|
97
|
+
* @private
|
|
98
|
+
*/
|
|
99
|
+
private _doLog;
|
|
100
|
+
private _isLogLevelEnabled;
|
|
101
|
+
}
|
package/error.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
declare const GlobalError: ErrorConstructor;
|
|
2
|
+
declare global {
|
|
3
|
+
interface ErrorConstructor {
|
|
4
|
+
new (message?: string, options?: {
|
|
5
|
+
cause?: unknown;
|
|
6
|
+
}): globalThis.Error;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A custom error class that enforces the use of error ids.
|
|
11
|
+
*
|
|
12
|
+
* An error must be constructed from a (computer-readable) id and a human readable informational text.
|
|
13
|
+
* An optional `cause` option can be passed to indicate the nested reason for an error.
|
|
14
|
+
*
|
|
15
|
+
* # Conventions
|
|
16
|
+
*
|
|
17
|
+
* Error ids should be scoped to a package to avoid conflicts.
|
|
18
|
+
* Each package should pick a sensible namespace prefix.
|
|
19
|
+
*
|
|
20
|
+
* An example for a good error id is `runtime:dependency-cycle`.
|
|
21
|
+
*/
|
|
22
|
+
export declare class Error extends GlobalError {
|
|
23
|
+
/** Unique error id */
|
|
24
|
+
readonly id: string;
|
|
25
|
+
/** Human readable error message */
|
|
26
|
+
readonly text: string;
|
|
27
|
+
constructor(id: string, text: string, options?: {
|
|
28
|
+
cause?: unknown;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns the error chain for the given `err`, starting with this error.
|
|
33
|
+
* The error chain contains the error itself and all its causes.
|
|
34
|
+
* The first entry is `err` itself.
|
|
35
|
+
*/
|
|
36
|
+
export declare function getErrorChain(err: globalThis.Error): globalThis.Error[];
|
|
37
|
+
/**
|
|
38
|
+
* Returns true if the error represents an abort error.
|
|
39
|
+
*/
|
|
40
|
+
export declare function isAbortError(err: unknown): unknown;
|
|
41
|
+
/**
|
|
42
|
+
* Throws an abort error (`.name` === `"AbortError"`).
|
|
43
|
+
*/
|
|
44
|
+
export declare function throwAbortError(): never;
|
|
45
|
+
/**
|
|
46
|
+
* Returns an abort error (`.name` === `"AbortError"`).
|
|
47
|
+
*/
|
|
48
|
+
export declare function createAbortError(): globalThis.Error;
|
|
49
|
+
export {};
|
package/events.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Resource } from "./resources";
|
|
2
|
+
declare const state: unique symbol;
|
|
3
|
+
export type EventNames<Events extends Record<string, unknown>> = keyof Events & string;
|
|
4
|
+
type ArgType<T> = [T] extends [void] ? [] : [event: T];
|
|
5
|
+
type EventType<Events extends Record<string, unknown>, Name extends keyof Events> = ArgType<Events[Name]>;
|
|
6
|
+
/**
|
|
7
|
+
* A support class that implements emitting and listening for events.
|
|
8
|
+
*
|
|
9
|
+
* This class supports inheritance or direct use:
|
|
10
|
+
*
|
|
11
|
+
* ```js
|
|
12
|
+
* const emitter = new EventEmitter();
|
|
13
|
+
* class MyClass extends EventEmitter {};
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* When using this class from TypeScript, declare your supported event
|
|
17
|
+
* types using an interface first:
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* interface Events {
|
|
21
|
+
* // key: event name, value: event type
|
|
22
|
+
* "mouse-clicked": MouseEvent;
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* const emitter = new EventEmitter<Events>();
|
|
26
|
+
* emitter.on("mouse-clicked", (event) => {
|
|
27
|
+
* // event is a MouseEvent
|
|
28
|
+
* });
|
|
29
|
+
* emitter.emit("mouse-clicked", new MouseEvent(...));
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare class EventEmitter<Events extends {}> {
|
|
33
|
+
private [state];
|
|
34
|
+
/**
|
|
35
|
+
* Registers the given listener function as an event handler for `eventName`.
|
|
36
|
+
*
|
|
37
|
+
* The listener function should be unregistered by destroying the returned {@link Resource}
|
|
38
|
+
* when it is no longer needed.
|
|
39
|
+
*/
|
|
40
|
+
on<Name extends EventNames<Events>>(eventName: Name, listener: (...args: EventType<Events, Name>) => void): Resource;
|
|
41
|
+
/**
|
|
42
|
+
* Registers the given listener function to listen for `eventName` events _once_.
|
|
43
|
+
* The listener function will automatically be unregistered after it has been called.
|
|
44
|
+
*
|
|
45
|
+
* The listener function should be unregistered by destroying the returned {@link Resource}.
|
|
46
|
+
*/
|
|
47
|
+
once<Name extends EventNames<Events>>(eventName: Name, listener: (...args: EventType<Events, Name>) => void): Resource;
|
|
48
|
+
/**
|
|
49
|
+
* Emits an event of the given name and calls the registered event handlers.
|
|
50
|
+
*
|
|
51
|
+
* _Note:_ event handlers run synchronously.
|
|
52
|
+
* After `emit()` has completed, all listeners will already have been invoked.
|
|
53
|
+
*/
|
|
54
|
+
emit<Name extends EventNames<Events>>(eventName: Name, ...args: EventType<Events, Name>): void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Read-only version of the {@link EventEmitter} interface that only allows listening for events.
|
|
58
|
+
*/
|
|
59
|
+
export type EventSource<Events extends {}> = Pick<EventEmitter<Events>, "on" | "once">;
|
|
60
|
+
export {};
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { Error, getErrorChain, isAbortError, throwAbortError, createAbortError } from "./error";
|
|
2
|
+
export { EventEmitter, type EventSource, type EventNames } from "./events";
|
|
3
|
+
export { destroyResource, type Resource } from "./resources";
|
|
4
|
+
export { createLogger, type Logger, type LogLevel, type LogMethod } from "./Logger";
|
|
5
|
+
export { createManualPromise, type ManualPromise } from "./utils";
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@open-pioneer/core",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.2",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"exports": {
|
|
7
7
|
"./package.json": "./package.json",
|
|
8
8
|
".": {
|
|
9
|
-
"import": "./index.js"
|
|
9
|
+
"import": "./index.js",
|
|
10
|
+
"types": "./index.d.ts"
|
|
10
11
|
}
|
|
11
12
|
},
|
|
12
13
|
"openPioneerFramework": {
|
package/resources.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An object that has some cleanup code associated with it.
|
|
3
|
+
*/
|
|
4
|
+
export interface Resource {
|
|
5
|
+
/** A function that releases any state held by the resource. */
|
|
6
|
+
destroy(): void;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A helper function that invokes `destroy()` on the given resource and returns undefined.
|
|
10
|
+
*
|
|
11
|
+
* Example:
|
|
12
|
+
*
|
|
13
|
+
* ```js
|
|
14
|
+
* class Holder {
|
|
15
|
+
* private myResource: Resource | undefined;
|
|
16
|
+
*
|
|
17
|
+
* destroy() {
|
|
18
|
+
* this.myResource = destroyResource(this.myResource);
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function destroyResource<R extends Resource>(resource: R | undefined): undefined;
|
package/utils.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A manual promise that also exposes its `resolve` and `reject` functions.
|
|
3
|
+
*
|
|
4
|
+
* The user must take care to always call either `resolve` or `reject` at least once,
|
|
5
|
+
* otherwise clients waiting for the `promise` may wait forever.
|
|
6
|
+
*/
|
|
7
|
+
export interface ManualPromise<T> {
|
|
8
|
+
promise: Promise<T>;
|
|
9
|
+
resolve(value: T): void;
|
|
10
|
+
reject(error: unknown): void;
|
|
11
|
+
}
|
|
12
|
+
export declare function createManualPromise<T>(): ManualPromise<T>;
|