首次提交

This commit is contained in:
2026-07-13 12:06:16 +08:00
commit e3c810b1a6
1690 changed files with 228324 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 Christoph Guttandin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+9
View File
@@ -0,0 +1,9 @@
# worker-timers-worker
**The worker which is used by the worker-timers package.**
[![version](https://img.shields.io/npm/v/worker-timers-worker.svg?style=flat-square)](https://www.npmjs.com/package/worker-timers-worker)
## Security contact information
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
+75
View File
@@ -0,0 +1,75 @@
{
"author": "Christoph Guttandin",
"bugs": {
"url": "https://github.com/chrisguttandin/worker-timers-worker/issues"
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"contributors": [
{
"email": "vac872089248@gmail.com",
"name": "Knissing"
}
],
"dependencies": {
"@babel/runtime": "^7.29.2",
"tslib": "^2.8.1",
"worker-factory": "^7.0.49"
},
"description": "The worker which is used by the worker-timers package.",
"devDependencies": {
"@babel/core": "^7.29.0",
"@babel/plugin-external-helpers": "^7.27.1",
"@babel/plugin-transform-runtime": "^7.29.0",
"@babel/preset-env": "^7.29.2",
"@commitlint/cli": "^20.5.0",
"@commitlint/config-angular": "^20.5.0",
"@rollup/plugin-babel": "^7.0.0",
"@vitest/browser-webdriverio": "^4.1.0",
"commitizen": "^4.3.1",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.57.0",
"eslint-config-holy-grail": "^61.0.11",
"husky": "^9.1.7",
"lint-staged": "^16.4.0",
"prettier": "^3.8.1",
"rimraf": "^6.1.3",
"rollup": "^4.59.0",
"tsconfig-holy-grail": "^15.0.3",
"tslint": "^6.1.3",
"tslint-config-holy-grail": "^56.0.7",
"typescript": "^5.9.3",
"vitest": "^4.1.0"
},
"files": [
"build/es2019/",
"build/es5/",
"src/"
],
"homepage": "https://github.com/chrisguttandin/worker-timers-worker",
"license": "MIT",
"main": "build/es5/bundle.js",
"module": "build/es2019/module.js",
"name": "worker-timers-worker",
"repository": {
"type": "git",
"url": "https://github.com/chrisguttandin/worker-timers-worker.git"
},
"scripts": {
"build": "rimraf build/* && tsc --project src/tsconfig.json && rollup --config config/rollup/bundle.mjs",
"lint": "npm run lint:config && npm run lint:src && npm run lint:test",
"lint:config": "eslint --config config/eslint/config.json --ext .cjs --ext .js --ext .mjs --report-unused-disable-directives config/",
"lint:src": "tslint --config config/tslint/src.json --project src/tsconfig.json src/*.ts src/**/*.ts",
"lint:test": "eslint --config config/eslint/test.json --ext .js --report-unused-disable-directives test/",
"prepare": "husky",
"prepublishOnly": "npm run build",
"test": "npm run lint && npm run build && npm run test:integration && npm run test:unit",
"test:integration": "if [ \"$TYPE\" = \"\" -o \"$TYPE\" = \"integration\" ]; then npx vitest --config config/vitest/integration.ts; fi",
"test:unit": "if [ \"$TYPE\" = \"\" -o \"$TYPE\" = \"unit\" ]; then npx vitest --config config/vitest/unit.ts; fi"
},
"types": "build/es2019/module.d.ts",
"version": "9.0.14"
}
@@ -0,0 +1,20 @@
import { TResolveSetResponseResultPromise } from '../types';
export const createClearTimer =
(clearTimeout: (typeof globalThis)['clearTimeout'], identifiersAndResolvers: Map<number, [number, TResolveSetResponseResultPromise]>) =>
(timerId: number) => {
const identifiersAndResolver = identifiersAndResolvers.get(timerId);
if (identifiersAndResolver === undefined) {
return Promise.resolve(false);
}
const [identifier, resolveSetResponseResultPromise] = identifiersAndResolver;
clearTimeout(identifier);
identifiersAndResolvers.delete(timerId);
resolveSetResponseResultPromise(false);
return Promise.resolve(true);
};
@@ -0,0 +1,24 @@
import { TResolveSetResponseResultPromise } from '../types';
export const createSetTimeoutCallback = (performance: Pick<Performance, 'now'>, setTimeout: (typeof globalThis)['setTimeout']) => {
const setTimeoutCallback = (
expected: number,
identifiersAndResolvers: Map<number, [number, TResolveSetResponseResultPromise]>,
resolveSetResponseResultPromise: TResolveSetResponseResultPromise,
timerId: number
) => {
const remainingDelay = expected - performance.now();
if (remainingDelay > 0) {
identifiersAndResolvers.set(timerId, [
setTimeout(setTimeoutCallback, remainingDelay, expected, identifiersAndResolvers, resolveSetResponseResultPromise, timerId),
resolveSetResponseResultPromise
]);
} else {
identifiersAndResolvers.delete(timerId);
resolveSetResponseResultPromise(true);
}
};
return setTimeoutCallback;
};
@@ -0,0 +1,21 @@
import { TResolveSetResponseResultPromise } from '../types';
import type { createSetTimeoutCallback } from './set-timeout-callback';
export const createSetTimer =
(
identifiersAndResolvers: Map<number, [number, TResolveSetResponseResultPromise]>,
performance: Pick<Performance, 'now' | 'timeOrigin'>,
setTimeout: (typeof globalThis)['setTimeout'],
setTimeoutCallback: ReturnType<typeof createSetTimeoutCallback>
) =>
(delay: number, nowAndTimeOrigin: number, timerId: number) => {
const expected = delay + nowAndTimeOrigin - performance.timeOrigin;
const remainingDelay = expected - performance.now();
return new Promise((resolve) => {
identifiersAndResolvers.set(timerId, [
setTimeout(setTimeoutCallback, remainingDelay, expected, identifiersAndResolvers, resolve, timerId),
resolve
]);
});
};
@@ -0,0 +1 @@
export * from './worker-timers-worker-custom-definition';
@@ -0,0 +1,32 @@
import { IWorkerDefinition } from 'worker-factory';
import { TTimerType } from '../types';
export interface IWorkerTimersWorkerCustomDefinition extends IWorkerDefinition {
clear: {
params: {
timerId: number;
timerType: TTimerType;
};
response: {
result: boolean;
};
};
set: {
params: {
delay: number;
now: number;
timerId: number;
timerType: TTimerType;
};
response: {
result: boolean;
};
};
}
+30
View File
@@ -0,0 +1,30 @@
import { TWorkerImplementation, createWorker } from 'worker-factory';
import { createClearTimer } from './factories/clear-timer';
import { createSetTimeoutCallback } from './factories/set-timeout-callback';
import { createSetTimer } from './factories/set-timer';
import { IWorkerTimersWorkerCustomDefinition } from './interfaces';
import { TResolveSetResponseResultPromise } from './types';
/*
* @todo Explicitly referencing the barrel file seems to be necessary when enabling the
* isolatedModules compiler option.
*/
export * from './interfaces/index';
export * from './types/index';
const intervalIdentifiersAndResolvers: Map<number, [number, TResolveSetResponseResultPromise]> = new Map();
const clearInterval = createClearTimer(globalThis.clearTimeout, intervalIdentifiersAndResolvers);
const timeoutIdentifiersAndResolvers: Map<number, [number, TResolveSetResponseResultPromise]> = new Map();
const clearTimeout = createClearTimer(globalThis.clearTimeout, timeoutIdentifiersAndResolvers);
const setTimeoutCallback = createSetTimeoutCallback(performance, globalThis.setTimeout);
const setInterval = createSetTimer(intervalIdentifiersAndResolvers, performance, globalThis.setTimeout, setTimeoutCallback);
const setTimeout = createSetTimer(timeoutIdentifiersAndResolvers, performance, globalThis.setTimeout, setTimeoutCallback);
createWorker<IWorkerTimersWorkerCustomDefinition>(self, <TWorkerImplementation<IWorkerTimersWorkerCustomDefinition>>{
clear: async ({ timerId, timerType }) => {
return { result: await (timerType === 'interval' ? clearInterval(timerId) : clearTimeout(timerId)) };
},
set: async ({ delay, now, timerId, timerType }) => {
return { result: await (timerType === 'interval' ? setInterval : setTimeout)(delay, now, timerId) };
}
});
+6
View File
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"isolatedModules": true
},
"extends": "tsconfig-holy-grail/src/tsconfig-web-worker"
}
+3
View File
@@ -0,0 +1,3 @@
export * from './resolve-set-response-result-promise';
export * from './timer-type';
export * from './worker-timers-worker-definition';
@@ -0,0 +1,5 @@
import { TWorkerTimersWorkerDefinition } from './worker-timers-worker-definition';
export type TResolveSetResponseResultPromise = Parameters<
ConstructorParameters<typeof Promise<TWorkerTimersWorkerDefinition['set']['response']['result']>>[0]
>[0];
@@ -0,0 +1 @@
export type TTimerType = 'interval' | 'timeout';
@@ -0,0 +1,4 @@
import { TWorkerDefinition } from 'worker-factory';
import { IWorkerTimersWorkerCustomDefinition } from '../interfaces';
export type TWorkerTimersWorkerDefinition = TWorkerDefinition<IWorkerTimersWorkerCustomDefinition>;