-
-
Notifications
You must be signed in to change notification settings - Fork 139
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
fix(server): message handling order when using beforeHandleMessage
(#880)
#901
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,10 @@ export class Connection { | |
|
||
logger: Debugger | ||
|
||
private beforeHandleMessageQueue: Uint8Array[] = [] | ||
|
||
private beforeHandleMessageProcessing = false | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
|
@@ -216,21 +220,40 @@ export class Connection { | |
* @public | ||
*/ | ||
public handleMessage(data: Uint8Array): void { | ||
if (this.beforeHandleMessageQueue.length > 0 || this.beforeHandleMessageProcessing) { | ||
this.beforeHandleMessageQueue.push(data) | ||
return | ||
} | ||
|
||
this.processBeforeHandleMessage(data) | ||
} | ||
|
||
/** Process an incoming message */ | ||
private processBeforeHandleMessage(data: Uint8Array): void { | ||
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. The name of the method is out of sync with the comment. I like the comment more, as what this methos does is process an incoming message after applying the beforeHandleMessage hooks. 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. I would prefer using async-await syntax for this method. This would allow you to use a |
||
this.beforeHandleMessageProcessing = true | ||
|
||
const message = new IncomingMessage(data) | ||
const documentName = message.readVarString() | ||
|
||
if (documentName !== this.document.name) return | ||
if (documentName !== this.document.name) { | ||
this.nextProcessBeforeHandleMessage() | ||
return | ||
} | ||
|
||
message.writeVarString(documentName) | ||
|
||
this.callbacks.beforeHandleMessage(this, data) | ||
.then(() => { | ||
this.nextProcessBeforeHandleMessage() | ||
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. The ordering of things here seems a bit off to me: you trigger the processing of a next message before you've processed this one (below). 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. The invocation of |
||
|
||
new MessageReceiver( | ||
message, | ||
this.logger, | ||
).apply(this.document, this) | ||
}) | ||
.catch((e: any) => { | ||
this.nextProcessBeforeHandleMessage() | ||
|
||
console.log('closing connection because of exception', e) | ||
this.close({ | ||
code: 'code' in e ? e.code : Forbidden.code, | ||
|
@@ -239,6 +262,14 @@ export class Connection { | |
}) | ||
} | ||
|
||
/** Process next an incoming message */ | ||
private nextProcessBeforeHandleMessage(): void { | ||
this.beforeHandleMessageProcessing = false | ||
|
||
if (this.beforeHandleMessageQueue.length > 0) { | ||
this.processBeforeHandleMessage(this.beforeHandleMessageQueue.shift()!) | ||
} | ||
} | ||
} | ||
|
||
export default Connection |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might be able to do with just 1 additional state variable if you
shift
ed the message only after successfully processing it.