observe_sequence.js
function (sequenceFunc, callbacks) {
    var lastSeq = null;
    var activeObserveHandle = null;

    // 'lastSeqArray' contains the previous value of the sequence
    // we're observing. It is an array of objects with '_id' and
    // 'item' fields.  'item' is the element in the array, or the
    // document in the cursor.
    //
    // '_id' is whichever of the following is relevant, unless it has
    // already appeared -- in which case it's randomly generated.
    //
    // * if 'item' is an object:
    //   * an '_id' field, if present
    //   * otherwise, the index in the array
    //
    // * if 'item' is a number or string, use that value
    //
    // XXX this can be generalized by allowing {{#each}} to accept a
    // general 'key' argument which could be a function, a dotted
    // field name, or the special @index value.
    var lastSeqArray = []; // elements are objects of form {_id, item}
    var computation = Tracker.autorun(function () {
      var seq = sequenceFunc();

      Tracker.nonreactive(function () {
        var seqArray; // same structure as `lastSeqArray` above.

        if (activeObserveHandle) {
          // If we were previously observing a cursor, replace lastSeqArray with
          // more up-to-date information.  Then stop the old observe.
          lastSeqArray = _.map(lastSeq.fetch(), function (doc) {
            return {_id: doc._id, item: doc};
          });
          activeObserveHandle.stop();
          activeObserveHandle = null;
        }

        if (!seq) {
          seqArray = seqChangedToEmpty(lastSeqArray, callbacks);
        } else if (seq instanceof Array) {
          seqArray = seqChangedToArray(lastSeqArray, seq, callbacks);
        } else if (isStoreCursor(seq)) {
          var result /* [seqArray, activeObserveHandle] */ =
                seqChangedToCursor(lastSeqArray, seq, callbacks);
          seqArray = result[0];
          activeObserveHandle = result[1];
        } else {
          throw badSequenceError();
        }

        diffArray(lastSeqArray, seqArray, callbacks);
        lastSeq = seq;
        lastSeqArray = seqArray;
      });
    });

    return {
      stop: function () {
        computation.stop();
        if (activeObserveHandle)
          activeObserveHandle.stop();
      }
    };
  }
function (sequenceFunc, callbacks) {
    var lastSeq = null;
    var activeObserveHandle = null;

    // 'lastSeqArray' contains the previous value of the sequence
    // we're observing. It is an array of objects with '_id' and
    // 'item' fields.  'item' is the element in the array, or the
    // document in the cursor.
    //
    // '_id' is whichever of the following is relevant, unless it has
    // already appeared -- in which case it's randomly generated.
    //
    // * if 'item' is an object:
    //   * an '_id' field, if present
    //   * otherwise, the index in the array
    //
    // * if 'item' is a number or string, use that value
    //
    // XXX this can be generalized by allowing {{#each}} to accept a
    // general 'key' argument which could be a function, a dotted
    // field name, or the special @index value.
    var lastSeqArray = []; // elements are objects of form {_id, item}
    var computation = Tracker.autorun(function () {
      var seq = sequenceFunc();

      Tracker.nonreactive(function () {
        var seqArray; // same structure as `lastSeqArray` above.

        if (activeObserveHandle) {
          // If we were previously observing a cursor, replace lastSeqArray with
          // more up-to-date information.  Then stop the old observe.
          lastSeqArray = _.map(lastSeq.fetch(), function (doc) {
            return {_id: doc._id, item: doc};
          });
          activeObserveHandle.stop();
          activeObserveHandle = null;
        }

        if (!seq) {
          seqArray = seqChangedToEmpty(lastSeqArray, callbacks);
        } else if (seq instanceof Array) {
          seqArray = seqChangedToArray(lastSeqArray, seq, callbacks);
        } else if (isStoreCursor(seq)) {
          var result /* [seqArray, activeObserveHandle] */ =
                seqChangedToCursor(lastSeqArray, seq, callbacks);
          seqArray = result[0];
          activeObserveHandle = result[1];
        } else {
          throw badSequenceError();
        }

        diffArray(lastSeqArray, seqArray, callbacks);
        lastSeq = seq;
        lastSeqArray = seqArray;
      });
    });

    return {
      stop: function () {
        computation.stop();
        if (activeObserveHandle)
          activeObserveHandle.stop();
      }
    };
  }
