posipaki 0.5.4 → 0.6.0
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/util.d.ts +2 -2
- package/dist/xfetch.d.ts +2 -2
- package/package.json +9 -3
- package/src/index.ts +2 -2
- package/src/process.ts +44 -6
- package/src/util.ts +34 -7
- package/src/xfetch.ts +2 -2
package/dist/util.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type Process from './process';
|
|
2
|
-
import type { ProcessCtx, Message } from './process';
|
|
1
|
+
import type Process from './process.js';
|
|
2
|
+
import type { ProcessCtx, Message } from './process.js';
|
|
3
3
|
type ReducerClosure<M> = (msg: M) => void;
|
|
4
4
|
type ReadyFn = () => boolean;
|
|
5
5
|
type NotifyFn = () => void;
|
package/dist/xfetch.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ExitMessage } from './util.js';
|
|
2
|
-
import type Process from './process';
|
|
3
|
-
import type { ProcessCtx } from './process';
|
|
2
|
+
import type Process from './process.js';
|
|
3
|
+
import type { ProcessCtx } from './process.js';
|
|
4
4
|
export type FetchState<T> = {
|
|
5
5
|
code: 'pending' | 'loading' | 'aborted' | 'failed' | 'ok';
|
|
6
6
|
data: T | null;
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "posipaki",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build": "tsc"
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "jest"
|
|
9
10
|
},
|
|
10
11
|
"files": [
|
|
11
12
|
"dist/*js",
|
|
@@ -14,6 +15,11 @@
|
|
|
14
15
|
"src/*ts"
|
|
15
16
|
],
|
|
16
17
|
"devDependencies": {
|
|
17
|
-
"
|
|
18
|
+
"@babel/parser": "^7.22.5",
|
|
19
|
+
"@babel/types": "^7.22.5",
|
|
20
|
+
"@types/jest": "^29.5.2",
|
|
21
|
+
"jest": "^29.5.0",
|
|
22
|
+
"ts-jest": "^29.1.0",
|
|
23
|
+
"typescript": "^5.1.3"
|
|
18
24
|
}
|
|
19
25
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import Process, { spawn, Message, ProcessCtx, ProcessFn } from './process
|
|
2
|
-
import { ExitMessage, runDispatch } from './util
|
|
1
|
+
import Process, { spawn, Message, ProcessCtx, ProcessFn } from './process';
|
|
2
|
+
import { ExitMessage, runDispatch } from './util';
|
|
3
3
|
|
|
4
4
|
export { Process, spawn, runDispatch, Message, ExitMessage, ProcessCtx, ProcessFn }
|
package/src/process.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ExitMessage, watchExit, Waiter, defer, makeWaiter } from './util
|
|
1
|
+
import { ExitMessage, watchExit, Waiter, defer, DeferredCall, makeWaiter } from './util';
|
|
2
2
|
|
|
3
3
|
export interface Message {
|
|
4
4
|
type: string;
|
|
@@ -39,9 +39,11 @@ class Process<Args, State, InMessage extends Message, OutMessage extends Message
|
|
|
39
39
|
|
|
40
40
|
private current: ProcessGenerator<State, InMessage> | null;
|
|
41
41
|
private buffer: Array<InMessage>;
|
|
42
|
+
private nextTick: DeferredCall | null;
|
|
42
43
|
private children: Array<Process<unknown, unknown, Message, Message>>;
|
|
43
44
|
private subscribers: Array<NotifyFn>;
|
|
44
45
|
private exitWaiter: Waiter;
|
|
46
|
+
private _isPaused: boolean = false;
|
|
45
47
|
|
|
46
48
|
constructor(fn: ProcessFn<Args, State, InMessage, OutMessage>, pname: string, toParent: ProcessMessageCb<OutMessage> | undefined) {
|
|
47
49
|
this.pgenerator = fn;
|
|
@@ -53,6 +55,7 @@ class Process<Args, State, InMessage extends Message, OutMessage extends Message
|
|
|
53
55
|
this.current = null;
|
|
54
56
|
this.state = null;
|
|
55
57
|
this.buffer = [];
|
|
58
|
+
this.nextTick = null;
|
|
56
59
|
|
|
57
60
|
this.children = [];
|
|
58
61
|
this.subscribers = [];
|
|
@@ -70,7 +73,7 @@ class Process<Args, State, InMessage extends Message, OutMessage extends Message
|
|
|
70
73
|
this.current = task;
|
|
71
74
|
let ret = task.next();
|
|
72
75
|
this.state = ret.value || null;
|
|
73
|
-
this.
|
|
76
|
+
this._eatResult(task.next());
|
|
74
77
|
}
|
|
75
78
|
|
|
76
79
|
fork<ChildArgs, ChildState, ChildIM extends Message, ChildOM extends Message> (fn : ProcessFn<ChildArgs, ChildState, ChildIM, ChildOM>, pname : string) {
|
|
@@ -85,16 +88,23 @@ class Process<Args, State, InMessage extends Message, OutMessage extends Message
|
|
|
85
88
|
}
|
|
86
89
|
}
|
|
87
90
|
|
|
88
|
-
_tick (
|
|
91
|
+
_tick () {
|
|
89
92
|
if (!this.current) {
|
|
90
93
|
return;
|
|
91
94
|
}
|
|
92
|
-
|
|
93
95
|
let msg: InMessage | undefined;
|
|
96
|
+
let ret: IteratorResult<State | null, void> | null = null;
|
|
94
97
|
while(msg = this.buffer.shift()) {
|
|
95
|
-
this.
|
|
98
|
+
ret = this.current.next(msg);
|
|
99
|
+
if (ret.done) {
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
96
102
|
}
|
|
97
103
|
this.notify();
|
|
104
|
+
this._eatResult(ret);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
_eatResult(ret: IteratorResult<State | null, void> | null) {
|
|
98
108
|
if (ret && ret.done) {
|
|
99
109
|
this.exitWaiter.resolve();
|
|
100
110
|
}
|
|
@@ -106,7 +116,24 @@ class Process<Args, State, InMessage extends Message, OutMessage extends Message
|
|
|
106
116
|
|
|
107
117
|
send(msg: InMessage) {
|
|
108
118
|
this.buffer.push(msg);
|
|
109
|
-
|
|
119
|
+
this._scheduleTick();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
tick() {
|
|
123
|
+
this.nextTick?.flush();
|
|
124
|
+
this.nextTick = null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
_scheduleTick() {
|
|
128
|
+
if (this._isPaused) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
this.nextTick?.cancel();
|
|
133
|
+
this.nextTick = defer(()=> {
|
|
134
|
+
this.nextTick = null;
|
|
135
|
+
this._tick()
|
|
136
|
+
});
|
|
110
137
|
}
|
|
111
138
|
|
|
112
139
|
notify() {
|
|
@@ -128,6 +155,17 @@ class Process<Args, State, InMessage extends Message, OutMessage extends Message
|
|
|
128
155
|
}
|
|
129
156
|
}
|
|
130
157
|
|
|
158
|
+
pause() {
|
|
159
|
+
this.nextTick?.cancel();
|
|
160
|
+
this.nextTick = null;
|
|
161
|
+
this._isPaused = true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
resume() {
|
|
165
|
+
this._isPaused = false;
|
|
166
|
+
this._scheduleTick();
|
|
167
|
+
}
|
|
168
|
+
|
|
131
169
|
wait() {
|
|
132
170
|
return this.exitWaiter.promise;
|
|
133
171
|
}
|
package/src/util.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type Process from './process';
|
|
2
|
-
import type { ProcessCtx, Message } from './process';
|
|
1
|
+
import type Process from './process.js';
|
|
2
|
+
import type { ProcessCtx, Message } from './process.js';
|
|
3
3
|
|
|
4
4
|
function debugLog(level: boolean, ...args: Array<unknown>) {
|
|
5
5
|
if (level) {
|
|
@@ -34,15 +34,42 @@ function watchExit<Args, State, InMessage extends Message, OutMessage extends (M
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
type DeferCb = () => void;
|
|
37
|
-
type Defer = (fn: DeferCb) =>
|
|
37
|
+
type Defer = (fn: DeferCb) => unknown;
|
|
38
|
+
type Cancel = (taskId: any)=> void;
|
|
38
39
|
type WindowGlobal = {
|
|
39
|
-
setImmediate?: Defer
|
|
40
|
+
setImmediate?: Defer,
|
|
41
|
+
clearImmediate?: Cancel,
|
|
42
|
+
requestIdleCallback?: Defer,
|
|
43
|
+
cancelIdleCallback?: Cancel,
|
|
44
|
+
setTimeout: Defer,
|
|
45
|
+
clearTimeout: Cancel,
|
|
40
46
|
};
|
|
41
47
|
|
|
42
|
-
|
|
48
|
+
export type DeferredCall = {
|
|
49
|
+
cancel: ()=> void,
|
|
50
|
+
flush: () => void,
|
|
51
|
+
};
|
|
52
|
+
function defer(fn: DeferCb) : DeferredCall {
|
|
43
53
|
const g = globalThis as WindowGlobal;
|
|
44
|
-
|
|
45
|
-
|
|
54
|
+
function schedule(deferFn: Defer, cancelFn: Cancel) {
|
|
55
|
+
const taskId = deferFn(fn);
|
|
56
|
+
return ()=> cancelFn(taskId);
|
|
57
|
+
}
|
|
58
|
+
let cancel : ()=> void;
|
|
59
|
+
if (g.setImmediate && g.clearImmediate) {
|
|
60
|
+
cancel = schedule(g.setImmediate, g.clearImmediate);
|
|
61
|
+
} else if (g.requestIdleCallback && g.cancelIdleCallback) {
|
|
62
|
+
cancel = schedule(g.requestIdleCallback, g.cancelIdleCallback);
|
|
63
|
+
} else {
|
|
64
|
+
cancel = schedule((f) => setTimeout(f, 0), clearTimeout);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function flush() {
|
|
68
|
+
cancel();
|
|
69
|
+
fn();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return { flush, cancel };
|
|
46
73
|
}
|
|
47
74
|
export type Waiter = {
|
|
48
75
|
promise: Promise<void>;
|
package/src/xfetch.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ExitMessage, runDispatch } from './util.js';
|
|
2
|
-
import type Process from './process';
|
|
3
|
-
import type { ProcessCtx, Message } from './process';
|
|
2
|
+
import type Process from './process.js';
|
|
3
|
+
import type { ProcessCtx, Message } from './process.js';
|
|
4
4
|
|
|
5
5
|
function isJsonHelper(res : Response) {
|
|
6
6
|
const ct = res.headers.get('content-type');
|