@wixc3/patterns
Advanced tools
Comparing version 15.0.0 to 15.1.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Disposables = exports.createDisposables = exports.DEFAULT_TIMEOUT = exports.DEFAULT_GROUP = void 0; | ||
const common_1 = require("@wixc3/common"); | ||
const constraints_1 = require("./constraints"); | ||
@@ -98,3 +99,3 @@ const disposables_group_1 = require("./disposables-group"); | ||
} | ||
group.disposables.add(dispose, timeout, `[${this.name}]: ${name}`); | ||
group.disposables.add(dispose, timeout, `[${this.name}]: ${name}`, (0, common_1.getStackTrace)({ skipLines: 3 })); | ||
return () => group.disposables.remove(dispose); | ||
@@ -101,0 +102,0 @@ } |
@@ -8,3 +8,3 @@ /** | ||
dispose(): Promise<void>; | ||
add(disposable: DisposableItem, timeout: number, name: string): () => boolean; | ||
add(disposable: DisposableItem, timeout: number, name: string, trace: string): () => boolean; | ||
remove(target: DisposableItem): void; | ||
@@ -23,3 +23,4 @@ list(): { | ||
name: string; | ||
trace: string; | ||
}; | ||
//# sourceMappingURL=disposables-group.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DisposablesGroup = void 0; | ||
const common_1 = require("@wixc3/common"); | ||
const promise_assist_1 = require("promise-assist"); | ||
@@ -14,3 +15,2 @@ /** | ||
async dispose() { | ||
var _a; | ||
const _disposables = Array.from(this.disposables).reverse(); | ||
@@ -20,14 +20,19 @@ this.disposables.clear(); | ||
try { | ||
await (0, promise_assist_1.timeout)(disposeOf(disposable), details.timeout, `Disposal timed out: "${details.name}" after ${details.timeout}ms`); | ||
await (0, promise_assist_1.timeout)(disposeOf(disposable), details.timeout, message(details)); | ||
} | ||
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 : ''}`); | ||
if (e.message === message(details)) { | ||
throw (0, common_1.errorWithTrace)(message(details), details.trace); | ||
} | ||
throw (0, common_1.errorWithTrace)(`Disposal failed: "${details.name}"`, details.trace, { | ||
cause: e, | ||
}); | ||
} | ||
} | ||
} | ||
add(disposable, timeout, name) { | ||
add(disposable, timeout, name, trace) { | ||
if (this.disposables.has(disposable)) { | ||
throw new Error(`Disposable already added`); | ||
} | ||
this.disposables.set(disposable, { timeout, name }); | ||
this.disposables.set(disposable, { timeout, name, trace }); | ||
return () => this.disposables.delete(disposable); | ||
@@ -43,2 +48,5 @@ } | ||
exports.DisposablesGroup = DisposablesGroup; | ||
function message(details) { | ||
return `Disposal timed out: "${details.name}" after ${details.timeout}ms`; | ||
} | ||
async function disposeOf(dispose) { | ||
@@ -45,0 +53,0 @@ if (typeof dispose === 'function') { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SafeDisposable = void 0; | ||
const common_1 = require("@wixc3/common"); | ||
const _1 = require("."); | ||
@@ -85,3 +86,3 @@ const promise_assist_1 = require("promise-assist"); | ||
if (this.isDisposed && !(this._isDisposing && usedWhileDisposing)) { | ||
throw new Error('Instance was disposed'); | ||
throw (0, common_1.errorWithTrace)('Instance was disposed', (0, common_1.getStackTrace)()); | ||
} | ||
@@ -88,0 +89,0 @@ const { promise: canDispose, resolve: done } = (0, promise_assist_1.deferred)(); |
@@ -0,1 +1,2 @@ | ||
import { getStackTrace } from '@wixc3/common'; | ||
import { getGroupConstrainedIndex, normalizeConstraints } from './constraints'; | ||
@@ -94,3 +95,3 @@ import { DisposablesGroup } from './disposables-group'; | ||
} | ||
group.disposables.add(dispose, timeout, `[${this.name}]: ${name}`); | ||
group.disposables.add(dispose, timeout, `[${this.name}]: ${name}`, getStackTrace({ skipLines: 3 })); | ||
return () => group.disposables.remove(dispose); | ||
@@ -97,0 +98,0 @@ } |
@@ -8,3 +8,3 @@ /** | ||
dispose(): Promise<void>; | ||
add(disposable: DisposableItem, timeout: number, name: string): () => boolean; | ||
add(disposable: DisposableItem, timeout: number, name: string, trace: string): () => boolean; | ||
remove(target: DisposableItem): void; | ||
@@ -23,3 +23,4 @@ list(): { | ||
name: string; | ||
trace: string; | ||
}; | ||
//# sourceMappingURL=disposables-group.d.ts.map |
@@ -0,1 +1,2 @@ | ||
import { errorWithTrace } from '@wixc3/common'; | ||
import { timeout } from 'promise-assist'; | ||
@@ -11,3 +12,2 @@ /** | ||
async dispose() { | ||
var _a; | ||
const _disposables = Array.from(this.disposables).reverse(); | ||
@@ -17,14 +17,19 @@ this.disposables.clear(); | ||
try { | ||
await timeout(disposeOf(disposable), details.timeout, `Disposal timed out: "${details.name}" after ${details.timeout}ms`); | ||
await timeout(disposeOf(disposable), details.timeout, message(details)); | ||
} | ||
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 : ''}`); | ||
if (e.message === message(details)) { | ||
throw errorWithTrace(message(details), details.trace); | ||
} | ||
throw errorWithTrace(`Disposal failed: "${details.name}"`, details.trace, { | ||
cause: e, | ||
}); | ||
} | ||
} | ||
} | ||
add(disposable, timeout, name) { | ||
add(disposable, timeout, name, trace) { | ||
if (this.disposables.has(disposable)) { | ||
throw new Error(`Disposable already added`); | ||
} | ||
this.disposables.set(disposable, { timeout, name }); | ||
this.disposables.set(disposable, { timeout, name, trace }); | ||
return () => this.disposables.delete(disposable); | ||
@@ -39,2 +44,5 @@ } | ||
} | ||
function message(details) { | ||
return `Disposal timed out: "${details.name}" after ${details.timeout}ms`; | ||
} | ||
async function disposeOf(dispose) { | ||
@@ -41,0 +49,0 @@ if (typeof dispose === 'function') { |
@@ -0,1 +1,2 @@ | ||
import { errorWithTrace, getStackTrace } from '@wixc3/common'; | ||
import { Disposables } from '.'; | ||
@@ -82,3 +83,3 @@ import { deferred } from 'promise-assist'; | ||
if (this.isDisposed && !(this._isDisposing && usedWhileDisposing)) { | ||
throw new Error('Instance was disposed'); | ||
throw errorWithTrace('Instance was disposed', getStackTrace()); | ||
} | ||
@@ -85,0 +86,0 @@ const { promise: canDispose, resolve: done } = deferred(); |
{ | ||
"name": "@wixc3/patterns", | ||
"version": "15.0.0", | ||
"version": "15.1.0", | ||
"description": "A utility for saving objects to be disposed", | ||
@@ -21,5 +21,5 @@ "main": "dist/cjs/index.js", | ||
"dependencies": { | ||
"@wixc3/common": "^15.0.0", | ||
"@wixc3/common": "^15.1.0", | ||
"promise-assist": "^2.0.1" | ||
} | ||
} |
@@ -0,1 +1,2 @@ | ||
import { getStackTrace } from '@wixc3/common'; | ||
import { DisposalGroup, getGroupConstrainedIndex, GroupConstraints, normalizeConstraints } from './constraints'; | ||
@@ -75,6 +76,3 @@ import { DisposableItem, DisposablesGroup } from './disposables-group'; | ||
private readonly constrains: GroupConstraints[] = []; | ||
constructor( | ||
private name: string, | ||
initialGroups: string[] = [], | ||
) { | ||
constructor(private name: string, initialGroups: string[] = []) { | ||
this.dispose = this.dispose.bind(this); | ||
@@ -124,3 +122,3 @@ this.groups.push(...initialGroups.map(createGroup)); | ||
throw new Error( | ||
`Invalid disposable: must be a function or object with a dispose method got ${disposable}`, | ||
`Invalid disposable: must be a function or object with a dispose method got ${disposable}` | ||
); | ||
@@ -136,3 +134,3 @@ } | ||
group.disposables.add(dispose, timeout, `[${this.name}]: ${name}`); | ||
group.disposables.add(dispose, timeout, `[${this.name}]: ${name}`, getStackTrace({ skipLines: 3 })); | ||
return () => group.disposables.remove(dispose); | ||
@@ -139,0 +137,0 @@ } |
@@ -0,1 +1,2 @@ | ||
import { errorWithTrace } from '@wixc3/common'; | ||
import { timeout } from 'promise-assist'; | ||
@@ -14,9 +15,10 @@ | ||
try { | ||
await timeout( | ||
disposeOf(disposable), | ||
details.timeout, | ||
`Disposal timed out: "${details.name}" after ${details.timeout}ms`, | ||
); | ||
await timeout(disposeOf(disposable), details.timeout, message(details)); | ||
} catch (e) { | ||
throw new Error(`Disposal failed: "${details.name}"\n${(e as Error)?.stack ?? ''}`); | ||
if ((e as Error).message === message(details)) { | ||
throw errorWithTrace(message(details), details.trace); | ||
} | ||
throw errorWithTrace(`Disposal failed: "${details.name}"`, details.trace, { | ||
cause: e, | ||
}); | ||
} | ||
@@ -26,7 +28,7 @@ } | ||
add(disposable: DisposableItem, timeout: number, name: string) { | ||
add(disposable: DisposableItem, timeout: number, name: string, trace: string) { | ||
if (this.disposables.has(disposable)) { | ||
throw new Error(`Disposable already added`); | ||
} | ||
this.disposables.set(disposable, { timeout, name }); | ||
this.disposables.set(disposable, { timeout, name, trace }); | ||
return () => this.disposables.delete(disposable); | ||
@@ -50,4 +52,9 @@ } | ||
name: string; | ||
trace: string; | ||
}; | ||
function message(details: NamedDisposable): string { | ||
return `Disposal timed out: "${details.name}" after ${details.timeout}ms`; | ||
} | ||
async function disposeOf(dispose: DisposableItem) { | ||
@@ -54,0 +61,0 @@ if (typeof dispose === 'function') { |
@@ -0,1 +1,2 @@ | ||
import { errorWithTrace, getStackTrace } from '@wixc3/common'; | ||
import { Disposables } from '.'; | ||
@@ -44,3 +45,3 @@ import { deferred } from 'promise-assist'; | ||
* // disposal may begin | ||
* } | ||
* } | ||
* } | ||
@@ -125,3 +126,3 @@ * ``` | ||
if (this.isDisposed && !(this._isDisposing && usedWhileDisposing)) { | ||
throw new Error('Instance was disposed'); | ||
throw errorWithTrace('Instance was disposed', getStackTrace()); | ||
} | ||
@@ -128,0 +129,0 @@ const { promise: canDispose, resolve: done } = deferred(); |
@@ -53,3 +53,4 @@ import { expect, use } from 'chai'; | ||
name: 'disposing with error', | ||
dispose: () => { | ||
// eslint-disable-next-line @typescript-eslint/require-await | ||
dispose: async () => { | ||
throw new Error('failed!'); | ||
@@ -59,5 +60,15 @@ }, | ||
await expect(disposables.dispose()).to.be.rejectedWith( | ||
/Disposal failed: "\[test\]: disposing with error"\nError: failed!/, | ||
); | ||
try { | ||
await disposables.dispose(); | ||
} catch (e) { | ||
const { message, stack, cause } = e as Error; | ||
expect(message, 'message').to.equal('Disposal failed: "[test]: disposing with error"'); | ||
expect((cause as Error).message, 'cause').to.equal('failed!'); | ||
const [_, disposeFn, mocha] = stack?.split('\n') ?? []; | ||
expect(disposeFn, 'dispose fn').to.match(/at Context\.<anonymous>.*disposables\.unit\.ts:\d+:\d+/); | ||
expect(mocha, 'mocha').to.match(/at Context\.runnable\.fn/); | ||
return; | ||
} | ||
throw new Error('Expected error to be throws'); | ||
}); | ||
@@ -64,0 +75,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
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
258811
4985
Updated@wixc3/common@^15.1.0