-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-event-listener.js
More file actions
137 lines (135 loc) · 4.53 KB
/
angular-event-listener.js
File metadata and controls
137 lines (135 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Include this module at the root of your app and require the factories
*
* Will listen for events and do its best to batch them into actions. Results will be
* console tabled. To use, simple start clicking around an app
*/
angular.module('EventListenerModule', [])
.factory('eventListener', ['$timeout', '$rootScope', function ($timeout, $rootScope) {
(function() {
var _$on = $rootScope.$on;
var _$broadcast = $rootScope.$broadcast;
var _$emit = $rootScope.$emit;
$rootScope.$on = function(name, listener) {
saveEvent(this.$id, '$on:register', name);
return _$on.apply(this, arguments);
};
$rootScope.$broadcast = function(name, args) {
saveEvent(this.$id, '$broadcast', name, args);
logBroadcastListeners.call(this, name, args);
return _$broadcast.apply(this, arguments);
};
$rootScope.$emit = function(name, args) {
saveEvent(this.$id, '$emit', name, args);
logEmitListeners.call(this, name, args);
return _$emit.apply(this, arguments);
};
})();
var lastEventTime = null;
var lastActionTime = null;
var lastAction = 0;
var newActionDifference = 2000;
var nextAction = null;
var actionEvents = [];
var logBroadcastListeners = function (eventName, eventArgs) {
if (!isReportableEvent(eventName)) { return; }
// Go down
// Lifted from angular's rootScope.js $broadcast
var target = this,
current = target,
next = target;
if (!target.$$listenerCount[eventName]) return;
var listeners, i, length;
while ((current = next)) {
listeners = current.$$listeners[eventName] || [];
for (i = 0, length = listeners.length; i < length; i++) {
// if listeners were deregistered, defragment the array
if (!listeners[i]) {
listeners.splice(i, 1);
i--;
length--;
continue;
}
saveEvent(current.$id, '$on:$broadcast', eventName, eventArgs);
}
// Insanity Warning: scope depth-first traversal
// yes, this code is a bit crazy, but it works and we have tests to prove it!
// this piece should be kept in sync with the traversal in $digest
// (though it differs due to having the extra check for $$listenerCount)
if (!(next = ((current.$$listenerCount[eventName] && current.$$childHead) ||
(current !== target && current.$$nextSibling)))) {
while (current !== target && !(next = current.$$nextSibling)) {
current = current.$parent;
}
}
}
};
var logEmitListeners = function (eventName, eventArgs) {
var empty = [],
namedListeners,
scope = this;
if (!isReportableEvent(eventName)) { return; }
// Go up
// Lifted from angular's rootScope.js $emit
do {
namedListeners = scope.$$listeners[eventName] || empty;
for (i = 0, length = namedListeners.length; i < length; i++) {
// if listeners were deregistered, defragment the array
if (!namedListeners[i]) {
namedListeners.splice(i, 1);
i--;
length--;
continue;
}
saveEvent(scope.$id, '$on:$emit', eventName, eventArgs);
}
//traverse upwards
scope = scope.$parent;
} while (scope);
};
var isReportableEvent = function (eventName) {
return eventName.indexOf('$') !== 0;
};
var saveEvent = function (scopeId, eventType, eventName, eventArgs) {
var dir = '';
if (!isReportableEvent(eventName)) return;
lastEventTime = new Date();
if (eventType === '$on:$emit' || eventType === '$on:$broadcast') {
dir = '<';
} else if (eventType === '$emit' || eventType === '$broadcast') {
dir = '-->';
}
actionEvents.push({
'scopeId': scopeId,
'dir': dir,
'type': eventType,
'name': eventName,
'args': eventArgs
});
};
var waitForNewAction = function () {
var lastActionDifference;
if (nextAction) {
console.log(nextAction);
nextAction = null;
}
if (lastEventTime) {
lastActionDifference = (new Date()) - lastEventTime;
if (lastActionDifference > newActionDifference && lastEventTime > lastActionTime) {
lastAction = lastAction +1;
console.table(actionEvents);
startAction(lastAction);
return;
}
}
$timeout(waitForNewAction, 10);
};
var startAction = function(actionName) {
nextAction = "ACTION:", actionName.toString();
actionEvents = [];
lastActionTime = new Date();
waitForNewAction();
};
startAction('pageLoad');
return {};
}]);