UNPKG

diffusion

Version:

Diffusion JavaScript client

87 lines (67 loc) 2.56 kB
var UnsubscribeReason = require('../../topics/topics').UnsubscribeReason; var Arrays = require('util/array'); module.exports = function TopicCacheEntryImpl(streams, path, details, specification) { function proxies(details, registry) { var p = streams; if (p.length === 0) { p = registry.getFallbacks(details); } return p; } var self = this; this.getTopicPath = function() { return path; }; this.getTopicDetails = function() { return details; }; this.getTopicSpecification = function() { return specification; }; this.notifyInitialSubscription = function(registry) { proxies(specification, registry).forEach(function(proxy) { proxy.onSubscription(path, details, specification); }); }; this.notifySubscription = function(proxy) { proxy.onSubscription(path, details, specification); self.notifyValueToNewStream(path, details, specification, proxy); }; this.notifyUnsubscription = function(reason, registry) { proxies(specification, registry).forEach(function(proxy) { proxy.onUnsubscription(path, details, specification, reason); }); }; this.notifyValue = function(content, oldValue, newValue, registry) { proxies(specification, registry).forEach(function(proxy) { proxy.onValue(path, details, specification, content, oldValue, newValue); }); }; this.notifyDelta = function(content, delta, oldValue, newValue, registry) { proxies(specification, registry).forEach(function(proxy) { proxy.onDelta(path, details, specification, content, delta, oldValue, newValue); }); }; this.addStream = function(stream, registry) { if (streams.length === 0) { registry.getFallbacks(specification).forEach(function(fallback) { fallback.onUnsubscription(path, details, specification, UnsubscribeReason.STREAM_CHANGE); }); } if (streams.indexOf(stream) < 0) { streams.push(stream); self.notifySubscription(stream); } }; this.removeStream = function(stream, registry) { Arrays.remove(streams, stream); if (streams.length === 0) { registry.getFallbacks(specification).forEach(function(fallback) { self.notifySubscription(fallback); }); } }; this.removeAllStreams = function() { streams.length = 0; }; };