@e280/stz 0.0.0-37 → 0.0.0-39

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e280/stz",
3
- "version": "0.0.0-37",
3
+ "version": "0.0.0-39",
4
4
  "description": "everyday ts fns for everything",
5
5
  "license": "MIT",
6
6
  "author": "Chase Moskal <chasemoskal@gmail.com>",
package/s/constructor.ts CHANGED
@@ -1,3 +1,15 @@
1
1
 
2
2
  export type Constructor<T extends {} = {}> = new(...args: any[]) => T
3
3
 
4
+ export type Ctor<
5
+ Params extends any[] = any[],
6
+ Instance extends {} = {},
7
+ > = new(...params: Params) => Instance
8
+
9
+ export function denew<
10
+ Params extends any[] = any[],
11
+ Instance extends {} = {},
12
+ >(C: Ctor<Params, Instance>) {
13
+ return (...p: Params) => new C(...p)
14
+ }
15
+
package/s/scope.ts CHANGED
@@ -1,4 +1,6 @@
1
1
 
2
+ import {Constructor} from "./constructor.js"
3
+
2
4
  export type Scoped<Item> = [item: Item, dispose: () => void]
3
5
  export type Disposable = {dispose(): void}
4
6
 
@@ -6,71 +8,97 @@ export function scoped<Item>(item: Item, dispose: () => void) {
6
8
  return [item, dispose] as Scoped<Item>
7
9
  }
8
10
 
