forked from microsoft/tsyringe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Add support for cleaning up disposable instances, part deux (m…
…icrosoft#183) * [feat] Add support for cleaning up disposable instances * [feat] Add support for asynchronous disposables Co-authored-by: Steven Hobson-Campbell <[email protected]> Co-authored-by: Lee C <[email protected]>
- Loading branch information
1 parent
837692c
commit 675c3c4
Showing
8 changed files
with
218 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Disposable, {isDisposable} from "../types/disposable"; | ||
|
||
describe("Disposable", () => { | ||
describe("isDisposable", () => { | ||
it("returns false for non-disposable object", () => { | ||
const nonDisposable = {}; | ||
|
||
expect(isDisposable(nonDisposable)).toBeFalsy(); | ||
}); | ||
|
||
it("returns false when dispose method takes too many args", () => { | ||
const specialDisposable = { | ||
dispose(_: any) {} | ||
}; | ||
|
||
expect(isDisposable(specialDisposable)).toBeFalsy(); | ||
}); | ||
|
||
it("returns true for disposable object", () => { | ||
const disposable: Disposable = { | ||
dispose() {} | ||
}; | ||
|
||
expect(isDisposable(disposable)).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export default interface Disposable { | ||
dispose(): Promise<void> | void; | ||
} | ||
|
||
export function isDisposable(value: any): value is Disposable { | ||
if (typeof value.dispose !== "function") return false; | ||
|
||
const disposeFun: Function = value.dispose; | ||
|
||
// `.dispose()` takes in no arguments | ||
if (disposeFun.length > 0) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
Oops, something went wrong.