-
Notifications
You must be signed in to change notification settings - Fork 181
New collection event system #555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| "@tanstack/db": patch | ||
| --- | ||
|
|
||
| Added a new events system for subscribing to status changes and other internal events. |
This file contains hidden or 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,169 @@ | ||
| import type { Collection } from "./collection" | ||
| import type { CollectionStatus } from "./types" | ||
|
|
||
| /** | ||
| * Event emitted when the collection status changes | ||
| */ | ||
| export interface CollectionStatusChangeEvent { | ||
| type: `status:change` | ||
| collection: Collection | ||
| previousStatus: CollectionStatus | ||
| status: CollectionStatus | ||
| } | ||
|
|
||
| /** | ||
| * Event emitted when the collection status changes to a specific status | ||
| */ | ||
| export interface CollectionStatusEvent<T extends CollectionStatus> { | ||
| type: `status:${T}` | ||
| collection: Collection | ||
| previousStatus: CollectionStatus | ||
| status: T | ||
| } | ||
|
|
||
| /** | ||
| * Event emitted when the number of subscribers to the collection changes | ||
| */ | ||
| export interface CollectionSubscribersChangeEvent { | ||
| type: `subscribers:change` | ||
| collection: Collection | ||
| previousSubscriberCount: number | ||
| subscriberCount: number | ||
| } | ||
|
|
||
| export type AllCollectionEvents = { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Export a union: export type CollectionEvent = AllCollectionEvents[keyof AllCollectionEvents] for switch(event.type) ergonomics. |
||
| "status:change": CollectionStatusChangeEvent | ||
| "subscribers:change": CollectionSubscribersChangeEvent | ||
| } & { | ||
| [K in CollectionStatus as `status:${K}`]: CollectionStatusEvent<K> | ||
| } | ||
|
|
||
| export type CollectionEvent = | ||
| | AllCollectionEvents[keyof AllCollectionEvents] | ||
| | CollectionStatusChangeEvent | ||
| | CollectionSubscribersChangeEvent | ||
|
|
||
| export type CollectionEventHandler<T extends keyof AllCollectionEvents> = ( | ||
| event: AllCollectionEvents[T] | ||
| ) => void | ||
|
|
||
| export class CollectionEvents { | ||
| private collection: Collection<any, any, any, any, any> | ||
| private listeners = new Map< | ||
| keyof AllCollectionEvents, | ||
| Set<CollectionEventHandler<any>> | ||
| >() | ||
|
|
||
| constructor(collection: Collection<any, any, any, any, any>) { | ||
| this.collection = collection | ||
| } | ||
|
|
||
| on<T extends keyof AllCollectionEvents>( | ||
| event: T, | ||
| callback: CollectionEventHandler<T> | ||
| ) { | ||
| if (!this.listeners.has(event)) { | ||
| this.listeners.set(event, new Set()) | ||
| } | ||
| this.listeners.get(event)!.add(callback) | ||
|
|
||
| return () => { | ||
| this.listeners.get(event)!.delete(callback) | ||
| } | ||
| } | ||
|
|
||
| once<T extends keyof AllCollectionEvents>( | ||
| event: T, | ||
| callback: CollectionEventHandler<T> | ||
| ) { | ||
| const unsubscribe = this.on(event, (eventPayload) => { | ||
| callback(eventPayload) | ||
| unsubscribe() | ||
| }) | ||
| return unsubscribe | ||
| } | ||
|
|
||
| off<T extends keyof AllCollectionEvents>( | ||
samwillis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| event: T, | ||
| callback: CollectionEventHandler<T> | ||
| ) { | ||
| this.listeners.get(event)?.delete(callback) | ||
| } | ||
|
|
||
| waitFor<T extends keyof AllCollectionEvents>( | ||
| event: T, | ||
| timeout?: number | ||
| ): Promise<AllCollectionEvents[T]> { | ||
| return new Promise((resolve, reject) => { | ||
| let timeoutId: NodeJS.Timeout | undefined | ||
| const unsubscribe = this.on(event, (eventPayload) => { | ||
| if (timeoutId) { | ||
| clearTimeout(timeoutId) | ||
| timeoutId = undefined | ||
| } | ||
| resolve(eventPayload) | ||
| unsubscribe() | ||
| }) | ||
| if (timeout) { | ||
| timeoutId = setTimeout(() => { | ||
| timeoutId = undefined | ||
| unsubscribe() | ||
| reject(new Error(`Timeout waiting for event ${event}`)) | ||
| }, timeout) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| emit<T extends keyof AllCollectionEvents>( | ||
| event: T, | ||
| eventPayload: AllCollectionEvents[T] | ||
| ) { | ||
| this.listeners.get(event)?.forEach((listener) => { | ||
| try { | ||
| listener(eventPayload) | ||
| } catch (error) { | ||
| // Re-throw in a microtask to surface the error | ||
| queueMicrotask(() => { | ||
| throw error | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| emitStatusChange<T extends CollectionStatus>( | ||
| status: T, | ||
| previousStatus: CollectionStatus | ||
| ) { | ||
| this.emit(`status:change`, { | ||
| type: `status:change`, | ||
| collection: this.collection, | ||
| previousStatus, | ||
| status, | ||
| }) | ||
|
|
||
| // Emit specific status event using type assertion | ||
| const eventKey: `status:${T}` = `status:${status}` | ||
| this.emit(eventKey, { | ||
| type: eventKey, | ||
| collection: this.collection, | ||
| previousStatus, | ||
| status, | ||
| } as AllCollectionEvents[`status:${T}`]) | ||
| } | ||
|
|
||
| emitSubscribersChange( | ||
| subscriberCount: number, | ||
| previousSubscriberCount: number | ||
| ) { | ||
| this.emit(`subscribers:change`, { | ||
| type: `subscribers:change`, | ||
| collection: this.collection, | ||
| previousSubscriberCount, | ||
| subscriberCount, | ||
| }) | ||
| } | ||
|
|
||
| cleanup() { | ||
| this.listeners.clear() | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.