From bc54cecb9e9ca224eb83105531638c0b77eb481f Mon Sep 17 00:00:00 2001 From: Geoff Schmidt Date: Sat, 4 Aug 2012 08:12:08 -0700 Subject: [PATCH] refactor isolate to simplify. breaks an order-sensitive test, others pass. --- packages/spark/spark.js | 64 ++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 42 deletions(-) diff --git a/packages/spark/spark.js b/packages/spark/spark.js index 89eff3bab4..4b50b12207 100644 --- a/packages/spark/spark.js +++ b/packages/spark/spark.js @@ -427,53 +427,33 @@ Spark.isolate = function (htmlFunc) { if (!renderer) return htmlFunc(); - var ctx = new Meteor.deps.Context; + var ctx; var slain = false; - var html = - renderer.annotate( - ctx.run(htmlFunc), Spark._ANNOTATION_ISOLATE, - function (range) { - range.finalize = function () { - // Spark.finalize() was called on us (presumably because we - // were removed from the document.) Tear down our structures - // without doing any more updates. - slain = true; - ctx.invalidate(); - }; + return renderer.annotate('', Spark._ANNOTATION_ISOLATE, function (range) { + range.finalize = function () { + // Spark.finalize() was called on us (presumably because we + // were removed from the document.) Tear down our structures + // without doing any more updates. + slain = true; + ctx.invalidate(); + }; - ctx.on_invalidate(function () { - if (slain) - return; // killed by finalize. range has already been destroyed. + var refresh = function () { + if (slain) + return; // killed by finalize. range has already been destroyed. - // htmlFunc changed its mind about what it returns. Rerender it. - var frag = Spark.render(function () { - return Spark.isolate(htmlFunc); - }); - - var tempRange = new LiveRange(Spark._TAG, frag, null, - true /* inner */); - tempRange.operate(function (start, end) { - // Wrap contents of frag, *inside* the ISOLATE annotation, - // as appropriate for insertion into `range`. We want the - // wrapping inside the range so that if you have a - // containing an isolate, and the isolate returns a - // sometimes and a other times, the wrapping will - // change as appropriate. - DomUtils.wrapFragmentForContainer(frag, range.containerNode()); - }); - tempRange.destroy(); - - // Note that we depend on the fact that the ISOLATE - // liverange is the outermost liverange in frag (or will be - // once all temporary liveranges are destroyed.) If it were - // not, then every time we are invalidated we'll leave - // behind a few more junk liveranges. - replaceContentsPreservingLandmarks(range, frag); - range.destroy(); - }); + ctx = new Meteor.deps.Context; + var frag = Spark.render(function () { + return ctx.run(htmlFunc); }); + DomUtils.wrapFragmentForContainer(frag, range.containerNode()); + replaceContentsPreservingLandmarks(range, frag); - return html; + ctx.on_invalidate(refresh); + }; + + refresh(); + }); }; /******************************************************************************/