Socket
Socket
Sign inDemoInstall

@wixc3/patterns

Package Overview
Dependencies
Maintainers
64
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 13.4.0 to 14.0.0

30

dist/cjs/disposables/create-disposables.d.ts

@@ -7,2 +7,10 @@ import { GroupConstraints } from './constraints';

/**
* disposable name, used in error or when timed out
*/
name: string;
/**
* the subject to dispose
*/
dispose: DisposableItem;
/**
* @default DEFAULT_TIMEOUT

@@ -12,6 +20,2 @@ */

/**
* disposable name, used in error when timed out
*/
name?: string;
/**
* disposal group name

@@ -33,3 +37,3 @@ * @default DEFAULT_GROUP

* ```ts
* const disposables = createDisposables();
* const disposables = createDisposables('sample');
* disposables.add(() => console.log('disposable 1'));

@@ -44,3 +48,3 @@ * disposables.add({dispose: () => console.log('disposable 2')});

* ```ts
* const disposables = createDisposables();
* const disposables = createDisposables('sample');
* disposables.registerGroup('first', {before: DEFAULT_GROUP});

@@ -60,7 +64,8 @@ * disposables.registerGroup('last', {after: DEFAULT_GROUP});

*/
export declare function createDisposables(initialGroups?: string[]): Disposables;
export declare function createDisposables(name: string, initialGroups?: string[]): Disposables;
export declare class Disposables {
private name;
private readonly groups;
private readonly constrains;
constructor(initialGroups?: string[]);
constructor(name: string, initialGroups?: string[]);
/**

@@ -70,4 +75,9 @@ * register a new constrained disposal group

*/
registerGroup(name: string, _constraints: GroupConstraints[] | GroupConstraints): void;
registerGroup(groupName: string, constraints: GroupConstraints[] | GroupConstraints): void;
/**
* register a new constrained disposal group if it doesn't exist ignore otherwise
* @param constraints - constraints for the group must contain {before: groupName} or {after: groupName}
*/
registerGroupIfNotExists(groupName: string, constraints: GroupConstraints[] | GroupConstraints): void;
/**
* @param disposable a function or object with a dispose method

@@ -77,3 +87,3 @@ * @param options if string, will be used as group name

*/
add(disposable: DisposableItem, options?: DisposableOptions | string): () => void;
add(...[nameOrOptions, disposable]: [id: string, disposable: DisposableItem] | [options: DisposableOptions]): () => void;
/**

@@ -80,0 +90,0 @@ * removes a disposable from all disposal group

@@ -6,3 +6,2 @@ "use strict";

const disposables_group_1 = require("./disposables-group");
const common_1 = require("@wixc3/common");
exports.DEFAULT_GROUP = 'default';

@@ -14,8 +13,2 @@ exports.DEFAULT_TIMEOUT = 1000;

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

@@ -32,3 +25,3 @@ * Disposables allow adding of disposal async functions,

* ```ts
* const disposables = createDisposables();
* const disposables = createDisposables('sample');
* disposables.add(() => console.log('disposable 1'));

@@ -43,3 +36,3 @@ * disposables.add({dispose: () => console.log('disposable 2')});

* ```ts
* const disposables = createDisposables();
* const disposables = createDisposables('sample');
* disposables.registerGroup('first', {before: DEFAULT_GROUP});

@@ -59,8 +52,9 @@ * disposables.registerGroup('last', {after: DEFAULT_GROUP});

*/
function createDisposables(initialGroups = []) {
return new Disposables(initialGroups);
function createDisposables(name, initialGroups = []) {
return new Disposables(name, initialGroups);
}
exports.createDisposables = createDisposables;
class Disposables {
constructor(initialGroups = []) {
constructor(name, initialGroups = []) {
this.name = name;
this.groups = [createGroup(exports.DEFAULT_GROUP)];

@@ -83,14 +77,24 @@ this.constrains = [];

*/
registerGroup(name, _constraints) {
const nConstraints = (0, constraints_1.normalizeConstraints)(_constraints, name, this.groups);
registerGroup(groupName, constraints) {
const nConstraints = (0, constraints_1.normalizeConstraints)(constraints, groupName, this.groups);
const { lastAfter, firstBefore } = (0, constraints_1.getGroupConstrainedIndex)(nConstraints, this.groups);
this.constrains.push(...nConstraints);
if (lastAfter > 0) {
this.groups.splice(lastAfter + 1, 0, createGroup(name));
this.groups.splice(lastAfter + 1, 0, createGroup(groupName));
}
else {
this.groups.splice(firstBefore, 0, createGroup(name));
this.groups.splice(firstBefore, 0, createGroup(groupName));
}
}
/**
* register a new constrained disposal group if it doesn't exist ignore otherwise
* @param constraints - constraints for the group must contain {before: groupName} or {after: groupName}
*/
registerGroupIfNotExists(groupName, constraints) {
const existing = this.groups.some((g) => g.name === groupName);
if (!existing) {
this.registerGroup(groupName, constraints);
}
}
/**
* @param disposable a function or object with a dispose method

@@ -100,7 +104,10 @@ * @param options if string, will be used as group name

*/
add(disposable, options) {
if (typeof options === 'string') {
options = { group: options };
add(...[nameOrOptions, disposable]) {
if (typeof nameOrOptions === 'string') {
if (!disposable) {
throw new Error(`Invalid disposable: must be a function or object with a dispose method got ${disposable}`);
}
nameOrOptions = { name: nameOrOptions, dispose: disposable };
}
const { group: groupName, name, timeout } = withDefaults(options);
const { group: groupName = exports.DEFAULT_GROUP, name, dispose, timeout = exports.DEFAULT_TIMEOUT } = nameOrOptions;
const group = this.groups.find((g) => g.name === groupName);

@@ -110,4 +117,4 @@ if (!group) {

}
group.disposables.add(disposable, timeout, name);
return () => group.disposables.remove(disposable);
group.disposables.add(dispose, timeout, `[${this.name}]: ${name}`);
return () => group.disposables.remove(dispose);
}

@@ -114,0 +121,0 @@ /**

@@ -35,3 +35,3 @@ import { Disposables } from '.';

private intervals;
constructor();
constructor(name?: string);
/**

@@ -38,0 +38,0 @@ * Starts instance disposal:

@@ -36,10 +36,10 @@ "use strict";

class Disposable {
constructor() {
constructor(name) {
this._isDisposed = false;
this._isDisposing = false;
this.disposables = new _1.Disposables();
this.timeouts = new Set();
this.intervals = new Set();
this.disposables = new _1.Disposables(name !== null && name !== void 0 ? name : this.constructor.name);
this.disposables.registerGroup(DELAY_DISPOSAL, { before: 'default' });
this.disposables.add(() => {
this.disposables.add('dispose timeouts and intervals', () => {
this.timeouts.forEach((t) => clearTimeout(t));

@@ -85,6 +85,7 @@ this.intervals.forEach((i) => clearInterval(i));

const { promise: canDispose, resolve: done } = (0, promise_assist_1.deferred)();
const remove = this.disposables.add(() => canDispose, {
const remove = this.disposables.add({
group: DELAY_DISPOSAL,
name,
timeout,
dispose: () => canDispose,
});

@@ -91,0 +92,0 @@ canDispose.then(remove).catch(common_1.noop);

@@ -14,6 +14,12 @@ "use strict";

async dispose() {
var _a;
const _disposables = Array.from(this.disposables).reverse();
this.disposables.clear();
for (const [disposable, details] of _disposables) {
await (0, promise_assist_1.timeout)(disposeOf(disposable), details.timeout, `Disposal timed out: "${details.name}" after ${details.timeout}ms`);
try {
await (0, promise_assist_1.timeout)(disposeOf(disposable), details.timeout, `Disposal timed out: "${details.name}" after ${details.timeout}ms`);
}
catch (e) {
throw new Error(`Disposal failed: "${details.name}"\n${(_a = e === null || e === void 0 ? void 0 : e.stack) !== null && _a !== void 0 ? _a : ''}`);
}
}

@@ -20,0 +26,0 @@ }

@@ -7,2 +7,10 @@ import { GroupConstraints } from './constraints';

/**
* disposable name, used in error or when timed out
*/
name: string;
/**
* the subject to dispose
*/
dispose: DisposableItem;
/**
* @default DEFAULT_TIMEOUT

@@ -12,6 +20,2 @@ */

/**
* disposable name, used in error when timed out
*/
name?: string;
/**
* disposal group name

@@ -33,3 +37,3 @@ * @default DEFAULT_GROUP

* ```ts
* const disposables = createDisposables();
* const disposables = createDisposables('sample');
* disposables.add(() => console.log('disposable 1'));

@@ -44,3 +48,3 @@ * disposables.add({dispose: () => console.log('disposable 2')});

* ```ts
* const disposables = createDisposables();
* const disposables = createDisposables('sample');
* disposables.registerGroup('first', {before: DEFAULT_GROUP});

@@ -60,7 +64,8 @@ * disposables.registerGroup('last', {after: DEFAULT_GROUP});

*/
export declare function createDisposables(initialGroups?: string[]): Disposables;
export declare function createDisposables(name: string, initialGroups?: string[]): Disposables;
export declare class Disposables {
private name;
private readonly groups;
private readonly constrains;
constructor(initialGroups?: string[]);
constructor(name: string, initialGroups?: string[]);
/**

@@ -70,4 +75,9 @@ * register a new constrained disposal group

*/
registerGroup(name: string, _constraints: GroupConstraints[] | GroupConstraints): void;
registerGroup(groupName: string, constraints: GroupConstraints[] | GroupConstraints): void;
/**
* register a new constrained disposal group if it doesn't exist ignore otherwise
* @param constraints - constraints for the group must contain {before: groupName} or {after: groupName}
*/
registerGroupIfNotExists(groupName: string, constraints: GroupConstraints[] | GroupConstraints): void;
/**
* @param disposable a function or object with a dispose method

@@ -77,3 +87,3 @@ * @param options if string, will be used as group name

*/
add(disposable: DisposableItem, options?: DisposableOptions | string): () => void;
add(...[nameOrOptions, disposable]: [id: string, disposable: DisposableItem] | [options: DisposableOptions]): () => void;
/**

@@ -80,0 +90,0 @@ * removes a disposable from all disposal group

import { getGroupConstrainedIndex, normalizeConstraints } from './constraints';
import { DisposablesGroup } from './disposables-group';
import { defaults } from '@wixc3/common';
export const DEFAULT_GROUP = 'default';

@@ -10,8 +9,2 @@ export const DEFAULT_TIMEOUT = 1000;

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

@@ -28,3 +21,3 @@ * Disposables allow adding of disposal async functions,

* ```ts
* const disposables = createDisposables();
* const disposables = createDisposables('sample');
* disposables.add(() => console.log('disposable 1'));

@@ -39,3 +32,3 @@ * disposables.add({dispose: () => console.log('disposable 2')});

* ```ts
* const disposables = createDisposables();
* const disposables = createDisposables('sample');
* disposables.registerGroup('first', {before: DEFAULT_GROUP});

@@ -55,7 +48,8 @@ * disposables.registerGroup('last', {after: DEFAULT_GROUP});

*/
export function createDisposables(initialGroups = []) {
return new Disposables(initialGroups);
export function createDisposables(name, initialGroups = []) {
return new Disposables(name, initialGroups);
}
export class Disposables {
constructor(initialGroups = []) {
constructor(name, initialGroups = []) {
this.name = name;
this.groups = [createGroup(DEFAULT_GROUP)];

@@ -78,14 +72,24 @@ this.constrains = [];

*/
registerGroup(name, _constraints) {
const nConstraints = normalizeConstraints(_constraints, name, this.groups);
registerGroup(groupName, constraints) {
const nConstraints = normalizeConstraints(constraints, groupName, this.groups);
const { lastAfter, firstBefore } = getGroupConstrainedIndex(nConstraints, this.groups);
this.constrains.push(...nConstraints);
if (lastAfter > 0) {
this.groups.splice(lastAfter + 1, 0, createGroup(name));
this.groups.splice(lastAfter + 1, 0, createGroup(groupName));
}
else {
this.groups.splice(firstBefore, 0, createGroup(name));
this.groups.splice(firstBefore, 0, createGroup(groupName));
}
}
/**
* register a new constrained disposal group if it doesn't exist ignore otherwise
* @param constraints - constraints for the group must contain {before: groupName} or {after: groupName}
*/
registerGroupIfNotExists(groupName, constraints) {
const existing = this.groups.some((g) => g.name === groupName);
if (!existing) {
this.registerGroup(groupName, constraints);
}
}
/**
* @param disposable a function or object with a dispose method

@@ -95,7 +99,10 @@ * @param options if string, will be used as group name

*/
add(disposable, options) {
if (typeof options === 'string') {
options = { group: options };
add(...[nameOrOptions, disposable]) {
if (typeof nameOrOptions === 'string') {
if (!disposable) {
throw new Error(`Invalid disposable: must be a function or object with a dispose method got ${disposable}`);
}
nameOrOptions = { name: nameOrOptions, dispose: disposable };
}
const { group: groupName, name, timeout } = withDefaults(options);
const { group: groupName = DEFAULT_GROUP, name, dispose, timeout = DEFAULT_TIMEOUT } = nameOrOptions;
const group = this.groups.find((g) => g.name === groupName);

@@ -105,4 +112,4 @@ if (!group) {

}
group.disposables.add(disposable, timeout, name);
return () => group.disposables.remove(disposable);
group.disposables.add(dispose, timeout, `[${this.name}]: ${name}`);
return () => group.disposables.remove(dispose);
}

@@ -109,0 +116,0 @@ /**

@@ -35,3 +35,3 @@ import { Disposables } from '.';

private intervals;
constructor();
constructor(name?: string);
/**

@@ -38,0 +38,0 @@ * Starts instance disposal:

@@ -33,10 +33,10 @@ import { defaults, noop } from '@wixc3/common';

export class Disposable {
constructor() {
constructor(name) {
this._isDisposed = false;
this._isDisposing = false;
this.disposables = new Disposables();
this.timeouts = new Set();
this.intervals = new Set();
this.disposables = new Disposables(name !== null && name !== void 0 ? name : this.constructor.name);
this.disposables.registerGroup(DELAY_DISPOSAL, { before: 'default' });
this.disposables.add(() => {
this.disposables.add('dispose timeouts and intervals', () => {
this.timeouts.forEach((t) => clearTimeout(t));

@@ -82,6 +82,7 @@ this.intervals.forEach((i) => clearInterval(i));

const { promise: canDispose, resolve: done } = deferred();
const remove = this.disposables.add(() => canDispose, {
const remove = this.disposables.add({
group: DELAY_DISPOSAL,
name,
timeout,
dispose: () => canDispose,
});

@@ -88,0 +89,0 @@ canDispose.then(remove).catch(noop);

@@ -11,6 +11,12 @@ import { timeout } from 'promise-assist';

async dispose() {
var _a;
const _disposables = Array.from(this.disposables).reverse();
this.disposables.clear();
for (const [disposable, details] of _disposables) {
await timeout(disposeOf(disposable), details.timeout, `Disposal timed out: "${details.name}" after ${details.timeout}ms`);
try {
await timeout(disposeOf(disposable), details.timeout, `Disposal timed out: "${details.name}" after ${details.timeout}ms`);
}
catch (e) {
throw new Error(`Disposal failed: "${details.name}"\n${(_a = e === null || e === void 0 ? void 0 : e.stack) !== null && _a !== void 0 ? _a : ''}`);
}
}

@@ -17,0 +23,0 @@ }

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

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

"dependencies": {
"@wixc3/common": "^13.4.0",
"@wixc3/common": "^14.0.0",
"promise-assist": "^2.0.1"
}
}
import { DisposalGroup, getGroupConstrainedIndex, GroupConstraints, normalizeConstraints } from './constraints';
import { DisposableItem, DisposablesGroup } from './disposables-group';
import { defaults } from '@wixc3/common';

@@ -15,2 +14,10 @@ export const DEFAULT_GROUP = 'default';

/**
* disposable name, used in error or when timed out
*/
name: string;
/**
* the subject to dispose
*/
dispose: DisposableItem;
/**
* @default DEFAULT_TIMEOUT

@@ -20,6 +27,2 @@ */

/**
* disposable name, used in error when timed out
*/
name?: string;
/**
* disposal group name

@@ -31,10 +34,2 @@ * @default DEFAULT_GROUP

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

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

* ```ts
* const disposables = createDisposables();
* const disposables = createDisposables('sample');
* disposables.add(() => console.log('disposable 1'));

@@ -62,3 +57,3 @@ * disposables.add({dispose: () => console.log('disposable 2')});

* ```ts
* const disposables = createDisposables();
* const disposables = createDisposables('sample');
* disposables.registerGroup('first', {before: DEFAULT_GROUP});

@@ -78,4 +73,4 @@ * disposables.registerGroup('last', {after: DEFAULT_GROUP});

*/
export function createDisposables(initialGroups: string[] = []) {
return new Disposables(initialGroups);
export function createDisposables(name: string, initialGroups: string[] = []) {
return new Disposables(name, initialGroups);
}

@@ -86,3 +81,3 @@

private readonly constrains: GroupConstraints[] = [];
constructor(initialGroups: string[] = []) {
constructor(private name: string, initialGroups: string[] = []) {
this.groups.push(...initialGroups.map(createGroup));

@@ -94,4 +89,4 @@ }

*/
registerGroup(name: string, _constraints: GroupConstraints[] | GroupConstraints) {
const nConstraints = normalizeConstraints(_constraints, name, this.groups);
registerGroup(groupName: string, constraints: GroupConstraints[] | GroupConstraints) {
const nConstraints = normalizeConstraints(constraints, groupName, this.groups);
const { lastAfter, firstBefore } = getGroupConstrainedIndex(nConstraints, this.groups);

@@ -101,7 +96,17 @@ this.constrains.push(...nConstraints);

if (lastAfter > 0) {
this.groups.splice(lastAfter + 1, 0, createGroup(name));
this.groups.splice(lastAfter + 1, 0, createGroup(groupName));
} else {
this.groups.splice(firstBefore, 0, createGroup(name));
this.groups.splice(firstBefore, 0, createGroup(groupName));
}
}
/**
* register a new constrained disposal group if it doesn't exist ignore otherwise
* @param constraints - constraints for the group must contain {before: groupName} or {after: groupName}
*/
registerGroupIfNotExists(groupName: string, constraints: GroupConstraints[] | GroupConstraints) {
const existing = this.groups.some((g) => g.name === groupName);
if (!existing) {
this.registerGroup(groupName, constraints);
}
}

@@ -113,7 +118,13 @@ /**

*/
add(disposable: DisposableItem, options?: DisposableOptions | string) {
if (typeof options === 'string') {
options = { group: options };
add(...[nameOrOptions, disposable]: [id: string, disposable: DisposableItem] | [options: DisposableOptions]) {
if (typeof nameOrOptions === 'string') {
if (!disposable) {
throw new Error(
`Invalid disposable: must be a function or object with a dispose method got ${disposable}`
);
}
nameOrOptions = { name: nameOrOptions, dispose: disposable };
}
const { group: groupName, name, timeout } = withDefaults(options);
const { group: groupName = DEFAULT_GROUP, name, dispose, timeout = DEFAULT_TIMEOUT } = nameOrOptions;
const group = this.groups.find((g) => g.name === groupName);

@@ -123,4 +134,5 @@ if (!group) {

}
group.disposables.add(disposable, timeout, name);
return () => group.disposables.remove(disposable);
group.disposables.add(dispose, timeout, `[${this.name}]: ${name}`);
return () => group.disposables.remove(dispose);
}

@@ -127,0 +139,0 @@

@@ -36,8 +36,9 @@ import { defaults, noop } from '@wixc3/common';

private _isDisposing = false;
public readonly disposables = new Disposables();
public readonly disposables: Disposables;
private timeouts = new Set<ReturnType<typeof setTimeout>>();
private intervals = new Set<ReturnType<typeof setInterval>>();
constructor() {
constructor(name?: string) {
this.disposables = new Disposables(name ?? this.constructor.name);
this.disposables.registerGroup(DELAY_DISPOSAL, { before: 'default' });
this.disposables.add(() => {
this.disposables.add('dispose timeouts and intervals', () => {
this.timeouts.forEach((t) => clearTimeout(t));

@@ -116,6 +117,7 @@ this.intervals.forEach((i) => clearInterval(i));

const { promise: canDispose, resolve: done } = deferred();
const remove = this.disposables.add(() => canDispose, {
const remove = this.disposables.add({
group: DELAY_DISPOSAL,
name,
timeout,
dispose: () => canDispose,
});

@@ -122,0 +124,0 @@ canDispose.then(remove).catch(noop);

@@ -13,7 +13,11 @@ import { timeout } from 'promise-assist';

for (const [disposable, details] of _disposables) {
await timeout(
disposeOf(disposable),
details.timeout,
`Disposal timed out: "${details.name}" after ${details.timeout}ms`
);
try {
await timeout(
disposeOf(disposable),
details.timeout,
`Disposal timed out: "${details.name}" after ${details.timeout}ms`
);
} catch (e) {
throw new Error(`Disposal failed: "${details.name}"\n${(e as Error)?.stack ?? ''}`);
}
}

@@ -20,0 +24,0 @@ }

@@ -16,3 +16,3 @@ import { expect } from 'chai';

let wasDisposed = false;
disposable.disposables.add(() => (wasDisposed = true));
disposable.disposables.add('wasDisposed', () => (wasDisposed = true));

@@ -27,3 +27,3 @@ expect(wasDisposed).to.be.false;

const disposable = new Disposable();
disposable.disposables.add(() => sleep(20));
disposable.disposables.add('sleep', () => sleep(20));
void disposable.dispose();

@@ -34,3 +34,3 @@ expect(() => disposable.disposalGuard()).to.throw('Instance was disposed');

const disposable = new Disposable();
disposable.disposables.add(() => sleep(20));
disposable.disposables.add('sleep', () => sleep(20));
const disposing = disposable.dispose();

@@ -47,3 +47,3 @@ expect(() => disposable.disposalGuard({ async: false, usedWhileDisposing: true })).not.to.throw();

let disposeCalled = false;
disposable.disposables.add(() => {
disposable.disposables.add('disposeCalled', () => {
disposeCalled = true;

@@ -61,3 +61,3 @@ });

let disposeCalled = false;
disposable.disposables.add(() => {
disposable.disposables.add('disposeCalled', () => {
disposeCalled = true;

@@ -64,0 +64,0 @@ });

@@ -11,5 +11,5 @@ import { expect, use } from 'chai';

const disposed: number[] = [];
const disposables = createDisposables();
disposables.add(() => disposed.push(1));
disposables.add({ dispose: () => disposed.push(2) });
const disposables = createDisposables('test');
disposables.add('first', () => disposed.push(1));
disposables.add({ name: 'second', dispose: () => disposed.push(2) });
await disposables.dispose();

@@ -20,9 +20,9 @@ expect(disposed).to.deep.equal([2, 1]);

const disposed: number[] = [];
const disposables = createDisposables();
disposables.add(() => disposed.push(1));
disposables.add(async () => {
const disposables = createDisposables('test');
disposables.add('sync 1', () => disposed.push(1));
disposables.add('sleep 2', async () => {
await sleep(10);
disposed.push(2);
});
disposables.add(async () => {
disposables.add('sleep 3', async () => {
await sleep(50);

@@ -35,10 +35,24 @@ disposed.push(3);

it('times out when the disposal takes too long', async () => {
const disposables = createDisposables();
disposables.add(
async () => {
const disposables = createDisposables('test');
disposables.add({
name: 'slow',
timeout: 1,
dispose: async () => {
await sleep(100);
},
{ name: 'slow', timeout: 1 }
});
await expect(disposables.dispose()).to.eventually.be.rejectedWith('Disposal timed out: "[test]: slow"');
});
it('fail with the name of specific dispose', async () => {
const disposables = createDisposables('test');
disposables.add({
name: 'disposing with error',
dispose: () => {
throw new Error('failed!');
},
});
await expect(disposables.dispose()).to.be.rejectedWith(
/Disposal failed: "\[test\]: disposing with error"\nError: failed!/
);
await expect(disposables.dispose()).to.eventually.be.rejectedWith('Disposal timed out: "slow"');
});

@@ -49,5 +63,5 @@ });

const disposed: number[] = [];
const disposables = createDisposables(['A', 'B']);
disposables.add(() => disposed.push(2), 'B');
disposables.add(() => disposed.push(1), 'A');
const disposables = createDisposables('test', ['A', 'B']);
disposables.add('B', () => disposed.push(2));
disposables.add('A', () => disposed.push(1));
await disposables.dispose();

@@ -58,7 +72,7 @@ expect(disposed).to.deep.equal([1, 2]);

const disposed: number[] = [];
const disposables = createDisposables(['A', 'C']);
const disposables = createDisposables('test', ['A', 'C']);
disposables.registerGroup('B', { before: 'C' });
disposables.add(() => disposed.push(1), 'A');
disposables.add(() => disposed.push(2), 'B');
disposables.add(() => disposed.push(3), 'C');
disposables.add({ name: 'A', group: 'A', dispose: () => disposed.push(1) });
disposables.add({ name: 'B', group: 'B', dispose: () => disposed.push(2) });
disposables.add({ name: 'C', group: 'C', dispose: () => disposed.push(3) });
await disposables.dispose();

@@ -71,3 +85,3 @@ expect(disposed).to.deep.equal([1, 2, 3]);

it('throws for missing groups', () => {
const groups = createDisposables();
const groups = createDisposables('test');
expect(() => groups.registerGroup('group1', { before: 'group2' })).to.throw(

@@ -78,3 +92,3 @@ `Invalid constraint: "before: group2" - group not found`

it('throws for no constraints', () => {
const groups = createDisposables();
const groups = createDisposables('test');
expect(() => groups.registerGroup('group1', [])).to.throw(

@@ -85,3 +99,3 @@ `Invalid disposal group: must have at least one constraint`

it('throws for contradictory constraints', () => {
const groups = createDisposables();
const groups = createDisposables('test');
groups.registerGroup('before', { before: 'default' });

@@ -95,3 +109,3 @@ groups.registerGroup('after', { after: 'default' });

it('requires a unique group name', () => {
const groups = createDisposables();
const groups = createDisposables('test');
expect(() => groups.registerGroup('default', { before: 'default' })).to.throw(

@@ -104,18 +118,18 @@ `Invalid group: "default" already exists`

it('add (with a string as options)', () => {
const groups = createDisposables();
groups.registerGroup('first', { before: 'default' });
groups.add(() => void 0, 'first');
const groups = createDisposables('test');
groups.add('first', () => void 0);
expect(groups.list().groups[0]?.disposables).to.have.length(1);
});
it('add (with options obj)', () => {
const groups = createDisposables();
const groups = createDisposables('test');
groups.registerGroup('first', { before: 'default' });
groups.add(() => void 0, {
groups.add({
name: 'lucky',
group: 'first',
name: 'lucky',
timeout: 1,
dispose: () => void 0,
});
expect(groups.list().groups[0]?.disposables).to.eql([
{
name: 'lucky',
name: '[test]: lucky',
timeout: 1,

@@ -126,4 +140,4 @@ },

it('add returns a remove func', () => {
const disposables = createDisposables();
const remove = disposables.add(() => void 0);
const disposables = createDisposables('test');
const remove = disposables.add('no effect', () => void 0);

@@ -136,5 +150,5 @@ expect(disposables.list().groups[0]?.disposables).to.have.length(1);

it('added disposables can be removed by reference', () => {
const disposables = createDisposables();
const disposables = createDisposables('test');
const disposable = () => void 0;
disposables.add(disposable);
disposables.add('no effect', disposable);

@@ -147,5 +161,5 @@ expect(disposables.list().groups[0]?.disposables).to.have.length(1);

it('removing missing disposables have no effect', () => {
const disposables = createDisposables();
const disposables = createDisposables('test');
const disposable = () => void 0;
disposables.add(disposable);
disposables.add('no effect', disposable);

@@ -160,17 +174,20 @@ expect(disposables.list().groups[0]?.disposables).to.have.length(1);

it('returns the list of groups', () => {
const groups = createDisposables();
const groups = createDisposables('test');
groups.registerGroup('first', { before: 'default' });
groups.add(() => void 0, {
groups.add({
name: '1',
group: 'first',
timeout: 1,
dispose: () => void 0,
});
groups.add(() => void 0, {
groups.add({
name: '2',
group: 'first',
timeout: 10,
dispose: () => void 0,
});
groups.add(() => void 0, {
groups.add({
name: '3',
timeout: 100,
dispose: () => void 0,
});

@@ -187,3 +204,19 @@

});
describe('ensureGroup', () => {
it('register a group', () => {
const groups = createDisposables('test');
groups.registerGroupIfNotExists('first', { before: 'default' });
expect(groups.list().groups[0]?.name).to.eql('first');
});
it('ignore if group exist', () => {
const groups = createDisposables('test');
groups.registerGroupIfNotExists('first', { after: 'default' });
groups.registerGroupIfNotExists('first', { before: 'default' });
expect(groups.list().groups[0]?.name).to.eql('default');
expect(groups.list().groups[1]?.name).to.eql('first');
});
});
});
});

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

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