@tsdotnet/disposable
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -8,3 +8,3 @@ { | ||
"extends": [ | ||
"standard" | ||
"typescript" | ||
], | ||
@@ -24,15 +24,4 @@ "globals": { | ||
"rules": { | ||
"no-unused-vars": "off", | ||
"@typescript-eslint/no-unused-vars": "error", | ||
"@typescript-eslint/semi": "error", | ||
"semi": "off", | ||
"no-tabs": "off", | ||
"indent": [ | ||
"error", | ||
"tab" | ||
], | ||
"space-before-function-paren": "off", | ||
"no-inner-declarations": "off", | ||
"no-redeclare": "off" | ||
"@typescript-eslint/no-inferrable-types": "off" | ||
} | ||
} |
@@ -5,9 +5,10 @@ /*! | ||
*/ | ||
import IDisposableAware from './IDisposableAware'; | ||
declare abstract class DisposableBase implements IDisposableAware { | ||
import DisposableAware from './DisposableAware'; | ||
declare abstract class DisposableBase implements DisposableAware { | ||
protected _disposableObjectName: string; | ||
private readonly __finalizer?; | ||
private __wasDisposed; | ||
protected constructor(_disposableObjectName: string, __finalizer?: (() => void | null) | undefined); | ||
private __wasDisposed; | ||
get wasDisposed(): boolean; | ||
dispose(): void; | ||
/** | ||
@@ -19,3 +20,2 @@ * Utility for throwing exception when this object is accessed. | ||
protected throwIfDisposed(message?: string, objectName?: string): true | never; | ||
dispose(): void; | ||
/** | ||
@@ -22,0 +22,0 @@ * Is called when this object is disposed. Should not be called directly. |
@@ -1,2 +0,1 @@ | ||
/* tslint:disable:variable-name */ | ||
/*! | ||
@@ -8,3 +7,2 @@ * @author electricessence / https://github.com/electricessence/ | ||
class DisposableBase { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor(_disposableObjectName, __finalizer) { | ||
@@ -18,27 +16,17 @@ this._disposableObjectName = _disposableObjectName; | ||
} | ||
/** | ||
* Utility for throwing exception when this object is accessed. | ||
* @param message | ||
* @param objectName Optional object name override. | ||
*/ | ||
throwIfDisposed(message, objectName = this._disposableObjectName) { | ||
if (this.__wasDisposed) | ||
throw new ObjectDisposedException(objectName); | ||
return true; | ||
} | ||
// NOTE: Do not override this method. Override _onDispose instead. | ||
dispose() { | ||
const _ = this; | ||
if (!_.__wasDisposed) { | ||
if (!this.__wasDisposed) { | ||
// Preemptively set wasDisposed in order to prevent repeated disposing. | ||
// NOTE: in true multi-threaded scenarios, this would need to be synchronized. | ||
_.__wasDisposed = true; | ||
this.__wasDisposed = true; | ||
try { | ||
_._onDispose(); // Protected override. | ||
this._onDispose(); // Protected override. | ||
} | ||
finally { | ||
if (_.__finalizer) { | ||
if (this.__finalizer) { | ||
// Private finalizer... | ||
_.__finalizer(); | ||
_.__finalizer = undefined; | ||
this.__finalizer(); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this.__finalizer = undefined; | ||
} | ||
@@ -48,4 +36,13 @@ } | ||
} | ||
/** | ||
* Utility for throwing exception when this object is accessed. | ||
* @param message | ||
* @param objectName Optional object name override. | ||
*/ | ||
throwIfDisposed(message, objectName = this._disposableObjectName) { | ||
if (this.__wasDisposed) | ||
throw new ObjectDisposedException(objectName); | ||
return true; | ||
} | ||
// Placeholder for overrides. | ||
// tslint:disable-next-line:no-empty | ||
/** | ||
@@ -52,0 +49,0 @@ * Is called when this object is disposed. Should not be called directly. |
@@ -5,4 +5,4 @@ /*! | ||
*/ | ||
import IDisposable from './IDisposable'; | ||
export declare type DisposableItem = IDisposable | null | undefined; | ||
import Disposable from './Disposable'; | ||
export declare type DisposableItem = Disposable | null | undefined; | ||
export declare type DisposableItemArray = DisposableItem[] | null | undefined; | ||
@@ -83,3 +83,3 @@ /** | ||
*/ | ||
export declare function using<TDisposable extends IDisposable, TReturn>(disposable: TDisposable, closure: (disposable: TDisposable) => TReturn): TReturn; | ||
export declare function using<TDisposable extends Disposable, TReturn>(disposable: TDisposable, closure: (disposable: TDisposable) => TReturn): TReturn; | ||
export default dispose; |
@@ -5,2 +5,3 @@ /*! | ||
*/ | ||
/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-namespace,no-inner-declarations */ | ||
/** | ||
@@ -7,0 +8,0 @@ * Takes any number of disposables as arguments and attempts to dispose them. |
@@ -5,8 +5,8 @@ /*! | ||
*/ | ||
import IDisposable from './IDisposable'; | ||
import IDisposableAware from './IDisposableAware'; | ||
import Disposable from './Disposable'; | ||
import DisposableAware from './DisposableAware'; | ||
import ObjectDisposedException from './ObjectDisposedException'; | ||
import DisposableBase from './DisposableBase'; | ||
import dispose from './dispose'; | ||
export { IDisposable, IDisposableAware, ObjectDisposedException, dispose, DisposableBase }; | ||
export { Disposable, DisposableAware, ObjectDisposedException, dispose, DisposableBase }; | ||
export default DisposableBase; |
@@ -5,7 +5,7 @@ /*! | ||
*/ | ||
import IDisposableAware from './IDisposableAware'; | ||
import DisposableAware from './DisposableAware'; | ||
export default class ObjectDisposedException extends Error { | ||
readonly objectName: string; | ||
constructor(objectName: string, message?: string); | ||
static throwIfDisposed(disposable: IDisposableAware, objectName: string, message?: string): true | never; | ||
static throwIfDisposed(disposable: DisposableAware, objectName: string, message?: string): true | never; | ||
} |
{ | ||
"name": "@tsdotnet/disposable", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "A disposable base class and minimal set of interfaces to properly implement an object disposal pattern.", | ||
@@ -21,5 +21,4 @@ "author": "electricessence", | ||
"bump": "npm version patch", | ||
"format": "prettier --write \"src/**/*.ts\"", | ||
"lint": "eslint src/**/*.ts", | ||
"prepublishOnly": "npm run build && npm run lint", | ||
"precommit": "npm run lint && npm run build", | ||
"preversion": "npm run lint", | ||
@@ -41,9 +40,6 @@ "postversion": "git push && git push --tags" | ||
"eslint": "^6.8.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-config-standard": "^14.1.1", | ||
"eslint-config-typescript": "^3.0.0", | ||
"eslint-plugin-import": "^2.20.2", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-standard": "^4.0.1", | ||
"prettier": "^2.0.5", | ||
"rimraf": "^3.0.2", | ||
@@ -50,0 +46,0 @@ "ts-node": "^8.9.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
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
11
25257
20
420