Skip to content

Implement RTL4h #1986

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions src/common/lib/client/realtimechannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,28 +324,40 @@ class RealtimeChannel extends EventEmitter {
return;
}

if (this.state !== 'attaching' || forceReattach) {
this.requestState('attaching', attachReason);
let operationComplete;
if (this.state === 'attaching' || this.state === 'detaching') {
// RTL4h, waits for current operation to complete
operationComplete = new Promise((resolve) => {
this.once(resolve);
});
} else {
operationComplete = Promise.resolve();
}

this.once(function (this: { event: string }, stateChange: ChannelStateChange) {
switch (this.event) {
case 'attached':
callback?.(null, stateChange);
break;
case 'detached':
case 'suspended':
case 'failed':
callback?.(
stateChange.reason ||
connectionManager.getError() ||
new ErrorInfo('Unable to attach; reason unknown; state = ' + this.event, 90000, 500),
);
break;
case 'detaching':
callback?.(new ErrorInfo('Attach request superseded by a subsequent detach request', 90000, 409));
break;
operationComplete.then(() => {
if (this.state !== 'attaching' || forceReattach) {
this.requestState('attaching', attachReason); // request a new attach
}

this.once(function (this: { event: string }, stateChange: ChannelStateChange) {
switch (this.event) {
case 'attached':
callback?.(null, stateChange);
break;
case 'detached':
case 'suspended':
case 'failed':
callback?.(
stateChange.reason ||
connectionManager.getError() ||
new ErrorInfo('Unable to attach; reason unknown; state = ' + this.event, 90000, 500),
);
break;
case 'detaching':
callback?.(new ErrorInfo('Attach request superseded by a subsequent detach request', 90000, 409));
break;
}
});
});
}

Expand Down
Loading