Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cordisjs/timer

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cordisjs/timer - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

21

lib/index.d.ts

@@ -1,17 +0,24 @@

/// <reference types="node" />
import { Context, Service } from 'cordis';
import Logger from 'reggol';
export { Logger };
declare module 'cordis' {
interface Context {
timer: TimerService;
setTimeout(callback: () => void, delay: number): () => void;
setInterval(callback: () => void, delay: number): () => void;
sleep(delay: number): Promise<void>;
throttle<F extends (...args: any[]) => void>(callback: F, delay: number, noTrailing?: boolean): WithDispose<F>;
debounce<F extends (...args: any[]) => void>(callback: F, delay: number): WithDispose<F>;
}
}
type WithDispose<T> = T & {
dispose: () => void;
};
declare class TimerService extends Service {
constructor(ctx: Context);
createTimerDispose(timer: number | NodeJS.Timeout): () => boolean | undefined;
setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): () => boolean | undefined;
setInterval(callback: (...args: any[]) => void, ms: number, ...args: any[]): () => boolean | undefined;
sleep(ms: number): Promise<void>;
setTimeout(callback: () => void, delay: number): () => void;
setInterval(callback: () => void, delay: number): () => void;
sleep(delay: number): Promise<void>;
private createWrapper;
throttle<F extends (...args: any[]) => void>(callback: F, delay: number, noTrailing?: boolean): WithDispose<F>;
debounce<F extends (...args: any[]) => void>(callback: F, delay: number): WithDispose<F>;
}
export default TimerService;
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;

@@ -21,10 +19,2 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });

};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

@@ -35,3 +25,2 @@

__export(src_exports, {
Logger: () => import_reggol.default,
default: () => src_default

@@ -42,29 +31,25 @@ });

