Skip to content

Commit

Permalink
chore: v2.0.0-beta.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed May 14, 2018
1 parent 5bbc3db commit 64da0c1
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 96 deletions.
138 changes: 91 additions & 47 deletions dist/vue-meteor-tracker.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ var CMeteorSub = {
var parameters = typeof this.parameters === 'function' ? this.parameters : function () {
return _this.parameters || [];
};
this.$_unsub = this.$addReactiveSub(this.name, parameters);
this.$_unsub = this.$subscribe(this.name, parameters);
}
},

Expand Down Expand Up @@ -1715,6 +1715,16 @@ var index = {
}, merge(toData, fromData));
};

function getResult(result) {
if (result && typeof result.fetch === 'function') {
result = result.fetch();
}
if (Vue.config.meteor.freeze) {
result = Object.freeze(result);
}
return result;
}

function prepare() {
var _this = this;

Expand Down Expand Up @@ -1744,21 +1754,16 @@ var index = {

if (!isServer || ssr) {
// Subscriptions
if (meteor.subscribe || meteor.$subscribe) {
var subscribeOptions = Object.assign({}, meteor.subscribe, meteor.$subscribe);
for (var key in subscribeOptions) {
this.$addReactiveSub(key, subscribeOptions[key]);
if (meteor.$subscribe) {
for (var key in meteor.$subscribe) {
this.$subscribe(key, meteor.$subscribe[key]);
}
}

var data = Object.assign({}, lodash_omit(meteor, ['subscribe', 'data']), meteor.data);

// Reactive data
if (data) {
for (var _key in data) {
if (_key.charAt(0) !== '$') {
this.$addMeteorData(_key, data[_key]);
}
for (var _key in meteor) {
if (_key.charAt(0) !== '$') {
this.$addMeteorData(_key, meteor[_key]);
}
}
}
Expand All @@ -1783,6 +1788,14 @@ var index = {
if (this.$options.meteor && !this.$options.meteor.$lazy) {
launch.call(this);
}

// Computed props
var computed = this._computedWatchers;
if (computed) {
for (var key in computed) {
this.$addComputed(key, computed[key]);
}
}
},


Expand All @@ -1791,7 +1804,7 @@ var index = {
},

methods: {
$subscribe: function $subscribe() {
$_subscribe: function $_subscribe() {
var _this2 = this;

for (var _len = arguments.length, args = Array(_len), _key2 = 0; _key2 < _len; _key2++) {
Expand Down Expand Up @@ -1828,6 +1841,34 @@ var index = {
throw new Error('You must provide the publication name to $subscribe.');
}
},
$subscribe: function $subscribe(key, options) {
var _this3 = this;

var handle = void 0,
unwatch = void 0;
var subscribe = function subscribe(params) {
handle = _this3.$_subscribe.apply(_this3, [key].concat(toConsumableArray(params)));
};

if (typeof options === 'function') {
if (isServer) {
subscribe(options.bind(this)());
} else {
unwatch = this.$watch(options, function (params) {
subscribe(params);
}, {
immediate: true
});
}
} else {
subscribe(options);
}

return function () {
if (unwatch) unwatch();
if (handle) _this3.$stopHandle(handle);
};
},
$autorun: function $autorun(reactiveFunction) {
var handle = Tracker.autorun(reactiveFunction);
this._trackerHandles.push(handle);
Expand Down Expand Up @@ -1857,34 +1898,6 @@ var index = {
this._trackerHandles = null;
this._meteorActive = false;
},
$addReactiveSub: function $addReactiveSub(key, options) {
var _this3 = this;

var handle = void 0,
unwatch = void 0;
var subscribe = function subscribe(params) {
handle = _this3.$subscribe.apply(_this3, [key].concat(toConsumableArray(params)));
};

if (typeof options === 'function') {
if (isServer) {
subscribe(options.bind(this)());
} else {
unwatch = this.$watch(options, function (params) {
subscribe(params);
}, {
immediate: true
});
}
} else {
subscribe(options);
}

return function () {
if (unwatch) unwatch();
if (handle) _this3.$stopHandle(handle);
};
},
$addMeteorData: function $addMeteorData(key, func) {
var _this4 = this;

Expand All @@ -1908,12 +1921,7 @@ var index = {

// Function run
var setResult = function setResult(result) {
if (result && typeof result.fetch === 'function') {
result = result.fetch();
}
if (Vue.config.meteor.freeze) {
result = Object.freeze(result);
}
result = getResult(result);
set$1(_this4.$data.$meteor.data, key, result);
};

Expand Down Expand Up @@ -1941,6 +1949,42 @@ var index = {
unwatch();
unautorun();
};
},
$addComputed: function $addComputed(key, watcher) {
var _this5 = this;

var computation = void 0,
autorunMethod = void 0;
var autorun = function autorun(cb) {
if (!computation) {
// Update from Meteor
var dirty = false;
computation = autorunMethod(function (computation) {
dirty = true;
watcher.value = getResult(cb.call(_this5));
watcher.deps.forEach(function (dep) {
return dep.notify();
});
dirty = false;
});
// Update from Vue (override)
watcher.update = function () {
if (!dirty) {
computation.invalidate();
}
};
}
return watcher.value;
};
// Override getter to expose $autorun
var func = watcher.getter;
watcher.getter = function () {
autorunMethod = _this5.$autorun;
_this5.$autorun = autorun;
var result = func.call(_this5, _this5);
_this5.$autorun = autorunMethod;
return result;
};
}
}
}));
Expand Down
Loading

0 comments on commit 64da0c1

Please # to comment.