@routinejs/core 0.0.1 → 0.0.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/dist/index.d.cts CHANGED
@@ -1,5 +1,44 @@
1
1
  export declare type AllowedYield<Defs extends EventDefs> = typeof INDEFINITELY | EventReq<EventUnion<Defs>>;
2
2
 
3
+ export declare class AsyncChildScope implements AsyncDisposable {
4
+ #private;
5
+ readonly ready: Promise<void>;
6
+ disposed: boolean;
7
+ constructor(make: () => AsyncRoutineGen, runner?: AsyncRunner);
8
+ resume(value?: any): Promise<void>;
9
+ [Symbol.asyncDispose](): Promise<void>;
10
+ }
11
+
12
+ export declare type AsyncEventRoutine<T extends AsyncRoutineNode, Defs extends EventDefs> = {
13
+ run(root: T): Promise<void>;
14
+ };
15
+
16
+ export declare function asyncEventRoutine<T extends AsyncRoutineNode, Defs extends EventDefs>(defs: Defs, make: (opts: unknown, ctx: Defs & {
17
+ root: T;
18
+ }) => AsyncGenerator<AllowedYield<Defs>, void, any>): AsyncEventRoutine<T, Defs>;
19
+
20
+ export declare type AsyncRoutine<T extends AsyncRoutineNode> = {
21
+ run(root: T): Promise<void>;
22
+ };
23
+
24
+ export declare function asyncRoutine<T extends AsyncRoutineNode>(make: (root: T) => AsyncGenerator<any, void, any>): AsyncRoutine<T>;
25
+
26
+ /** An async routine generator. */
27
+ export declare type AsyncRoutineGen = AsyncGenerator<Yieldable, void, any>;
28
+
29
+ export declare abstract class AsyncRoutineNode implements AsyncDisposable {
30
+ #private;
31
+ own<T extends Disposable | AsyncDisposable | null | undefined>(d: T): T;
32
+ child(make: () => AsyncRoutineGen): Promise<AsyncChildScope>;
33
+ [Symbol.asyncDispose](): Promise<void>;
34
+ }
35
+
36
+ export declare interface AsyncRunner {
37
+ attach(gen: AsyncRoutineGen): void;
38
+ step(gen: AsyncRoutineGen, resumeValue?: any): Promise<void>;
39
+ disposeGen(gen: AsyncRoutineGen): Promise<void>;
40
+ }
41
+
3
42
  export declare class ChildScope implements Disposable {
4
43
  #private;
5
44
  disposed: boolean;
@@ -8,6 +47,8 @@ export declare class ChildScope implements Disposable {
8
47
  [Symbol.dispose](): void;
9
48
  }
10
49
 
50
+ export declare function defaultAsyncRunner(): AsyncRunner;
51
+
11
52
  export declare function defaultRunner(): Runner;
12
53
 
13
54
  /** Create a RoutineEmitter from a subscribe function. */
@@ -36,6 +77,8 @@ declare type EventUnion<Defs extends EventDefs> = EventPayload<Defs[keyof Defs]>
36
77
 
37
78
  export declare const INDEFINITELY: unique symbol;
38
79
 
80
+ export declare function isAsyncDisposable(x: any): x is AsyncDisposable;
81
+
39
82
  export declare function isDisposable(x: any): x is Disposable;
40
83
 
41
84
  export declare type Routine<T extends RoutineNode> = {
@@ -54,7 +97,7 @@ export declare type RoutineGen = Generator<Yieldable, void, any>;
54
97
 
55
98
  export declare abstract class RoutineNode implements Disposable {
56
99
  #private;
57
- own<T extends Disposable | AsyncDisposable | null | undefined>(d: T): T;
100
+ own<T extends Disposable | null | undefined>(d: T): T;
58
101
  child(make: () => RoutineGen): ChildScope;
59
102
  [Symbol.dispose](): void;
60
103
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,44 @@
1
1
  export declare type AllowedYield<Defs extends EventDefs> = typeof INDEFINITELY | EventReq<EventUnion<Defs>>;
2
2
 
3
+ export declare class AsyncChildScope implements AsyncDisposable {
4
+ #private;
5
+ readonly ready: Promise<void>;
6
+ disposed: boolean;
7
+ constructor(make: () => AsyncRoutineGen, runner?: AsyncRunner);
8
+ resume(value?: any): Promise<void>;
9
+ [Symbol.asyncDispose](): Promise<void>;
10
+ }
11
+
12
+ export declare type AsyncEventRoutine<T extends AsyncRoutineNode, Defs extends EventDefs> = {
13
+ run(root: T): Promise<void>;
14
+ };
15
+
16
+ export declare function asyncEventRoutine<T extends AsyncRoutineNode, Defs extends EventDefs>(defs: Defs, make: (opts: unknown, ctx: Defs & {
17
+ root: T;
18
+ }) => AsyncGenerator<AllowedYield<Defs>, void, any>): AsyncEventRoutine<T, Defs>;
19
+
20
+ export declare type AsyncRoutine<T extends AsyncRoutineNode> = {
21
+ run(root: T): Promise<void>;
22
+ };
23
+
24
+ export declare function asyncRoutine<T extends AsyncRoutineNode>(make: (root: T) => AsyncGenerator<any, void, any>): AsyncRoutine<T>;
25
+
26
+ /** An async routine generator. */
27
+ export declare type AsyncRoutineGen = AsyncGenerator<Yieldable, void, any>;
28
+
29
+ export declare abstract class AsyncRoutineNode implements AsyncDisposable {
30
+ #private;
31
+ own<T extends Disposable | AsyncDisposable | null | undefined>(d: T): T;
32
+ child(make: () => AsyncRoutineGen): Promise<AsyncChildScope>;
33
+ [Symbol.asyncDispose](): Promise<void>;
34
+ }
35
+
36
+ export declare interface AsyncRunner {
37
+ attach(gen: AsyncRoutineGen): void;
38
+ step(gen: AsyncRoutineGen, resumeValue?: any): Promise<void>;
39
+ disposeGen(gen: AsyncRoutineGen): Promise<void>;
40
+ }
41
+
3
42
  export declare class ChildScope implements Disposable {
4
43
  #private;
5
44
  disposed: boolean;
@@ -8,6 +47,8 @@ export declare class ChildScope implements Disposable {
8
47
  [Symbol.dispose](): void;
9
48
  }
10
49
 
50
+ export declare function defaultAsyncRunner(): AsyncRunner;
51
+
11
52
  export declare function defaultRunner(): Runner;
12
53
 
13
54
  /** Create a RoutineEmitter from a subscribe function. */
@@ -36,6 +77,8 @@ declare type EventUnion<Defs extends EventDefs> = EventPayload<Defs[keyof Defs]>
36
77
 
37
78
  export declare const INDEFINITELY: unique symbol;
38
79
 
80
+ export declare function isAsyncDisposable(x: any): x is AsyncDisposable;
81
+
39
82
  export declare function isDisposable(x: any): x is Disposable;
40
83
 
41
84
  export declare type Routine<T extends RoutineNode> = {
@@ -54,7 +97,7 @@ export declare type RoutineGen = Generator<Yieldable, void, any>;
54
97
 
55
98
  export declare abstract class RoutineNode implements Disposable {
56
99
  #private;
57
- own<T extends Disposable | AsyncDisposable | null | undefined>(d: T): T;
100
+ own<T extends Disposable | null | undefined>(d: T): T;
58
101
  child(make: () => RoutineGen): ChildScope;
59
102
  [Symbol.dispose](): void;
60
103
  }
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=Symbol("INDEFINITELY");function w(e){return{_subscribe:e}}function m(e){return e}function f(){const e=new WeakMap;function t(s){let n=e.get(s);return n||(n={stack:new DisposableStack,parked:!1},e.set(s,n)),n}function i(s){s.waiting=void 0}const o={attach(s){t(s)},step(s,n){const r=t(s);if(r.parked)return;i(r);let c;try{c=s.next(n)}catch(u){throw o.disposeGen(s),u}if(c.done){o.disposeGen(s);return}const d=c.value;if(d===p){r.parked=!0;return}const a=d;if(a&&typeof a._subscribe=="function"){let u=!0;const l=a._subscribe(y=>{if(u){u=!1;try{l[Symbol.dispose]()}catch{}o.step(s,y)}});r.waiting={unsub:l,active:!0},r.stack.use(l);return}throw new Error("Invalid yield: only INDEFINITELY or EventReq<T> are allowed.")},disposeGen(s){const n=e.get(s);if(n){e.delete(s);try{s.return?.()}catch{}try{n.stack.dispose()}catch{}}}};return o}class h{#t;#e;disposed=!1;constructor(t,i){this.#t=i??f(),this.#e=t(),this.#t.attach(this.#e),this.#t.step(this.#e)}resume(t){this.disposed||this.#t.step(this.#e,t)}[Symbol.dispose](){this.disposed||(this.disposed=!0,this.#t.disposeGen(this.#e))}}function b(e){return e&&typeof e=="object"&&typeof e[Symbol.dispose]=="function"}class I{#t=new DisposableStack;own(t){if(!t)return t;if(b(t))this.#t.use(t);else{const i=t;this.#t.use({[Symbol.dispose](){i[Symbol.asyncDispose]()}})}return t}child(t){const i=new h(t);return this.own(i),i}[Symbol.dispose](){this.#t.dispose()}}function S(e){return{run(t){t.child(()=>e(t))}}}function v(e,t){return{run(i){i.child(()=>t(void 0,{...e,root:i}))}}}exports.ChildScope=h;exports.INDEFINITELY=p;exports.RoutineNode=I;exports.defaultRunner=f;exports.emitter=w;exports.eventRoutine=v;exports.isDisposable=b;exports.routine=S;exports.waitFor=m;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=Symbol("INDEFINITELY");function S(e){return{_subscribe:e}}function m(e){return e}function h(){const e=new WeakMap;function t(s){let n=e.get(s);return n||(n={stack:new DisposableStack,parked:!1},e.set(s,n)),n}function i(s){s.waiting=void 0}const o={attach(s){t(s)},step(s,n){const r=t(s);if(r.parked)return;i(r);let c;try{c=s.next(n)}catch(a){throw o.disposeGen(s),a}if(c.done){o.disposeGen(s);return}const l=c.value;if(l===y){r.parked=!0;return}const u=l;if(u&&typeof u._subscribe=="function"){let a=!0;const d=u._subscribe(p=>{if(a){a=!1;try{d[Symbol.dispose]()}catch{}o.step(s,p)}});r.waiting={unsub:d,active:!0},r.stack.use(d);return}throw new Error("Invalid yield: only INDEFINITELY or EventReq<T> are allowed.")},disposeGen(s){const n=e.get(s);if(n){e.delete(s);try{s.return?.()}catch{}try{n.stack.dispose()}catch{}}}};return o}function f(){const e=new WeakMap;function t(s){let n=e.get(s);return n||(n={stack:new DisposableStack,parked:!1},e.set(s,n)),n}function i(s){s.waiting=void 0}const o={attach(s){t(s)},async step(s,n){const r=t(s);if(r.parked)return;i(r);let c;try{c=await s.next(n)}catch(a){throw await o.disposeGen(s),a}if(c.done){await o.disposeGen(s);return}const l=c.value;if(l===y){r.parked=!0;return}const u=l;if(u&&typeof u._subscribe=="function"){let a=!0;const d=u._subscribe(p=>{if(a){a=!1;try{d[Symbol.dispose]()}catch{}o.step(s,p)}});r.waiting={unsub:d,active:!0},r.stack.use(d);return}throw new Error("Invalid yield: only INDEFINITELY or EventReq<T> are allowed.")},async disposeGen(s){const n=e.get(s);if(n){e.delete(s);try{await s.return(void 0)}catch{}try{n.stack.dispose()}catch{}}}};return o}class w{#t;#e;disposed=!1;constructor(t,i){this.#t=i??h(),this.#e=t(),this.#t.attach(this.#e),this.#t.step(this.#e)}resume(t){this.disposed||this.#t.step(this.#e,t)}[Symbol.dispose](){this.disposed||(this.disposed=!0,this.#t.disposeGen(this.#e))}}class b{#t;#e;ready;disposed=!1;constructor(t,i){this.#t=i??f(),this.#e=t(),this.#t.attach(this.#e),this.ready=this.#t.step(this.#e)}async resume(t){this.disposed||await this.#t.step(this.#e,t)}async[Symbol.asyncDispose](){this.disposed||(this.disposed=!0,await this.#t.disposeGen(this.#e))}}function k(e){return e&&typeof e=="object"&&typeof e[Symbol.dispose]=="function"}function v(e){return e&&typeof e=="object"&&typeof e[Symbol.asyncDispose]=="function"}class E{#t=new DisposableStack;own(t){return t&&(this.#t.use(t),t)}child(t){const i=new w(t);return this.own(i),i}[Symbol.dispose](){this.#t.dispose()}}class R{#t=new AsyncDisposableStack;own(t){return t&&(v(t)?this.#t.use(t):k(t)&&this.#t.use(t),t)}async child(t){const i=new b(t);return await i.ready,this.own(i),i}async[Symbol.asyncDispose](){await this.#t.disposeAsync()}}function I(e){return{run(t){t.child(()=>e(t))}}}function D(e,t){return{run(i){i.child(()=>t(void 0,{...e,root:i}))}}}function N(e){return{async run(t){await t.child(()=>e(t))}}}function A(e,t){return{async run(i){await i.child(()=>t(void 0,{...e,root:i}))}}}exports.AsyncChildScope=b;exports.AsyncRoutineNode=R;exports.ChildScope=w;exports.INDEFINITELY=y;exports.RoutineNode=E;exports.asyncEventRoutine=A;exports.asyncRoutine=N;exports.defaultAsyncRunner=f;exports.defaultRunner=h;exports.emitter=S;exports.eventRoutine=D;exports.isAsyncDisposable=v;exports.isDisposable=k;exports.routine=I;exports.waitFor=m;
@@ -1,8 +1,8 @@
1
- const h = /* @__PURE__ */ Symbol("INDEFINITELY");
2
- function w(e) {
1
+ const l = /* @__PURE__ */ Symbol("INDEFINITELY");
2
+ function m(e) {
3
3
  return { _subscribe: e };
4
4
  }
5
- function m(e) {
5
+ function E(e) {
6
6
  return e;
7
7
  }
8
8
  function f() {
@@ -22,32 +22,32 @@ function f() {
22
22
  const r = t(s);
23
23
  if (r.parked) return;
24
24
  i(r);
25
- let u;
25
+ let c;
26
26
  try {
27
- u = s.next(n);
28
- } catch (c) {
29
- throw o.disposeGen(s), c;
27
+ c = s.next(n);
28
+ } catch (a) {
29
+ throw o.disposeGen(s), a;
30
30
  }
31
- if (u.done) {
31
+ if (c.done) {
32
32
  o.disposeGen(s);
33
33
  return;
34
34
  }
35
- const l = u.value;
36
- if (l === h) {
35
+ const d = c.value;
36
+ if (d === l) {
37
37
  r.parked = !0;
38
38
  return;
39
39
  }
40
- const a = l;
41
- if (a && typeof a._subscribe == "function") {
42
- let c = !0;
43
- const p = a._subscribe((d) => {
44
- if (c) {
45
- c = !1;
40
+ const u = d;
41
+ if (u && typeof u._subscribe == "function") {
42
+ let a = !0;
43
+ const p = u._subscribe((h) => {
44
+ if (a) {
45
+ a = !1;
46
46
  try {
47
47
  p[Symbol.dispose]();
48
48
  } catch {
49
49
  }
50
- o.step(s, d);
50
+ o.step(s, h);
51
51
  }
52
52
  });
53
53
  r.waiting = { unsub: p, active: !0 }, r.stack.use(p);
@@ -74,7 +74,76 @@ function f() {
74
74
  };
75
75
  return o;
76
76
  }
77
- class y {
77
+ function y() {
78
+ const e = /* @__PURE__ */ new WeakMap();
79
+ function t(s) {
80
+ let n = e.get(s);
81
+ return n || (n = { stack: new DisposableStack(), parked: !1 }, e.set(s, n)), n;
82
+ }
83
+ function i(s) {
84
+ s.waiting = void 0;
85
+ }
86
+ const o = {
87
+ attach(s) {
88
+ t(s);
89
+ },
90
+ async step(s, n) {
91
+ const r = t(s);
92
+ if (r.parked) return;
93
+ i(r);
94
+ let c;
95
+ try {
96
+ c = await s.next(n);
97
+ } catch (a) {
98
+ throw await o.disposeGen(s), a;
99
+ }
100
+ if (c.done) {
101
+ await o.disposeGen(s);
102
+ return;
103
+ }
104
+ const d = c.value;
105
+ if (d === l) {
106
+ r.parked = !0;
107
+ return;
108
+ }
109
+ const u = d;
110
+ if (u && typeof u._subscribe == "function") {
111
+ let a = !0;
112
+ const p = u._subscribe((h) => {
113
+ if (a) {
114
+ a = !1;
115
+ try {
116
+ p[Symbol.dispose]();
117
+ } catch {
118
+ }
119
+ o.step(s, h);
120
+ }
121
+ });
122
+ r.waiting = { unsub: p, active: !0 }, r.stack.use(p);
123
+ return;
124
+ }
125
+ throw new Error(
126
+ "Invalid yield: only INDEFINITELY or EventReq<T> are allowed."
127
+ );
128
+ },
129
+ async disposeGen(s) {
130
+ const n = e.get(s);
131
+ if (n) {
132
+ e.delete(s);
133
+ try {
134
+ await s.return(void 0);
135
+ } catch {
136
+ }
137
+ try {
138
+ n.stack.dispose();
139
+ } catch {
140
+ }
141
+ }
142
+ }
143
+ };
144
+ return o;
145
+ }
146
+ class w {
78
147
  #t;
79
148
  #e;
80
149
  disposed = !1;
@@ -88,55 +157,95 @@ class y {
88
157
  this.disposed || (this.disposed = !0, this.#t.disposeGen(this.#e));
89
158
  }
90
159
  }
91
- function b(e) {
160
+ class b {
161
+ #t;
162
+ #e;
163
+ ready;
164
+ disposed = !1;
165
+ constructor(t, i) {
166
+ this.#t = i ?? y(), this.#e = t(), this.#t.attach(this.#e), this.ready = this.#t.step(this.#e);
167
+ }
168
+ async resume(t) {
169
+ this.disposed || await this.#t.step(this.#e, t);
170
+ }
171
+ async [Symbol.asyncDispose]() {
172
+ this.disposed || (this.disposed = !0, await this.#t.disposeGen(this.#e));
173
+ }
174
+ }
175
+ function k(e) {
92
176
  return e && typeof e == "object" && typeof e[Symbol.dispose] == "function";
93
177
  }
94
- class k {
178
+ function v(e) {
179
+ return e && typeof e == "object" && typeof e[Symbol.asyncDispose] == "function";
180
+ }
181
+ class S {
95
182
  #t = new DisposableStack();
96
183
  own(t) {
97
- if (!t) return t;
98
- if (b(t))
99
- this.#t.use(t);
100
- else {
101
- const i = t;
102
- this.#t.use({
103
- [Symbol.dispose]() {
104
- i[Symbol.asyncDispose]();
105
- }
106
- });
107
- }
108
- return t;
184
+ return t && (this.#t.use(t), t);
109
185
  }
110
186
  child(t) {
111
- const i = new y(t);
187
+ const i = new w(t);
112
188
  return this.own(i), i;
113
189
  }
114
190
  [Symbol.dispose]() {
115
191
  this.#t.dispose();
116
192
  }
117
193
  }
118
- function I(e) {
194
+ class I {
195
+ #t = new AsyncDisposableStack();
196
+ own(t) {
197
+ return t && (v(t) ? this.#t.use(t) : k(t) && this.#t.use(t), t);
198
+ }
199
+ async child(t) {
200
+ const i = new b(t);
201
+ return await i.ready, this.own(i), i;
202
+ }
203
+ async [Symbol.asyncDispose]() {
204
+ await this.#t.disposeAsync();
205
+ }
206
+ }
207
+ function D(e) {
119
208
  return {
120
209
  run(t) {
121
210
  t.child(() => e(t));
122
211
  }
123
212
  };
124
213
  }
125
- function S(e, t) {
214
+ function R(e, t) {
126
215
  return {
127
216
  run(i) {
128
217
  i.child(() => t(void 0, { ...e, root: i }));
129
218
  }
130
219
  };
131
220
  }
221
+ function N(e) {
222
+ return {
223
+ async run(t) {
224
+ await t.child(() => e(t));
225
+ }
226
+ };
227
+ }
228
+ function G(e, t) {
229
+ return {
230
+ async run(i) {
231
+ await i.child(() => t(void 0, { ...e, root: i }));
232
+ }
233
+ };
234
+ }
132
235
  export {
133
- y as ChildScope,
134
- h as INDEFINITELY,
135
- k as RoutineNode,
236
+ b as AsyncChildScope,
237
+ I as AsyncRoutineNode,
238
+ w as ChildScope,
239
+ l as INDEFINITELY,
240
+ S as RoutineNode,
241
+ G as asyncEventRoutine,
242
+ N as asyncRoutine,
243
+ y as defaultAsyncRunner,
136
244
  f as defaultRunner,
137
- w as emitter,
138
- S as eventRoutine,
139
- b as isDisposable,
140
- I as routine,
141
- m as waitFor
245
+ m as emitter,
246
+ R as eventRoutine,
247
+ v as isAsyncDisposable,
248
+ k as isDisposable,
249
+ D as routine,
250
+ E as waitFor
142
251
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routinejs/core",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Express ownership graphs and control flow together, with automatic resource management.",
5
5
  "license": "MIT",
6
6
  "repository": {