var import_cosmokit = require("cosmokit");
var import_reggol = __toESM(require("reggol"));
var _TimerService = class _TimerService extends import_cordis.Service {
constructor(ctx) {
super(ctx, "timer", true);
(0, import_cosmokit.defineProperty)(this, import_cordis.Context.current, ctx);
ctx.mixin("timer", ["setTimeout", "setInterval", "sleep", "throttle", "debounce"]);
}
createTimerDispose(timer) {
const dispose = /* @__PURE__ */ __name(() => {
clearTimeout(timer);
if (!this[import_cordis.Context.current].scope)
return;
return (0, import_cosmokit.remove)(this[import_cordis.Context.current].scope.disposables, dispose);
}, "dispose");
this[import_cordis.Context.current].scope.disposables.push(dispose);
setTimeout(callback, delay) {
const dispose = this[import_cordis.Context.current].effect(() => {
const timer = setTimeout(() => {
dispose();
callback();
}, delay);
return () => clearTimeout(timer);
});
return dispose;
}
setTimeout(callback, ms, ...args) {
const dispose = this.createTimerDispose(setTimeout(() => {
dispose();
callback();
}, ms, ...args));
return dispose;
setInterval(callback, delay) {
return this[import_cordis.Context.current].effect(() => {
const timer = setInterval(callback, delay);
return () => clearInterval(timer);
});
}
setInterval(callback, ms, ...args) {
return this.createTimerDispose(setInterval(callback, ms, ...args));
}
sleep(ms) {
sleep(delay) {
const caller = this[import_cordis.Context.current];
return new Promise((resolve, reject) => {

@@ -75,10 +60,50 @@ const dispose1 = this.setTimeout(() => {

resolve();
}, ms);
const dispose2 = this[import_cordis.Context.current].on("dispose", () => {
}, delay);
const dispose2 = caller.on("dispose", () => {
dispose1();
dispose2();
reject(new Error("Context disposed"));
reject(new Error("Context has been disposed"));
});
});
}
createWrapper(callback, isDisposed = false) {
const caller = this[import_cordis.Context.current];
caller.scope.assertActive();
let timer;
const dispose = /* @__PURE__ */ __name(() => {
isDisposed = true;
(0, import_cosmokit.remove)(caller.scope.disposables, dispose);
clearTimeout(timer);
}, "dispose");
const wrapper = /* @__PURE__ */ __name((...args) => {
clearTimeout(timer);
timer = callback(args, () => !isDisposed && caller.scope.isActive);
}, "wrapper");
wrapper.dispose = dispose;
caller.scope.disposables.push(dispose);
return wrapper;
}
throttle(callback, delay, noTrailing) {
let lastCall = -Infinity;
const execute = /* @__PURE__ */ __name((...args) => {
lastCall = Date.now();
callback(...args);
}, "execute");
return this.createWrapper((args, isActive) => {
const now = Date.now();
const remaining = delay - (now - lastCall);
if (remaining <= 0) {
execute(...args);
} else if (isActive()) {
return setTimeout(execute, remaining, ...args);
}
}, noTrailing);
}
debounce(callback, delay) {
return this.createWrapper((args, isActive) => {
if (!isActive())
return;
return setTimeout(callback, delay, ...args);
});
}
};

@@ -88,5 +113,1 @@ __name(_TimerService, "TimerService");

var src_default = TimerService;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Logger
});
{
"name": "@cordisjs/timer",
"description": "Timer plugin for cordis",
"version": "0.1.1",
"version": "0.2.0",
"main": "lib/index.js",

@@ -30,7 +30,7 @@ "module": "lib/index.mjs",

"devDependencies": {
"cordis": "^3.4.0"
"cordis": "^3.5.1"
},
"peerDependencies": {
"cordis": "^3.4.0"
"cordis": "^3.5.1"
}
}
import { Context, Service } from 'cordis'
import { defineProperty, remove } from 'cosmokit'
import Logger from 'reggol'
import { remove } from 'cosmokit'
export { Logger }
declare module 'cordis' {
interface Context {
timer: TimerService
setTimeout(callback: () => void, delay: number): () => void
setInterval(callback: () => void, delay: number): () => void
sleep(delay: number): Promise<void>
throttle<F extends (...args: any[]) => void>(callback: F, delay: number, noTrailing?: boolean): WithDispose<F>
debounce<F extends (...args: any[]) => void>(callback: F, delay: number): WithDispose<F>
}
}
type WithDispose<T> = T & { dispose: () => void }
class TimerService extends Service {
constructor(ctx: Context) {
super(ctx, 'timer', true)
defineProperty(this, Context.current, ctx)
ctx.mixin('timer', ['setTimeout', 'setInterval', 'sleep', 'throttle', 'debounce'])
}
createTimerDispose(timer: number | NodeJS.Timeout) {
const dispose = () => {
clearTimeout(timer)
if (!this[Context.current].scope) return
return remove(this[Context.current].scope.disposables, dispose)
}
this[Context.current].scope.disposables.push(dispose)
setTimeout(callback: () => void, delay: number) {
const dispose = this[Context.current].effect(() => {
const timer = setTimeout(() => {
dispose()
callback()
}, delay)
return () => clearTimeout(timer)
})
return dispose
}
setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]) {
const dispose = this.createTimerDispose(setTimeout(() => {
dispose()
callback()
}, ms, ...args))
return dispose
setInterval(callback: () => void, delay: number) {
return this[Context.current].effect(() => {
const timer = setInterval(callback, delay)
return () => clearInterval(timer)
})
}
setInterval(callback: (...args: any[]) => void, ms: number, ...args: any[]) {
return this.createTimerDispose(setInterval(callback, ms, ...args))
}
sleep(ms: number) {
sleep(delay: number) {
const caller = this[Context.current]
return new Promise<void>((resolve, reject) => {

@@ -47,12 +48,56 @@ const dispose1 = this.setTimeout(() => {

resolve()
}, ms)
const dispose2 = this[Context.current].on('dispose', () => {
}, delay)
const dispose2 = caller.on('dispose', () => {
dispose1()
dispose2()
reject(new Error('Context disposed'))
reject(new Error('Context has been disposed'))
})
})
}
private createWrapper(callback: (args: any[], check: () => boolean) => any, isDisposed = false) {
const caller = this[Context.current]
caller.scope.assertActive()
let timer: number | NodeJS.Timeout | undefined
const dispose = () => {
isDisposed = true
remove(caller.scope.disposables, dispose)
clearTimeout(timer)
}
const wrapper: any = (...args: any[]) => {
clearTimeout(timer)
timer = callback(args, () => !isDisposed && caller.scope.isActive)
}
wrapper.dispose = dispose
caller.scope.disposables.push(dispose)
return wrapper
}
throttle<F extends (...args: any[]) => void>(callback: F, delay: number, noTrailing?: boolean): WithDispose<F> {
let lastCall = -Infinity
const execute = (...args: any[]) => {
lastCall = Date.now()
callback(...args)
}
return this.createWrapper((args, isActive) => {
const now = Date.now()
const remaining = delay - (now - lastCall)
if (remaining <= 0) {
execute(...args)
} else if (isActive()) {
return setTimeout(execute, remaining, ...args)
}
}, noTrailing)
}
debounce<F extends (...args: any[]) => void>(callback: F, delay: number): WithDispose<F> {
return this.createWrapper((args, isActive) => {
if (!isActive()) return
return setTimeout(callback, delay, ...args)
})
}
}
export default TimerService

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc