-
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.
- Loading branch information
1 parent
9690b84
commit d905675
Showing
5 changed files
with
132 additions
and
15 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,36 @@ | ||
/** | ||
* # pure/sync/Debounce | ||
* Debounce executes an action after a certain delay. Any | ||
* call to the action during this delay resets the timer. | ||
* | ||
* ## Warning | ||
* The implementation is naive and does not handle the case | ||
* where the action keeps getting called before the delay, | ||
* in which case the action will never be executed. This | ||
* will be improved in the future... | ||
*/ | ||
export class Debounce<TResult> { | ||
private timer: number | undefined; | ||
constructor( | ||
private fn: () => Promise<TResult>, | ||
private delay: number, | ||
) { | ||
this.timer = undefined; | ||
} | ||
|
||
public execute(): Promise<TResult> { | ||
if (this.timer !== undefined) { | ||
clearTimeout(this.timer); | ||
} | ||
return new Promise<TResult>((resolve, reject) => { | ||
this.timer = setTimeout(async () => { | ||
this.timer = undefined; | ||
try { | ||
resolve(await this.fn()); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}, this.delay); | ||
}); | ||
} | ||
} |
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,77 @@ | ||
import type { Result } from "../result/index.ts"; | ||
|
||
/** | ||
* # pure/sync/Latest | ||
* | ||
* Latest is a synchronization utility to allow | ||
* only one async operation to be executed at a time, | ||
* and any call will only return the result of the latest | ||
* operation. | ||
* | ||
* ## Example | ||
* In the example below, both call will return the result | ||
* of the second call (2) | ||
* ```typescript | ||
* import { Latest } from "@pistonite/pure/sync"; | ||
* | ||
* let counter = 0; | ||
* | ||
* const operation = async () => { | ||
* counter++; | ||
* await new Promise((resolve) => setTimeout(() => { | ||
* resolve(counter); | ||
* }, 1000)); | ||
* } | ||
* | ||
* const call = new Latest(operation); | ||
* | ||
* const result1 = call.execute(); | ||
* const result2 = call.execute(); | ||
* console.log(await result1); // 2 | ||
* console.log(await result2); // 2 | ||
* ``` | ||
*/ | ||
export class Latest<TResult> { | ||
private isRunning = false; | ||
private pending: { | ||
resolve: (result: TResult) => void; | ||
reject: (error: unknown) => void; | ||
}[] = []; | ||
|
||
constructor(private fn: () => Promise<TResult>) {} | ||
|
||
public async execute(): Promise<TResult> { | ||
if (this.isRunning) { | ||
return new Promise<TResult>((resolve, reject) => { | ||
this.pending.push({ resolve, reject }); | ||
}); | ||
} | ||
this.isRunning = true; | ||
const alreadyPending = []; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let result: Result<TResult, unknown> = { err: "not executed" }; | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
try { | ||
const fn = this.fn; | ||
result = { val: await fn() }; | ||
} catch (error) { | ||
result = { err: error }; | ||
} | ||
if (this.pending.length) { | ||
alreadyPending.push(...this.pending); | ||
this.pending = []; | ||
continue; | ||
} | ||
break; | ||
} | ||
this.isRunning = false; | ||
if ("err" in result) { | ||
alreadyPending.forEach(({ reject }) => reject(result.err)); | ||
throw result.err; | ||
} else { | ||
alreadyPending.forEach(({ resolve }) => resolve(result.val)); | ||
return result.val; | ||
} | ||
} | ||
} |
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