UNPKG

@drift-labs/common

Version:

Common functions for Drift

47 lines 1.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SharedInterval = void 0; const rxjs_1 = require("rxjs"); /** * This class creates an interval which multiple "subscribers" can add their "props" to. * * The interval has a base tick rate, and the subscribers add their tick multiple and props to the interval. * * The interval will tick at the base rate, and for each tick it will the props of any subscribers whose tick multiple is a factor of the current tick. */ class SharedInterval { constructor(tickRate) { this.tickCount = 0; this.subscription = new rxjs_1.Subject(); this.subscribers = new Map(); this.tickRate = tickRate; } addSubscriber(tickMultiple, id, props) { this.subscribers.set(id, { tickMultiple, props, }); } removeSubscriber(id) { this.subscribers.delete(id); } start() { this.interval = setInterval(() => { this.tickCount++; const propsForTick = Array.from(this.subscribers.values()).map((subscriber) => { if (this.tickCount % subscriber.tickMultiple === 0) { return subscriber.props; } }); this.subscription.next(propsForTick); }, this.tickRate); } stop() { clearInterval(this.interval); } subscribe() { return this.subscription.asObservable(); } } exports.SharedInterval = SharedInterval; //# sourceMappingURL=SharedInterval.js.map