From 20766ac05e6df8a4762c5bd7706fb77e07bb7453 Mon Sep 17 00:00:00 2001 From: David Glasser Date: Mon, 27 Jan 2014 14:26:12 -0800 Subject: [PATCH] Backport observeHandle._fetch from shark --- packages/minimongo/observe.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/packages/minimongo/observe.js b/packages/minimongo/observe.js index e7c868f721..0dbc672f04 100644 --- a/packages/minimongo/observe.js +++ b/packages/minimongo/observe.js @@ -175,5 +175,33 @@ LocalCollection._observeFromObserveChanges = function (cursor, observeCallbacks) {callbacks: observeChangesCallbacks}); var handle = cursor.observeChanges(changeObserver.applyChange); suppressed = false; + + if (changeObserver.ordered) { + // Fetches the current list of documents, in order, as an array. Can be + // called at any time. Internal API assumed by the `observe-sequence` + // package (used by Meteor UI for `#each` blocks). Only defined on ordered + // observes (those that listen on `addedAt` or similar). Continues to work + // after `stop()` is called on the handle. + // + // Because we already materialize the full OrderedDict of all documents, it + // seems nice to provide access to the view rather than making the data + // consumer reconstitute it. This gives the consumer a shot at doing + // something smart with the feed like proxying it, since firing callbacks + // like `changed` and `movedTo` basically requires omniscience (knowing old + // and new documents, old and new indices, and the correct value for + // `before`). + // + // NOTE: If called from an observe callback for a certain change, the result + // is *not* guaranteed to be a snapshot of the cursor up to that + // change. This is because the callbacks are invoked before updating docs. + handle._fetch = function () { + var docsArray = []; + changeObserver.docs.forEach(function (doc) { + docsArray.push(transform(doc)); + }); + return docsArray; + }; + } + return handle; };