Skip to content

Commit 802356c

Browse files
author
Kamil Kisiela
committed
feat(Core): onStart callback
1 parent f10bfde commit 802356c

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/modules/core.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ angular.module(name, [
4545
fn = this.$bindToContext($Mixer.caller, fn || angular.noop);
4646
cb = cb ? this.$bindToContext($Mixer.caller, cb) : angular.noop;
4747

48+
// Additional callbacks specific for this library
49+
// onStart - right after Meteor.subscribe()
50+
const hooks = {
51+
onStart: angular.noop
52+
};
53+
4854
if (!_.isString(subName)) {
4955
throw Error('argument 1 must be a string');
5056
}
@@ -55,6 +61,16 @@ angular.module(name, [
5561
throw Error('argument 3 must be a function or an object');
5662
}
5763

64+
if (_.isObject(cb)) {
65+
for (const hook in hooks) {
66+
if (hooks.hasOwnProperty(hook) && cb[hook]) {
67+
// Don't use any of additional callbacks in Meteor.subscribe
68+
hooks[hook] = cb[hook];
69+
delete cb[hook];
70+
}
71+
}
72+
}
73+
5874
const result = {};
5975

6076
const computation = this.autorun(() => {
@@ -66,6 +82,9 @@ angular.module(name, [
6682
}
6783

6884
const subscription = Meteor.subscribe(subName, ...args, cb);
85+
86+
hooks.onStart();
87+
6988
result.ready = subscription.ready.bind(subscription);
7089
result.subscriptionId = subscription.subscriptionId;
7190
});

tests/integration/core.spec.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ describe('angular-meteor.core', function() {
180180
it('should call subscription callbacks object using view model as context', function(done) {
181181
var vm = scope.viewModel({});
182182

183-
var next = _.after(2, done);
183+
var next = _.after(3, done);
184184

185185
spyOn(Meteor, 'subscribe').and.callFake(function(name, cbs) {
186186
cbs.onReady();
@@ -197,6 +197,26 @@ describe('angular-meteor.core', function() {
197197
onReady: function() {
198198
expect(this).toEqual(vm);
199199
next();
200+
},
201+
202+
onStart: function() {
203+
expect(this).toEqual(vm);
204+
next();
205+
}
206+
});
207+
});
208+
209+
it('should call onStart callback after Meteor.subscribe has been called', function(done) {
210+
var vm = scope.viewModel({});
211+
212+
spyOn(Meteor, 'subscribe').and.callFake(function() {
213+
return { ready: angular.noop };
214+
});
215+
216+
vm.subscribe('test', angular.noop, {
217+
onStart: function() {
218+
expect(Meteor.subscribe).toHaveBeenCalled();
219+
done();
200220
}
201221
});
202222
});

0 commit comments

Comments
 (0)