-
Notifications
You must be signed in to change notification settings - Fork 11
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
Cursor methods #36
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
38ebfad
Remove console.log
thani-sh 198dd16
Update existing tests to include `info`
thani-sh 569f338
Remove console.log
thani-sh 0eaa5a1
Update existing code to set info
thani-sh 4bc062d
Add template helpers info
thani-sh 9752344
Disable adding Meteor.call events
thani-sh f7fc4ab
Refactor utils.js
thani-sh d241e75
Fix tests for updated `info` format
thani-sh 5d1e502
Add timestamp to zone info
thani-sh da90008
Add timestamp for events
thani-sh 48b4990
Reuse a function to hijack helpers
thani-sh 4ff4111
Use `_avoidZones` instead of `_doNotTrack`
thani-sh 649027a
Fix tests
thani-sh 11a051e
Fix typo
thani-sh b62dd81
Remove console.log
thani-sh 5dc708c
Refactor and hijack global template helpers
thani-sh 55784fb
Set info with cursor methods {fetch, forEach, map}
thani-sh 976228f
Remove unnecessary events from zone.events array
thani-sh 59d7608
Add document info to info and create a zone for cursor methods
thani-sh f45a569
Add time field for ownerInfo
thani-sh d662d33
Add real ownerInfo for Cursor methods
thani-sh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
} | ||
|
||
|
@@ -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); | ||
} | ||
}) | ||
|
@@ -71,6 +80,38 @@ 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 (doc, index) { | ||
var args = Array.prototype.slice.call(arguments); | ||
var ownerInfo = {type: type, collection: self.collection.name}; | ||
var zoneInfo = {type: type, collection: self.collection.name}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. set the incoming document as well. |
||
zoneInfo.document = doc; | ||
zoneInfo.index = index; | ||
zone.setInfo(type, zoneInfo); | ||
callback = zone.bind(callback, false, ownerInfo. pickAllArgs); | ||
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) { | ||
|
@@ -79,15 +120,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); | ||
} | ||
}); | ||
|
@@ -106,6 +157,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]; | ||
|
@@ -118,26 +170,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); | ||
|
@@ -156,6 +199,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 = [ | ||
|
@@ -178,7 +285,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); | ||
} | ||
} | ||
}); | ||
|
@@ -189,6 +301,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]; | ||
|
@@ -202,10 +318,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); | ||
} | ||
}); | ||
|
||
|
@@ -228,12 +349,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); | ||
} | ||
} | ||
|
||
|
@@ -251,11 +377,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); | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).There was a problem hiding this comment.
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.