Skip to content

Commit 6e1dbc2

Browse files
committed
Implement TM2p,k,o (new mutable-message fields)
Tests for TM2p pending serverside implementation of field name changes being deployed
1 parent 598e899 commit 6e1dbc2

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

ably.d.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,34 +2332,40 @@ export interface Message {
23322332
name?: string;
23332333
/**
23342334
* Timestamp of when the message was received by Ably, as milliseconds since the Unix epoch.
2335+
* (This is the timestamp of the current version of the message)
23352336
*/
23362337
timestamp?: number;
23372338
/**
23382339
* The action type of the message, one of the {@link MessageAction} enum values.
23392340
*/
23402341
action?: MessageAction;
23412342
/**
2342-
* This message's unique serial.
2343+
* This message's unique serial (an identifier that will be the same in all future
2344+
* updates of this message).
23432345
*/
23442346
serial?: string;
23452347
/**
2346-
* The serial of the message that this message is a reference to.
2348+
* If this message references another, the serial of that message.
23472349
*/
23482350
refSerial?: string;
23492351
/**
2350-
* The type of reference this message is, in relation to the message it references.
2352+
* If this message references another, the type of reference that is.
23512353
*/
23522354
refType?: string;
23532355
/**
2354-
* If an `update` operation was applied to this message, this will be the timestamp the update occurred.
2356+
* The timestamp of the very first version of a given message (will differ from
2357+
* createdAt only if the message has been updated or deleted).
23552358
*/
2356-
updatedAt?: number;
2359+
createdAt?: number;
23572360
/**
2358-
* The serial of the operation that updated this message.
2361+
* The version of the message, lexicographically-comparable with other versions (that
2362+
* share the same serial) Will differ from the serial only if the message has been
2363+
* updated or deleted.
23592364
*/
2360-
updateSerial?: string;
2365+
version?: string;
23612366
/**
2362-
* If this message resulted from an operation, this will contain the operation details.
2367+
* In the case of an updated or deleted message, this will contain metadata about the
2368+
* update or delete operation.
23632369
*/
23642370
operation?: Operation;
23652371
}

src/common/lib/client/realtimechannel.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ class RealtimeChannel extends EventEmitter {
622622
firstMessage = messages[0],
623623
lastMessage = messages[messages.length - 1],
624624
id = message.id,
625+
channelSerial = message.channelSerial,
625626
connectionId = message.connectionId,
626627
timestamp = message.timestamp;
627628

@@ -670,9 +671,14 @@ class RealtimeChannel extends EventEmitter {
670671
if (!msg.connectionId) msg.connectionId = connectionId;
671672
if (!msg.timestamp) msg.timestamp = timestamp;
672673
if (!msg.id) msg.id = id + ':' + i;
674+
if (!msg.version) msg.version = channelSerial + ':' + i.toString().padStart(3, '0');
675+
676+
// already done in fromWireProtocol -- but for realtime messages the source
677+
// fields might be copied from the protocolmessage, so need to do it again
678+
msg.expandFields()
673679
}
674680
this._lastPayload.messageId = lastMessage.id;
675-
this._lastPayload.protocolMessageChannelSerial = message.channelSerial;
681+
this._lastPayload.protocolMessageChannelSerial = channelSerial;
676682
this.onEvent(messages);
677683
break;
678684
}

src/common/lib/types/message.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ export function fromValues(values: Properties<Message>): Message {
282282

283283
export function fromWireProtocol(values: WireProtocolMessage): Message {
284284
const action = toMessageActionString(values.action as number) || values.action;
285-
return Object.assign(new Message(), { ...values, action });
285+
const res = Object.assign(new Message(), { ...values, action });
286+
res.expandFields();
287+
return res;
286288
}
287289

288290
export function fromValuesArray(values: Properties<Message>[]): Message[] {
@@ -316,8 +318,8 @@ class Message {
316318
serial?: string;
317319
refSerial?: string;
318320
refType?: string;
319-
updatedAt?: number;
320-
updateSerial?: string;
321+
createdAt?: number;
322+
version?: string;
321323
operation?: API.Operation;
322324

323325
/**
@@ -353,14 +355,28 @@ class Message {
353355
action: toMessageActionNumber(this.action as API.MessageAction) || this.action,
354356
refSerial: this.refSerial,
355357
refType: this.refType,
356-
updatedAt: this.updatedAt,
357-
updateSerial: this.updateSerial,
358+
createdAt: this.createdAt,
359+
version: this.version,
358360
operation: this.operation,
359361
encoding,
360362
data,
361363
};
362364
}
363365

366+
expandFields() {
367+
if (this.action === 'message.create') {
368+
// TM2k
369+
if (this.version && !this.serial) {
370+
this.serial = this.version;
371+
}
372+
// TM2o
373+
if (this.timestamp && !this.createdAt) {
374+
this.createdAt = this.timestamp;
375+
}
376+
}
377+
}
378+
379+
364380
toString(): string {
365381
let result = '[Message';
366382
if (this.name) result += '; name=' + this.name;
@@ -380,10 +396,10 @@ class Message {
380396

381397
if (this.action) result += '; action=' + this.action;
382398
if (this.serial) result += '; serial=' + this.serial;
399+
if (this.version) result += '; version=' + this.version;
383400
if (this.refSerial) result += '; refSerial=' + this.refSerial;
384401
if (this.refType) result += '; refType=' + this.refType;
385-
if (this.updatedAt) result += '; updatedAt=' + this.updatedAt;
386-
if (this.updateSerial) result += '; updateSerial=' + this.updateSerial;
402+
if (this.createdAt) result += '; createdAt=' + this.createdAt;
387403
if (this.operation) result += '; operation=' + JSON.stringify(this.operation);
388404
result += ']';
389405
return result;

test/realtime/message.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,25 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async
13101310
expect(message.toJSON()).to.deep.contains(expectedJSON);
13111311
});
13121312
});
1313+
1314+
/**
1315+
* @spec TM2k
1316+
* @spec TM2o
1317+
*/
1318+
it('create message should fill out serial and createdAt from version/timestamp', function () {
1319+
const values = { action: 1, timestamp: 12345, version: 'foo' };
1320+
const message = Message.fromWireProtocol(values);
1321+
expect(message.timestamp).to.equal(12345);
1322+
expect(message.createdAt).to.equal(12345);
1323+
expect(message.version).to.equal('foo');
1324+
expect(message.serial).to.equal('foo');
1325+
1326+
// should only apply to creates
1327+
const update = { action: 2, timestamp: 12345, version: 'foo' };
1328+
const updateMessage = Message.fromWireProtocol(update);
1329+
expect(updateMessage.createdAt).to.equal(undefined);
1330+
expect(updateMessage.serial).to.equal(undefined);
1331+
});
13131332
});
13141333

13151334
/**

0 commit comments

Comments
 (0)