Skip to content

Commit

Permalink
fix: Fix stardustFeed unsubscribe (remove subscribers when unsubscrib…
Browse files Browse the repository at this point in the history
…e requests comes through socket disconnect) + Fix a possible nullpointer in useBlockFeed
  • Loading branch information
msarcev committed Nov 21, 2023
1 parent 4c46520 commit bb96444
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
8 changes: 6 additions & 2 deletions api/src/routes/feed/unsubscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ export async function unsubscribe(
itemsService.unsubscribe(request.subscriptionId);
}
} else if (networkConfig.protocolVersion === STARDUST) {
ValidationHelper.string(request.feedSelect, "feedSelect");
ValidationHelper.oneOf(request.feedSelect, ["block", "milestone"], "feedSelect");
const service = ServiceFactory.get<StardustFeed>(`feed-${request.network}`);

if (request.feedSelect === "block") {
service?.unsubscribeBlocks(request.subscriptionId);
} else if (request.feedSelect === "milestone") {
service?.unsubscribeMilestones(request.subscriptionId);
} else {
// when unsubscribe comes from index.ts socket callback (broswer page reload)
// we don't have the feedSelect so unsubscribe both
service?.unsubscribeBlocks(request.subscriptionId);
service?.unsubscribeMilestones(request.subscriptionId);
}
} else {
Expand Down
8 changes: 8 additions & 0 deletions api/src/services/stardust/feed/stardustFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export class StardustFeed {
*/
private readonly networkProtocolVersion: number;

/**
* The network in context (from Init).
*/
private readonly network: string;

/**
* Creates a new instance of StardustFeed.
* @param networkId The network id.
Expand All @@ -65,6 +70,7 @@ export class StardustFeed {
this.blockSubscribers = {};
this.milestoneSubscribers = {};
this.blockMetadataCache = new Map();
this.network = networkId;
this._mqttClient = ServiceFactory.get<Client>(`mqtt-${networkId}`);
const nodeInfoService = ServiceFactory.get<NodeInfoService>(`node-info-${networkId}`);

Expand Down Expand Up @@ -111,6 +117,7 @@ export class StardustFeed {
* @param subscriptionId The id to unsubscribe.
*/
public unsubscribeBlocks(subscriptionId: string): void {
logger.debug(`[StardustFeed] Removing subscriber ${subscriptionId} from blocks (${this.network})`);
delete this.blockSubscribers[subscriptionId];
}

Expand All @@ -119,6 +126,7 @@ export class StardustFeed {
* @param subscriptionId The id to unsubscribe.
*/
public unsubscribeMilestones(subscriptionId: string): void {
logger.debug(`[StardustFeed] Removing subscriber ${subscriptionId} from milestones (${this.network})`);
delete this.milestoneSubscribers[subscriptionId];
}

Expand Down
2 changes: 1 addition & 1 deletion client/src/helpers/hooks/useBlockFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function useBlockFeed(network: string): [
const fetchLatestCachedMilestones = useCallback(async () => {
if (apiClient) {
const latestMilestones: ILatestMilestonesReponse = await apiClient.latestMilestones(network);
if (isMounted) {
if (isMounted && latestMilestones.milestones.length > 0) {
setMilestones(
latestMilestones.milestones.slice(0, MAX_MILESTONE_ITEMS)
);
Expand Down

0 comments on commit bb96444

Please sign in to comment.