9
- export class Scope {
11
+ /**
12
+ * a trashcan you can fill with garbage, then call `scope.dispose()` to dump it all
13
+ * - `add/stow/scoped` methods are for dealing with disposer fns
14
+ * - `keep` methods are for dealing with disposable objects (with a dispose method)
15
+ * - `sub` for creating nested sub-scopes
16
+ */
17
+ export class Scope implements Disposable {
10
18
  #disposers: (() => void)[] = []
11
19
 
12
- /** add a dispose fn */
20
+ /** add disposer fn */
13
21
  add(dispose: () => void) {
14
22
  this.#disposers.push(dispose)
23
+ return this
15
24
  }
16
25
 
17
- /** add a dispose fn, and return the item */
18
- reg<Item>(item: Item, dispose: () => void) {
26
+ /** add disposer, return item */
27
+ stow<Item>(item: Item, dispose: () => void) {
19
28
  this.add(dispose)
20
29
  return item
21
30
  }
22
31
 
23
- /** add a scoped item's disposer, and return the item */
24
- register<Item>([item, dispose]: Scoped<Item>) {
32
+ /** add scoped disposer, return item */
33
+ scoped<Item>([item, dispose]: Scoped<Item>) {
25
34
  this.add(dispose)
26
35
  return item
27
36
  }
28
37
 
29
- /** register and return disposable item */
30
- registerDisposable<Item extends Disposable>(item: Item) {
31
- this.#disposers.push(() => item.dispose)
32
- return item
38
+ /** add and return a disposable object */
39
+ keep<D extends Disposable>(disposable: D) {
40
+ this.add(() => disposable.dispose())
41
+ return disposable
33
42
  }
34
43
 
35
- /** augment a fn to register its returned scoped item */
36
- fn<Params extends any[], Item>(fn: (...params: Params) => Scoped<Item>) {
44
+ /** wrap a fn, auto-add returned disposers */
45
+ scopedFn<Params extends any[], Item>(
46
+ fn: (...params: Params) => Scoped<Item>
47
+ ) {
37
48
  return (...a: Params) => {
38
49
  const scoped = fn(...a)
39
- return this.register(scoped)
50
+ return this.scoped(scoped)
40
51
  }
41
52
  }
42
53
 
43
- /** augment an async fn to register its returned scoped item */
44
- fnAsync<Params extends any[], Item>(fn: (...params: Params) => Promise<Scoped<Item>>) {
54
+ /** wrap an async fn, auto-add returned disposers */
55
+ scopedFnAsync<Params extends any[], Item>(
56
+ fn: (...params: Params) => Promise<Scoped<Item>>
57
+ ) {
45
58
  return async(...a: Params) => {
46
59
  const scoped = await fn(...a)
47
- return this.register(scoped)
60
+ return this.scoped(scoped)
48
61
  }
49
62
  }
50
63
 
51
- /** augment a fn to register its returned disposable item */
52
- fnDisposable<Params extends any[], Item extends Disposable>(fn: (...params: Params) => Item) {
64
+ /** wrap a fn, auto-add returned disposables */
65
+ keepFn<Params extends any[], D extends Disposable>(
66
+ fn: (...params: Params) => D
67
+ ) {
53
68
  return (...a: Params) => {
54
- const scoped = fn(...a)
55
- return this.registerDisposable(scoped)
69
+ const disposable = fn(...a)
70
+ return this.keep(disposable)
56
71
  }
57
72
  }
58
73
 
59
- /** augment an async fn to register its returned disposable item */
60
- fnAsyncDisposable<Params extends any[], Item extends Disposable>(fn: (...params: Params) => Promise<Item>) {
74
+ /** wrap an async fn, auto-add returned disposables */
75
+ keepFnAsync<Params extends any[], D extends Disposable>(
76
+ fn: (...params: Params) => Promise<D>
77
+ ) {
61
78
  return async(...a: Params) => {
62
- const scoped = await fn(...a)
63
- return this.registerDisposable(scoped)
79
+ const disposable = await fn(...a)
80
+ return this.keep(disposable)
64
81
  }
65
82
  }
66
83
 
67
- /** create a subscope that is registered, so disposing the parent will dispose the children */
84
+ /** wrap a constructor, auto-add returned disposables */
85
+ keepConstructor<C extends Constructor<Disposable>>(Ctor: C) {
86
+ const scope = this
87
+ return class extends Ctor {
88
+ constructor(...p: any[]) {
89
+ super(...p)
90
+ scope.keep(this)
91
+ }
92
+ } as C
93
+ }
94
+
95
+ /** create a subscope, child gets disposed when parent does */
68
96
  sub() {
69
- const scope = new Scope()
70
- return this.reg(scope, () => scope.dispose())
97
+ const subscope = new Scope()
98
+ return this.stow(subscope, () => subscope.dispose())
71
99
  }
72
100
 
73
- /** dispose all the disposers added to this scope */
101
+ /** dispose everything in this scope, in reverse order */
74
102
  dispose() {
75
103
  for (const fn of this.#disposers.reverse())
76
104
  fn()
@@ -1 +1,3 @@
1
1
  export type Constructor<T extends {} = {}> = new (...args: any[]) => T;
2
+ export type Ctor<Params extends any[] = any[], Instance extends {} = {}> = new (...params: Params) => Instance;
3
+ export declare function denew<Params extends any[] = any[], Instance extends {} = {}>(C: Ctor<Params, Instance>): (...p: Params) => Instance;
package/x/constructor.js CHANGED
@@ -1,2 +1,4 @@
1
- export {};
1
+ export function denew(C) {
2
+ return (...p) => new C(...p);
3
+ }
2
4
  //# sourceMappingURL=constructor.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constructor.js","sourceRoot":"","sources":["../s/constructor.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"constructor.js","sourceRoot":"","sources":["../s/constructor.ts"],"names":[],"mappings":"AAQA,MAAM,UAAU,KAAK,CAGlB,CAAyB;IAC3B,OAAO,CAAC,GAAG,CAAS,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;AACrC,CAAC"}
package/x/scope.d.ts CHANGED
@@ -1,28 +1,37 @@
1
+ import { Constructor } from "./constructor.js";
1
2
  export type Scoped<Item> = [item: Item, dispose: () => void];
2
3
  export type Disposable = {
3
4
  dispose(): void;
4
5
  };
5
6
  export declare function scoped<Item>(item: Item, dispose: () => void): Scoped<Item>;
6
- export declare class Scope {
7
+ /**
8
+ * a trashcan you can fill with garbage, then call `scope.dispose()` to dump it all
9
+ * - `add/stow/scoped` methods are for dealing with disposer fns
10
+ * - `keep` methods are for dealing with disposable objects (with a dispose method)
11
+ * - `sub` for creating nested sub-scopes
12
+ */
13
+ export declare class Scope implements Disposable {
7
14
  #private;
8
- /** add a dispose fn */
9
- add(dispose: () => void): void;
10
- /** add a dispose fn, and return the item */
11
- reg<Item>(item: Item, dispose: () => void): Item;
12
- /** add a scoped item's disposer, and return the item */
13
- register<Item>([item, dispose]: Scoped<Item>): Item;
14
- /** register and return disposable item */
15
- registerDisposable<Item extends Disposable>(item: Item): Item;
16
- /** augment a fn to register its returned scoped item */
17
- fn<Params extends any[], Item>(fn: (...params: Params) => Scoped<Item>): (...a: Params) => Item;
18
- /** augment an async fn to register its returned scoped item */
19
- fnAsync<Params extends any[], Item>(fn: (...params: Params) => Promise<Scoped<Item>>): (...a: Params) => Promise<Item>;
20
- /** augment a fn to register its returned disposable item */
21
- fnDisposable<Params extends any[], Item extends Disposable>(fn: (...params: Params) => Item): (...a: Params) => Item;
22
- /** augment an async fn to register its returned disposable item */
23
- fnAsyncDisposable<Params extends any[], Item extends Disposable>(fn: (...params: Params) => Promise<Item>): (...a: Params) => Promise<Item>;
24
- /** create a subscope that is registered, so disposing the parent will dispose the children */
15
+ /** add disposer fn */
16
+ add(dispose: () => void): this;
17
+ /** add disposer, return item */
18
+ stow<Item>(item: Item, dispose: () => void): Item;
19
+ /** add scoped disposer, return item */
20
+ scoped<Item>([item, dispose]: Scoped<Item>): Item;
21
+ /** add and return a disposable object */
22
+ keep<D extends Disposable>(disposable: D): D;
23
+ /** wrap a fn, auto-add returned disposers */
24
+ scopedFn<Params extends any[], Item>(fn: (...params: Params) => Scoped<Item>): (...a: Params) => Item;
25
+ /** wrap an async fn, auto-add returned disposers */
26
+ scopedFnAsync<Params extends any[], Item>(fn: (...params: Params) => Promise<Scoped<Item>>): (...a: Params) => Promise<Item>;
27
+ /** wrap a fn, auto-add returned disposables */
28
+ keepFn<Params extends any[], D extends Disposable>(fn: (...params: Params) => D): (...a: Params) => D;
29
+ /** wrap an async fn, auto-add returned disposables */
30
+ keepFnAsync<Params extends any[], D extends Disposable>(fn: (...params: Params) => Promise<D>): (...a: Params) => Promise<D>;
31
+ /** wrap a constructor, auto-add returned disposables */
32
+ keepConstructor<C extends Constructor<Disposable>>(Ctor: C): C;
33
+ /** create a subscope, child gets disposed when parent does */
25
34
  sub(): Scope;
26
- /** dispose all the disposers added to this scope */
35
+ /** dispose everything in this scope, in reverse order */
27
36
  dispose(): void;
28
37
  }
package/x/scope.js CHANGED
@@ -1,61 +1,78 @@
1
1
  export function scoped(item, dispose) {
2
2
  return [item, dispose];
3
3
  }
4
+ /**
5
+ * a trashcan you can fill with garbage, then call `scope.dispose()` to dump it all
6
+ * - `add/stow/scoped` methods are for dealing with disposer fns
7
+ * - `keep` methods are for dealing with disposable objects (with a dispose method)
8
+ * - `sub` for creating nested sub-scopes
9
+ */
4
10
  export class Scope {
5
11
  #disposers = [];
6
- /** add a dispose fn */
12
+ /** add disposer fn */
7
13
  add(dispose) {
8
14
  this.#disposers.push(dispose);
15
+ return this;
9
16
  }
10
- /** add a dispose fn, and return the item */
11
- reg(item, dispose) {
17
+ /** add disposer, return item */
18
+ stow(item, dispose) {
12
19
  this.add(dispose);
13
20
  return item;
14
21
  }
15
- /** add a scoped item's disposer, and return the item */
16
- register([item, dispose]) {
22
+ /** add scoped disposer, return item */
23
+ scoped([item, dispose]) {
17
24
  this.add(dispose);
18
25
  return item;
19
26
  }
20
- /** register and return disposable item */
21
- registerDisposable(item) {
22
- this.#disposers.push(() => item.dispose);
23
- return item;
27
+ /** add and return a disposable object */
28
+ keep(disposable) {
29
+ this.add(() => disposable.dispose());
30
+ return disposable;
24
31
  }
25
- /** augment a fn to register its returned scoped item */
26
- fn(fn) {
32
+ /** wrap a fn, auto-add returned disposers */
33
+ scopedFn(fn) {
27
34
  return (...a) => {
28
35
  const scoped = fn(...a);
29
- return this.register(scoped);
36
+ return this.scoped(scoped);
30
37
  };
31
38
  }
32
- /** augment an async fn to register its returned scoped item */
33
- fnAsync(fn) {
39
+ /** wrap an async fn, auto-add returned disposers */
40
+ scopedFnAsync(fn) {
34
41
  return async (...a) => {
35
42
  const scoped = await fn(...a);
36
- return this.register(scoped);
43
+ return this.scoped(scoped);
37
44
  };
38
45
  }
39
- /** augment a fn to register its returned disposable item */
40
- fnDisposable(fn) {
46
+ /** wrap a fn, auto-add returned disposables */
47
+ keepFn(fn) {
41
48
  return (...a) => {
42
- const scoped = fn(...a);
43
- return this.registerDisposable(scoped);
49
+ const disposable = fn(...a);
50
+ return this.keep(disposable);
44
51
  };
45
52
  }
46
- /** augment an async fn to register its returned disposable item */
47
- fnAsyncDisposable(fn) {
53
+ /** wrap an async fn, auto-add returned disposables */
54
+ keepFnAsync(fn) {
48
55
  return async (...a) => {
49
- const scoped = await fn(...a);
50
- return this.registerDisposable(scoped);
56
+ const disposable = await fn(...a);
57
+ return this.keep(disposable);
58
+ };
59
+ }
60
+ /** wrap a constructor, auto-add returned disposables */
61
+ keepConstructor(Ctor) {
62
+ const scope = this;
63
+ return class extends Ctor {
64
+ constructor(...p) {
65
+ super(...p);
66
+ scope.keep(this);
67
+ }
51
68
  };
52
69
  }
53
- /** create a subscope that is registered, so disposing the parent will dispose the children */
70
+ /** create a subscope, child gets disposed when parent does */
54
71
  sub() {
55
- const scope = new Scope();
56
- return this.reg(scope, () => scope.dispose());
72
+ const subscope = new Scope();
73
+ return this.stow(subscope, () => subscope.dispose());
57
74
  }
58
- /** dispose all the disposers added to this scope */
75
+ /** dispose everything in this scope, in reverse order */
59
76
  dispose() {
60
77
  for (const fn of this.#disposers.reverse())
61
78
  fn();
package/x/scope.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"scope.js","sourceRoot":"","sources":["../s/scope.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,MAAM,CAAO,IAAU,EAAE,OAAmB;IAC3D,OAAO,CAAC,IAAI,EAAE,OAAO,CAAiB,CAAA;AACvC,CAAC;AAED,MAAM,OAAO,KAAK;IACjB,UAAU,GAAmB,EAAE,CAAA;IAE/B,uBAAuB;IACvB,GAAG,CAAC,OAAmB;QACtB,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IAC9B,CAAC;IAED,4CAA4C;IAC5C,GAAG,CAAO,IAAU,EAAE,OAAmB;QACxC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QACjB,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,wDAAwD;IACxD,QAAQ,CAAO,CAAC,IAAI,EAAE,OAAO,CAAe;QAC3C,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QACjB,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,0CAA0C;IAC1C,kBAAkB,CAA0B,IAAU;QACrD,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QACxC,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,wDAAwD;IACxD,EAAE,CAA6B,EAAuC;QACrE,OAAO,CAAC,GAAG,CAAS,EAAE,EAAE;YACvB,MAAM,MAAM,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;YACvB,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;QA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