-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
208 additions
and
16 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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from "./auto/index.js"; | ||
export * from "./box/index.js"; | ||
export * from "./slot/index.js"; | ||
export * from "./tick/index.js"; | ||
|
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,40 @@ | ||
/** | ||
* A reference that can only be disposed once | ||
*/ | ||
export class Once<T> { | ||
|
||
#disposed = false | ||
|
||
/** | ||
* A reference that can only be disposed once | ||
* @param inner | ||
*/ | ||
constructor( | ||
readonly inner: T | ||
) { } | ||
|
||
[Symbol.dispose](this: Once<Disposable>) { | ||
if (this.#disposed) | ||
return | ||
this.#disposed = true | ||
|
||
this.inner[Symbol.dispose]?.() | ||
} | ||
|
||
async [Symbol.asyncDispose](this: Once<AsyncDisposable>) { | ||
if (this.#disposed) | ||
return | ||
this.#disposed = true | ||
|
||
await this.inner[Symbol.asyncDispose]?.() | ||
} | ||
|
||
get disposed() { | ||
return this.#disposed | ||
} | ||
|
||
get() { | ||
return this.inner | ||
} | ||
|
||
} |
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,68 @@ | ||
/** | ||
* A reference that will be disposed after some delay | ||
*/ | ||
export class Tick<T extends Disposable> { | ||
|
||
#timeout: NodeJS.Timeout | ||
|
||
/** | ||
* A reference that will be disposed after some delay | ||
* @param inner | ||
* @param delay | ||
*/ | ||
constructor( | ||
readonly inner: T, | ||
readonly delay = 0 | ||
) { | ||
this.#timeout = setTimeout(() => this.inner[Symbol.dispose](), delay) | ||
} | ||
|
||
[Symbol.dispose]() { | ||
clearTimeout(this.#timeout) | ||
} | ||
|
||
get() { | ||
return this.inner | ||
} | ||
|
||
unwrap() { | ||
using _ = this | ||
return this.inner | ||
} | ||
|
||
} | ||
|
||
/** | ||
* A reference that will be disposed after some delay | ||
*/ | ||
export class AsyncTick<T extends AsyncDisposable> { | ||
|
||
#timeout: NodeJS.Timeout | ||
|
||
/** | ||
* A reference that will be disposed after some delay | ||
* @param inner | ||
* @param delay | ||
*/ | ||
constructor( | ||
readonly inner: T, | ||
readonly delay = 0 | ||
) { | ||
this.#timeout = setTimeout(() => this.inner[Symbol.asyncDispose]().then(undefined, console.error), delay) | ||
} | ||
|
||
|
||
[Symbol.dispose]() { | ||
clearTimeout(this.#timeout) | ||
} | ||
|
||
get() { | ||
return this.inner | ||
} | ||
|
||
unwrap() { | ||
using _ = this | ||
return this.inner | ||
} | ||
|
||
} |