@wixc3/patterns
Advanced tools
Comparing version
@@ -11,3 +11,2 @@ /** | ||
remove(disposable: Disposable): void; | ||
remove(name: string): void; | ||
list(): { | ||
@@ -14,0 +13,0 @@ name: string; |
@@ -16,3 +16,3 @@ "use strict"; | ||
constructor() { | ||
this.disposables = new Map(); | ||
this.disposables = new Set(); | ||
} | ||
@@ -27,17 +27,10 @@ async dispose() { | ||
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 }); | ||
this.disposables.add({ 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) { | ||
const oldSize = this.disposables.size; | ||
this.disposables = new Set((0, common_1.filter)(this.disposables, (d) => d.dispose !== target)); | ||
if (oldSize === this.disposables.size) { | ||
throw new Error(`Disposable not found`); | ||
} | ||
if (!this.disposables.has(name)) { | ||
throw new Error(`Disposable with name "${name}" not found`); | ||
} | ||
this.disposables.delete(name); | ||
} | ||
@@ -44,0 +37,0 @@ list() { |
@@ -11,3 +11,2 @@ /** | ||
remove(disposable: Disposable): void; | ||
remove(name: string): void; | ||
list(): { | ||
@@ -14,0 +13,0 @@ name: string; |
@@ -1,2 +0,2 @@ | ||
import { find } from '@wixc3/common'; | ||
import { filter } from '@wixc3/common'; | ||
import { timeout } from 'promise-assist'; | ||
@@ -12,3 +12,3 @@ /** | ||
constructor() { | ||
this.disposables = new Map(); | ||
this.disposables = new Set(); | ||
} | ||
@@ -23,17 +23,10 @@ async dispose() { | ||
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 }); | ||
this.disposables.add({ 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) { | ||
const oldSize = this.disposables.size; | ||
this.disposables = new Set(filter(this.disposables, (d) => d.dispose !== target)); | ||
if (oldSize === this.disposables.size) { | ||
throw new Error(`Disposable not found`); | ||
} | ||
if (!this.disposables.has(name)) { | ||
throw new Error(`Disposable with name "${name}" not found`); | ||
} | ||
this.disposables.delete(name); | ||
} | ||
@@ -40,0 +33,0 @@ list() { |
{ | ||
"name": "@wixc3/patterns", | ||
"version": "9.2.0", | ||
"version": "9.2.1", | ||
"description": "A utility for saving objects to be disposed", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
@@ -1,2 +0,2 @@ | ||
import { find } from '@wixc3/common'; | ||
import { filter } from '@wixc3/common'; | ||
import { timeout } from 'promise-assist'; | ||
@@ -13,3 +13,3 @@ | ||
export class Disposables { | ||
private disposables = new Map<string, NamedDisposable>(); | ||
private disposables = new Set<NamedDisposable>(); | ||
async dispose() { | ||
@@ -28,20 +28,12 @@ const _disposables = Array.from(this.disposables.values()).reverse(); | ||
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 }); | ||
this.disposables.add({ 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) { | ||
remove(target: Disposable): void { | ||
const oldSize = this.disposables.size; | ||
this.disposables = new Set(filter(this.disposables, (d) => d.dispose !== target)); | ||
if (oldSize === this.disposables.size) { | ||
throw new Error(`Disposable not found`); | ||
} | ||
if (!this.disposables.has(name)) { | ||
throw new Error(`Disposable with name "${name}" not found`); | ||
} | ||
this.disposables.delete(name); | ||
} | ||
@@ -48,0 +40,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
159227
-1.04%2917
-0.82%