generated from homebridge/homebridge-plugin-template
-
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
baran.wang
committed
Feb 15, 2024
1 parent
910e70a
commit 81a8ade
Showing
13 changed files
with
346 additions
and
123 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
export * from './lumi.airer.acn02'; |
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,41 @@ | ||
type RequestFunction<T1, R> = (args: T1) => Promise<R>; | ||
type MergeFunction<T1, T2> = (requests: T2[]) => T1; | ||
|
||
interface RequestParams<T> { | ||
args: T; | ||
resolve: (value: any) => void; | ||
reject: (reason?: any) => void; | ||
} | ||
export class BatchRequester<T1, T2 = T1, R = any> { | ||
private requests: RequestParams<T2>[] = []; | ||
private timer: NodeJS.Timeout | null = null; | ||
|
||
constructor( | ||
private readonly requestFunction: RequestFunction<T1, R>, | ||
private readonly mergeFunction: MergeFunction<T1, T2>, | ||
private readonly waitTime: number = 100, | ||
) {} | ||
|
||
request(args: T2): Promise<R> { | ||
return new Promise((resolve, reject) => { | ||
this.requests.push({ args, resolve, reject }); | ||
|
||
if (!this.timer) { | ||
this.timer = setTimeout(async () => { | ||
this.timer = null; | ||
|
||
const requests = this.requests; | ||
this.requests = []; | ||
|
||
try { | ||
const mergedArgs = this.mergeFunction(requests.map((request) => request.args)); | ||
const result = await this.requestFunction(mergedArgs); | ||
requests.forEach(({ resolve }) => resolve(result)); | ||
} catch (error) { | ||
requests.forEach(({ reject }) => reject(error)); | ||
} | ||
}, this.waitTime); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.