Skip to content

Commit bb41379

Browse files
committed
Change Counter update object to have amount property instead of inc
`amount` is more suitable in this case as the value can be negative in case the counter was decremented. Resolves PUB-1540
1 parent fbccd00 commit bb41379

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,11 +747,11 @@ await root.remove('name');
747747

748748
await counter.increment(5);
749749
// LiveCounter new value: 5
750-
// LiveCounter update details: { update: { inc: 5 } }
750+
// LiveCounter update details: { update: { amount: 5 } }
751751

752752
await counter.decrement(2);
753753
// LiveCounter new value: 3
754-
// LiveCounter update details: { update: { inc: -2 } }
754+
// LiveCounter update details: { update: { amount: -2 } }
755755
```
756756
757757
You can deregister subscription listeners as follows:

ably.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2415,7 +2415,7 @@ export declare interface LiveCounterUpdate extends LiveObjectUpdate {
24152415
/**
24162416
* The value by which the counter was incremented or decremented.
24172417
*/
2418-
inc: number;
2418+
amount: number;
24192419
};
24202420
}
24212421

src/plugins/objects/livecounter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface LiveCounterData extends LiveObjectData {
88
}
99

1010
export interface LiveCounterUpdate extends LiveObjectUpdate {
11-
update: { inc: number };
11+
update: { amount: number };
1212
}
1313

1414
export class LiveCounter extends LiveObject<LiveCounterData, LiveCounterUpdate> {
@@ -291,7 +291,7 @@ export class LiveCounter extends LiveObject<LiveCounterData, LiveCounterUpdate>
291291

292292
protected _updateFromDataDiff(prevDataRef: LiveCounterData, newDataRef: LiveCounterData): LiveCounterUpdate {
293293
const counterDiff = newDataRef.data - prevDataRef.data;
294-
return { update: { inc: counterDiff } };
294+
return { update: { amount: counterDiff } };
295295
}
296296

297297
protected _mergeInitialDataFromCreateOperation(stateOperation: StateOperation): LiveCounterUpdate {
@@ -302,7 +302,7 @@ export class LiveCounter extends LiveObject<LiveCounterData, LiveCounterUpdate>
302302
this._dataRef.data += stateOperation.counter?.count ?? 0;
303303
this._createOperationIsMerged = true;
304304

305-
return { update: { inc: stateOperation.counter?.count ?? 0 } };
305+
return { update: { amount: stateOperation.counter?.count ?? 0 } };
306306
}
307307

308308
private _throwNoPayloadError(op: StateOperation): void {
@@ -332,6 +332,6 @@ export class LiveCounter extends LiveObject<LiveCounterData, LiveCounterUpdate>
332332

333333
private _applyCounterInc(op: StateCounterOp): LiveCounterUpdate {
334334
this._dataRef.data += op.amount;
335-
return { update: { inc: op.amount } };
335+
return { update: { amount: op.amount } };
336336
}
337337
}

test/package/browser/template/src/index-objects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ globalThis.testAblyPackage = async function () {
5757
// same deal with nullish coalescing
5858
const value: number = counter?.value()!;
5959
const counterSubscribeResponse = counter?.subscribe(({ update }) => {
60-
const shouldBeANumber: number = update.inc;
60+
const shouldBeANumber: number = update.amount;
6161
});
6262
counterSubscribeResponse?.unsubscribe();
6363

test/realtime/objects.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ define(['ably', 'shared_helper', 'chai', 'objects', 'objects_helper'], function
778778
root.get('counter').subscribe((update) => {
779779
try {
780780
expect(update).to.deep.equal(
781-
{ update: { inc: -1 } },
781+
{ update: { amount: -1 } },
782782
'Check counter subscription callback is called with an expected update object after STATE_SYNC sequence with "tombstone=true"',
783783
);
784784
resolve();
@@ -1776,7 +1776,7 @@ define(['ably', 'shared_helper', 'chai', 'objects', 'objects_helper'], function
17761776
root.get('counter').subscribe((update) => {
17771777
try {
17781778
expect(update).to.deep.equal(
1779-
{ update: { inc: -1 } },
1779+
{ update: { amount: -1 } },
17801780
'Check counter subscription callback is called with an expected update object after OBJECT_DELETE operation',
17811781
);
17821782
resolve();
@@ -3567,7 +3567,7 @@ define(['ably', 'shared_helper', 'chai', 'objects', 'objects_helper'], function
35673567
counter.subscribe((update) => {
35683568
try {
35693569
expect(update).to.deep.equal(
3570-
{ update: { inc: 1 } },
3570+
{ update: { amount: 1 } },
35713571
'Check counter subscription callback is called with an expected update object for COUNTER_INC operation',
35723572
);
35733573
resolve();
@@ -3604,7 +3604,7 @@ define(['ably', 'shared_helper', 'chai', 'objects', 'objects_helper'], function
36043604
try {
36053605
const expectedInc = expectedCounterIncrements[currentUpdateIndex];
36063606
expect(update).to.deep.equal(
3607-
{ update: { inc: expectedInc } },
3607+
{ update: { amount: expectedInc } },
36083608
`Check counter subscription callback is called with an expected update object for ${currentUpdateIndex + 1} times`,
36093609
);
36103610

0 commit comments

Comments
 (0)