* Subscribe to changes to the graph. * @param observer The observer to subscribe to. * @returns A function to unsubscribe from the changes.
(observer: Observer<YGraphChange>)
| 55 | * @returns A function to unsubscribe from the changes. |
| 56 | */ |
| 57 | public subscribe(observer: Observer<YGraphChange>) { |
| 58 | const subscribers = this.#subscribers; |
| 59 | subscribers.add(observer); |
| 60 | if (this.#subscription === undefined) { |
| 61 | this.#subscription = createGraphObserver(this).subscribe({ |
| 62 | next(value) { |
| 63 | for (const subscriber of subscribers) { |
| 64 | subscriber.next?.(value); |
| 65 | } |
| 66 | }, |
| 67 | }); |
| 68 | } |
| 69 | return () => { |
| 70 | subscribers.delete(observer); |
| 71 | if (subscribers.size === 0) { |
| 72 | this.#subscription?.unsubscribe(); |
| 73 | this.#subscription = undefined; |
| 74 | } |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Create a live query. |
no test coverage detected