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

@wixc3/patterns

Package Overview
Dependencies
Maintainers
68
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wixc3/patterns - npm Package Compare versions

Comparing version 8.2.1 to 9.0.0

15

dist/cjs/disposables/disposable.d.ts

@@ -5,7 +5,10 @@ /**

*/
export declare function createSimpleDisposable(): {
export declare function createSimpleDisposable(): Disposables;
export declare class Disposables {
private disposables;
dispose(): Promise<void>;
add(disposable: Disposable): void;
add(disposable: Disposable, timeout: number, name: string): void;
remove(disposable: Disposable): void;
};
remove(name: string): void;
}
export type DisposeFunction = () => unknown;

@@ -15,3 +18,7 @@ export type Disposable = {

} | DisposeFunction;
export type Disposables = ReturnType<typeof createSimpleDisposable>;
export type NamedDisposable = {
timeout: number;
name: string;
dispose: Disposable;
};
//# sourceMappingURL=disposable.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSimpleDisposable = void 0;
exports.Disposables = exports.createSimpleDisposable = void 0;
const common_1 = require("@wixc3/common");
const promise_assist_1 = require("promise-assist");
/**

@@ -9,25 +11,43 @@ * Disposables allow adding of disposal async functions,

function createSimpleDisposable() {
const disposables = new Set();
return {
async dispose() {
const toDispose = Array.from(disposables).reverse();
disposables.clear();
for (const dispose of toDispose) {
if (typeof dispose === 'function') {
await dispose();
}
else {
await dispose.dispose();
}
}
},
add(disposable) {
disposables.add(disposable);
},
remove(disposable) {
disposables.delete(disposable);
},
};
return new Disposables();
}
exports.createSimpleDisposable = createSimpleDisposable;
class Disposables {
constructor() {
this.disposables = new Map();
}
async dispose() {
const _disposables = Array.from(this.disposables.values()).reverse();
this.disposables.clear();
for (const disposable of _disposables) {
await (0, promise_assist_1.timeout)(disposeOf(disposable), disposable.timeout, `Disposal timed out: "${disposable.name}" after ${disposable.timeout}ms`);
}
}
add(disposable, timeout, name) {
if (this.disposables.has(name)) {
throw new Error(`Disposable with name "${name}" already exists`);
}
this.disposables.set(name, { dispose: disposable, timeout, name });
}
remove(target) {
var _a;
const name = typeof target === 'string' ? target : (_a = (0, common_1.find)(this.disposables, ([_, d]) => d.dispose === target)) === null || _a === void 0 ? void 0 : _a[0];
if (!name) {
throw new Error(`Disposable not found`);
}
if (!this.disposables.has(name)) {
throw new Error(`Disposable with name "${name}" not found`);
}
this.disposables.delete(name);
}
}
exports.Disposables = Disposables;
async function disposeOf({ dispose }) {
if (typeof dispose === 'function') {
await dispose();
}
else {
await dispose.dispose();
}
}
//# sourceMappingURL=disposable.js.map
import { GroupConstraints } from './constraints';
import { Disposable } from '.';
export declare const DEFAULT_GROUP = "default";
export declare const DEFAULT_TIMEOUT = 10000;
export type DisposableOptions = {
/**
* @default DEFAULT_TIMEOUT
*/
timeout?: number;
/**
* disposable name, used in error when timed out
*/
name?: string;
/**
* disposal group name
* @default DEFAULT_GROUP
*/
group?: string;
};
/**

@@ -46,3 +62,3 @@ * Disposables allow adding of disposal async functions,

registerGroup: (name: string, constraints: GroupConstraints[] | GroupConstraints) => void;
add: (disposable: Disposable, name?: string) => void;
add: (disposable: Disposable, options?: DisposableOptions) => void;
/**

@@ -49,0 +65,0 @@ * removes a disposable from all disposal group

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDisposables = exports.DEFAULT_GROUP = void 0;
exports.createDisposables = exports.DEFAULT_TIMEOUT = exports.DEFAULT_GROUP = void 0;
const constraints_1 = require("./constraints");
const _1 = require(".");
const common_1 = require("@wixc3/common");
exports.DEFAULT_GROUP = 'default';
exports.DEFAULT_TIMEOUT = 10000;
const createGroup = (name) => ({

@@ -11,2 +13,8 @@ name,

});
let count = 0;
const withDefaults = (d) => (0, common_1.defaults)(d || {}, {
timeout: exports.DEFAULT_TIMEOUT,
group: exports.DEFAULT_GROUP,
name: `unnamed-${count++}`,
});
/**

@@ -65,8 +73,9 @@ * Disposables allow adding of disposal async functions,

},
add: (disposable, name = exports.DEFAULT_GROUP) => {
const group = groups.find((g) => g.name === name);
add: (disposable, options) => {
const { group: groupName, name, timeout } = withDefaults(options);
const group = groups.find((g) => g.name === groupName);
if (!group) {
throw new Error(`Invalid group: "${name}" doesn't exists`);
throw new Error(`Invalid group: "${groupName}" doesn't exists`);
}
group.disposables.add(disposable);
group.disposables.add(disposable, timeout, name);
},

@@ -73,0 +82,0 @@ /**

@@ -5,7 +5,10 @@ /**

*/
export declare function createSimpleDisposable(): {
export declare function createSimpleDisposable(): Disposables;
export declare class Disposables {
private disposables;
dispose(): Promise<void>;
add(disposable: Disposable): void;
add(disposable: Disposable, timeout: number, name: string): void;
remove(disposable: Disposable): void;
};
remove(name: string): void;
}
export type DisposeFunction = () => unknown;

