Skip to content

Commit 211fa83

Browse files
committed
(angular-server): digest cycle in server
1 parent 750730f commit 211fa83

File tree

7 files changed

+90
-21
lines changed

7 files changed

+90
-21
lines changed

.versions

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ [email protected]
1414
1515
1616
17-
local-test:urigo:[email protected].4
17+
local-test:urigo:[email protected].5-alpha.1
1818
1919
2020
@@ -27,4 +27,4 @@ [email protected]
2727
2828
2929
30-
30+
urigo:[email protected].5-alpha.1

angular-meteor-client.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ Meteor.subscribe('serverInstances', function () {
132132
}]);
133133
}
134134
else {
135-
$provide.factory(instance.name, ['$q', function ($q) {
135+
$provide.factory(instance.name, ['$q', '$rootScope', function ($q, $rootScope) {
136136
var serviceInstance = {};
137137
angular.forEach(instance.funcDefs, function (funcDef) {
138138
serviceInstance[funcDef] = function () {
@@ -157,6 +157,17 @@ Meteor.subscribe('serverInstances', function () {
157157
};
158158
});
159159

160+
angular.extend(serviceInstance, instance.properties);
161+
162+
serverInstances.find({}).observe({
163+
changed : function (newDpc) {
164+
angular.extend(serviceInstance, newDpc.properties);
165+
if (!$rootScope.$$phase) {
166+
$rootScope.$apply();
167+
}
168+
}
169+
});
170+
160171
return serviceInstance;
161172
}]);
162173
}

angular-meteor-common.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ angular.module('angular-meteor')
2424

2525
// XXX - think if better to apply with meteor's this or the service
2626
if (Meteor.isClient && angular.isDefined(service.$$originalInstance)) {
27-
return service.$$originalInstance[prop].apply(this, args);
27+
return service.$$originalInstance[prop].apply(service, args);
2828
}
2929
else {
30-
return service[prop].apply(this, args);
30+
return service[prop].apply(service, args);
3131
}
3232
}
3333
});

angular-meteor-server.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*/
44
var serverInstances = new Meteor.Collection(null);
55

6-
angular.module('angular-meteor', [])
7-
.run(['ServerAPI', '$injector', function(ServerAPI, $injector) {
6+
angular.module('angular-meteor', ['angular-meteor.meteor-collection'])
7+
.run(['ServerAPI', '$injector', '$rootScope', function(ServerAPI, $injector, $rootScope) {
88
angular.forEach(ServerAPI.getServerAPIS(), function(api) {
99
var instance = $injector.get(api);
1010
var funcDefs = [];
@@ -13,7 +13,15 @@ angular.module('angular-meteor', [])
1313
funcDefs.push(key);
1414
}
1515
}
16-
serverInstances.insert({ name : api, funcDefs : funcDefs });
16+
var instanceId = serverInstances.insert({ name : api, funcDefs : funcDefs, properties : [] });
17+
18+
$rootScope.$watch(function() {
19+
return instance;
20+
}, function() {
21+
console.log('updating', instanceId, instance);
22+
serverInstances.update({ _id : instanceId }, { $set : { properties : instance } });
23+
}, true);
24+
1725
});
1826
}])
1927
.run(function() {
@@ -24,6 +32,7 @@ angular.module('angular-meteor', [])
2432
self.added('serverInstances', id, fields);
2533
},
2634
changed: function (id, fields) {
35+
console.log('changed', id, fields);
2736
self.changed('serverInstances', id, fields);
2837
},
2938
removed: function (id) {
@@ -37,11 +46,36 @@ angular.module('angular-meteor', [])
3746
handle.stop();
3847
})
3948
});
49+
})
50+
.run(function($rootScope, ServerAPI) {
51+
//var origRun = Meteor._SynchronousQueue.prototype._run;
52+
//
53+
//console.log('replacing run');
54+
//Meteor._SynchronousQueue.prototype._run = function() {
55+
// console.log('running from queue');
56+
// var result = origRun.apply(this, arguments);
57+
// $rootScope.$apply();
58+
// return result;
59+
//};
60+
var Fibers = Npm.require('fibers');
61+
var origRun = Fibers.prototype.run;
62+
63+
Fibers.prototype.run = function() {
64+
var result = origRun.apply(this, arguments);
65+
if (!$rootScope.$$phase) {
66+
var startTime = new Date();
67+
$rootScope.$apply();
68+
console.log ('digest time:', new Date() - startTime);
69+
}
70+
return result;
71+
};
4072
});
4173

4274
var origBootstrap = angular.bootstrap;
4375
angular.bootstrap = function(modules, config) {
44-
return origBootstrap(document, modules, config);
76+
Meteor.startup(function() {
77+
origBootstrap(document, modules, config);
78+
});
4579
};
4680

4781

modules/angular-meteor-meteorCollection.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,23 @@ angularMeteorCollections.factory('$meteorCollection', ['AngularMeteorCollection'
277277
}
278278
}
279279

280-
/**
281-
* Fetches the latest data from Meteor and update the data variable.
282-
*/
283-
Tracker.autorun(function () {
284-
// When the reactive func gets recomputated we need to stop any previous
285-
// observeChanges
286-
Tracker.onInvalidate(function () {
287-
ngCollection.stop();
280+
if (Meteor.isClient) {
281+
/**
282+
* Fetches the latest data from Meteor and update the data variable.
283+
*/
284+
Tracker.autorun(function () {
285+
// When the reactive func gets recomputated we need to stop any previous
286+
// observeChanges
287+
Tracker.onInvalidate(function () {
288+
ngCollection.stop();
289+
});
290+
ngCollection.updateCursor(reactiveFunc());
291+
setAutoBind();
288292
});
293+
}
294+
else {
289295
ngCollection.updateCursor(reactiveFunc());
290-
setAutoBind();
291-
});
296+
}
292297

293298
return ngCollection;
294299
}

package.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ Package.on_use(function (api) {
5757
'angular-meteor-client.js'
5858
], 'client');
5959

60-
api.add_files('angular-meteor-server.js', 'server');
60+
api.add_files(['modules/angular-meteor-meteorCollection.js',
61+
'modules/angular-meteor-subscribe.js',
62+
'modules/angular-meteor-utils.js',
63+
'lib/diff-array.js',
64+
'angular-meteor-server.js'], 'server');
6165

6266
api.add_files('angular-meteor-common.js');
6367
});

versions.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
],
2323
[
2424
"dburles:mongo-collection-instances",
25-
"0.3.1"
25+
"0.3.3"
2626
],
2727
[
2828
"ddp",
@@ -48,6 +48,10 @@
4848
"json",
4949
"1.0.1"
5050
],
51+
[
52+
"lai:collection-extensions",
53+
"0.1.3"
54+
],
5155
[
5256
"logging",
5357
"1.0.5"
@@ -92,6 +96,17 @@
9296
"pluginDependencies": [
9397
[
9498
"compileAngularTemplates",
99+
{
100+
"deps": "1.0.5",
101+
"tracker": "1.0.3",
102+
"html-tools": "1.0.2",
103+
"underscore": "1.0.1",
104+
"meteor": "1.1.3",
105+
"htmljs": "1.0.2"
106+
}
107+
],
108+
[
109+
"ngAnnotate",
95110
{}
96111
]
97112
],

0 commit comments

Comments
 (0)