UNPKG

sub-events

Version:

Lightweight, strongly-typed events, with monitored subscriptions.

30 lines (29 loc) 864 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromInterval = fromInterval; var src_1 = require("../dist/src"); /** * Creates a time-interval event: * * - The interval re-starts when the first subscriber registers; * - The interval stops when the last subscription is cancelled. */ function fromInterval(timeout, options) { var sec = new src_1.SubEventCount(); var timer; sec.onCount.subscribe(function (info) { var start = info.prevCount === 0; // fresh start var stop = info.newCount === 0; // no subscriptions left if (start) { timer = setInterval(function () { sec.emit(undefined, options); }, timeout); } else { if (stop) { clearInterval(timer); } } }); return sec; }