Skip to content

Commit

Permalink
Merge pull request #1945 from ably/fix-not-populating-serial
Browse files Browse the repository at this point in the history
Message: fix parent-populated version not being propagated to serial
  • Loading branch information
SimonWoolf authored Jan 14, 2025
2 parents 3bf3d7a + 4997199 commit 94d676b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
10 changes: 1 addition & 9 deletions src/common/lib/client/realtimechannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,7 @@ class RealtimeChannel extends EventEmitter {

const encoded = message.messages!,
firstMessage = encoded[0],
lastMessage = encoded[encoded.length - 1],
channelSerial = message.channelSerial;
lastMessage = encoded[encoded.length - 1];

if (
firstMessage.extras &&
Expand Down Expand Up @@ -639,13 +638,6 @@ class RealtimeChannel extends EventEmitter {
}
}

for (let i = 0; i < messages.length; i++) {
const msg = messages[i];
if (channelSerial && !msg.version) {
msg.version = channelSerial + ':' + i.toString().padStart(3, '0');
}
}

this._lastPayload.messageId = lastMessage.id;
this._lastPayload.protocolMessageChannelSerial = message.channelSerial;
this.onEvent(messages);
Expand Down
24 changes: 19 additions & 5 deletions src/common/lib/types/basemessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,20 @@ export function wireToJSON(this: BaseMessage, ...args: any[]): any {

// in-place, generally called on the protocol message before decoding
export function populateFieldsFromParent(parent: ProtocolMessage) {
const { id, connectionId, timestamp, channelSerial } = parent;

let msgs: BaseMessage[];
switch (parent.action) {
case actions.MESSAGE:
case actions.MESSAGE: {
msgs = parent.messages!;
for (let i = 0; i < msgs.length; i++) {
const msg = parent.messages![i];
if (channelSerial && !msg.version) {
msg.version = channelSerial + ':' + i.toString().padStart(3, '0');
}
}
break;
}
case actions.PRESENCE:
case actions.SYNC:
msgs = parent.presence!;
Expand All @@ -231,12 +240,17 @@ export function populateFieldsFromParent(parent: ProtocolMessage) {
throw new ErrorInfo('Unexpected action ' + parent.action, 40000, 400);
}

const { id, connectionId, timestamp } = parent;
for (let i = 0; i < msgs.length; i++) {
const msg = msgs[i];
if (!msg.connectionId) msg.connectionId = connectionId;
if (!msg.timestamp) msg.timestamp = timestamp;
if (id && !msg.id) msg.id = id + ':' + i;
if (!msg.connectionId) {
msg.connectionId = connectionId;
}
if (!msg.timestamp) {
msg.timestamp = timestamp;
}
if (id && !msg.id) {
msg.id = id + ':' + i;
}
}
}

Expand Down

0 comments on commit 94d676b

Please sign in to comment.