ringcentral/ringcentral-js

On renew error, there is no way to know subscription id for which this event was emitted

nerdchacha opened this issue · 3 comments

I want to maintain the subscription details of all the subscriptions I have created using my app.
I am not using the cache provided by this SDK.
Instead, I am hooking things with the redux store and I am persisting the redux store details in local storage on every action to hydrate the state on page reloads.
Assuming, I leave the session ideal for a few hours eventually, my auth token expired.
This causes the SDK subscription renewal to fail.
The challenge I am facing is when there is a renewalError event published, the SDK clears the subscription details of the object
https://github.com/ringcentral/ringcentral-js/blob/master/subscriptions/src/subscription/Subscription.ts#L205

I am unable to find a way to clear the subscription from my redux store/self-implemented cache because I do not get to know which subscription Id failed to renew

My code is as follows

import * as RingCentral from '@ringcentral/sdk'
import { Subscriptions } from "@ringcentral/subscriptions";

sdk = new RingCentral.SDK({
    server: serverUrl,
    clientId: appKey,
    clientSecret: appSecret,
    redirectUri: `${window.location.origin}/redirect.html`
});
platform = sdk.platform()
subscriptions = new Subscriptions({ sdk });
const subscription = subscriptions.createSubscription();
subscription.on(subscription.events.renewError, (data) => listener({source: subscription, event: subscription.events.renewError, data}))
await subscription.setEventFilters(eventFilters).register();

Somewhere else in the code

const listener  = () => ({source: subscription, event, data}) => {
if (event === subscription.events.renewError) {
    // Clear redux store
   // No way to know what subscription failed since subscription.subscription() returns empty object as it was removed before emitting this event in the SDK
  }
}

One potential solution is to form a closure and pass the subscription id while attaching the event listener but that feels counter-intuitive.
I think we should return the subscription Id/entire subscriptionData as part of the event data when renewal/subscription fails so that someone implementing their own persistence mechanism can do the cleanup

@kirill-konshin

Calling subscription() does not work since before emitting the renewError message, the sdk is clearing the subscription data. This means calling subscription() after renewError has occurred returns an empty object

https://github.com/ringcentral/ringcentral-js/blob/master/subscriptions/src/subscription/Subscription.ts#L205
this.reset().emit(this.events.renewError, e);
reset will clear the subscription info leading to returning {} when called .subscription()

For anyone else reading this wanting to look for a potential workaround, you could create a closure to make sure you are maintaining the subscriptionId when renewError is emitted.
Flipping the order of emit (admitting renewError) and clear (clear subscription data) in the SDK could work but that might cause backward compatibility problems.
There are no plans to change this for now considering there is a workaround available