/// 'use strict'; import {ChangeDetectorRef} from 'angular2/core'; import {DefaultIterableDifferFactory, CollectionChangeRecord} from 'angular2/src/core/change_detection/differs/default_iterable_differ'; import {ObservableWrapper} from 'angular2/src/facade/async'; import {MongoCursorObserver, AddChange, MoveChange, RemoveChange} from './mongo_cursor_observer'; export interface ObserverFactory { create(cursor: Object): Object; } class MongoCursorObserverFactory implements ObserverFactory { create(cursor: Object): Object { if (cursor instanceof Mongo.Cursor) { return new MongoCursorObserver(cursor); } return null; } } export class MongoCursorDifferFactory extends DefaultIterableDifferFactory { supports(obj: Object): boolean { return obj instanceof Mongo.Cursor; } create(cdRef: ChangeDetectorRef): MongoCursorDiffer { return new MongoCursorDiffer(cdRef, new MongoCursorObserverFactory()); } } export class MongoCursorDiffer { private _inserted: Array = []; private _removed: Array = []; private _moved: Array = []; private _curObserver: MongoCursorObserver; private _lastChanges: Array; private _listSize: number = 0; private _cursor: Mongo.Cursor; private _obsFactory: ObserverFactory; private _subscription: Object; constructor(cdRef: ChangeDetectorRef, obsFactory: ObserverFactory) { this._obsFactory = obsFactory; } forEachAddedItem(fn: Function) { for (let i = 0; i < this._inserted.length; i++) { fn(this._inserted[i]); } } forEachMovedItem(fn: Function) { for (let i = 0; i < this._moved.length; i++) { fn(this._moved[i]); } } forEachRemovedItem(fn: Function) { for (let i = 0; i < this._removed.length; i++) { fn(this._removed[i]); } } diff(cursor: Mongo.Cursor) { this._reset(); let newCursor = false; if (cursor && this._cursor !== cursor) { newCursor = true; this._destroyObserver(); this._cursor = cursor; this._curObserver = this._obsFactory.create(cursor); this._subscription = ObservableWrapper.subscribe(this._curObserver, zone.bind(changes => { this._updateLatestValue(changes); })); } if (this._lastChanges) { this._applyChanges(this._lastChanges); } /** * If either last changes or new cursor is true, then * return "this" to notify Angular2 to re-build views. * If last changes or new cursor are true simultaneously * means that Mongo cursor has been changed and it's expected * that last changes (if any) of that cursor are additions only * (otherwise it won't likely work). * So removals of the previous cursor and additions of * the new one will processed at the same time. */ if (this._lastChanges || newCursor) { this._lastChanges = null; return this; } return null; } onDestroy() { this._destroyObserver(); } get observer() { return this._curObserver; } _destroyObserver() { if (this._curObserver) { this._curObserver.destroy(); } this._applyCleanup(); } _updateLatestValue(changes) { this._lastChanges = changes; } _reset() { this._inserted.length = 0; this._moved.length = 0; this._removed.length = 0; } // Reset previous state of the differ by removing all currently shown documents. _applyCleanup() { for (let index = 0; index < this._listSize; index++) { this._removed.push(this._createChangeRecord( null, index, null)); } this._listSize = 0; } _applyChanges(changes) { for (let i = 0; i < changes.length; i++) { if (changes[i] instanceof AddChange) { this._inserted.push(this._createChangeRecord( changes[i].index, null, changes[i].item)); this._listSize++; } if (changes[i] instanceof MoveChange) { this._moved.push(this._createChangeRecord( changes[i].toIndex, changes[i].fromIndex, changes[i].item)); } if (changes[i] instanceof RemoveChange) { this._removed.push(this._createChangeRecord( null, changes[i].index, changes[i].item)); this._listSize--; } } } _createChangeRecord(currentIndex, prevIndex, item) { let record = new CollectionChangeRecord(item); record.currentIndex = currentIndex; record.previousIndex = prevIndex; return record; } }