microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
packages/asset-emitter/src/placeholder.ts
18lines · modecode
| 1 | /** |
| 2 | * Keeps track of a value we don't know yet because of a circular reference. Use |
| 3 | * the `onValue` method to provide a callback with how to handle the |
| 4 | * placeholder's value becoming available. Generally the callback will replace |
| 5 | * this placeholder with the value in whatever references the placeholder. |
| 6 | */ |
| 7 | export class Placeholder<T> { |
| 8 | #listeners: ((value: T) => void)[] = []; |
| 9 | setValue(value: T) { |
| 10 | for (const listener of this.#listeners) { |
| 11 | listener(value); |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | onValue(cb: (value: T) => void) { |
| 16 | this.#listeners.push(cb); |
| 17 | } |
| 18 | } |
| 19 | |