UNPKG

rxdeep

Version:
39 lines 1.24 kB
export class Watcher { constructor(initial, keyFunc) { this.keyFunc = keyFunc; this._keymap = {}; this.changes(initial); } changes(list = []) { const changes = { additions: [], deletions: [], moves: [], }; const keymap = list.reduce((map, item, index) => { const _key = this.keyFunc(item); map[_key] = { index, item }; if (!(_key in this._keymap)) changes.additions.push({ index, item }); return map; }, {}); Object.entries(this._keymap).forEach(([_key, entry]) => { if (!(_key in keymap)) changes.deletions.push(entry); else { const _newEntry = keymap[_key]; if (_newEntry.index !== entry.index) { changes.moves.push({ oldIndex: entry.index, newIndex: _newEntry.index, item: entry.item }); } } }); this._keymap = keymap; return changes; } get keymap() { return this._keymap; } } //# sourceMappingURL=watcher.js.map