1

I am using Mr Fawcett's the famous streaming component (https://github.com/afawcett/streamingcomponent) with his another great wrapper for deploying Custom Metadata Types (https://github.com/afawcett/custommetadataapi).

I've noticed that c:streaming component quite aggressively polls the streaming API immediately after init. In my use case, I'd rather setup handshake after user clicks Save or so.

What would be a good solution for this? Setting some sort of Boolean in afterScriptLoaded handler that indicates whether Save button is enabled?

1 Answer 1

2

You could just add a bit of code:

<aura:attribute name="streamingEnabled" type="Boolean" default="false" />
<aura:attribute name="streamingLoaded" type="Boolean" default="false" />
<aura:registerEvent name="onloaded" type="c:streamingEnabled" />
<aura:handler name="change" value="{!v.streamingEnabled}" action="{!c.handleEnable}" />

Note: I'm not including the event here, but you need a new event; we can use this to signal to whatever is using our component that we can use it.

From there, you can change doInit:

doInit: function(component, event, helper) {
  component.set("v.streamingLoaded", true);
  if(component.get("v.streamingEnabled")) {
    helper.startStreaming(component);
  }
}

And add your change handler:

handleEnable: function(component, event, helper) {
  if(component.get("v.streamingLoaded")) {
    if(component.get("v.streamingEnabled")) {
      helper.startStreaming(component);
    } else {
      helper.stopStreaming(component);
    }
  }
}

You'll need a bit more work than just this, but basically, you just need to attach an extra listener (aura:valueChange), and then toggle the streaming API off/on as you need.

4
  • 1
    Done a bit differently with promises. Created an aura:method and replaced some code from from doInit to return a promise. Now I can go component.find('StreamingService').init().then(...).
    – dzh
    Jun 8, 2018 at 11:26
  • @dzh makes sense. I wasn't trying to hyperoptimize, just make it workable with the question I had on hand.
    – sfdcfox
    Jun 8, 2018 at 11:28
  • @dzh +1 For the promise approach. Exposing two aura:method : subscribe and unsubscribe gives the flexibility to enable/disable the subscription. You could post your comment as an answer.
    – Praveen
    Jun 10, 2018 at 17:35
  • 1
    @Praveen Indeed I had to add a method call to close the subscription after my action has completed as subsequent init's would fail :)
    – dzh
    Jun 12, 2018 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .