UNPKG

meteor-sdk

Version:

The aim of this library is to simplify the process of working with meteor server over DDP protocol using external JS environments

29 lines (25 loc) 753 B
/** * DDP change listener class. * @constructor * @param {Object} obj - Describes changes of interest. * @param {*} inst - Event handler instance. * @param {simpleDDP} [listenersArray = 'onChangeFuncs'] - Property name of event handler instance, array of listeners. */ export function ddpOnChange<T extends { [x: string]: any[] }>(obj: {}, inst: T, listenersArray: any = 'onChangeFuncs') { let _isStopped = true const start = () => { if (_isStopped) { inst[listenersArray].push(obj) _isStopped = false } } const stop = () => { const index = inst[listenersArray].indexOf(obj) if (index > -1) { inst[listenersArray].splice(index, 1) _isStopped = true } } start() return { start, stop } }