@@ -15,3 +18,7 @@ export type Disposable = {

} | DisposeFunction;
export type Disposables = ReturnType<typeof createSimpleDisposable>;
export type NamedDisposable = {
timeout: number;
name: string;
dispose: Disposable;
};
//# sourceMappingURL=disposable.d.ts.map

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

import { find } from '@wixc3/common';
import { timeout } from 'promise-assist';
/**

@@ -6,24 +8,41 @@ * Disposables allow adding of disposal async functions,

export function createSimpleDisposable() {
const disposables = new Set();
return {
async dispose() {
const toDispose = Array.from(disposables).reverse();
disposables.clear();
for (const dispose of toDispose) {
if (typeof dispose === 'function') {
await dispose();
}
else {
await dispose.dispose();
}
}
},
add(disposable) {
disposables.add(disposable);
},
remove(disposable) {
disposables.delete(disposable);
},
};
return new Disposables();
}
export class Disposables {
constructor() {
this.disposables = new Map();
}
async dispose() {
const _disposables = Array.from(this.disposables.values()).reverse();
this.disposables.clear();
for (const disposable of _disposables) {
await timeout(disposeOf(disposable), disposable.timeout, `Disposal timed out: "${disposable.name}" after ${disposable.timeout}ms`);
}
}
add(disposable, timeout, name) {
if (this.disposables.has(name)) {
throw new Error(`Disposable with name "${name}" already exists`);
}
this.disposables.set(name, { dispose: disposable, timeout, name });
}
remove(target) {
var _a;
const name = typeof target === 'string' ? target : (_a = find(this.disposables, ([_, d]) => d.dispose === target)) === null || _a === void 0 ? void 0 : _a[0];
if (!name) {
throw new Error(`Disposable not found`);
}
if (!this.disposables.has(name)) {
throw new Error(`Disposable with name "${name}" not found`);
}
this.disposables.delete(name);
}
}
async function disposeOf({ dispose }) {
if (typeof dispose === 'function') {
await dispose();
}
else {
await dispose.dispose();
}
}
//# sourceMappingURL=disposable.js.map
import { GroupConstraints } from './constraints';
import { Disposable } from '.';
export declare const DEFAULT_GROUP = "default";
export declare const DEFAULT_TIMEOUT = 10000;
export type DisposableOptions = {
/**
* @default DEFAULT_TIMEOUT
*/
timeout?: number;
/**
* disposable name, used in error when timed out
*/
name?: string;
/**
* disposal group name
* @default DEFAULT_GROUP
*/
group?: string;
};
/**

@@ -46,3 +62,3 @@ * Disposables allow adding of disposal async functions,

registerGroup: (name: string, constraints: GroupConstraints[] | GroupConstraints) => void;
add: (disposable: Disposable, name?: string) => void;
add: (disposable: Disposable, options?: DisposableOptions) => void;
/**

@@ -49,0 +65,0 @@ * removes a disposable from all disposal group

import { getValidatedConstantsGroups, normalizeConstraints } from './constraints';
import { createSimpleDisposable } from '.';
import { defaults } from '@wixc3/common';
export const DEFAULT_GROUP = 'default';
export const DEFAULT_TIMEOUT = 10000;
const createGroup = (name) => ({

@@ -8,2 +10,8 @@ name,

});
let count = 0;
const withDefaults = (d) => defaults(d || {}, {
timeout: DEFAULT_TIMEOUT,
group: DEFAULT_GROUP,
name: `unnamed-${count++}`,
});
/**

@@ -62,8 +70,9 @@ * Disposables allow adding of disposal async functions,

},
add: (disposable, name = DEFAULT_GROUP) => {
const group = groups.find((g) => g.name === name);
add: (disposable, options) => {
const { group: groupName, name, timeout } = withDefaults(options);
const group = groups.find((g) => g.name === groupName);
if (!group) {
throw new Error(`Invalid group: "${name}" doesn't exists`);
throw new Error(`Invalid group: "${groupName}" doesn't exists`);
}
group.disposables.add(disposable);
group.disposables.add(disposable, timeout, name);
},

@@ -70,0 +79,0 @@ /**

{
"name": "@wixc3/patterns",
"version": "8.2.1",
"version": "9.0.0",
"description": "A utility for saving objects to be disposed",

@@ -21,5 +21,5 @@ "main": "dist/cjs/index.js",

"dependencies": {
"@wixc3/common": "^8.2.1",
"@wixc3/common": "^9.0.0",
"promise-assist": "^2.0.1"
}
}

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

import { find } from '@wixc3/common';
import { timeout } from 'promise-assist';
/**

@@ -6,23 +9,39 @@ * Disposables allow adding of disposal async functions,

export function createSimpleDisposable() {
const disposables = new Set<Disposable>();
return new Disposables();
}
return {
async dispose() {
const toDispose = Array.from(disposables).reverse();
disposables.clear();
for (const dispose of toDispose) {
if (typeof dispose === 'function') {
await dispose();
} else {
await dispose.dispose();
}
}
},
add(disposable: Disposable) {
disposables.add(disposable);
},
remove(disposable: Disposable) {
disposables.delete(disposable);
},
};
export class Disposables {
private disposables = new Map<string, NamedDisposable>();
async dispose() {
const _disposables = Array.from(this.disposables.values()).reverse();
this.disposables.clear();
for (const disposable of _disposables) {
await timeout(
disposeOf(disposable),
disposable.timeout,
`Disposal timed out: "${disposable.name}" after ${disposable.timeout}ms`
);
}
}
add(disposable: Disposable, timeout: number, name: string) {
if (this.disposables.has(name)) {
throw new Error(`Disposable with name "${name}" already exists`);
}
this.disposables.set(name, { dispose: disposable, timeout, name });
}
remove(disposable: Disposable): void;
remove(name: string): void;
remove(target: string | Disposable): void {
const name =
typeof target === 'string' ? target : find(this.disposables, ([_, d]) => d.dispose === target)?.[0];
if (!name) {
throw new Error(`Disposable not found`);
}
if (!this.disposables.has(name)) {
throw new Error(`Disposable with name "${name}" not found`);
}
this.disposables.delete(name);
}
}

@@ -32,2 +51,15 @@

export type Disposable = { dispose: DisposeFunction } | DisposeFunction;
export type Disposables = ReturnType<typeof createSimpleDisposable>;
export type NamedDisposable = {
timeout: number;
name: string;
dispose: Disposable;
};
async function disposeOf({ dispose }: NamedDisposable) {
if (typeof dispose === 'function') {
await dispose();
} else {
await dispose.dispose();
}
}
import { DisposalGroup, getValidatedConstantsGroups, GroupConstraints, normalizeConstraints } from './constraints';
import { createSimpleDisposable, Disposable } from '.';
import { defaults } from '@wixc3/common';
export const DEFAULT_GROUP = 'default';
export const DEFAULT_TIMEOUT = 10000;

@@ -11,2 +13,26 @@ const createGroup = (name: string): DisposalGroup => ({

export type DisposableOptions = {
/**
* @default DEFAULT_TIMEOUT
*/
timeout?: number;
/**
* disposable name, used in error when timed out
*/
name?: string;
/**
* disposal group name
* @default DEFAULT_GROUP
*/
group?: string;
};
let count = 0;
const withDefaults = (d?: DisposableOptions): Required<DisposableOptions> =>
defaults(d || {}, {
timeout: DEFAULT_TIMEOUT,
group: DEFAULT_GROUP,
name: `unnamed-${count++}`,
});
/**

@@ -66,8 +92,9 @@ * Disposables allow adding of disposal async functions,

add: (disposable: Disposable, name = DEFAULT_GROUP) => {
const group = groups.find((g) => g.name === name);
add: (disposable: Disposable, options?: DisposableOptions) => {
const { group: groupName, name, timeout } = withDefaults(options);
const group = groups.find((g) => g.name === groupName);
if (!group) {
throw new Error(`Invalid group: "${name}" doesn't exists`);
throw new Error(`Invalid group: "${groupName}" doesn't exists`);
}
group.disposables.add(disposable);
group.disposables.add(disposable, timeout, name);
},

@@ -74,0 +101,0 @@

@@ -1,4 +0,6 @@

import { expect } from 'chai';
import { expect, use } from 'chai';
import { sleep } from 'promise-assist';
import { createDisposables } from '../disposables';
import asPromised from 'chai-as-promised';
use(asPromised);

@@ -30,2 +32,12 @@ describe('disposables', () => {

});
it('times out when the disposal takes too long', async () => {
const disposables = createDisposables();
disposables.add(
async () => {
await sleep(100);
},
{ name: 'slow', timeout: 1 }
);
await expect(disposables.dispose()).to.eventually.be.rejectedWith('Disposal timed out: "slow"');
});
});

@@ -32,0 +44,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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