-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support annotations (rest and realtime)
- Loading branch information
1 parent
a6fee8a
commit 14c9963
Showing
15 changed files
with
505 additions
and
15 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
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,87 @@ | ||
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 type { CipherOptions } from '../types/basemessage'; | ||
import ErrorInfo from '../types/errorinfo'; | ||
import RealtimeChannel from './realtimechannel'; | ||
|
||
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(refSerial: string, refType: string, data: any): Promise<void> { | ||
const channelName = this.channel.name; | ||
const annotation = Annotation.fromValues({ | ||
action: 'annotation.create', | ||
refSerial, | ||
refType, | ||
data, | ||
}); | ||
|
||
// TODO get rid of CipherOptions type assertion, indicates channeloptions types are broken | ||
const wireAnnotation = await annotation.encode(this.channel.channelOptions as CipherOptions); | ||
|
||
this.channel._throwIfUnpublishableState(); | ||
|
||
Logger.logAction( | ||
this.logger, | ||
Logger.LOG_MICRO, | ||
'RealtimeAnnotations.publish()', | ||
'channelName = ' + channelName + ', sending annotation with refSerial = ' + refSerial + ', refType = ' + refType, | ||
); | ||
|
||
const pm = protocolMessageFromValues({ | ||
action: actions.ANNOTATION, | ||
channel: channelName, | ||
annotations: [wireAnnotation], | ||
}); | ||
return this.channel.sendMessage(pm); | ||
} | ||
|
||
async subscribe(..._args: unknown[] /* [refType], 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()); | ||
} | ||
|
||
await channel.attach(); | ||
|
||
if ((this.channel._mode & flags.ANNOTATION_SUBSCRIBE) === 0) { | ||
throw new ErrorInfo( | ||
"You're 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, | ||
400, | ||
); | ||
} | ||
|
||
this.subscriptions.on(event, listener); | ||
} | ||
|
||
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.refType || '', annotation); | ||
} | ||
} | ||
} | ||
|
||
export default RealtimeAnnotations; |
Oops, something went wrong.