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

Trigger digest when Subscription ready changes #1655

Open
wants to merge 5 commits into
base: angular1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 61 additions & 11 deletions dist/angular-meteor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1759,7 +1759,8 @@ return /******/ (function(modules) { // webpackBootstrap
// Extend prototype with the defined mixins
this._extend = function (obj, options) {
var _$defaults = _underscore2.default.defaults({}, options, {
pattern: /.*/ }),
pattern: /.*/ // The patterns of the keys which will be filtered
}),
pattern = _$defaults.pattern,
context = _$defaults.context;

Expand Down Expand Up @@ -1900,6 +1901,8 @@ return /******/ (function(modules) { // webpackBootstrap
// Calls Meteor.subscribe() which will be digested after each invokation
// and automatically destroyed
$$Core.subscribe = function (subName, fn, cb) {
var _this = this;

fn = this.$bindToContext($Mixer.caller, fn || angular.noop);
cb = cb ? this.$bindToContext($Mixer.caller, cb) : angular.noop;

Expand All @@ -1919,18 +1922,42 @@ return /******/ (function(modules) { // webpackBootstrap
throw Error('argument 3 must be a function or an object');
}

if (_underscore2.default.isObject(cb)) {
for (var hook in hooks) {
if (hooks.hasOwnProperty(hook) && cb[hook]) {
// Don't use any of additional callbacks in Meteor.subscribe
hooks[hook] = cb[hook];
delete cb[hook];
}
if (_underscore2.default.isFunction(cb)) {
cb = {
onReady: cb
};
}

for (var hook in hooks) {
if (hooks.hasOwnProperty(hook) && cb[hook]) {
// Don't use any of additional callbacks in Meteor.subscribe
hooks[hook] = cb[hook];
delete cb[hook];
}
}

var result = {};

var startStopBalance = 0;

var onReadyHook = cb.onReady || angular.noop;
cb.onReady = function () {
result.isLoading = false;
result.error = null;
onReadyHook();
};

var onStopHook = cb.onStop || angular.noop;
cb.onStop = function (error) {
startStopBalance -= 1;

if (startStopBalance === 0) {
result.isLoading = false;
result.error = error;
}
onStopHook(error);
};

var computation = this.autorun(function () {
var _Meteor;

Expand All @@ -1941,9 +1968,32 @@ return /******/ (function(modules) { // webpackBootstrap
throw Error('reactive function\'s return value must be an array');
}

var oldError = result.error;
result.isLoading = true;
result.error = null;
startStopBalance += 1;
hooks.onStart();

var subscription = (_Meteor = Meteor).subscribe.apply(_Meteor, [subName].concat(_toConsumableArray(args), [cb]));

hooks.onStart();
// In case no new subscription is established in Meteor.
// Happens if the autorun was triggered, but the params of the subscription didn't change.
if (result.subscriptionId === subscription.subscriptionId) {
startStopBalance -= 1;

if (startStopBalance === 0) {
result.isLoading = false;
result.error = oldError;
}
}

Tracker.autorun(function () {
// Subscribe to changes on the ready-property by calling the ready-method.
subscription.ready();

// Re-run the digest cycle if we are not in one already.
_this.$$throttledDigest();
});

result.ready = subscription.ready.bind(subscription);
result.subscriptionId = subscription.subscriptionId;
Expand Down Expand Up @@ -1999,7 +2049,7 @@ return /******/ (function(modules) { // webpackBootstrap

// Digests scope only if there is no phase at the moment
$$Core.$$throttledDigest = function () {
var _this = this;
var _this2 = this;

var isDigestable = !this.$$destroyed && !this.$$phase && !this.$root.$$phase;

Expand All @@ -2008,7 +2058,7 @@ return /******/ (function(modules) { // webpackBootstrap
// we want to run this second autorun in a non-reactive manner.
// thus preventing inner autoruns from being dependent on their parents.
Tracker.nonreactive(function () {
return _this.$digest();
return _this2.$digest();
});
}
};
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-meteor.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/angular-meteor.min.js.map

Large diffs are not rendered by default.

63 changes: 55 additions & 8 deletions src/modules/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,42 @@ angular.module(name, [
throw Error('argument 3 must be a function or an object');
}

if (_.isObject(cb)) {
for (const hook in hooks) {
if (hooks.hasOwnProperty(hook) && cb[hook]) {
// Don't use any of additional callbacks in Meteor.subscribe
hooks[hook] = cb[hook];
delete cb[hook];
}
if (_.isFunction(cb)) {
cb = {
onReady: cb,
};
}

for (const hook in hooks) {
if (hooks.hasOwnProperty(hook) && cb[hook]) {
// Don't use any of additional callbacks in Meteor.subscribe
hooks[hook] = cb[hook];
delete cb[hook];
}
}

const result = {};

let startStopBalance = 0;

const onReadyHook = cb.onReady || angular.noop;
cb.onReady = function () {
result.isLoading = false;
result.error = null;
onReadyHook();
};

const onStopHook = cb.onStop || angular.noop;
cb.onStop = function (error) {
startStopBalance -= 1;

if (startStopBalance === 0) {
result.isLoading = false;
result.error = error;
}
onStopHook(error);
};

const computation = this.autorun(() => {
let args = fn();
if (angular.isUndefined(args)) args = [];
Expand All @@ -81,9 +105,32 @@ angular.module(name, [
throw Error(`reactive function's return value must be an array`);
}

const oldError = result.error;
result.isLoading = true;
result.error = null;
startStopBalance += 1;
hooks.onStart();

const subscription = Meteor.subscribe(subName, ...args, cb);

hooks.onStart();
// In case no new subscription is established in Meteor.
// Happens if the autorun was triggered, but the params of the subscription didn't change.
if (result.subscriptionId === subscription.subscriptionId) {
startStopBalance -= 1;

if (startStopBalance === 0) {
result.isLoading = false;
result.error = oldError;
}
}

Tracker.autorun(() => {
// Subscribe to changes on the ready-property by calling the ready-method.
subscription.ready();

// Re-run the digest cycle if we are not in one already.
this.$$throttledDigest();
});

result.ready = subscription.ready.bind(subscription);
result.subscriptionId = subscription.subscriptionId;
Expand Down