Example icon indicating copy to clipboard operation
Example copied to clipboard

Subscribe timer

Open MidoMiddle opened this issue 3 years ago • 1 comments

async () => {
  setInterval(() => {
    const stats = lib.resmon.getStatistics();
    context.client.emit('example/resmon', stats);
  }, config.resmon.interval);
  return { subscribed: 'resmon' };
};

In this case, if we reload page, we have error:

10:37:21  W3   error   TypeError: Cannot read property 'connection' of undefined
  Client.emit (/node_modules/metacom/lib/channel.js:75:18)
  Timeout._onTimeout (/application/api/example.1/subscribe.js:10:20)
  listOnTimeout (internal/timers.js:554:17)
  processTimers (internal/timers.js:497:7)

We can add try/catch to fix timer:

async () => {
  const timer = setInterval(() => {
    const stats = lib.resmon.getStatistics();
    try { context.client.emit('example/resmon', stats); }
    catch (e) { clearInterval(timer) };
  }, config.resmon.interval);
  return { subscribed: 'resmon' };
};

MidoMiddle avatar May 16 '21 10:05 MidoMiddle

Yes, you can make PR with this quick patch and issue to implement 'disconnect' event and example how to use this event with subscription.

tshemsedinov avatar May 16 '21 15:05 tshemsedinov

You didn't provide file names and location, I can't reproduce this, and even understand. I think this is not relevant as of today, a lot has already changed, release 3.0 is coming next week, try to use it.

tshemsedinov avatar Jun 22 '23 23:06 tshemsedinov