-
-
Notifications
You must be signed in to change notification settings - Fork 819
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
we don't need a pubsub to subscribe only to the first published valuee, we can use an expectant store that returns a promise when the store does not yet contain the value, where all the relevant promises resolve on a future set. this change means we can retire the in-house pubsub. repeaters probably make in-house pubsub unnecessary anyway, and were not yet used elsewhere in the codebase also somewhat relevant, see repeaterjs/repeater#67 (comment)
- Loading branch information
Showing
9 changed files
with
102 additions
and
219 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
interface Settler<T> { | ||
resolve(value: T): void; | ||
reject(reason?: any): void; | ||
} | ||
|
||
export class ExpectantStore<T> { | ||
protected settlers: Record<string, Set<Settler<T>>> = {}; | ||
protected cache: Record<string, T> = {}; | ||
|
||
set(key: string, value: T): void { | ||
this.cache[key] = value; | ||
const settlers = this.settlers[key]; | ||
if (settlers != null) { | ||
for (const { resolve } of settlers) { | ||
resolve(value); | ||
} | ||
settlers.clear(); | ||
delete this.settlers[key]; | ||
} | ||
} | ||
|
||
get(key: string): T { | ||
return this.cache[key]; | ||
} | ||
|
||
request(key: string): Promise<T> | T { | ||
const value = this.cache[key]; | ||
|
||
if (value !== undefined) { | ||
return value; | ||
} | ||
|
||
let settlers = this.settlers[key]; | ||
if (settlers != null) { | ||
settlers = this.settlers[key] = new Set(); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
settlers.add({ resolve, reject }); | ||
}); | ||
} | ||
|
||
clear(reason?: any): void { | ||
for (const settlers of Object.values(this.settlers)) { | ||
for (const { reject } of settlers) { | ||
reject(reason); | ||
} | ||
} | ||
|
||
this.settlers = {}; | ||
this.cache = {}; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.