@tobes31415/dispose
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -21,5 +21,5 @@ | ||
▸ **assertNotDisposed**(`object`: object, `message?`: undefined | string): *void* | ||
▸ **assertNotDisposed**(`object`: object, `message`: string): *void* | ||
*Defined in [dispose.ts:91](https://github.com/tobes31415/dispose/blob/5e812d6/src/dispose.ts#L91)* | ||
*Defined in [dispose.ts:91](https://github.com/tobes31415/dispose/blob/dfb44ec/src/dispose.ts#L91)* | ||
@@ -30,6 +30,6 @@ Throws an error if the object has been disposed | ||
Name | Type | Description | | ||
------ | ------ | ------ | | ||
`object` | object | The object to inspect | | ||
`message?` | undefined | string | An optional message, otherwise the error will say "Object has been disposed" | | ||
Name | Type | Default | Description | | ||
------ | ------ | ------ | ------ | | ||
`object` | object | - | The object to inspect | | ||
`message` | string | "Object has been disposed" | The message for the error object | | ||
@@ -42,5 +42,5 @@ **Returns:** *void* | ||
▸ **createDisposeableFunctionWrapper**<**T**>(`fnRef`: T, `message?`: undefined | string, `silent`: boolean): *T* | ||
▸ **createDisposeableFunctionWrapper**<**T**>(`fnRef`: T, `message`: string, `silent`: boolean): *T* | ||
*Defined in [dispose.ts:103](https://github.com/tobes31415/dispose/blob/5e812d6/src/dispose.ts#L103)* | ||
*Defined in [dispose.ts:103](https://github.com/tobes31415/dispose/blob/dfb44ec/src/dispose.ts#L103)* | ||
@@ -58,4 +58,4 @@ Wraps a function so that it can be disposed. Once disposed the function will no longer execute | ||
`fnRef` | T | - | The function to be wrapped | | ||
`message?` | undefined | string | - | The message to be included in the error if invoked after the wrapper is disposed. Mutually exclusive with silent | | ||
`silent` | boolean | false | Optional[false] If true attempting to invoke the function after the wrapper has been disposed will fail silently, otherwise it'll throw an exception. Mutually exclusive with message. | | ||
`message` | string | "Function has been disposed" | The message to be included in the error if invoked after the wrapper is disposed. Mutually exclusive with silent | | ||
`silent` | boolean | false | If true attempting to invoke the function after the wrapper has been disposed will fail silently, otherwise it'll throw an error. Mutually exclusive with message. | | ||
@@ -70,3 +70,3 @@ **Returns:** *T* | ||
*Defined in [dispose.ts:36](https://github.com/tobes31415/dispose/blob/5e812d6/src/dispose.ts#L36)* | ||
*Defined in [dispose.ts:36](https://github.com/tobes31415/dispose/blob/dfb44ec/src/dispose.ts#L36)* | ||
@@ -89,3 +89,3 @@ Disposes an object | ||
*Defined in [dispose.ts:28](https://github.com/tobes31415/dispose/blob/5e812d6/src/dispose.ts#L28)* | ||
*Defined in [dispose.ts:28](https://github.com/tobes31415/dispose/blob/dfb44ec/src/dispose.ts#L28)* | ||
@@ -108,3 +108,3 @@ Returns true if an object has been disposed, otherwise false | ||
*Defined in [dispose.ts:55](https://github.com/tobes31415/dispose/blob/5e812d6/src/dispose.ts#L55)* | ||
*Defined in [dispose.ts:55](https://github.com/tobes31415/dispose/blob/dfb44ec/src/dispose.ts#L55)* | ||
@@ -130,3 +130,3 @@ Attaches a callback that will be invoked when the object is disposed. | ||
*Defined in [dispose.ts:82](https://github.com/tobes31415/dispose/blob/5e812d6/src/dispose.ts#L82)* | ||
*Defined in [dispose.ts:82](https://github.com/tobes31415/dispose/blob/dfb44ec/src/dispose.ts#L82)* | ||
@@ -150,3 +150,3 @@ When objectA is disposed, objectB will also be disposed | ||
*Defined in [dispose.ts:70](https://github.com/tobes31415/dispose/blob/5e812d6/src/dispose.ts#L70)* | ||
*Defined in [dispose.ts:70](https://github.com/tobes31415/dispose/blob/dfb44ec/src/dispose.ts#L70)* | ||
@@ -153,0 +153,0 @@ Explicitly instructs an object to dispose recursively. This is actually the default behaviour. You only need to call this if you need to override the dispoal of an object but still want the dispose to be recursively applied. |
@@ -34,3 +34,3 @@ declare type action = () => void; | ||
* @param object The object to inspect | ||
* @param message An optional message, otherwise the error will say "Object has been disposed" | ||
* @param message The message for the error object | ||
*/ | ||
@@ -42,5 +42,5 @@ export declare function assertNotDisposed(object: object, message?: string): void; | ||
* @param message The message to be included in the error if invoked after the wrapper is disposed. Mutually exclusive with silent | ||
* @param silent Optional[false] If true attempting to invoke the function after the wrapper has been disposed will fail silently, otherwise it'll throw an exception. Mutually exclusive with message. | ||
* @param silent If true attempting to invoke the function after the wrapper has been disposed will fail silently, otherwise it'll throw an error. Mutually exclusive with message. | ||
*/ | ||
export declare function createDisposeableFunctionWrapper<T extends Function>(fnRef: T, message?: string, silent?: boolean): T; | ||
export {}; |
@@ -14,3 +14,3 @@ const DISPOSE_CALLBACKS = Symbol("Dispose Callbacks"); | ||
if (Array.isArray(object)) { | ||
object.forEach(action); | ||
object.filter(i => i).forEach(action); | ||
} | ||
@@ -82,7 +82,7 @@ else { | ||
* @param object The object to inspect | ||
* @param message An optional message, otherwise the error will say "Object has been disposed" | ||
* @param message The message for the error object | ||
*/ | ||
export function assertNotDisposed(object, message) { | ||
export function assertNotDisposed(object, message = "Object has been disposed") { | ||
if (isDisposed(object)) { | ||
throw new Error(message || "Object has been disposed"); | ||
throw new Error(message); | ||
} | ||
@@ -94,9 +94,9 @@ } | ||
* @param message The message to be included in the error if invoked after the wrapper is disposed. Mutually exclusive with silent | ||
* @param silent Optional[false] If true attempting to invoke the function after the wrapper has been disposed will fail silently, otherwise it'll throw an exception. Mutually exclusive with message. | ||
* @param silent If true attempting to invoke the function after the wrapper has been disposed will fail silently, otherwise it'll throw an error. Mutually exclusive with message. | ||
*/ | ||
export function createDisposeableFunctionWrapper(fnRef, message, silent = false) { | ||
export function createDisposeableFunctionWrapper(fnRef, message = "Function has been disposed", silent = false) { | ||
if (!fnRef) { | ||
throw new Error("fnRef is required"); | ||
} | ||
if (message && silent) { | ||
if (!!message && message !== "Function has been disposed" && silent) { | ||
throw new Error("message and silent are mutually exclusive"); | ||
@@ -103,0 +103,0 @@ } |
@@ -1,1 +0,1 @@ | ||
const s=Symbol("Dispose Callbacks"),e=Symbol("Is Disposed"),o=Symbol("Force recursive dispose");function r(s){try{s()}catch(s){console.error("Error occured during dispose: "+s)}}function i(s,e){Array.isArray(s)?s.forEach(e):i(Object.values(s),e)}export function isDisposed(s){return!!s[e]};export function dispose(o){isDisposed(o)||(o[e]=!0,o[s]?o[s].forEach(r):i(o,dispose))};export function onDispose(e,o){isDisposed(e)?r(o):(e[s]||(e[s]=[]),e[s].push(o))};export function onDisposeDisposeRecursively(s){s[o]||(s[o]=!0,onDispose(s,()=>i(s,dispose)))};export function onDisposeChain(s,e){onDispose(s,()=>dispose(e))};export function assertNotDisposed(s,e){if(isDisposed(s))throw new Error(e||"Object has been disposed")};export function createDisposeableFunctionWrapper(s,e,o=!1){if(!s)throw new Error("fnRef is required");if(e&&o)throw new Error("message and silent are mutually exclusive");const r=(...i)=>{if(o){if(isDisposed(r))return}else assertNotDisposed(r,e);return s.apply(this,i)};return r}; | ||
const s=Symbol("Dispose Callbacks"),e=Symbol("Is Disposed"),o=Symbol("Force recursive dispose");function i(s){try{s()}catch(s){console.error("Error occured during dispose: "+s)}}function r(s,e){Array.isArray(s)?s.filter(s=>s).forEach(e):r(Object.values(s),e)}export function isDisposed(s){return!!s[e]};export function dispose(o){isDisposed(o)||(o[e]=!0,o[s]?o[s].forEach(i):r(o,dispose))};export function onDispose(e,o){isDisposed(e)?i(o):(e[s]||(e[s]=[]),e[s].push(o))};export function onDisposeDisposeRecursively(s){s[o]||(s[o]=!0,onDispose(s,()=>r(s,dispose)))};export function onDisposeChain(s,e){onDispose(s,()=>dispose(e))};export function assertNotDisposed(s,e="Object has been disposed"){if(isDisposed(s))throw new Error(e)};export function createDisposeableFunctionWrapper(s,e="Function has been disposed",o=!1){if(!s)throw new Error("fnRef is required");if(e&&"Function has been disposed"!==e&&o)throw new Error("message and silent are mutually exclusive");const i=(...r)=>{if(o){if(isDisposed(i))return}else assertNotDisposed(i,e);return s.apply(this,r)};return i}; |
{ | ||
"name": "@tobes31415/dispose", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Dispose objects using the decorator pattern", | ||
@@ -8,4 +8,5 @@ "main": "lib/dispose.min.js", | ||
"scripts": { | ||
"test": "npm run build:compile:quick && npx jest bin", | ||
"build": "npm run build:compile:full && npm run build:copy && npm run uglify && npm run flowgen && npm run apidocs", | ||
"test": "npm run build:compile:quick && npm run test:jest", | ||
"test:jest": "jest bin", | ||
"build": "npm run test && npm run build:compile:full && npm run build:copy && npm run uglify && npm run flowgen && npm run apidocs", | ||
"build:compile:quick": "tsc", | ||
@@ -12,0 +13,0 @@ "build:compile:full": "tsc --declaration", |
@@ -145,2 +145,21 @@ import * as gc from "./dispose"; | ||
expect(didInvoke).toBe(true); | ||
}) | ||
test("Chains of objects do get disposed", () => { | ||
const a = {}; | ||
const b = {}; | ||
const c = {}; | ||
gc.onDisposeChain(a,b); | ||
gc.onDisposeChain(b,c); | ||
let didInvoke = false; | ||
gc.onDispose(c, () => didInvoke = true); | ||
gc.dispose(a); | ||
expect(didInvoke).toBe(true); | ||
}) | ||
test.only("Recursively disposing objects with null properties doesn't fail", () => { | ||
const subObj = {}; | ||
const obj = [null, subObj]; | ||
gc.dispose(obj); | ||
expect(gc.isDisposed(subObj)).toBe(true); | ||
}) |
@@ -18,3 +18,3 @@ type action = () => void; | ||
if (Array.isArray(object)) { | ||
object.forEach(action); | ||
object.filter(i => i).forEach(action); | ||
} else { | ||
@@ -90,7 +90,7 @@ forEachChild(Object.values(object), action); | ||
* @param object The object to inspect | ||
* @param message An optional message, otherwise the error will say "Object has been disposed" | ||
* @param message The message for the error object | ||
*/ | ||
export function assertNotDisposed(object: object, message?: string): void { | ||
export function assertNotDisposed(object: object, message: string = "Object has been disposed"): void { | ||
if (isDisposed(object)) { | ||
throw new Error(message || "Object has been disposed"); | ||
throw new Error(message); | ||
} | ||
@@ -103,7 +103,7 @@ } | ||
* @param message The message to be included in the error if invoked after the wrapper is disposed. Mutually exclusive with silent | ||
* @param silent Optional[false] If true attempting to invoke the function after the wrapper has been disposed will fail silently, otherwise it'll throw an exception. Mutually exclusive with message. | ||
* @param silent If true attempting to invoke the function after the wrapper has been disposed will fail silently, otherwise it'll throw an error. Mutually exclusive with message. | ||
*/ | ||
export function createDisposeableFunctionWrapper<T extends Function>( | ||
fnRef: T, | ||
message?: string, | ||
message: string = "Function has been disposed", | ||
silent: boolean = false | ||
@@ -114,3 +114,3 @@ ): T { | ||
} | ||
if (message && silent) { | ||
if (!!message && message !== "Function has been disposed" && silent) { | ||
throw new Error("message and silent are mutually exclusive"); | ||
@@ -117,0 +117,0 @@ } |
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
37190
511