Skip to content

Commit

Permalink
handle subscribe result delta flag
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-emelin committed May 5, 2024
1 parent 70d9fef commit d2b2d98
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/client.proto.json
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,10 @@
"was_recovering": {
"type": "bool",
"id": 12
},
"delta": {
"type": "bool",
"id": 13
}
},
"reserved": [
Expand Down
9 changes: 8 additions & 1 deletion src/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class Subscription extends (EventEmitter as new () => TypedEventEmitter<S
private _resubscribeAttempts: number;
private _promiseId: number;
private _delta: string;
private _delta_negotiated: boolean;
private _token: string;
private _data: any | null;
private _getData: null | ((ctx: SubscriptionDataContext) => Promise<any>);
Expand Down Expand Up @@ -62,6 +63,7 @@ export class Subscription extends (EventEmitter as new () => TypedEventEmitter<S
this._inflight = false;
this._refreshTimeout = null;
this._delta = '';
this._delta_negotiated = false;
this._prevValue = null;
this._setOptions(options);
// @ts-ignore – we are hiding some symbols from public API autocompletion.
Expand Down Expand Up @@ -225,6 +227,11 @@ export class Subscription extends (EventEmitter as new () => TypedEventEmitter<S
this._offset = result.offset || 0;
this._epoch = result.epoch || '';
}
if (result.delta) {
this._delta_negotiated = true;
} else {
this._delta_negotiated = false;
}

this._setState(SubscriptionState.Subscribed);
// @ts-ignore – we are hiding some methods from public API autocompletion.
Expand Down Expand Up @@ -455,7 +462,7 @@ export class Subscription extends (EventEmitter as new () => TypedEventEmitter<S
}

private _handlePublication(pub: any) {
if (this._delta) {
if (this._delta && this._delta_negotiated) {
// @ts-ignore – we are hiding some methods from public API autocompletion.
const {newData, newPrevValue} = this._centrifuge._codec.applyDeltaIfNeeded(pub, this._prevValue)
pub.data = newData;
Expand Down

0 comments on commit d2b2d98

Please sign in to comment.