@e280/stz 0.0.0-36 → 0.0.0-38
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/package.json +1 -1
- package/s/index.ts +1 -0
- package/s/scope.ts +111 -0
- package/x/index.d.ts +1 -0
- package/x/index.js +1 -0
- package/x/index.js.map +1 -1
- package/x/scope.d.ts +45 -0
- package/x/scope.js +84 -0
- package/x/scope.js.map +1 -0
package/package.json
CHANGED
package/s/index.ts
CHANGED
package/s/scope.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
|
|
2
|
+
export type Scoped<Item> = [item: Item, dispose: () => void]
|
|
3
|
+
export abstract class Disposable { abstract dispose(): void }
|
|
4
|
+
|
|
5
|
+
export type DisposableClass = {
|
|
6
|
+
new(...p: any[]): any
|
|
7
|
+
dispose(): void
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function scoped<Item>(item: Item, dispose: () => void) {
|
|
11
|
+
return [item, dispose] as Scoped<Item>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* a trashcan you can fill with garbage, then call `scope.dispose()` to dump it all
|
|
16
|
+
* - `add/stow/scoped` methods are for dealing with disposer fns
|
|
17
|
+
* - `keep` methods are for dealing with disposable objects (with a dispose method)
|
|
18
|
+
* - `sub` for creating nested sub-scopes
|
|
19
|
+
*/
|
|
20
|
+
export class Scope extends Disposable {
|
|
21
|
+
#disposers: (() => void)[] = []
|
|
22
|
+
|
|
23
|
+
/** add disposer fn */
|
|
24
|
+
add(dispose: () => void) {
|
|
25
|
+
this.#disposers.push(dispose)
|
|
26
|
+
return this
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** add disposer, return item */
|
|
30
|
+
stow<Item>(item: Item, dispose: () => void) {
|
|
31
|
+
this.add(dispose)
|
|
32
|
+
return item
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** add scoped disposer, return item */
|
|
36
|
+
scoped<Item>([item, dispose]: Scoped<Item>) {
|
|
37
|
+
this.add(dispose)
|
|
38
|
+
return item
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** add and return a disposable object */
|
|
42
|
+
keep<D extends Disposable>(disposable: D) {
|
|
43
|
+
this.add(() => disposable.dispose())
|
|
44
|
+
return disposable
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** wrap a fn, auto-add returned disposers */
|
|
48
|
+
scopedFn<Params extends any[], Item>(
|
|
49
|
+
fn: (...params: Params) => Scoped<Item>
|
|
50
|
+
) {
|
|
51
|
+
return (...a: Params) => {
|
|
52
|
+
const scoped = fn(...a)
|
|
53
|
+
return this.scoped(scoped)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** wrap an async fn, auto-add returned disposers */
|
|
58
|
+
scopedFnAsync<Params extends any[], Item>(
|
|
59
|
+
fn: (...params: Params) => Promise<Scoped<Item>>
|
|
60
|
+
) {
|
|
61
|
+
return async(...a: Params) => {
|
|
62
|
+
const scoped = await fn(...a)
|
|
63
|
+
return this.scoped(scoped)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** wrap a fn, auto-add returned disposables */
|
|
68
|
+
keepFn<Params extends any[], D extends Disposable>(
|
|
69
|
+
fn: (...params: Params) => D
|
|
70
|
+
) {
|
|
71
|
+
return (...a: Params) => {
|
|
72
|
+
const disposable = fn(...a)
|
|
73
|
+
return this.keep(disposable)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** wrap an async fn, auto-add returned disposables */
|
|
78
|
+
keepFnAsync<Params extends any[], D extends Disposable>(
|
|
79
|
+
fn: (...params: Params) => Promise<D>
|
|
80
|
+
) {
|
|
81
|
+
return async(...a: Params) => {
|
|
82
|
+
const disposable = await fn(...a)
|
|
83
|
+
return this.keep(disposable)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** wrap a constructor, auto-add returned disposables */
|
|
88
|
+
keepConstructor<C extends DisposableClass>(Ctor: C) {
|
|
89
|
+
const scope = this
|
|
90
|
+
return class extends Ctor {
|
|
91
|
+
constructor(...p: any[]) {
|
|
92
|
+
super(...p)
|
|
93
|
+
scope.keep(this as any)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** create a subscope, child gets disposed when parent does */
|
|
99
|
+
sub() {
|
|
100
|
+
const subscope = new Scope()
|
|
101
|
+
return this.stow(subscope, () => subscope.dispose())
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** dispose everything in this scope, in reverse order */
|
|
105
|
+
dispose() {
|
|
106
|
+
for (const fn of this.#disposers.reverse())
|
|
107
|
+
fn()
|
|
108
|
+
this.#disposers = []
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
package/x/index.d.ts
CHANGED
package/x/index.js
CHANGED
package/x/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../s/index.ts"],"names":[],"mappings":"AACA,cAAc,0BAA0B,CAAA;AACxC,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA;AAE7B,cAAc,wBAAwB,CAAA;AACtC,cAAc,qBAAqB,CAAA;AAEnC,cAAc,gBAAgB,CAAA;AAE9B,cAAc,kBAAkB,CAAA;AAEhC,cAAc,eAAe,CAAA;AAC7B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,mBAAmB,CAAA;AACjC,cAAc,SAAS,CA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,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA"}
|
package/x/scope.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export type Scoped<Item> = [item: Item, dispose: () => void];
|
|
2
|
+
export declare abstract class Disposable {
|
|
3
|
+
abstract dispose(): void;
|
|
4
|
+
}
|
|
5
|
+
export type DisposableClass = {
|
|
6
|
+
new (...p: any[]): any;
|
|
7
|
+
dispose(): void;
|
|
8
|
+
};
|
|
9
|
+
export declare function scoped<Item>(item: Item, dispose: () => void): Scoped<Item>;
|
|
10
|
+
/**
|
|
11
|
+
* a trashcan you can fill with garbage, then call `scope.dispose()` to dump it all
|
|
12
|
+
* - `add/stow/scoped` methods are for dealing with disposer fns
|
|
13
|
+
* - `keep` methods are for dealing with disposable objects (with a dispose method)
|
|
14
|
+
* - `sub` for creating nested sub-scopes
|
|
15
|
+
*/
|
|
16
|
+
export declare class Scope extends Disposable {
|
|
17
|
+
#private;
|
|
18
|
+
/** add disposer fn */
|
|
19
|
+
add(dispose: () => void): this;
|
|
20
|
+
/** add disposer, return item */
|
|
21
|
+
stow<Item>(item: Item, dispose: () => void): Item;
|
|
22
|
+
/** add scoped disposer, return item */
|
|
23
|
+
scoped<Item>([item, dispose]: Scoped<Item>): Item;
|
|
24
|
+
/** add and return a disposable object */
|
|
25
|
+
keep<D extends Disposable>(disposable: D): D;
|
|
26
|
+
/** wrap a fn, auto-add returned disposers */
|
|
27
|
+
scopedFn<Params extends any[], Item>(fn: (...params: Params) => Scoped<Item>): (...a: Params) => Item;
|
|
28
|
+
/** wrap an async fn, auto-add returned disposers */
|
|
29
|
+
scopedFnAsync<Params extends any[], Item>(fn: (...params: Params) => Promise<Scoped<Item>>): (...a: Params) => Promise<Item>;
|
|
30
|
+
/** wrap a fn, auto-add returned disposables */
|
|
31
|
+
keepFn<Params extends any[], D extends Disposable>(fn: (...params: Params) => D): (...a: Params) => D;
|
|
32
|
+
/** wrap an async fn, auto-add returned disposables */
|
|
33
|
+
keepFnAsync<Params extends any[], D extends Disposable>(fn: (...params: Params) => Promise<D>): (...a: Params) => Promise<D>;
|
|
34
|
+
/** wrap a constructor, auto-add returned disposables */
|
|
35
|
+
keepConstructor<C extends DisposableClass>(Ctor: C): {
|
|
36
|
+
new (...p: any[]): {
|
|
37
|
+
[x: string]: any;
|
|
38
|
+
};
|
|
39
|
+
dispose(): void;
|
|
40
|
+
} & C;
|
|
41
|
+
/** create a subscope, child gets disposed when parent does */
|
|
42
|
+
sub(): Scope;
|
|
43
|
+
/** dispose everything in this scope, in reverse order */
|
|
44
|
+
dispose(): void;
|
|
45
|
+
}
|
package/x/scope.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export class Disposable {
|
|
2
|
+
}
|
|
3
|
+
export function scoped(item, dispose) {
|
|
4
|
+
return [item, dispose];
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* a trashcan you can fill with garbage, then call `scope.dispose()` to dump it all
|
|
8
|
+
* - `add/stow/scoped` methods are for dealing with disposer fns
|
|
9
|
+
* - `keep` methods are for dealing with disposable objects (with a dispose method)
|
|
10
|
+
* - `sub` for creating nested sub-scopes
|
|
11
|
+
*/
|
|
12
|
+
export class Scope extends Disposable {
|
|
13
|
+
#disposers = [];
|
|
14
|
+
/** add disposer fn */
|
|
15
|
+
add(dispose) {
|
|
16
|
+
this.#disposers.push(dispose);
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
/** add disposer, return item */
|
|
20
|
+
stow(item, dispose) {
|
|
21
|
+
this.add(dispose);
|
|
22
|
+
return item;
|
|
23
|
+
}
|
|
24
|
+
/** add scoped disposer, return item */
|
|
25
|
+
scoped([item, dispose]) {
|
|
26
|
+
this.add(dispose);
|
|
27
|
+
return item;
|
|
28
|
+
}
|
|
29
|
+
/** add and return a disposable object */
|
|
30
|
+
keep(disposable) {
|
|
31
|
+
this.add(() => disposable.dispose());
|
|
32
|
+
return disposable;
|
|
33
|
+
}
|
|
34
|
+
/** wrap a fn, auto-add returned disposers */
|
|
35
|
+
scopedFn(fn) {
|
|
36
|
+
return (...a) => {
|
|
37
|
+
const scoped = fn(...a);
|
|
38
|
+
return this.scoped(scoped);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** wrap an async fn, auto-add returned disposers */
|
|
42
|
+
scopedFnAsync(fn) {
|
|
43
|
+
return async (...a) => {
|
|
44
|
+
const scoped = await fn(...a);
|
|
45
|
+
return this.scoped(scoped);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** wrap a fn, auto-add returned disposables */
|
|
49
|
+
keepFn(fn) {
|
|
50
|
+
return (...a) => {
|
|
51
|
+
const disposable = fn(...a);
|
|
52
|
+
return this.keep(disposable);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/** wrap an async fn, auto-add returned disposables */
|
|
56
|
+
keepFnAsync(fn) {
|
|
57
|
+
return async (...a) => {
|
|
58
|
+
const disposable = await fn(...a);
|
|
59
|
+
return this.keep(disposable);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** wrap a constructor, auto-add returned disposables */
|
|
63
|
+
keepConstructor(Ctor) {
|
|
64
|
+
const scope = this;
|
|
65
|
+
return class extends Ctor {
|
|
66
|
+
constructor(...p) {
|
|
67
|
+
super(...p);
|
|
68
|
+
scope.keep(this);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/** create a subscope, child gets disposed when parent does */
|
|
73
|
+
sub() {
|
|
74
|
+
const subscope = new Scope();
|
|
75
|
+
return this.stow(subscope, () => subscope.dispose());
|
|
76
|
+
}
|
|
77
|
+
/** dispose everything in this scope, in reverse order */
|
|
78
|
+
dispose() {
|
|
79
|
+
for (const fn of this.#disposers.reverse())
|
|
80
|
+
fn();
|
|
81
|
+
this.#disposers = [];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=scope.js.map
|
package/x/scope.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scope.js","sourceRoot":"","sources":["../s/scope.ts"],"names":[],"mappings":"AAEA,MAAM,OAAgB,UAAU;CAA6B;AAO7D,MAAM,UAAU,MAAM,CAAO,IAAU,EAAE,OAAmB;IAC3D,OAAO,CAAC,IAAI,EAAE,OAAO,CAAiB,CAAA;AACvC,CAAC;AAED;;;;;GAKG;AACH,MAAM,OAAO,KAAM,SAAQ,UAAU;IACpC,UAAU,GAAmB,EAAE,CAAA;IAE/B,sBAAsB;IACtB,GAAG,CAAC,OAAmB;QACtB,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAC7B,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,gCAAgC;IAChC,IAAI,CAAO,IAAU,EAAE,OAAmB;QACzC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QACjB,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,uCAAuC;IACvC,MAAM,CAAO,CAAC,IAAI,EAAE,OAAO,CAAe;QACzC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QACjB,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,yCAAyC;IACzC,IAAI,CAAuB,UAAa;QACvC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,CAAA;QACpC,OAAO,UAAU,CAAA;IAClB,CAAC;IAED,6CAA6C;IAC7C,QAAQ,CACN,EAAuC;QAExC,OAAO,CAAC,GAAG,CAAS,EAAE,EAAE;YACvB,MAAM,MAAM,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;YACvB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;QAC3B,CAAC,CAAA;IACF,CAAC;IAED,oDAAoD;IACpD,aAAa,CACX,EAAgD;QAEjD,O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