Skip to content

Commit

Permalink
add: Added Thenable object.
Browse files Browse the repository at this point in the history
  • Loading branch information
Byloth committed Nov 4, 2024
1 parent b6f85ca commit be18f89
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export {
SmartIterator,
SmartAsyncIterator,
SmartPromise,
Thenable,
TimeoutException,
TimedPromise,
TypeException,
Expand Down
2 changes: 1 addition & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import GameLoop from "./game-loop.js";

export { SmartIterator, SmartAsyncIterator } from "./iterators/index.js";
export { JSONStorage } from "./json/index.js";
export { DeferredPromise, SmartPromise, TimedPromise } from "./promises/index.js";
export { DeferredPromise, SmartPromise, Thenable, TimedPromise } from "./promises/index.js";

import Publisher from "./publisher.js";

Expand Down
97 changes: 97 additions & 0 deletions src/models/promises/thenable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
export default class Thenable<T> implements Promise<T>
{
protected _onFulfilled: (result: T) => T;
protected _resolve(result: T): T
{
return this._onFulfilled(result);
}

public constructor()
{
this._onFulfilled = (result: T) => result;
}

public then(onFulfilled?: null): Thenable<T>;
public then<F = T>(onFulfilled: (result: T) => F, onRejected?: null): Thenable<F>;
public then<F = T, R = never>(onFulfilled: (result: T) => F, onRejected: (reason: unknown) => R)
: Thenable<F | R>;
public then<F = T, R = never>(onFulfilled?: ((result: T) => F) | null, onRejected?: ((reason: unknown) => R) | null)
: Thenable<F | R>
{
if (onRejected)
{
const _previousOnFulfilled = this._onFulfilled;
this._onFulfilled = (result: T) =>
{
try
{
result = _previousOnFulfilled(result);

return (onFulfilled!(result) as unknown) as T;
}
catch (error)
{
return (onRejected(error) as unknown) as T;
}
};
}
else if (onFulfilled)
{
const _previousOnFulfilled = this._onFulfilled;
this._onFulfilled = (result: T) =>
{
result = _previousOnFulfilled(result);

return (onFulfilled(result) as unknown) as T;
};
}

return (this as unknown) as Thenable<F | R>;
}

public catch(onRejected?: null): Thenable<T>;
public catch<R = never>(onRejected: (reason: unknown) => R): Thenable<T | R>;
public catch<R = never>(onRejected?: ((reason: unknown) => R) | null): Thenable<T | R>
{
if (onRejected)
{
const _previousOnFulfilled = this._onFulfilled;
this._onFulfilled = (result) =>
{
try
{
return _previousOnFulfilled(result);
}
catch (error)
{
return (onRejected(error) as unknown) as T;
}
};
}

return this as Thenable<T | R>;
}

public finally(onFinally?: (() => void) | null): Thenable<T>
{
if (onFinally)
{
const _previousOnFulfilled = this._onFulfilled;
this._onFulfilled = (result) =>
{
try
{
return _previousOnFulfilled(result);
}
finally
{
onFinally();
}
};
}

return this;
}

public readonly [Symbol.toStringTag]: string = "Thenable";
}

0 comments on commit be18f89

Please sign in to comment.