@routinejs/core 0.0.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/LICENSE +21 -0
- package/dist/index.d.cts +73 -0
- package/dist/index.d.ts +73 -0
- package/dist/routinejs-core.cjs +1 -0
- package/dist/routinejs-core.js +142 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Jason Nall
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export declare type AllowedYield<Defs extends EventDefs> = typeof INDEFINITELY | EventReq<EventUnion<Defs>>;
|
|
2
|
+
|
|
3
|
+
export declare class ChildScope implements Disposable {
|
|
4
|
+
#private;
|
|
5
|
+
disposed: boolean;
|
|
6
|
+
constructor(make: () => RoutineGen, runner?: Runner);
|
|
7
|
+
resume(value?: any): void;
|
|
8
|
+
[Symbol.dispose](): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export declare function defaultRunner(): Runner;
|
|
12
|
+
|
|
13
|
+
/** Create a RoutineEmitter from a subscribe function. */
|
|
14
|
+
export declare function emitter<T>(subscribe: (cb: (ev: T) => void) => Disposable): RoutineEmitter<T>;
|
|
15
|
+
|
|
16
|
+
declare const EVENT_REQ: unique symbol;
|
|
17
|
+
|
|
18
|
+
export declare type EventDefs = Record<string, RoutineEmitter<any>>;
|
|
19
|
+
|
|
20
|
+
declare type EventPayload<E> = E extends RoutineEmitter<infer T> ? T : never;
|
|
21
|
+
|
|
22
|
+
/** Yield token: "wait for one event of type T" */
|
|
23
|
+
export declare type EventReq<T> = {
|
|
24
|
+
readonly [EVENT_REQ]: T;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export declare type EventRoutine<T extends RoutineNode, Defs extends EventDefs> = {
|
|
28
|
+
run(root: T): void;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export declare function eventRoutine<T extends RoutineNode, Defs extends EventDefs>(defs: Defs, make: (opts: unknown, ctx: Defs & {
|
|
32
|
+
root: T;
|
|
33
|
+
}) => Generator<AllowedYield<Defs>, void, any>): EventRoutine<T, Defs>;
|
|
34
|
+
|
|
35
|
+
declare type EventUnion<Defs extends EventDefs> = EventPayload<Defs[keyof Defs]>;
|
|
36
|
+
|
|
37
|
+
export declare const INDEFINITELY: unique symbol;
|
|
38
|
+
|
|
39
|
+
export declare function isDisposable(x: any): x is Disposable;
|
|
40
|
+
|
|
41
|
+
export declare type Routine<T extends RoutineNode> = {
|
|
42
|
+
run(root: T): void;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export declare function routine<T extends RoutineNode>(make: (root: T) => Generator<any, void, any>): Routine<T>;
|
|
46
|
+
|
|
47
|
+
/** Routine event emitter (runtime subscribes, resumes generator on first event) */
|
|
48
|
+
export declare type RoutineEmitter<T> = {
|
|
49
|
+
_subscribe(cb: (ev: T) => void): Disposable;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** A routine generator. */
|
|
53
|
+
export declare type RoutineGen = Generator<Yieldable, void, any>;
|
|
54
|
+
|
|
55
|
+
export declare abstract class RoutineNode implements Disposable {
|
|
56
|
+
#private;
|
|
57
|
+
own<T extends Disposable | AsyncDisposable | null | undefined>(d: T): T;
|
|
58
|
+
child(make: () => RoutineGen): ChildScope;
|
|
59
|
+
[Symbol.dispose](): void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export declare interface Runner {
|
|
63
|
+
attach(gen: RoutineGen): void;
|
|
64
|
+
step(gen: RoutineGen, resumeValue?: any): void;
|
|
65
|
+
disposeGen(gen: RoutineGen): void;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Create a typed request token; yield this to receive a single event payload. */
|
|
69
|
+
export declare function waitFor<T>(e: RoutineEmitter<T>): EventReq<T>;
|
|
70
|
+
|
|
71
|
+
export declare type Yieldable = typeof INDEFINITELY | EventReq<any>;
|
|
72
|
+
|
|
73
|
+
export { }
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export declare type AllowedYield<Defs extends EventDefs> = typeof INDEFINITELY | EventReq<EventUnion<Defs>>;
|
|
2
|
+
|
|
3
|
+
export declare class ChildScope implements Disposable {
|
|
4
|
+
#private;
|
|
5
|
+
disposed: boolean;
|
|
6
|
+
constructor(make: () => RoutineGen, runner?: Runner);
|
|
7
|
+
resume(value?: any): void;
|
|
8
|
+
[Symbol.dispose](): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export declare function defaultRunner(): Runner;
|
|
12
|
+
|
|
13
|
+
/** Create a RoutineEmitter from a subscribe function. */
|
|
14
|
+
export declare function emitter<T>(subscribe: (cb: (ev: T) => void) => Disposable): RoutineEmitter<T>;
|
|
15
|
+
|
|
16
|
+
declare const EVENT_REQ: unique symbol;
|
|
17
|
+
|
|
18
|
+
export declare type EventDefs = Record<string, RoutineEmitter<any>>;
|
|
19
|
+
|
|
20
|
+
declare type EventPayload<E> = E extends RoutineEmitter<infer T> ? T : never;
|
|
21
|
+
|
|
22
|
+
/** Yield token: "wait for one event of type T" */
|
|
23
|
+
export declare type EventReq<T> = {
|
|
24
|
+
readonly [EVENT_REQ]: T;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export declare type EventRoutine<T extends RoutineNode, Defs extends EventDefs> = {
|
|
28
|
+
run(root: T): void;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export declare function eventRoutine<T extends RoutineNode, Defs extends EventDefs>(defs: Defs, make: (opts: unknown, ctx: Defs & {
|
|
32
|
+
root: T;
|
|
33
|
+
}) => Generator<AllowedYield<Defs>, void, any>): EventRoutine<T, Defs>;
|
|
34
|
+
|
|
35
|
+
declare type EventUnion<Defs extends EventDefs> = EventPayload<Defs[keyof Defs]>;
|
|
36
|
+
|
|
37
|
+
export declare const INDEFINITELY: unique symbol;
|
|
38
|
+
|
|
39
|
+
export declare function isDisposable(x: any): x is Disposable;
|
|
40
|
+
|
|
41
|
+
export declare type Routine<T extends RoutineNode> = {
|
|
42
|
+
run(root: T): void;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export declare function routine<T extends RoutineNode>(make: (root: T) => Generator<any, void, any>): Routine<T>;
|
|
46
|
+
|
|
47
|
+
/** Routine event emitter (runtime subscribes, resumes generator on first event) */
|
|
48
|
+
export declare type RoutineEmitter<T> = {
|
|
49
|
+
_subscribe(cb: (ev: T) => void): Disposable;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** A routine generator. */
|
|
53
|
+
export declare type RoutineGen = Generator<Yieldable, void, any>;
|
|
54
|
+
|
|
55
|
+
export declare abstract class RoutineNode implements Disposable {
|
|
56
|
+
#private;
|
|
57
|
+
own<T extends Disposable | AsyncDisposable | null | undefined>(d: T): T;
|
|
58
|
+
child(make: () => RoutineGen): ChildScope;
|
|
59
|
+
[Symbol.dispose](): void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export declare interface Runner {
|
|
63
|
+
attach(gen: RoutineGen): void;
|
|
64
|
+
step(gen: RoutineGen, resumeValue?: any): void;
|
|
65
|
+
disposeGen(gen: RoutineGen): void;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Create a typed request token; yield this to receive a single event payload. */
|
|
69
|
+
export declare function waitFor<T>(e: RoutineEmitter<T>): EventReq<T>;
|
|
70
|
+
|
|
71
|
+
export declare type Yieldable = typeof INDEFINITELY | EventReq<any>;
|
|
72
|
+
|
|
73
|
+
export { }
|
|
@@ -0,0 +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;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
const h = /* @__PURE__ */ Symbol("INDEFINITELY");
|
|
2
|
+
function w(e) {
|
|
3
|
+
return { _subscribe: e };
|
|
4
|
+
}
|
|
5
|
+
function m(e) {
|
|
6
|
+
return e;
|
|
7
|
+
}
|
|
8
|
+
function f() {
|
|
9
|
+
const e = /* @__PURE__ */ new WeakMap();
|
|
10
|
+
function t(s) {
|
|
11
|
+
let n = e.get(s);
|
|
12
|
+
return n || (n = { stack: new DisposableStack(), parked: !1 }, e.set(s, n)), n;
|
|
13
|
+
}
|
|
14
|
+
function i(s) {
|
|
15
|
+
s.waiting = void 0;
|
|
16
|
+
}
|
|
17
|
+
const o = {
|
|
18
|
+
attach(s) {
|
|
19
|
+
t(s);
|
|
20
|
+
},
|
|
21
|
+
step(s, n) {
|
|
22
|
+
const r = t(s);
|
|
23
|
+
if (r.parked) return;
|
|
24
|
+
i(r);
|
|
25
|
+
let u;
|
|
26
|
+
try {
|
|
27
|
+
u = s.next(n);
|
|
28
|
+
} catch (c) {
|
|
29
|
+
throw o.disposeGen(s), c;
|
|
30
|
+
}
|
|
31
|
+
if (u.done) {
|
|
32
|
+
o.disposeGen(s);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const l = u.value;
|
|
36
|
+
if (l === h) {
|
|
37
|
+
r.parked = !0;
|
|
38
|
+
return;
|
|
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;
|
|
46
|
+
try {
|
|
47
|
+
p[Symbol.dispose]();
|
|
48
|
+
} catch {
|
|
49
|
+
}
|
|
50
|
+
o.step(s, d);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
r.waiting = { unsub: p, active: !0 }, r.stack.use(p);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
throw new Error(
|
|
57
|
+
"Invalid yield: only INDEFINITELY or EventReq<T> are allowed."
|
|
58
|
+
);
|
|
59
|
+
},
|
|
60
|
+
disposeGen(s) {
|
|
61
|
+
const n = e.get(s);
|
|
62
|
+
if (n) {
|
|
63
|
+
e.delete(s);
|
|
64
|
+
try {
|
|
65
|
+
s.return?.();
|
|
66
|
+
} catch {
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
n.stack.dispose();
|
|
70
|
+
} catch {
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
return o;
|
|
76
|
+
}
|
|
77
|
+
class y {
|
|
78
|
+
#t;
|
|
79
|
+
#e;
|
|
80
|
+
disposed = !1;
|
|
81
|
+
constructor(t, i) {
|
|
82
|
+
this.#t = i ?? f(), this.#e = t(), this.#t.attach(this.#e), this.#t.step(this.#e);
|
|
83
|
+
}
|
|
84
|
+
resume(t) {
|
|
85
|
+
this.disposed || this.#t.step(this.#e, t);
|
|
86
|
+
}
|
|
87
|
+
[Symbol.dispose]() {
|
|
88
|
+
this.disposed || (this.disposed = !0, this.#t.disposeGen(this.#e));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function b(e) {
|
|
92
|
+
return e && typeof e == "object" && typeof e[Symbol.dispose] == "function";
|
|
93
|
+
}
|
|
94
|
+
class k {
|
|
95
|
+
#t = new DisposableStack();
|
|
96
|
+
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;
|
|
109
|
+
}
|
|
110
|
+
child(t) {
|
|
111
|
+
const i = new y(t);
|
|
112
|
+
return this.own(i), i;
|
|
113
|
+
}
|
|
114
|
+
[Symbol.dispose]() {
|
|
115
|
+
this.#t.dispose();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function I(e) {
|
|
119
|
+
return {
|
|
120
|
+
run(t) {
|
|
121
|
+
t.child(() => e(t));
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function S(e, t) {
|
|
126
|
+
return {
|
|
127
|
+
run(i) {
|
|
128
|
+
i.child(() => t(void 0, { ...e, root: i }));
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
export {
|
|
133
|
+
y as ChildScope,
|
|
134
|
+
h as INDEFINITELY,
|
|
135
|
+
k as RoutineNode,
|
|
136
|
+
f as defaultRunner,
|
|
137
|
+
w as emitter,
|
|
138
|
+
S as eventRoutine,
|
|
139
|
+
b as isDisposable,
|
|
140
|
+
I as routine,
|
|
141
|
+
m as waitFor
|
|
142
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@routinejs/core",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Express ownership graphs and control flow together, with automatic resource management.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/jsonnull/routinejs",
|
|
9
|
+
"directory": "packages/core"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"main": "./dist/routinejs-core.cjs",
|
|
19
|
+
"module": "./dist/routinejs-core.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/routinejs-core.js"
|
|
26
|
+
},
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./dist/index.d.cts",
|
|
29
|
+
"default": "./dist/routinejs-core.cjs"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"typescript": "^5.9.3",
|
|
35
|
+
"vite": "^7.3.1",
|
|
36
|
+
"vite-plugin-dts": "^4.5.4",
|
|
37
|
+
"vitest": "^4.0.18"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "vite build",
|
|
41
|
+
"dev": "vite build --watch",
|
|
42
|
+
"test": "vitest run",
|
|
43
|
+
"test:watch": "vitest",
|
|
44
|
+
"test:typecheck": "vitest --typecheck.only --typecheck.include='tests/**/*.test-d.ts' --run"
|
|
45
|
+
}
|
|
46
|
+
}
|