-
Notifications
You must be signed in to change notification settings - Fork 59
Support annotations #1953
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
Support annotations #1953
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7d98957
Add ANNOTATION_PUBLISH and ANNOTATION_SUBSCRIBE channel modes
SimonWoolf a013a10
RealtimeChannel: refactor to make sendMessage return a promise
SimonWoolf 2a98709
RealtimeChannel._mode: improve typings
SimonWoolf 6d7a37b
Support annotations (rest and realtime)
SimonWoolf c2aa1e5
Expose decodeAnnotation in modular variant
SimonWoolf 23d5b68
JSDoc: stop insisting on giant comment blocks
SimonWoolf 04fed9c
Add annotations.delete()
SimonWoolf 8b7f4d0
Rename meta.occupancy action to just meta
SimonWoolf 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
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,103 @@ | ||
| import EventEmitter from '../util/eventemitter'; | ||
| import Logger from '../util/logger'; | ||
| import Annotation from '../types/annotation'; | ||
| import { actions, flags } from '../types/protocolmessagecommon'; | ||
| import { fromValues as protocolMessageFromValues } from '../types/protocolmessage'; | ||
| import ErrorInfo from '../types/errorinfo'; | ||
| import RealtimeChannel from './realtimechannel'; | ||
| import RestAnnotations, { RestGetAnnotationsParams, constructValidateAnnotation } from './restannotations'; | ||
| import type { PaginatedResult } from './paginatedresource'; | ||
| import type Message from '../types/message'; | ||
| import type { Properties } from '../util/utils'; | ||
|
|
||
| class RealtimeAnnotations { | ||
| private channel: RealtimeChannel; | ||
| private logger: Logger; | ||
| private subscriptions: EventEmitter; | ||
|
|
||
| constructor(channel: RealtimeChannel) { | ||
| this.channel = channel; | ||
| this.logger = channel.logger; | ||
| this.subscriptions = new EventEmitter(this.logger); | ||
| } | ||
|
|
||
| async publish(msgOrSerial: string | Message, annotationValues: Partial<Properties<Annotation>>): Promise<void> { | ||
| const channelName = this.channel.name; | ||
| const annotation = constructValidateAnnotation(msgOrSerial, annotationValues); | ||
| const wireAnnotation = await annotation.encode(); | ||
|
|
||
| this.channel._throwIfUnpublishableState(); | ||
|
|
||
| Logger.logAction( | ||
| this.logger, | ||
| Logger.LOG_MICRO, | ||
| 'RealtimeAnnotations.publish()', | ||
| 'channelName = ' + | ||
| channelName + | ||
| ', sending annotation with messageSerial = ' + | ||
| annotation.messageSerial + | ||
| ', type = ' + | ||
| annotation.type, | ||
| ); | ||
|
|
||
| const pm = protocolMessageFromValues({ | ||
| action: actions.ANNOTATION, | ||
| channel: channelName, | ||
| annotations: [wireAnnotation], | ||
| }); | ||
| return this.channel.sendMessage(pm); | ||
| } | ||
|
|
||
| async delete(msgOrSerial: string | Message, annotationValues: Partial<Properties<Annotation>>): Promise<void> { | ||
| annotationValues.action = 'annotation.delete'; | ||
| return this.publish(msgOrSerial, annotationValues); | ||
| } | ||
|
|
||
| async subscribe(..._args: unknown[] /* [type], listener */): Promise<void> { | ||
| const args = RealtimeChannel.processListenerArgs(_args); | ||
| const event = args[0]; | ||
| const listener = args[1]; | ||
| const channel = this.channel; | ||
|
|
||
| if (channel.state === 'failed') { | ||
| throw ErrorInfo.fromValues(channel.invalidStateError()); | ||
| } | ||
|
|
||
| this.subscriptions.on(event, listener); | ||
|
|
||
| if (this.channel.channelOptions.attachOnSubscribe !== false) { | ||
| await channel.attach(); | ||
| } | ||
|
|
||
| // explicit check for attach state in caes attachOnSubscribe=false | ||
| if ((this.channel.state === 'attached' && this.channel._mode & flags.ANNOTATION_SUBSCRIBE) === 0) { | ||
| throw new ErrorInfo( | ||
| "You are trying to add an annotation listener, but you haven't requested the annotation_subscribe channel mode in ChannelOptions, so this won't do anything (we only deliver annotations to clients who have explicitly requested them)", | ||
| 93001, | ||
|
AndyTWF marked this conversation as resolved.
|
||
| 400, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| unsubscribe(..._args: unknown[] /* [event], listener */): void { | ||
| const args = RealtimeChannel.processListenerArgs(_args); | ||
| const event = args[0]; | ||
| const listener = args[1]; | ||
| this.subscriptions.off(event, listener); | ||
| } | ||
|
|
||
| _processIncoming(annotations: Annotation[]): void { | ||
| for (const annotation of annotations) { | ||
| this.subscriptions.emit(annotation.type || '', annotation); | ||
| } | ||
| } | ||
|
|
||
| async get( | ||
| msgOrSerial: string | Message, | ||
| params: RestGetAnnotationsParams | null, | ||
| ): Promise<PaginatedResult<Annotation>> { | ||
| return RestAnnotations.prototype.get.call(this, msgOrSerial, params); | ||
| } | ||
| } | ||
|
|
||
| export default RealtimeAnnotations; | ||
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.