posipaki 0.5.4 → 0.5.5

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 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.5.4",
4
+ "version": "0.5.5",
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
- "typescript": "^4.4.3"
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.js';
2
- import { ExitMessage, runDispatch } from './util.js';
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.js';
1
+ import { ExitMessage, watchExit, Waiter, defer, DeferredCall, makeWaiter } from './util';
2
2
 
3
3
  export interface Message {
4
4
  type: string;
@@ -39,6 +39,7 @@ 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;
@@ -53,6 +54,7 @@ class Process<Args, State, InMessage extends Message, OutMessage extends Message
53
54
  this.current = null;
54
55
  this.state = null;
55
56
  this.buffer = [];
57
+ this.nextTick = null;
56
58
 
57
59
  this.children = [];
58
60
  this.subscribers = [];
@@ -70,7 +72,7 @@ class Process<Args, State, InMessage extends Message, OutMessage extends Message
70
72
  this.current = task;
71
73
  let ret = task.next();
72
74
  this.state = ret.value || null;
73
- this._tick(task.next());
75
+ this._eatResult(task.next());
74
76
  }
75
77
 
76
78
  fork<ChildArgs, ChildState, ChildIM extends Message, ChildOM extends Message> (fn : ProcessFn<ChildArgs, ChildState, ChildIM, ChildOM>, pname : string) {
@@ -85,16 +87,23 @@ class Process<Args, State, InMessage extends Message, OutMessage extends Message
85
87
  }
86
88
  }
87
89
 
88
- _tick (ret: IteratorResult<State | null, void> | null) {
90
+ _tick () {
89
91
  if (!this.current) {
90
92
  return;
91
93
  }
92
-
93
94
  let msg: InMessage | undefined;
95
+ let ret: IteratorResult<State | null, void> | null = null;
94
96
  while(msg = this.buffer.shift()) {
95
- this._tick(this.current.next(msg));
97
+ ret = this.current.next(msg);
98
+ if (ret.done) {
99
+ break;
100
+ }
96
101
  }
97
102
  this.notify();
103
+ this._eatResult(ret);
104
+ }
105
+
106
+ _eatResult(ret: IteratorResult<State | null, void> | null) {
98
107
  if (ret && ret.done) {
99
108
  this.exitWaiter.resolve();
100
109
  }
@@ -106,7 +115,19 @@ class Process<Args, State, InMessage extends Message, OutMessage extends Message
106
115
 
107
116
  send(msg: InMessage) {
108
117
  this.buffer.push(msg);
109
- defer(()=> this._tick(null));
118
+ this._scheduleTick();
119
+ }
120
+
121
+ tick() {
122
+ this.nextTick?.flush();
123
+ }
124
+
125
+ _scheduleTick() {
126
+ this.nextTick?.cancel();
127
+ this.nextTick = defer(()=> {
128
+ this.nextTick = null;
129
+ this._tick()
130
+ });
110
131
  }
111
132
 
112
133
  notify() {
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) => void;
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
- function defer(fn: DeferCb) {
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
- const setDefer = (g.setImmediate || requestIdleCallback) as Defer;
45
- setDefer(fn);
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');