function ( sequenceFunc , callbacks ) { var lastSeq = null ; var activeObserveHandle = null ; // 'lastSeqArray' contains the previous value of the sequence // we're observing. It is an array of objects with '_id' and // 'item' fields.  'item' is the element in the array, or the // document in the cursor. // // '_id' is whichever of the following is relevant, unless it has // already appeared -- in which case it's randomly generated. // // * if 'item' is an object: //   * an '_id' field, if present //   * otherwise, the index in the array // // * if 'item' is a number or string, use that value // // XXX this can be generalized by allowing {{#each}} to accept a // general 'key' argument which could be a function, a dotted // field name, or the special @index value. var lastSeqArray = [ ] ; // elements are objects of form {_id, item} var computation = Tracker . autorun ( function ( ) { var seq = sequenceFunc ( ) ; Tracker . nonreactive ( function ( ) { var seqArray ; // same structure as `lastSeqArray` above. if ( activeObserveHandle ) { // If we were previously observing a cursor, replace lastSeqArray with // more up-to-date information.  Then stop the old observe. lastSeqArray = _ . map ( lastSeq . fetch ( ) , function ( doc ) { return { _id : doc . _id , item : doc } ; } ) ; activeObserveHandle . stop ( ) ; activeObserveHandle = null ; } if ( ! seq ) { seqArray = seqChangedToEmpty ( lastSeqArray , callbacks ) ; } else if ( seq instanceof Array ) { seqArray = seqChangedToArray ( lastSeqArray , seq , callbacks ) ; } else if ( isStoreCursor ( seq ) ) { var result /* [seqArray, activeObserveHandle] */ = seqChangedToCursor ( lastSeqArray , seq , callbacks ) ; seqArray = result [ 0 ] ; activeObserveHandle = result [ 1 ] ; } else { throw badSequenceError ( ) ; } diffArray ( lastSeqArray , seqArray , callbacks ) ; lastSeq = seq ; lastSeqArray = seqArray ; } ) ; } ) ; return { stop : function ( ) { computation . stop ( ) ; if ( activeObserveHandle ) activeObserveHandle . stop ( ) ; } } ; }
A mechanism similar to cursor.observe which receives a reactive function returning a sequence type and firing appropriate callbacks when the value changes.  @param sequenceFunc {Function} a reactive function returning a sequence type. The currently supported sequence types are: Array, Cursor, and null.  @param callbacks {Object} similar to a specific subset of callbacks passed to `cursor.observe` (http://docs.meteor.com/#observe), with minor variations to support the fact that not all sequences contain objects with _id fields.  Specifically:  * addedAt(id, item, atIndex, beforeId) * changedAt(id, newItem, oldItem, atIndex) * removedAt(id, oldItem, atIndex) * movedTo(id, item, fromIndex, toIndex, beforeId)  @returns {Object(stop: Function)} call 'stop' on the return value to stop observing this sequence function.  We don't make any assumptions about our ability to compare sequence elements (ie, we don't assume EJSON.equals works; maybe there is extra state/random methods on the objects) so unlike cursor.observe, we may sometimes call changedAt() when nothing actually changed. XXX consider if we *can* make the stronger assumption and avoid no-op changedAt calls (in some cases?)  XXX currently only supports the callbacks used by our implementation of {{#each}}, but this can be expanded.  XXX #each doesn't use the indices (though we'll eventually need a way to get them when we support `@index`), but calling `cursor.observe` causes the index to be calculated on every callback using a linear scan (unless you turn it off by passing `_no_indices`).  Any way to avoid calculating indices on a pure cursor observe like we used to?
A mechanism similar to cursor . observe which receives a reactive function returning a sequence type and firing appropriate callbacks when the value changes .