Skip to content

Commit 80b7069

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 c406bef commit 80b7069

File tree

4 files changed

+62
-16
lines changed

4 files changed

+62
-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: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@ export function fromValues(values: Properties<Message>): Message {
304304

305305
export function fromWireProtocol(values: WireProtocolMessage): Message {
306306
const action = toMessageActionString(values.action as number) || values.action;
307-
return Object.assign(new Message(), { ...values, action });
307+
const res = Object.assign(new Message(), { ...values, action });
308+
res.expandFields();
309+
return res;
308310
}
309311

310312
export function fromValuesArray(values: Properties<Message>[]): Message[] {
@@ -338,8 +340,8 @@ class Message {
338340
serial?: string;
339341
refSerial?: string;
340342
refType?: string;
341-
updatedAt?: number;
342-
updateSerial?: string;
343+
createdAt?: number;
344+
version?: string;
343345
operation?: API.Operation;
344346

345347
/**
@@ -375,14 +377,27 @@ class Message {
375377
action: toMessageActionNumber(this.action as API.MessageAction) || this.action,
376378
refSerial: this.refSerial,
377379
refType: this.refType,
378-
updatedAt: this.updatedAt,
379-
updateSerial: this.updateSerial,
380+
createdAt: this.createdAt,
381+
version: this.version,
380382
operation: this.operation,
381383
encoding,
382384
data,
383385
};
384386
}
385387

388+
expandFields() {
389+
if (this.action === 'message.create') {
390+
// TM2k
391+
if (this.version && !this.serial) {
392+
this.serial = this.version;
393+
}
394+
// TM2o
395+
if (this.timestamp && !this.createdAt) {
396+
this.createdAt = this.timestamp;
397+
}
398+
}
399+
}
400+
386401
toString(): string {
387402
let result = '[Message';
388403
if (this.name) result += '; name=' + this.name;
@@ -402,10 +417,10 @@ class Message {
402417

403418
if (this.action) result += '; action=' + this.action;
404419
if (this.serial) result += '; serial=' + this.serial;
420+
if (this.version) result += '; version=' + this.version;
405421
if (this.refSerial) result += '; refSerial=' + this.refSerial;
406422
if (this.refType) result += '; refType=' + this.refType;
407-
if (this.updatedAt) result += '; updatedAt=' + this.updatedAt;
408-
if (this.updateSerial) result += '; updateSerial=' + this.updateSerial;
423+
if (this.createdAt) result += '; createdAt=' + this.createdAt;
409424
if (this.operation) result += '; operation=' + JSON.stringify(this.operation);
410425
result += ']';
411426
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)