@e280/stz 0.0.0-36 → 0.0.0-37

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-36",
3
+ "version": "0.0.0-37",
4
4
  "description": "everyday ts fns for everything",
5
5
  "license": "MIT",
6
6
  "author": "Chase Moskal <chasemoskal@gmail.com>",
package/s/index.ts CHANGED
@@ -35,6 +35,7 @@ export * from "./once.js"
35
35
  export * from "./pipe.js"
36
36
  export * from "./pubsub.js"
37
37
  export * from "./repeat.js"
38
+ export * from "./scope.js"
38
39
  export * from "./templating.js"
39
40
  export * from "./time.js"
40
41
  export * from "./trash.js"
package/s/scope.ts ADDED
@@ -0,0 +1,80 @@
1
+
2
+ export type Scoped<Item> = [item: Item, dispose: () => void]
3
+ export type Disposable = {dispose(): void}
4
+
5
+ export function scoped<Item>(item: Item, dispose: () => void) {
6
+ return [item, dispose] as Scoped<Item>
7
+ }
8
+
9
+ export class Scope {
10
+ #disposers: (() => void)[] = []
11
+
12
+ /** add a dispose fn */
13
+ add(dispose: () => void) {
14
+ this.#disposers.push(dispose)
15
+ }
16
+
17
+ /** add a dispose fn, and return the item */
18
+ reg<Item>(item: Item, dispose: () => void) {
19
+ this.add(dispose)
20
+ return item
21
+ }
22
+
23
+ /** add a scoped item's disposer, and return the item */
24
+ register<Item>([item, dispose]: Scoped<Item>) {
25
+ this.add(dispose)
26
+ return item
27
+ }
28
+
29
+ /** register and return disposable item */
30
+ registerDisposable<Item extends Disposable>(item: Item) {
31
+ this.#disposers.push(() => item.dispose)
32
+ return item
33
+ }
34
+
35
+ /** augment a fn to register its returned scoped item */
36
+ fn<Params extends any[], Item>(fn: (...params: Params) => Scoped<Item>) {
37
+ return (...a: Params) => {
38
+ const scoped = fn(...a)
39
+ return this.register(scoped)
40
+ }
41
+ }
42
+
43
+ /** augment an async fn to register its returned scoped item */
44
+ fnAsync<Params extends any[], Item>(fn: (...params: Params) => Promise<Scoped<Item>>) {
45
+ return async(...a: Params) => {
46
+ const scoped = await fn(...a)
47
+ return this.register(scoped)
48
+ }
49
+ }
50
+
51
+ /** augment a fn to register its returned disposable item */
52
+ fnDisposable<Params extends any[], Item extends Disposable>(fn: (...params: Params) => Item) {
53
+ return (...a: Params) => {
54
+ const scoped = fn(...a)
55
+ return this.registerDisposable(scoped)
56
+ }
57
+ }
58
+
59
+ /** augment an async fn to register its returned disposable item */
60
+ fnAsyncDisposable<Params extends any[], Item extends Disposable>(fn: (...params: Params) => Promise<Item>) {
61
+ return async(...a: Params) => {
62
+ const scoped = await fn(...a)
63
+ return this.registerDisposable(scoped)
64
+ }
65
+ }
66
+
67
+ /** create a subscope that is registered, so disposing the parent will dispose the children */
68
+ sub() {
69
+ const scope = new Scope()
70
+ return this.reg(scope, () => scope.dispose())
71
+ }
72
+
73
+ /** dispose all the disposers added to this scope */
74
+ dispose() {
75
+ for (const fn of this.#disposers.reverse())
76
+ fn()
77
+ this.#disposers = []
78
+ }
79
+ }
80
+
package/x/index.d.ts CHANGED
@@ -30,6 +30,7 @@ export * from "./once.js";
30
30
  export * from "./pipe.js";
31
31
  export * from "./pubsub.js";
32
32
  export * from "./repeat.js";
33
+ export * from "./scope.js";
33
34
  export * from "./templating.js";
34
35
  export * from "./time.js";
35
36
  export * from "./trash.js";
package/x/index.js CHANGED
@@ -30,6 +30,7 @@ export * from "./once.js";
30
30
  export * from "./pipe.js";
31
31
  export * from "./pubsub.js";
32
32
  export * from "./repeat.js";
33
+ export * from "./scope.js";
33
34
  export * from "./templating.js";
34
35
  export * from "./time.js";
35
36
  export * from "./trash.js";
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,cA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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,CAAA;AACvB,cAAc,UAAU,CAAA;AACxB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,SAAS,CAAA;AACvB,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA;AACxB,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,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,28 @@
1
+ export type Scoped<Item> = [item: Item, dispose: () => void];
2
+ export type Disposable = {
3
+ dispose(): void;
4
+ };
5
+ export declare function scoped<Item>(item: Item, dispose: () => void): Scoped<Item>;
6
+ export declare class Scope {
7
+ #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 */
25
+ sub(): Scope;
26
+ /** dispose all the disposers added to this scope */
27
+ dispose(): void;
28
+ }
package/x/scope.js ADDED
@@ -0,0 +1,65 @@
1
+ export function scoped(item, dispose) {
2
+ return [item, dispose];
3
+ }
4
+ export class Scope {
5
+ #disposers = [];
6
+ /** add a dispose fn */
7
+ add(dispose) {
8
+ this.#disposers.push(dispose);
9
+ }
10
+ /** add a dispose fn, and return the item */
11
+ reg(item, dispose) {
12
+ this.add(dispose);
13
+ return item;
14
+ }
15
+ /** add a scoped item's disposer, and return the item */
16
+ register([item, dispose]) {
17
+ this.add(dispose);
18
+ return item;
19
+ }
20
+ /** register and return disposable item */
21
+ registerDisposable(item) {
22
+ this.#disposers.push(() => item.dispose);
23
+ return item;
24
+ }
25
+ /** augment a fn to register its returned scoped item */
26
+ fn(fn) {
27
+ return (...a) => {
28
+ const scoped = fn(...a);
29
+ return this.register(scoped);
30
+ };
31
+ }
32
+ /** augment an async fn to register its returned scoped item */
33
+ fnAsync(fn) {
34
+ return async (...a) => {
35
+ const scoped = await fn(...a);
36
+ return this.register(scoped);
37
+ };
38
+ }
39
+ /** augment a fn to register its returned disposable item */
40
+ fnDisposable(fn) {
41
+ return (...a) => {
42
+ const scoped = fn(...a);
43
+ return this.registerDisposable(scoped);
44
+ };
45
+ }
46
+ /** augment an async fn to register its returned disposable item */
47
+ fnAsyncDisposable(fn) {
48
+ return async (...a) => {
49
+ const scoped = await fn(...a);
50
+ return this.registerDisposable(scoped);
51
+ };
52
+ }
53
+ /** create a subscope that is registered, so disposing the parent will dispose the children */
54
+ sub() {
55
+ const scope = new Scope();
56
+ return this.reg(scope, () => scope.dispose());
57
+ }
58
+ /** dispose all the disposers added to this scope */
59
+ dispose() {
60
+ for (const fn of this.#disposers.reverse())
61
+ fn();
62
+ this.#disposers = [];
63
+ }
64
+ }
65
+ //# 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":"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;QAC7B,CAAC,CAAA;IACF,CAAC;IAED,+DAA+D;IAC/D,OAAO,CAA6B,EAAgD;QACnF,OAAO,KAAK,EAAC,GAAG,CAAS,EAAE,EA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