-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #413 from meteor/async-call-and-dot
Added support for `Promise`s in `Spacebars.call` and `Spacebars.dot`.
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,9 +117,49 @@ Spacebars.makeRaw = function (value) { | |
return HTML.Raw(value); | ||
}; | ||
|
||
// FIXME: Remove once `spacebars` can depend on `[email protected]` (Meteor 2.10.0). | ||
const _withComputation = Tracker.withComputation || function (computation, f) { | ||
var previousComputation = Tracker.currentComputation; | ||
|
||
Tracker.currentComputation = computation; | ||
Tracker.active = !!computation; | ||
|
||
try { | ||
return f(); | ||
} finally { | ||
Tracker.currentComputation = previousComputation; | ||
Tracker.active = !!previousComputation; | ||
} | ||
}; | ||
|
||
/*** | ||
* @sumamry Executes `fn` with the resolved value of `promise` while preserving | ||
* the context, i.e., `Blaze.currentView` and `Tracker.currentComputation`. | ||
* @template T | ||
* @template U | ||
* @param {Promise<T>} promise | ||
* @param {(x: T) => U} fn | ||
* @returns {Promise<U>} | ||
*/ | ||
function _thenWithContext(promise, fn) { | ||
const computation = Tracker.currentComputation; | ||
const view = Blaze.currentView; | ||
return promise.then(value => | ||
Blaze._withCurrentView(view, () => | ||
_withComputation(computation, () => | ||
fn(value) | ||
) | ||
) | ||
); | ||
} | ||
|
||
// If `value` is a function, evaluate its `args` (by calling them, if they | ||
// are functions), and then call it on them. Otherwise, return `value`. | ||
// | ||
// If any of the arguments is a `Promise` or a function returning one, then the | ||
// `value` will be called once all of the arguments resolve. If any of them | ||
// rejects, so will the call. | ||
// | ||
// If `value` is not a function and is not null, then this method will assert | ||
// that there are no args. We check for null before asserting because a user | ||
// may write a template like {{user.fullNameWithPrefix 'Mr.'}}, where the | ||
|
@@ -128,9 +168,15 @@ Spacebars.call = function (value/*, args*/) { | |
if (typeof value === 'function') { | ||
// Evaluate arguments by calling them if they are functions. | ||
var newArgs = []; | ||
let anyIsPromise = false; | ||
for (var i = 1; i < arguments.length; i++) { | ||
var arg = arguments[i]; | ||
newArgs[i-1] = (typeof arg === 'function' ? arg() : arg); | ||
anyIsPromise = anyIsPromise || newArgs[i-1] instanceof Promise; | ||
} | ||
|
||
if (anyIsPromise) { | ||
return _thenWithContext(Promise.all(newArgs), newArgs => value.apply(null, newArgs)); | ||
} | ||
|
||
return value.apply(null, newArgs); | ||
|
@@ -170,6 +216,10 @@ Spacebars.SafeString.prototype = Handlebars.SafeString.prototype; | |
// a wrapped version of `baz` that always uses `foo.bar` as | ||
// `this`). | ||
// | ||
// If any of the intermediate values is a `Promise`, the result will be one as | ||
// well, i.e., accessing a field of a `Promise` results in a `Promise` of the | ||
// accessed field. Rejections are passed-through. | ||
// | ||
// In `Spacebars.dot(foo, "bar")`, `foo` is assumed to be either | ||
// a non-function value or a "fully-bound" function wrapping a value, | ||
// where fully-bound means it takes no arguments and ignores `this`. | ||
|
@@ -200,6 +250,9 @@ Spacebars.dot = function (value, id1/*, id2, ...*/) { | |
if (! value) | ||
return value; // falsy, don't index, pass through | ||
|
||
if (value instanceof Promise) | ||
return _thenWithContext(value, value => Spacebars.dot(value, id1)); | ||
|
||
var result = value[id1]; | ||
if (typeof result !== 'function') | ||
return result; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters