@tsln/timers 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mateo Murphy
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,53 @@
1
+ # @tsln/timers
2
+
3
+ `setTimeout`, `setInterval` and their `clear` counterparts for `[v8]` scripts, which have none,
4
+ built on Max's `Task`.
5
+
6
+ ```ts
7
+ import timers = require("./vendor/timers");
8
+
9
+ const id = timers.setTimeout(redraw, 50);
10
+ timers.clearTimeout(id);
11
+
12
+ const tick = timers.setInterval(poll, 500, "arg");
13
+ timers.clearInterval(tick);
14
+
15
+ function notifydeleted() {
16
+ timers.clearAll(); // or the Tasks outlive the script
17
+ }
18
+ ```
19
+
20
+ They behave as they do anywhere else, give or take Max's scheduler: a Task runs in the
21
+ low-priority queue, so a delay is a minimum rather than a promise, and 0 means "next tick".
22
+ Both kinds share their ids, so `clearTimeout` clears an interval too, as in a browser. Extra
23
+ arguments are passed to the callback, and are kept in a closure rather than handed to the Task,
24
+ which would turn objects into null.
25
+
26
+ ## As globals
27
+
28
+ A bundled npm package looks for the timers as globals. `install()` provides them, leaving alone
29
+ any that already exist:
30
+
31
+ ```ts
32
+ timers.install();
33
+ import thing = require("./vendor/thing");
34
+ ```
35
+
36
+ ## Install
37
+
38
+ ```sh
39
+ npm install --save-dev @tsln/timers
40
+ ```
41
+
42
+ Max's `require()` searches Max's file path and knows nothing of `node_modules`, so the package's
43
+ single CommonJS file, `dist/index.js`, has to be copied or bundled to somewhere Max can find it
44
+ (the `max-msp` repository bundles it to `js/vendor/timers.js` with esbuild), with a declaration
45
+ file next to it for the types:
46
+
47
+ ```ts
48
+ // src/vendor/timers.d.ts
49
+ import timers = require("@tsln/timers");
50
+ export = timers;
51
+ ```
52
+
53
+ The package depends on `@tsln/max-types` for `Task`.
@@ -0,0 +1,22 @@
1
+ /// <reference types="@tsln/max-types" preserve="true" />
2
+ type Callback<A extends unknown[]> = (...args: A) => void;
3
+ declare function setTimeout<A extends unknown[]>(fn: Callback<A>, ms?: number, ...args: A): number;
4
+ declare function setInterval<A extends unknown[]>(fn: Callback<A>, ms?: number, ...args: A): number;
5
+ /** Clears either kind of timer, as in a browser, where the two share their ids. */
6
+ declare function clearTimeout(id?: number): void;
7
+ /** Stops everything still waiting. Worth calling from notifydeleted(), or Tasks outlive the script. */
8
+ declare function clearAll(): void;
9
+ /**
10
+ * Makes the timers global, for code that expects to find them there. Whatever is already
11
+ * defined is left alone, so this does nothing under node or in a Max that grows its own.
12
+ */
13
+ declare function install(): void;
14
+ declare const _default: {
15
+ setTimeout: typeof setTimeout;
16
+ clearTimeout: typeof clearTimeout;
17
+ setInterval: typeof setInterval;
18
+ clearInterval: typeof clearTimeout;
19
+ clearAll: typeof clearAll;
20
+ install: typeof install;
21
+ };
22
+ export = _default;
package/dist/index.js ADDED
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+
3
+ // src/index.ts
4
+ var pending = {};
5
+ var last = 0;
6
+ function delay(ms) {
7
+ return Math.max(0, Number(ms) || 0);
8
+ }
9
+ function release(id) {
10
+ const task = pending[id];
11
+ if (!task) {
12
+ return;
13
+ }
14
+ delete pending[id];
15
+ task.cancel();
16
+ task.freepeer();
17
+ }
18
+ function setTimeout(fn, ms, ...args) {
19
+ const id = ++last;
20
+ const task = new Task(() => {
21
+ release(id);
22
+ fn(...args);
23
+ });
24
+ pending[id] = task;
25
+ task.schedule(delay(ms));
26
+ return id;
27
+ }
28
+ function setInterval(fn, ms, ...args) {
29
+ const id = ++last;
30
+ const wait = Math.max(1, delay(ms));
31
+ const task = new Task(() => {
32
+ task.schedule(wait);
33
+ fn(...args);
34
+ });
35
+ pending[id] = task;
36
+ task.schedule(wait);
37
+ return id;
38
+ }
39
+ function clearTimeout(id) {
40
+ release(id);
41
+ }
42
+ function clearAll() {
43
+ Object.keys(pending).forEach(release);
44
+ }
45
+ function install() {
46
+ const target = globalThis;
47
+ const globals = { setTimeout, clearTimeout, setInterval, clearInterval: clearTimeout };
48
+ for (const [name, fn] of Object.entries(globals)) {
49
+ if (typeof target[name] !== "function") {
50
+ target[name] = fn;
51
+ }
52
+ }
53
+ }
54
+ module.exports = {
55
+ setTimeout,
56
+ clearTimeout,
57
+ setInterval,
58
+ clearInterval: clearTimeout,
59
+ clearAll,
60
+ install
61
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@tsln/timers",
3
+ "version": "0.0.0",
4
+ "description": "setTimeout, setInterval and their clear counterparts for Max [v8] scripts, built on Task, with an install() that makes them globals for bundled packages",
5
+ "license": "MIT",
6
+ "author": "Mateo Murphy",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/tsln-lab/max-packages.git",
10
+ "directory": "packages/timers"
11
+ },
12
+ "keywords": [
13
+ "max",
14
+ "msp",
15
+ "max-msp",
16
+ "max-for-live",
17
+ "v8",
18
+ "settimeout",
19
+ "setinterval",
20
+ "timers",
21
+ "typescript"
22
+ ],
23
+ "main": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "files": [
26
+ "dist",
27
+ "README.md",
28
+ "CHANGELOG.md",
29
+ "LICENSE"
30
+ ],
31
+ "sideEffects": false,
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "dependencies": {
36
+ "@tsln/max-types": "^0.1.0"
37
+ },
38
+ "devDependencies": {
39
+ "esbuild": "^0.28.2",
40
+ "typescript": "7.0.2"
41
+ },
42
+ "scripts": {
43
+ "build": "tsc -p tsconfig.build.json && esbuild src/index.ts --bundle --format=cjs --platform=neutral --target=es2022 --outfile=dist/index.js --log-level=warning",
44
+ "typecheck": "tsc -p tsconfig.json",
45
+ "test": "node tests/timers.test.cjs"
46
+ }
47
+ }