@elgato/utils 0.2.1 → 0.3.1
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/README.md +20 -2
- package/dist/event-emitter.d.ts +1 -1
- package/dist/event-emitter.js +1 -1
- package/dist/explicit-resource-management/deferred.d.ts +7 -0
- package/dist/{disposable.js → explicit-resource-management/deferred.js} +0 -3
- package/dist/explicit-resource-management/disposable-stack.d.ts +39 -0
- package/dist/explicit-resource-management/disposable-stack.js +75 -0
- package/dist/explicit-resource-management/disposable.d.ts +13 -0
- package/dist/explicit-resource-management/disposable.js +1 -0
- package/dist/explicit-resource-management/index.d.ts +3 -0
- package/dist/explicit-resource-management/index.js +6 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/logging/index.d.ts +0 -1
- package/dist/logging/index.js +0 -1
- package/dist/logging/{file-target.d.ts → node/file-target.d.ts} +2 -2
- package/dist/rpc/client.js +7 -1
- package/dist/rpc/server.d.ts +1 -1
- package/dist/rpc/server.js +1 -1
- package/package.json +5 -1
- package/dist/disposable.d.ts +0 -19
- /package/dist/logging/{file-target.js → node/file-target.js} +0 -0
package/README.md
CHANGED
|
@@ -24,14 +24,32 @@ Creates a new function that implements `Symbol.dispose` and accepts a disposer f
|
|
|
24
24
|
```js
|
|
25
25
|
import { deferredDisposable } from "@elgato/utils";
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
using
|
|
27
|
+
{
|
|
28
|
+
using deferredDisposable(() => console.log("Hello world"));
|
|
29
29
|
// ...
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
// "Hello world"
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
### `DisposableStack`
|
|
36
|
+
|
|
37
|
+
Stack of managed resources, controlled by a single disposer. Partial polyfill of TC39 Explicit Resource Management's `DisposableStack`.
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
import { DisposableStack } from "@elgato/utils";
|
|
41
|
+
|
|
42
|
+
{
|
|
43
|
+
using stack = new DisposableStack();
|
|
44
|
+
stack.defer(() => "Hello");
|
|
45
|
+
stack.defer(() => "world");
|
|
46
|
+
// ...
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// "Hello"
|
|
50
|
+
// "world"
|
|
51
|
+
```
|
|
52
|
+
|
|
35
53
|
## Events
|
|
36
54
|
|
|
37
55
|
### `EventEmitter`
|
package/dist/event-emitter.d.ts
CHANGED
package/dist/event-emitter.js
CHANGED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { IDisposable } from "./disposable.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a {@link IDisposable} that defers the disposing to the {@link dispose} function; disposing is guarded so that it may only occur once.
|
|
4
|
+
* @param dispose Function responsible for disposing.
|
|
5
|
+
* @returns Disposable whereby the disposing is delegated to the {@link dispose} function.
|
|
6
|
+
*/
|
|
7
|
+
export declare function deferredDisposable(dispose: () => void): IDisposable;
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
// Polyfill, explicit resource management https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management
|
|
2
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3
|
-
Symbol.dispose ??= Symbol("Symbol.dispose");
|
|
4
1
|
/**
|
|
5
2
|
* Creates a {@link IDisposable} that defers the disposing to the {@link dispose} function; disposing is guarded so that it may only occur once.
|
|
6
3
|
* @param dispose Function responsible for disposing.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { IDisposable } from "./disposable.js";
|
|
2
|
+
/**
|
|
3
|
+
* Stack-based container of disposable resources.
|
|
4
|
+
*/
|
|
5
|
+
export declare class DisposableStack implements IDisposable {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Gets a value indicating whether the stack has been disposed.
|
|
9
|
+
* @returns value indicating whether the stack has been disposed.
|
|
10
|
+
*/
|
|
11
|
+
get disposed(): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* @inheritdoc
|
|
14
|
+
*/
|
|
15
|
+
[Symbol.dispose](): void;
|
|
16
|
+
/**
|
|
17
|
+
* Adds a non-disposable resource and a disposal callback to the top of the stack.
|
|
18
|
+
* @param value Resource to be disposed.
|
|
19
|
+
* @param dispose Callback invoked to dispose the provided value.
|
|
20
|
+
* @returns This instance for chaining.
|
|
21
|
+
*/
|
|
22
|
+
adopt<T>(value: T, dispose: (value: T) => void): this;
|
|
23
|
+
/**
|
|
24
|
+
* Adds a disposal callback to the top of the stack.
|
|
25
|
+
* @param dispose Function to invoke when this object is disposed.
|
|
26
|
+
* @returns This instance for chaining.
|
|
27
|
+
*/
|
|
28
|
+
defer(dispose: () => void): this;
|
|
29
|
+
/**
|
|
30
|
+
* @inheritdoc
|
|
31
|
+
*/
|
|
32
|
+
dispose(): void;
|
|
33
|
+
/**
|
|
34
|
+
* Adds a resource to the top of the stack.
|
|
35
|
+
* @param value The `Disposable` to add.
|
|
36
|
+
* @returns This instance for chaining.
|
|
37
|
+
*/
|
|
38
|
+
use(value: IDisposable): this;
|
|
39
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { deferredDisposable } from "./deferred.js";
|
|
2
|
+
/**
|
|
3
|
+
* Stack-based container of disposable resources.
|
|
4
|
+
*/
|
|
5
|
+
export class DisposableStack {
|
|
6
|
+
/**
|
|
7
|
+
* Determines whether the stack is disposed.
|
|
8
|
+
*/
|
|
9
|
+
#disposed = false;
|
|
10
|
+
/**
|
|
11
|
+
* Resources managed by this stack.
|
|
12
|
+
*/
|
|
13
|
+
#disposables = [];
|
|
14
|
+
/**
|
|
15
|
+
* Gets a value indicating whether the stack has been disposed.
|
|
16
|
+
* @returns value indicating whether the stack has been disposed.
|
|
17
|
+
*/
|
|
18
|
+
get disposed() {
|
|
19
|
+
return this.#disposed;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @inheritdoc
|
|
23
|
+
*/
|
|
24
|
+
[Symbol.dispose]() {
|
|
25
|
+
this.dispose();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Adds a non-disposable resource and a disposal callback to the top of the stack.
|
|
29
|
+
* @param value Resource to be disposed.
|
|
30
|
+
* @param dispose Callback invoked to dispose the provided value.
|
|
31
|
+
* @returns This instance for chaining.
|
|
32
|
+
*/
|
|
33
|
+
adopt(value, dispose) {
|
|
34
|
+
if (this.disposed) {
|
|
35
|
+
throw new Error("Cannot add disposable resource to stack as the stack is already disposed.");
|
|
36
|
+
}
|
|
37
|
+
return this.use(deferredDisposable(() => dispose(value)));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Adds a disposal callback to the top of the stack.
|
|
41
|
+
* @param dispose Function to invoke when this object is disposed.
|
|
42
|
+
* @returns This instance for chaining.
|
|
43
|
+
*/
|
|
44
|
+
defer(dispose) {
|
|
45
|
+
if (this.disposed) {
|
|
46
|
+
throw new Error("Cannot add disposable resource to stack as the stack is already disposed.");
|
|
47
|
+
}
|
|
48
|
+
return this.use(deferredDisposable(dispose));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @inheritdoc
|
|
52
|
+
*/
|
|
53
|
+
dispose() {
|
|
54
|
+
if (this.disposed) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
let disposable;
|
|
58
|
+
while ((disposable = this.#disposables.pop())) {
|
|
59
|
+
disposable.dispose();
|
|
60
|
+
}
|
|
61
|
+
this.#disposed = true;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Adds a resource to the top of the stack.
|
|
65
|
+
* @param value The `Disposable` to add.
|
|
66
|
+
* @returns This instance for chaining.
|
|
67
|
+
*/
|
|
68
|
+
use(value) {
|
|
69
|
+
if (this.disposed) {
|
|
70
|
+
throw new Error("Cannot add disposable resource to stack as the stack is already disposed.");
|
|
71
|
+
}
|
|
72
|
+
this.#disposables.push(value);
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An extension of the upcoming explicit resource management disposable that defines a `dispose()` member.
|
|
3
|
+
*/
|
|
4
|
+
export interface IDisposable {
|
|
5
|
+
/**
|
|
6
|
+
* Disposes this instance.
|
|
7
|
+
*/
|
|
8
|
+
[Symbol.dispose](): void;
|
|
9
|
+
/**
|
|
10
|
+
* Disposes this instance.
|
|
11
|
+
*/
|
|
12
|
+
dispose(): void;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Polyfill, explicit resource management https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3
|
+
Symbol.dispose ??= Symbol("Symbol.dispose");
|
|
4
|
+
export * from "./deferred.js";
|
|
5
|
+
export * from "./disposable-stack.js";
|
|
6
|
+
export * from "./disposable.js";
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/logging/index.d.ts
CHANGED
package/dist/logging/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { LogEntryFormatter } from "
|
|
2
|
-
import type { LogEntry, LogTarget } from "
|
|
1
|
+
import type { LogEntryFormatter } from "../format.js";
|
|
2
|
+
import type { LogEntry, LogTarget } from "../target.js";
|
|
3
3
|
/**
|
|
4
4
|
* Provides a {@link LogTarget} capable of logging to a local file system.
|
|
5
5
|
*/
|
package/dist/rpc/client.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
var _a;
|
|
1
2
|
import { JsonRpcErrorCode } from "./json-rpc/error.js";
|
|
2
3
|
import { JsonRpcResponse } from "./json-rpc/response.js";
|
|
3
4
|
import {} from "./request.js";
|
|
@@ -6,6 +7,10 @@ import {} from "./response.js";
|
|
|
6
7
|
* Client capable of sending requests to, and receiving responses from, a server.
|
|
7
8
|
*/
|
|
8
9
|
export class RpcClient {
|
|
10
|
+
/**
|
|
11
|
+
* The timeout, in milliseconds, used when the request does not have a timeout specified; default 30 seconds.
|
|
12
|
+
*/
|
|
13
|
+
static #DEFAULT_TIMEOUT = 30000;
|
|
9
14
|
/**
|
|
10
15
|
* The client's options.
|
|
11
16
|
*/
|
|
@@ -119,7 +124,7 @@ export class RpcClient {
|
|
|
119
124
|
*/
|
|
120
125
|
async #sendRequest(request) {
|
|
121
126
|
const id = crypto.randomUUID();
|
|
122
|
-
const { method, params, timeout } = request;
|
|
127
|
+
const { method, params, timeout = _a.#DEFAULT_TIMEOUT } = request;
|
|
123
128
|
// Initialize the response handler.
|
|
124
129
|
const response = new Promise((resolve) => {
|
|
125
130
|
this.#requests.set(id, (res) => {
|
|
@@ -141,3 +146,4 @@ export class RpcClient {
|
|
|
141
146
|
return response;
|
|
142
147
|
}
|
|
143
148
|
}
|
|
149
|
+
_a = RpcClient;
|
package/dist/rpc/server.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type IDisposable } from "../disposable.js";
|
|
1
|
+
import { type IDisposable } from "../explicit-resource-management/disposable.js";
|
|
2
2
|
import type { JsonValue } from "../json.js";
|
|
3
3
|
import type { RpcRequestParameters } from "./request.js";
|
|
4
4
|
import { RpcRequestResponder, type RpcResponseResult } from "./response.js";
|
package/dist/rpc/server.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {} from "../disposable.js";
|
|
2
1
|
import { EventEmitter } from "../event-emitter.js";
|
|
2
|
+
import {} from "../explicit-resource-management/disposable.js";
|
|
3
3
|
import { JsonRpcErrorCode } from "./json-rpc/error.js";
|
|
4
4
|
import { JsonRpcRequest } from "./json-rpc/request.js";
|
|
5
5
|
import { RpcRequestResponder } from "./response.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elgato/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Utilities used throughout the Elgato ecosystem.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -29,6 +29,10 @@
|
|
|
29
29
|
"types": "./dist/logging/index.d.ts",
|
|
30
30
|
"default": "./dist/logging/index.js"
|
|
31
31
|
},
|
|
32
|
+
"./logging/*": {
|
|
33
|
+
"types": "./dist/logging/node/*.d.ts",
|
|
34
|
+
"default": "./dist/logging/node/*"
|
|
35
|
+
},
|
|
32
36
|
"./rpc": {
|
|
33
37
|
"types": "./dist/rpc/index.d.ts",
|
|
34
38
|
"default": "./dist/rpc/index.js"
|
package/dist/disposable.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates a {@link IDisposable} that defers the disposing to the {@link dispose} function; disposing is guarded so that it may only occur once.
|
|
3
|
-
* @param dispose Function responsible for disposing.
|
|
4
|
-
* @returns Disposable whereby the disposing is delegated to the {@link dispose} function.
|
|
5
|
-
*/
|
|
6
|
-
export declare function deferredDisposable(dispose: (...args: unknown[]) => void): IDisposable;
|
|
7
|
-
/**
|
|
8
|
-
* An extension of the upcoming explicit resource management disposable that defines a `dispose()` member.
|
|
9
|
-
*/
|
|
10
|
-
export interface IDisposable {
|
|
11
|
-
/**
|
|
12
|
-
* Disposes this instance.
|
|
13
|
-
*/
|
|
14
|
-
[Symbol.dispose](): void;
|
|
15
|
-
/**
|
|
16
|
-
* Disposes this instance.
|
|
17
|
-
*/
|
|
18
|
-
dispose(): void;
|
|
19
|
-
}
|
|
File without changes
|