Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reactivity for non-primitives #418

Merged
merged 7 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 16 additions & 6 deletions packages/blaze/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Blaze._calculateCondition = function (cond) {
/**
* @summary Constructs a View that renders content with a data context.
* @locus Client
* @param {Object|Function} data An object to use as the data context, or a function returning such an object. If a function is provided, it will be reactively re-run.
* @param {Object|Function} data An object to use as the data context, or a function returning such an object. If a
* function is provided, it will be reactively re-run.
* @param {Function} contentFunc A Function that returns [*renderable content*](#Renderable-Content).
*/
Blaze.With = function (data, contentFunc) {
Expand Down Expand Up @@ -39,7 +40,12 @@ Blaze.With = function (data, contentFunc) {
* @param {Binding} y
*/
function _isEqualBinding(x, y) {
return x && y ? x.error === y.error && x.value === y.value : x === y;
if (typeof x === 'object' && typeof y === 'object') {
return x.error === y.error && ReactiveVar._isEqual(x.value, y.value);
}
else {
return ReactiveVar._isEqual(x, y);
}
}

/**
Expand Down Expand Up @@ -88,9 +94,11 @@ Blaze.Let = function (bindings, contentFunc) {
/**
* @summary Constructs a View that renders content conditionally.
* @locus Client
* @param {Function} conditionFunc A function to reactively re-run. Whether the result is truthy or falsy determines whether `contentFunc` or `elseFunc` is shown. An empty array is considered falsy.
* @param {Function} conditionFunc A function to reactively re-run. Whether the result is truthy or falsy determines
* whether `contentFunc` or `elseFunc` is shown. An empty array is considered falsy.
* @param {Function} contentFunc A Function that returns [*renderable content*](#Renderable-Content).
* @param {Function} [elseFunc] Optional. A Function that returns [*renderable content*](#Renderable-Content). If no `elseFunc` is supplied, no content is shown in the "else" case.
* @param {Function} [elseFunc] Optional. A Function that returns [*renderable content*](#Renderable-Content). If no
* `elseFunc` is supplied, no content is shown in the "else" case.
*/
Blaze.If = function (conditionFunc, contentFunc, elseFunc, _not) {
var conditionVar = new ReactiveVar;
Expand All @@ -113,9 +121,11 @@ Blaze.If = function (conditionFunc, contentFunc, elseFunc, _not) {
/**
* @summary An inverted [`Blaze.If`](#Blaze-If).
* @locus Client
* @param {Function} conditionFunc A function to reactively re-run. If the result is falsy, `contentFunc` is shown, otherwise `elseFunc` is shown. An empty array is considered falsy.
* @param {Function} conditionFunc A function to reactively re-run. If the result is falsy, `contentFunc` is shown,
* otherwise `elseFunc` is shown. An empty array is considered falsy.
* @param {Function} contentFunc A Function that returns [*renderable content*](#Renderable-Content).
* @param {Function} [elseFunc] Optional. A Function that returns [*renderable content*](#Renderable-Content). If no `elseFunc` is supplied, no content is shown in the "else" case.
* @param {Function} [elseFunc] Optional. A Function that returns [*renderable content*](#Renderable-Content). If no
* `elseFunc` is supplied, no content is shown in the "else" case.
*/
Blaze.Unless = function (conditionFunc, contentFunc, elseFunc) {
return Blaze.If(conditionFunc, contentFunc, elseFunc, true /*_not*/);
Expand Down
8 changes: 5 additions & 3 deletions packages/spacebars/spacebars-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Spacebars.makeRaw = function (value) {
};

/***
* @sumamry Executes `fn` with the resolved value of `promise` while preserving
* @summary Executes `fn` with the resolved value of `promise` while preserving
* the context, i.e., `Blaze.currentView` and `Tracker.currentComputation`.
* @template T
* @template U
Expand Down Expand Up @@ -157,7 +157,7 @@ Spacebars.call = function (value/*, args*/) {
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;
anyIsPromise = anyIsPromise || isPromiseLike(newArgs[i-1]);
}

if (anyIsPromise) {
Expand All @@ -173,6 +173,8 @@ Spacebars.call = function (value/*, args*/) {
}
};

const isPromiseLike = x => typeof x?.then === 'function';

// Call this as `Spacebars.kw({ ... })`. The return value
// is `instanceof Spacebars.kw`.
Spacebars.kw = function (hash) {
Expand Down Expand Up @@ -235,7 +237,7 @@ Spacebars.dot = function (value, id1/*, id2, ...*/) {
if (! value)
return value; // falsy, don't index, pass through

if (value && typeof value.then === 'function')
if (isPromiseLike(value))
return _thenWithContext(value, value => Spacebars.dot(value, id1));

var result = value[id1];
Expand Down