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

Cursor methods #36

Merged
merged 21 commits into from
Aug 5, 2014
Merged
Show file tree
Hide file tree
Changes from 17 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
7 changes: 6 additions & 1 deletion assets/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ extendZone({
// but actions may happen even after the zone completed
// and we are not interested about those
if(this._events) {
event.time = this.getTime();
this._events.push(event);
}
},

setInfo: function(key, value) {
console.log(key, value);
if(this._info) {
value.time = this.getTime();
this._info[key] = value;
}
},
Expand Down Expand Up @@ -148,6 +149,10 @@ extendZone({
boundZone.dequeueTask(func);
return result;
}, false, ownerInfo, validateArgs);
},

getTime: function () {
return Date.now();
}
});

Expand Down
218 changes: 172 additions & 46 deletions assets/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ function hijackConnection(original, type) {
if(!isFromCall && args.length) {
var callback = args[args.length - 1];
if(typeof callback === 'function') {
var ownerInfo = {
type: type,
name: args[0],
args: args.slice(1, args.length - 1)
};
args[args.length - 1] = zone.bind(callback, false, ownerInfo, pickAllArgs);
var methodName = args[0];
var methodArgs = args.slice(1, args.length - 1);
var ownerInfo = {type: type, name: methodName, args: methodArgs};
args[args.length - 1] = function (argument) {
var args = Array.prototype.slice.call(arguments);
var zoneInfo = {type: type, name: methodName, args: methodArgs};
zone.setInfo(type, zoneInfo);
return callback.apply(this, args);
}
args[args.length - 1] = zone.bind(args[args.length - 1], false, ownerInfo, pickAllArgs);
}
}

Expand All @@ -34,22 +38,27 @@ function hijackSubscribe(originalFunction, type) {
var args = Array.prototype.slice.call(arguments);
if(args.length) {
var callback = args[args.length - 1];
var subName = args[0];
var subArgs = args.slice(1, args.length - 1);
if(typeof callback === 'function') {
var ownerInfo = {
type: type,
name: args[0],
args: args.slice(1, args.length - 1)
};
args[args.length - 1] = zone.bind(callback, false, ownerInfo, pickAllArgs);
var ownerInfo = {type: type, name: subName, args: subArgs};
args[args.length - 1] = function (argument) {
var args = Array.prototype.slice.call(arguments);
var zoneInfo = {type: type, name: subName, args: subArgs};
zone.setInfo(type, zoneInfo);
return callback.apply(this, args);
}
args[args.length - 1] = zone.bind(args[args.length - 1], false, ownerInfo, pickAllArgs);
} else if(callback) {
['onReady', 'onError'].forEach(function (funName) {
var ownerInfo = {
type: type,
name: args[0],
args: args.slice(1, args.length - 1),
callbackType: funName
};
var ownerInfo = {type: type, name: subName, args: subArgs, callbackType: funName};
if(typeof callback[funName] === "function") {
callback[funName] = function (argument) {
var args = Array.prototype.slice.call(arguments);
var zoneInfo = {type: type, name: subName, args: subArgs, callbackType: funName};
zone.setInfo(type, zoneInfo);
return callback.apply(this, args);
}
callback[funName] = zone.bind(callback[funName], false, ownerInfo, pickAllArgs);
}
})
Expand All @@ -71,6 +80,34 @@ function hijackCursor(Cursor) {
'removed', 'movedBefore'
]);

['fetch', 'forEach', 'map'].forEach(function (name) {
var original = Cursor[name];
Cursor[name] = function (callback, thisArg) {
var self = thisArg || this;
var args = Array.prototype.slice.call(arguments);
var type = 'MongoCursor.' + name;
var notFromForEach = Zone.notFromForEach.get();
if(!this._avoidZones
&& !notFromForEach
&& typeof callback === 'function') {
args[0] = function () {
var args = Array.prototype.slice.call(arguments);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have args right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First one was args for fetch, forEach, map (callback, thisArg) and the second one is for the callback (document, index, cursor).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah okay. I didn't see that. sorry.

var zoneInfo = {type: type, collection: self.collection.name};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set the incoming document as well.
Make sure to check the arguments size for info as well.

zone.setInfo(type, zoneInfo);
return callback.apply(this, args);
};
}

if(name !== 'forEach') {
return Zone.notFromForEach.withValue(true, function() {
return original.apply(self, args);
});
} else {
return original.apply(self, args);
}
}
});

function hijackFunction(type, callbacks) {
var original = Cursor[type];
Cursor[type] = function (options) {
Expand All @@ -79,15 +116,25 @@ function hijackCursor(Cursor) {
// if so, we don't need to track this request
var isFromObserve = Zone.fromObserve.get();

if(!isFromObserve && options) {
if(!this._avoidZones && !isFromObserve && options) {
callbacks.forEach(function (funName) {
var ownerInfo = {
type: 'MongoCursor.' + type,
callbackType: funName,
collection: self.collection.name
};

if(typeof options[funName] === 'function') {
var callback = options[funName];
if(typeof callback === 'function') {
var ownerInfo = {
type: 'MongoCursor.' + type,
callbackType: funName,
collection: self.collection.name
};
options[funName] = function () {
var args = Array.prototype.slice.call(arguments);
var zoneInfo = {
type: 'MongoCursor.' + type,
callbackType: funName,
collection: self.collection.name
};
zone.setInfo(type, zoneInfo);
return callback.apply(this, args);
}
options[funName] = zone.bind(options[funName], false, ownerInfo, pickAllArgs);
}
});
Expand All @@ -106,6 +153,7 @@ function hijackCursor(Cursor) {
}

function hijackComponentEvents(original) {
var type = 'Template.event';
return function (dict) {
var self = this;
var name = this.__templateName || this.kind.split('_')[1];
Expand All @@ -118,26 +166,17 @@ function hijackComponentEvents(original) {
function prepareHandler(handler, target) {
return function () {
var args = Array.prototype.slice.call(arguments);
zone.owner = {
type: 'Template.event',
event: target,
template: name
};
var ownerInfo = {type: type, event: target, template: name};
zone.owner = ownerInfo;
var zoneInfo = {type: type, event: target, template: name};
zone.setInfo(type, zoneInfo);
handler.apply(this, args);
};
}

}
}

function hijackTemplateRendered(original, name) {
return function () {
var args = Array.prototype.slice.call(arguments);
zone.addEvent({type: 'Template.rendered', template: name});
return original.apply(this, args);
}
}

function hijackDepsFlush(original, type) {
return function () {
var args = Array.prototype.slice.call(arguments);
Expand All @@ -156,6 +195,70 @@ function hijackSessionSet(original, type) {
}
}

var TemplateCoreFunctions = ['prototype', '__makeView', '__render'];

function hijackTemplateHelpers(template, templateName) {
_.each(template, function (hookFn, name) {
template[name] = hijackHelper(hookFn, name, templateName);
});
}

function hijackNewTemplateHelpers(original, templateName) {
return function (dict) {
dict && _.each(dict, function (hookFn, name) {
dict[name] = hijackHelper(hookFn, name, templateName);
});

var args = Array.prototype.slice.call(arguments);
return original.apply(this, args);
}
}

function hijackHelper(hookFn, name, templateName) {
if(hookFn
&& typeof hookFn === 'function'
&& _.indexOf(TemplateCoreFunctions, name) === -1) {
// Assuming the value is a template helper
return function () {
var args = Array.prototype.slice.call(arguments);
zone.setInfo('Template.helper', {name: name, template: templateName});
var result = hookFn.apply(this, args);
if(result && typeof result.observe === 'function') {
result._avoidZones = true;
}
return result;
}
} else {
return hookFn;
}
}

function hijackGlobalHelpers(helpers) {
var _ = Package.underscore._;
_(helpers || {}).each(function (helperFn, name) {
helpers[name] = hijackGlobalHelper(helperFn, name)
});
}

function hijackGlobalHelper(helperFn, name) {
var _ = Package.underscore._;
if(helperFn
&& typeof helperFn === 'function'
&& _.indexOf(TemplateCoreFunctions, name) === -1) {
return function () {
var args = Array.prototype.slice.call(arguments);
zone.setInfo('Global.helper', {name: name});
var result = helperFn.apply(this, args);
if(result && typeof result.observe === 'function') {
result._avoidZones = true;
}
return result;
}
} else {
return helperFn;
}
}

//--------------------------------------------------------------------------\\

var routerEvents = [
Expand All @@ -178,7 +281,12 @@ function hijackRouterConfigure(original, type) {
name: this.route.name,
path: this.path
});
hookFn.apply(this, args);
zone.setInfo('irHook', {
name: this.route.name,
hook: hookName,
path: this.path
});
return hookFn.apply(this, args);
}
}
});
Expand All @@ -189,6 +297,10 @@ function hijackRouterConfigure(original, type) {
function hijackRouterGlobalHooks(Router, type) {
routerEvents.forEach(function (hookName) {
var hookFn = Router[hookName];
/**
* Example
* Router.onBeforeAction( handler-function, options )
*/
Router[hookName] = function (hook, options) {
var args = Array.prototype.slice.call(arguments);
var hook = args[0];
Expand All @@ -202,10 +314,15 @@ function hijackRouterGlobalHooks(Router, type) {
name: this.route.name,
path: this.path
});
hook.apply(this, args);
zone.setInfo('irHook', {
name: this.route.name,
hook: hookName,
path: this.path
});
return hook.apply(this, args);
}
}
hookFn.apply(this, args);
return hookFn.apply(this, args);
}
});

Expand All @@ -228,12 +345,17 @@ function hijackRouterOptions(original, type) {
name: this.route.name,
path: this.path
});
hookFn.apply(this, args);
zone.setInfo('irHook', {
name: this.route.name,
hook: hookName,
path: this.path
});
return hookFn.apply(this, args);
}
}
});

original.apply(this, args);
return original.apply(this, args);
}
}

Expand All @@ -251,11 +373,15 @@ function hijackRouteController(original, type) {
name: this.route.name,
path: this.path
});
hookFn.apply(this, args);
zone.setInfo('irHook', {
name: this.route.name,
hook: hookName,
path: this.path
});
return hookFn.apply(this, args);
}
}
});
zone.addEvent({type: type});
return original.apply(this, args);
}
}
Expand Down
Loading