-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow user to unsubscribe when receiving an error (#351)
In the prior PR, the user had no mechanism to unsubscribe / cancel the stream. In this PR we allow the user to unsubscribe via the subscription response. We return the response from subscribe and also pass it as an argument to the error handler.
- Loading branch information
Showing
5 changed files
with
151 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Encapsulates a topic subscription stream state. | ||
*/ | ||
export class SubscriptionState { | ||
private _unsubscribeFn: () => void; | ||
public lastTopicSequenceNumber?: number; | ||
private _isSubscribed: boolean; | ||
constructor() { | ||
this._unsubscribeFn = () => { | ||
return; | ||
}; | ||
this._isSubscribed = false; | ||
} | ||
|
||
public get resumeAtTopicSequenceNumber(): number { | ||
return (this.lastTopicSequenceNumber ?? -1) + 1; | ||
} | ||
|
||
public setSubscribed(): void { | ||
this._isSubscribed = true; | ||
} | ||
|
||
public setUnsubscribed(): void { | ||
this._isSubscribed = false; | ||
} | ||
|
||
public get isSubscribed(): boolean { | ||
return this._isSubscribed; | ||
} | ||
|
||
public set unsubscribeFn(unsubscribeFn: () => void) { | ||
this._unsubscribeFn = unsubscribeFn; | ||
} | ||
|
||
public unsubscribe(): void { | ||
if (this.isSubscribed) { | ||
this._unsubscribeFn(); | ||
this.setUnsubscribed(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters