Socket
Socket
Sign inDemoInstall

@cdellacqua/sleep

Package Overview
Dependencies
0
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 5.0.0 to 6.0.0

26

dist/index.d.ts

@@ -1,10 +0,9 @@

import { ReadonlySignal } from '@cdellacqua/signals';
/**
* Any viable type that may be used by a setTimeout implementation.
*/
export declare type TimeoutIdentifier = string | number | boolean | symbol | bigint | object | null;
export type TimeoutIdentifier = string | number | boolean | symbol | bigint | object | null;
/**
* A basic timeout API must provide a setTimeout and a clearTimeout function.
*/
export declare type TimeoutAPI<T extends TimeoutIdentifier> = {
export type TimeoutAPI<T extends TimeoutIdentifier> = {
setTimeout(callback: () => void, ms: number): T;

@@ -16,12 +15,8 @@ clearTimeout(timeoutId: T): void;

*/
export declare type SleepConfig<T extends TimeoutIdentifier> = {
export type SleepConfig<T extends TimeoutIdentifier> = {
/**
* A [ReadonlySignal](https://www.npmjs.com/package/@cdellacqua/signals)
* that can be used to resolve (or reject) the promise before the specified
* delay has passed.
*
* Calling `emit` without any parameter will resolve the promise, while
* calling it with an Error instance will reject it with the passed Error.
* An optional AbortSignal that can be used
* to cancel (by rejecting with the abort reason) the sleep promise before its natural termination.
*/
hurry$?: ReadonlySignal<void | Error>;
signal?: AbortSignal;
/**

@@ -37,4 +32,4 @@ * A custom timeout API that provides setTimeout and clearTimeout.

* providing setTimeout and clearTimeout functions,
* and a [ReadonlySignal](https://www.npmjs.com/package/@cdellacqua/signals) that can be used
* to cancel the sleep promise before its natural termination.
* and an AbortSignal that can be used
* to cancel (by rejecting with the abort reason) the sleep promise before its natural termination.
*

@@ -44,9 +39,6 @@ * Overriding the timeout API can be useful in tests or in scenarios where you would want

*
* Calling `emit` without any parameter will resolve the promise, while
* calling it with an Error instance will reject it with the passed Error.
*
* Note: if the delay is 0 the returned Promise will be already resolved.
*
* @param ms a delay in milliseconds.
* @param config an object containing a hurry$ signal and custom timeout API providing setTimeout and clearTimeout functions.
* @param config an object containing a signal and custom timeout API providing setTimeout and clearTimeout functions.
* @returns a Promise

@@ -53,0 +45,0 @@ */

@@ -13,2 +13,3 @@ const maxSupportedTimeout = 2147483647;

function sleep(ms, config) {
var _a;
const normalizedMs = Math.max(0, Math.ceil(ms));

@@ -19,8 +20,7 @@ const timeoutApi = (config == null ? void 0 : config.timeoutApi) ? config.timeoutApi : {

};
if ((_a = config == null ? void 0 : config.signal) == null ? void 0 : _a.aborted) {
return Promise.reject(config.signal.reason);
}
if (normalizedMs === 0) {
const promise2 = new Promise((res) => {
res();
return () => void 0;
});
return promise2;
return Promise.resolve();
}

@@ -33,13 +33,9 @@ let id;

}, normalizedMs);
if (config == null ? void 0 : config.hurry$) {
const hurry$ = config.hurry$;
hurry$.subscribeOnce((reason) => {
if (config == null ? void 0 : config.signal) {
const signal = config.signal;
signal.addEventListener("abort", () => {
if (id !== void 0) {
timeoutApi.clearTimeout(id);
id = void 0;
if (reason !== void 0) {
rej(reason);
} else {
res();
}
rej(signal.reason);
}

@@ -46,0 +42,0 @@ });

@@ -1,1 +0,1 @@

(function(t,r){typeof exports=="object"&&typeof module!="undefined"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(t=typeof globalThis!="undefined"?globalThis:t||self,r(t.sleep={}))})(this,function(t){"use strict";const r=2147483647,p=(s,e,i)=>(i=i||{id:void 0},e>r?i.id=setTimeout(p,r,s,e-r,i):i.id=setTimeout(s,e),i),n=s=>clearTimeout(s.id);function T(s,e){const i=Math.max(0,Math.ceil(s)),m=(e==null?void 0:e.timeoutApi)?e.timeoutApi:{setTimeout:p,clearTimeout:n};if(i===0)return new Promise(o=>(o(),()=>{}));let u;return new Promise((d,o)=>{u=m.setTimeout(()=>{u=void 0,d()},i),(e==null?void 0:e.hurry$)&&e.hurry$.subscribeOnce(l=>{u!==void 0&&(m.clearTimeout(u),u=void 0,l!==void 0?o(l):d())})})}t.sleep=T,Object.defineProperty(t,"__esModule",{value:!0}),t[Symbol.toStringTag]="Module"});
(function(i,s){typeof exports=="object"&&typeof module!="undefined"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(i=typeof globalThis!="undefined"?globalThis:i||self,s(i.sleep={}))})(this,function(i){"use strict";const s=2147483647,u=(r,e,t)=>(t=t||{id:void 0},e>s?t.id=setTimeout(u,s,r,e-s,t):t.id=setTimeout(r,e),t),p=r=>clearTimeout(r.id);function m(r,e){var l;const t=Math.max(0,Math.ceil(r)),o=(e==null?void 0:e.timeoutApi)?e.timeoutApi:{setTimeout:u,clearTimeout:p};if((l=e==null?void 0:e.signal)==null?void 0:l.aborted)return Promise.reject(e.signal.reason);if(t===0)return Promise.resolve();let d;return new Promise((n,T)=>{if(d=o.setTimeout(()=>{d=void 0,n()},t),e==null?void 0:e.signal){const a=e.signal;a.addEventListener("abort",()=>{d!==void 0&&(o.clearTimeout(d),d=void 0,T(a.reason))})}})}i.sleep=m,Object.defineProperty(i,"__esModule",{value:!0}),i[Symbol.toStringTag]="Module"});

@@ -5,3 +5,3 @@ {

"description": "Delay execution using Promises.",
"version": "5.0.0",
"version": "6.0.0",
"type": "module",

@@ -55,3 +55,3 @@ "types": "dist/index.d.ts",

"@types/mocha": "^9.0.0",
"@types/node": "^16.11.10",
"@types/node": "^18.11.18",
"@typescript-eslint/eslint-plugin": "^5.29.0",

@@ -73,8 +73,5 @@ "c8": "^7.11.3",

"typedoc-plugin-markdown": "^3.12.1",
"typescript": "^4.7.4",
"typescript": "^4.9.5",
"vite": "^2.6.4"
},
"dependencies": {
"@cdellacqua/signals": "^5.0.2"
}
}

@@ -39,2 +39,16 @@ # @cdellacqua/sleep

Abort timeout after 500ms, rejecting the promise:
```ts
import {sleep} from '@cdellacqua/sleep';
async function example() {
try {
await sleep(1000, {signal: AbortSignal.timeout(500)});
} catch (err) {
console.log('see you in half a sec!');
}
}
```
Skip timeout after 500ms:

@@ -44,8 +58,6 @@

import {sleep} from '@cdellacqua/sleep';
import {makeSignal} from '@cdellacqua/signals';
async function example() {
const hurry$ = makeSignal<void>();
setTimeout(() => hurry$.emit(), 500);
await sleep(1000, {hurry$});
// Note the `.catch(...)`!
await sleep(1000, {signal: AbortSignal.timeout(500)}).catch(() => {});
console.log('see you in half a sec!');

@@ -55,15 +67,17 @@ }

Abort timeout after 500ms, rejecting the promise:
Abort timeout when an event happens, rejecting the promise:
```ts
import {sleep} from '@cdellacqua/sleep';
import {makeSignal} from '@cdellacqua/signals';
async function example() {
const hurry$ = makeSignal<void | Error>();
setTimeout(() => hurry$.emit(new Error('ops!')), 500);
const controller = new AbortController();
window.addEventListener('click', () => {
controller.abort();
});
try {
await sleep(1000, {hurry$});
await sleep(1000, {signal: controller.signal});
console.log('nobody clicked within 1 second!');
} catch (err) {
console.log('see you in half a sec!');
console.log('somebody clicked within 1 second!');
}

@@ -70,0 +84,0 @@ }

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc