@unieai/cordis-plugin-timer 1.1.3
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/README.md +36 -0
- package/lib/index.js +152 -0
- package/lib/types/index.d.ts +30 -0
- package/lib/types/index.d.ts.map +1 -0
- package/package.json +38 -0
- package/src/index.ts +147 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021-present Shigma
|
|
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/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @cordisjs/plugin-timer
|
|
2
|
+
|
|
3
|
+
Disposal-aware timer service for Cordis.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Context } from 'cordis'
|
|
9
|
+
import Timer from '@cordisjs/plugin-timer'
|
|
10
|
+
|
|
11
|
+
const root = new Context()
|
|
12
|
+
await root.plugin(Timer)
|
|
13
|
+
|
|
14
|
+
const dispose = root.timeout(() => {
|
|
15
|
+
root.logger.info('done')
|
|
16
|
+
}, 1000)
|
|
17
|
+
|
|
18
|
+
dispose()
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Timer handles are registered on the current fiber, so they are cleared
|
|
22
|
+
automatically when the plugin that created them is disposed.
|
|
23
|
+
|
|
24
|
+
## API
|
|
25
|
+
|
|
26
|
+
| API | Description |
|
|
27
|
+
| --- | --- |
|
|
28
|
+
| `ctx.timeout(callback, delay)` | Run once and return a disposer. |
|
|
29
|
+
| `ctx.timeout(delay)` | Return a promise that resolves after `delay`. |
|
|
30
|
+
| `ctx.interval(callback, delay)` | Run repeatedly and return a disposer. |
|
|
31
|
+
| `ctx.interval(delay)` | Return an async iterator that yields on each interval. |
|
|
32
|
+
| `ctx.throttle(callback, delay, noTrailing?)` | Return a throttled function with `.dispose()`. |
|
|
33
|
+
| `ctx.debounce(callback, delay)` | Return a debounced function with `.dispose()`. |
|
|
34
|
+
|
|
35
|
+
`ctx.setTimeout()` and `ctx.setInterval()` are kept as deprecated aliases for
|
|
36
|
+
`ctx.timeout()` and `ctx.interval()`.
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { Service } from "@unieai/cordis";
|
|
2
|
+
//#region lib/types/index.js
|
|
3
|
+
/** Disposable timer helpers mixed into Cordis contexts. */
|
|
4
|
+
var TimerService = class extends Service {
|
|
5
|
+
constructor(ctx) {
|
|
6
|
+
super(ctx, "timer");
|
|
7
|
+
ctx.mixin("timer", [
|
|
8
|
+
"timeout",
|
|
9
|
+
"interval",
|
|
10
|
+
"throttle",
|
|
11
|
+
"debounce",
|
|
12
|
+
"setTimeout",
|
|
13
|
+
"setInterval"
|
|
14
|
+
]);
|
|
15
|
+
}
|
|
16
|
+
/** @deprecated use `ctx.timeout()` instead */
|
|
17
|
+
setTimeout(callback, delay) {
|
|
18
|
+
return this.timeout(callback, delay);
|
|
19
|
+
}
|
|
20
|
+
/** @deprecated use `ctx.interval()` instead */
|
|
21
|
+
setInterval(callback, delay) {
|
|
22
|
+
return this.interval(callback, delay);
|
|
23
|
+
}
|
|
24
|
+
timeout(...args) {
|
|
25
|
+
const callback = typeof args[0] === "function" ? args.shift() : void 0;
|
|
26
|
+
const delay = args[0];
|
|
27
|
+
if (callback) {
|
|
28
|
+
const dispose = this.ctx.effect(() => {
|
|
29
|
+
const timer = setTimeout(() => {
|
|
30
|
+
dispose();
|
|
31
|
+
callback();
|
|
32
|
+
}, delay);
|
|
33
|
+
return () => clearTimeout(timer);
|
|
34
|
+
}, "ctx.timeout()");
|
|
35
|
+
return dispose;
|
|
36
|
+
} else {
|
|
37
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
38
|
+
const dispose = this.ctx.effect(() => {
|
|
39
|
+
const timer = setTimeout(resolve, delay);
|
|
40
|
+
return () => {
|
|
41
|
+
clearTimeout(timer);
|
|
42
|
+
reject(/* @__PURE__ */ new Error("Context has been disposed"));
|
|
43
|
+
};
|
|
44
|
+
}, "ctx.timeout()");
|
|
45
|
+
return promise.finally(dispose);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
interval(...args) {
|
|
49
|
+
const callback = typeof args[0] === "function" ? args.shift() : void 0;
|
|
50
|
+
const delay = args[0];
|
|
51
|
+
if (callback) return this.ctx.effect(() => {
|
|
52
|
+
const timer = setInterval(callback, delay);
|
|
53
|
+
return () => clearInterval(timer);
|
|
54
|
+
}, "ctx.interval()");
|
|
55
|
+
else {
|
|
56
|
+
let done;
|
|
57
|
+
let nextTask;
|
|
58
|
+
const dispose = this.ctx.effect(() => {
|
|
59
|
+
const timer = setInterval(() => {
|
|
60
|
+
nextTask?.resolve({
|
|
61
|
+
done: false,
|
|
62
|
+
value: void 0
|
|
63
|
+
});
|
|
64
|
+
}, delay);
|
|
65
|
+
return () => {
|
|
66
|
+
clearInterval(timer);
|
|
67
|
+
if (done) return;
|
|
68
|
+
done = {
|
|
69
|
+
kind: "throw",
|
|
70
|
+
reason: /* @__PURE__ */ new Error("Context has been disposed")
|
|
71
|
+
};
|
|
72
|
+
nextTask?.reject(done.reason);
|
|
73
|
+
};
|
|
74
|
+
}, "ctx.interval()");
|
|
75
|
+
return {
|
|
76
|
+
next: () => {
|
|
77
|
+
if (!done) return (nextTask = Promise.withResolvers()).promise;
|
|
78
|
+
if (done.kind === "return") return Promise.resolve({
|
|
79
|
+
done: true,
|
|
80
|
+
value: done.value
|
|
81
|
+
});
|
|
82
|
+
return Promise.reject(done.reason);
|
|
83
|
+
},
|
|
84
|
+
return: (value) => {
|
|
85
|
+
if (!done) done = {
|
|
86
|
+
kind: "return",
|
|
87
|
+
value
|
|
88
|
+
};
|
|
89
|
+
nextTask?.resolve({
|
|
90
|
+
done: true,
|
|
91
|
+
value
|
|
92
|
+
});
|
|
93
|
+
dispose();
|
|
94
|
+
return Promise.resolve({
|
|
95
|
+
done: true,
|
|
96
|
+
value
|
|
97
|
+
});
|
|
98
|
+
},
|
|
99
|
+
throw: (reason) => {
|
|
100
|
+
if (!done) done = {
|
|
101
|
+
kind: "throw",
|
|
102
|
+
reason
|
|
103
|
+
};
|
|
104
|
+
nextTask?.reject(reason);
|
|
105
|
+
dispose();
|
|
106
|
+
return Promise.resolve({
|
|
107
|
+
done: true,
|
|
108
|
+
value: void 0
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
[Symbol.asyncIterator]() {
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
_schedule(label, trigger, isDisposed = false) {
|
|
118
|
+
let timer;
|
|
119
|
+
const dispose = this.ctx.effect(() => () => {
|
|
120
|
+
isDisposed = true;
|
|
121
|
+
clearTimeout(timer);
|
|
122
|
+
}, label);
|
|
123
|
+
const wrapper = (...args) => {
|
|
124
|
+
clearTimeout(timer);
|
|
125
|
+
timer = trigger(args, isDisposed);
|
|
126
|
+
};
|
|
127
|
+
wrapper.dispose = dispose;
|
|
128
|
+
return wrapper;
|
|
129
|
+
}
|
|
130
|
+
/** Return a throttled function whose timer is disposed with the current fiber. */
|
|
131
|
+
throttle(callback, delay, noTrailing) {
|
|
132
|
+
let lastCall = -Infinity;
|
|
133
|
+
const execute = (...args) => {
|
|
134
|
+
lastCall = Date.now();
|
|
135
|
+
callback(...args);
|
|
136
|
+
};
|
|
137
|
+
return this._schedule("ctx.throttle()", (args, isDisposed) => {
|
|
138
|
+
const remaining = delay - Date.now() + lastCall;
|
|
139
|
+
if (remaining <= 0) execute(...args);
|
|
140
|
+
else if (!isDisposed) return setTimeout(execute, remaining, ...args);
|
|
141
|
+
}, noTrailing);
|
|
142
|
+
}
|
|
143
|
+
/** Return a debounced function whose timer is disposed with the current fiber. */
|
|
144
|
+
debounce(callback, delay) {
|
|
145
|
+
return this._schedule("ctx.debounce()", (args, isDisposed) => {
|
|
146
|
+
if (isDisposed) return;
|
|
147
|
+
return setTimeout(callback, delay, ...args);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
//#endregion
|
|
152
|
+
export { TimerService, TimerService as default };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Context, Service } from '@unieai/cordis';
|
|
2
|
+
declare module '@unieai/cordis' {
|
|
3
|
+
interface Context extends Pick<TimerService, 'interval' | 'timeout' | 'throttle' | 'debounce' | 'setTimeout' | 'setInterval'> {
|
|
4
|
+
timer: TimerService;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
type WithDispose<T> = T & {
|
|
8
|
+
dispose: () => void;
|
|
9
|
+
};
|
|
10
|
+
/** Disposable timer helpers mixed into Cordis contexts. */
|
|
11
|
+
export declare class TimerService extends Service {
|
|
12
|
+
constructor(ctx: Context);
|
|
13
|
+
/** @deprecated use `ctx.timeout()` instead */
|
|
14
|
+
setTimeout(callback: () => void, delay: number): () => void;
|
|
15
|
+
/** @deprecated use `ctx.interval()` instead */
|
|
16
|
+
setInterval(callback: () => void, delay: number): () => void;
|
|
17
|
+
/** Run a callback once, or return a promise that resolves after `delay`. */
|
|
18
|
+
timeout(callback: () => void, delay: number): () => void;
|
|
19
|
+
timeout(delay: number): Promise<void>;
|
|
20
|
+
/** Run a callback repeatedly, or return an async iterator of ticks. */
|
|
21
|
+
interval(callback: () => void, delay: number): () => void;
|
|
22
|
+
interval<R = any>(delay: number): AsyncIterableIterator<void, R, void>;
|
|
23
|
+
private _schedule;
|
|
24
|
+
/** Return a throttled function whose timer is disposed with the current fiber. */
|
|
25
|
+
throttle<F extends (...args: any[]) => void>(callback: F, delay: number, noTrailing?: boolean): WithDispose<F>;
|
|
26
|
+
/** Return a debounced function whose timer is disposed with the current fiber. */
|
|
27
|
+
debounce<F extends (...args: any[]) => void>(callback: F, delay: number): WithDispose<F>;
|
|
28
|
+
}
|
|
29
|
+
export default TimerService;
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAEjD,OAAO,QAAQ,gBAAgB,CAAC;IAC9B,UAAU,OAAQ,SAAQ,IAAI,CAAC,YAAY,EAAE,UAAU,GAAG,SAAS,GAAG,UAAU,GAAG,UAAU,GAAG,YAAY,GAAG,aAAa,CAAC;QAC3H,KAAK,EAAE,YAAY,CAAA;KACpB;CACF;AAED,KAAK,WAAW,CAAC,CAAC,IAAI,CAAC,GAAG;IAAE,OAAO,EAAE,MAAM,IAAI,CAAA;CAAE,CAAA;AAEjD,2DAA2D;AAC3D,qBAAa,YAAa,SAAQ,OAAO;gBAC3B,GAAG,EAAE,OAAO;IAKxB,8CAA8C;IAC9C,UAAU,CAAC,QAAQ,EAAE,MAAM,IAAI,EAAE,KAAK,EAAE,MAAM,SAUM,IAAI;IANxD,+CAA+C;IAC/C,WAAW,CAAC,QAAQ,EAAE,MAAM,IAAI,EAAE,KAAK,EAAE,MAAM,SAiCM,IAAI;IA7BzD,4EAA4E;IAC5E,OAAO,CAAC,QAAQ,EAAE,MAAM,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,IAAI;IACxD,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA0BrC,uEAAuE;IACvE,QAAQ,CAAC,QAAQ,EAAE,MAAM,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,IAAI;IACzD,QAAQ,CAAC,CAAC,GAAG,GAAG,EAAE,KAAK,EAAE,MAAM,GAAG,qBAAqB,CAAC,IAAI,EAAE,CAAC,EAAE,IAAI,CAAC;IAgDtE,OAAO,CAAC,SAAS;IAcjB,kFAAkF;IAClF,QAAQ,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,EAAE,QAAQ,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,OAAO,GAAG,WAAW,CAAC,CAAC,CAAC;IAiB9G,kFAAkF;IAClF,QAAQ,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,EAAE,QAAQ,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC;CAMzF;AAED,eAAe,YAAY,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unieai/cordis-plugin-timer",
|
|
3
|
+
"description": "Timer service for cordis",
|
|
4
|
+
"version": "1.1.3",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/UnieAI/UnieAI-Agent-Desktop.git",
|
|
11
|
+
"directory": "vendor/timer"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "lib/index.js",
|
|
15
|
+
"types": "lib/types/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./lib/types/index.d.ts",
|
|
19
|
+
"default": "./lib/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./src/*": "./src/*",
|
|
22
|
+
"./package.json": "./package.json"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"lib/index.js",
|
|
26
|
+
"lib/types/**/*.d.ts",
|
|
27
|
+
"lib/types/**/*.d.ts.map",
|
|
28
|
+
"src"
|
|
29
|
+
],
|
|
30
|
+
"author": "Shigma <shigma10826@gmail.com>",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@unieai/cordis": "^4.0.1"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@unieai/cosmokit": "^1.8.2"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { Context, Service } from '@unieai/cordis'
|
|
2
|
+
|
|
3
|
+
declare module '@unieai/cordis' {
|
|
4
|
+
interface Context extends Pick<TimerService, 'interval' | 'timeout' | 'throttle' | 'debounce' | 'setTimeout' | 'setInterval'> {
|
|
5
|
+
timer: TimerService
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
type WithDispose<T> = T & { dispose: () => void }
|
|
10
|
+
|
|
11
|
+
/** Disposable timer helpers mixed into Cordis contexts. */
|
|
12
|
+
export class TimerService extends Service {
|
|
13
|
+
constructor(ctx: Context) {
|
|
14
|
+
super(ctx, 'timer')
|
|
15
|
+
ctx.mixin('timer', ['timeout', 'interval', 'throttle', 'debounce', 'setTimeout', 'setInterval'])
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** @deprecated use `ctx.timeout()` instead */
|
|
19
|
+
setTimeout(callback: () => void, delay: number) {
|
|
20
|
+
return this.timeout(callback, delay)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @deprecated use `ctx.interval()` instead */
|
|
24
|
+
setInterval(callback: () => void, delay: number) {
|
|
25
|
+
return this.interval(callback, delay)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Run a callback once, or return a promise that resolves after `delay`. */
|
|
29
|
+
timeout(callback: () => void, delay: number): () => void
|
|
30
|
+
timeout(delay: number): Promise<void>
|
|
31
|
+
timeout(...args: any[]): any {
|
|
32
|
+
const callback = typeof args[0] === 'function' ? args.shift() : undefined
|
|
33
|
+
const delay = args[0] as number
|
|
34
|
+
if (callback) {
|
|
35
|
+
const dispose = this.ctx.effect(() => {
|
|
36
|
+
const timer = setTimeout(() => {
|
|
37
|
+
dispose()
|
|
38
|
+
callback()
|
|
39
|
+
}, delay)
|
|
40
|
+
return () => clearTimeout(timer)
|
|
41
|
+
}, 'ctx.timeout()')
|
|
42
|
+
return dispose
|
|
43
|
+
} else {
|
|
44
|
+
const { promise, resolve, reject } = Promise.withResolvers<void>()
|
|
45
|
+
const dispose = this.ctx.effect(() => {
|
|
46
|
+
const timer = setTimeout(resolve, delay)
|
|
47
|
+
return () => {
|
|
48
|
+
clearTimeout(timer)
|
|
49
|
+
reject(new Error('Context has been disposed'))
|
|
50
|
+
}
|
|
51
|
+
}, 'ctx.timeout()')
|
|
52
|
+
return promise.finally(dispose)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Run a callback repeatedly, or return an async iterator of ticks. */
|
|
57
|
+
interval(callback: () => void, delay: number): () => void
|
|
58
|
+
interval<R = any>(delay: number): AsyncIterableIterator<void, R, void>
|
|
59
|
+
interval(...args: any[]): any {
|
|
60
|
+
const callback = typeof args[0] === 'function' ? args.shift() : undefined
|
|
61
|
+
const delay = args[0] as number
|
|
62
|
+
if (callback) {
|
|
63
|
+
return this.ctx.effect(() => {
|
|
64
|
+
const timer = setInterval(callback, delay)
|
|
65
|
+
return () => clearInterval(timer)
|
|
66
|
+
}, 'ctx.interval()')
|
|
67
|
+
} else {
|
|
68
|
+
let done: { kind: 'return'; value: any } | { kind: 'throw'; reason: any } | undefined
|
|
69
|
+
let nextTask: PromiseWithResolvers<IteratorResult<void>> | undefined
|
|
70
|
+
const dispose = this.ctx.effect(() => {
|
|
71
|
+
const timer = setInterval(() => {
|
|
72
|
+
nextTask?.resolve({ done: false, value: undefined })
|
|
73
|
+
}, delay)
|
|
74
|
+
return () => {
|
|
75
|
+
clearInterval(timer)
|
|
76
|
+
if (done) return
|
|
77
|
+
done = { kind: 'throw', reason: new Error('Context has been disposed') }
|
|
78
|
+
nextTask?.reject(done.reason)
|
|
79
|
+
}
|
|
80
|
+
}, 'ctx.interval()')
|
|
81
|
+
return {
|
|
82
|
+
next: () => {
|
|
83
|
+
if (!done) return (nextTask = Promise.withResolvers()).promise
|
|
84
|
+
if (done.kind === 'return') return Promise.resolve({ done: true, value: done.value })
|
|
85
|
+
return Promise.reject(done.reason)
|
|
86
|
+
},
|
|
87
|
+
return: (value) => {
|
|
88
|
+
if (!done) done = { kind: 'return', value }
|
|
89
|
+
nextTask?.resolve({ done: true, value })
|
|
90
|
+
dispose()
|
|
91
|
+
return Promise.resolve({ done: true, value })
|
|
92
|
+
},
|
|
93
|
+
throw: (reason) => {
|
|
94
|
+
if (!done) done = { kind: 'throw', reason }
|
|
95
|
+
nextTask?.reject(reason)
|
|
96
|
+
dispose()
|
|
97
|
+
return Promise.resolve({ done: true, value: undefined })
|
|
98
|
+
},
|
|
99
|
+
[Symbol.asyncIterator]() {
|
|
100
|
+
return this
|
|
101
|
+
},
|
|
102
|
+
} satisfies AsyncIterableIterator<void>
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private _schedule(label: string, trigger: (args: any[], isDisposed: boolean) => any, isDisposed = false) {
|
|
107
|
+
let timer: number | NodeJS.Timeout | undefined
|
|
108
|
+
const dispose = this.ctx.effect(() => () => {
|
|
109
|
+
isDisposed = true
|
|
110
|
+
clearTimeout(timer)
|
|
111
|
+
}, label)
|
|
112
|
+
const wrapper: any = (...args: any[]) => {
|
|
113
|
+
clearTimeout(timer)
|
|
114
|
+
timer = trigger(args, isDisposed)
|
|
115
|
+
}
|
|
116
|
+
wrapper.dispose = dispose
|
|
117
|
+
return wrapper
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Return a throttled function whose timer is disposed with the current fiber. */
|
|
121
|
+
throttle<F extends (...args: any[]) => void>(callback: F, delay: number, noTrailing?: boolean): WithDispose<F> {
|
|
122
|
+
let lastCall = -Infinity
|
|
123
|
+
const execute = (...args: any[]) => {
|
|
124
|
+
lastCall = Date.now()
|
|
125
|
+
callback(...args)
|
|
126
|
+
}
|
|
127
|
+
return this._schedule('ctx.throttle()', (args, isDisposed) => {
|
|
128
|
+
const now = Date.now()
|
|
129
|
+
const remaining = delay - now + lastCall
|
|
130
|
+
if (remaining <= 0) {
|
|
131
|
+
execute(...args)
|
|
132
|
+
} else if (!isDisposed) {
|
|
133
|
+
return setTimeout(execute, remaining, ...args)
|
|
134
|
+
}
|
|
135
|
+
}, noTrailing)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Return a debounced function whose timer is disposed with the current fiber. */
|
|
139
|
+
debounce<F extends (...args: any[]) => void>(callback: F, delay: number): WithDispose<F> {
|
|
140
|
+
return this._schedule('ctx.debounce()', (args, isDisposed) => {
|
|
141
|
+
if (isDisposed) return
|
|
142
|
+
return setTimeout(callback, delay, ...args)
|
|
143
|
+
})
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export default TimerService
|