forked from sproutcore/sproutcore
-
Notifications
You must be signed in to change notification settings - Fork 2
/
invoke_once_last_debugging.js
259 lines (219 loc) · 10.2 KB
/
invoke_once_last_debugging.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// ==========================================================================
// Project: SproutCore - JavaScript Application Framework
// Copyright: ©2006-2011 Strobe Inc. and contributors.
// Portions ©2008-2010 Apple Inc. All rights reserved.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
// When in debug mode, it’s useful for our observer sets (which are used by
// invokeOnce and invokeLast) to record which code scheduled the
// invokeOnce/invokeLast targets/methods.
var SC = SC || {} ;
// Declaring the variable will make it easier for people who want to enter it
// inside consoles that auto-complete.
if (!SC.LOG_RUNLOOP_INVOCATIONS) SC.LOG_RUNLOOP_INVOCATIONS = false;
SC.addInvokeOnceLastDebuggingInfo = function() {
return;
SC.ObserverSet.add = function(target, method, context, originatingTarget, originatingMethod, originatingStack) {
var targetGuid = (target) ? SC.guidFor(target) : "__this__";
// get the set of methods
var methods = this[targetGuid] ;
if (!methods) {
methods = this[targetGuid] = SC.CoreSet.create() ;
methods.target = target ;
methods.isTargetSet = YES ; // used for getMembers().
this.targets++ ;
}
methods.add(method) ;
// context is really useful sometimes but not used that often so this
// implementation is intentionally lazy.
if (context !== undefined) {
var contexts = methods.contexts || (methods.contexts = {}) ;
contexts[SC.guidFor(method)] = context ;
}
// THIS IS THE PORTION THAT DIFFERS FROM THE STANDARD IMPLEMENTATION
// Recording the calling object/function can be a useful debugging tool.
// Since multiple object/functions can schedule the same target/method,
// this value could either be a single value or an array. (We won't
// always use an array because that adds a certain debugging burden to the
// "only one scheduled it" case.)
if (originatingMethod !== undefined) {
var originatingTargets = methods.originatingTargets;
var originatingMethods = methods.originatingMethods;
var originatingStacks = methods.originatingStacks;
if (!originatingTargets) originatingTargets = methods.originatingTargets = {};
if (!originatingMethods) originatingMethods = methods.originatingMethods = {};
if (!originatingStacks) originatingStacks = methods.originatingStacks = {};
var key = SC.guidFor(method);
var existingMethod = originatingMethods[key];
if (existingMethod && SC.typeOf(existingMethod) !== SC.T_ARRAY) {
// We previously had one entry and now we have two. We need to
// convert to an array!
var existingTarget = originatingTargets[key];
var existingStack = originatingStacks[key];
originatingTargets[key] = [existingTarget, originatingTarget];
originatingMethods[key] = [existingMethod, originatingMethod];
originatingStacks[key] = [existingStack, originatingStack];
}
else {
// We didn't previously have a value? Then no need to use an
// enclosing array.
originatingTargets[key] = originatingTarget;
originatingMethods[key] = originatingMethod;
originatingStacks[key] = originatingStack;
}
}
};
SC.ObserverSet.invokeMethods = function() {
// iterate through the set, look for sets.
for(var key in this) {
if (!this.hasOwnProperty(key)) continue ;
var value = this[key] ;
if (value && value.isTargetSet) {
var idx = value.length;
var target = value.target ;
// THIS IS THE PORTION THAT DIFFERS FROM THE STANDARD IMPLEMENTATION
var m, log = SC.LOG_RUNLOOP_INVOCATIONS;
while(--idx>=0) {
m = value[idx];
if (log) {
var mName = m.displayName || m;
var originatingKey = SC.guidFor(m),
originatingTargets = value.originatingTargets;
if (!originatingTargets) {
// If we didn't capture information for this invocation, just
// report what we can. (We assume we'll always have all three
// hashes or none.)
console.log("Invoking runloop-scheduled method %@ on %@, but we didn’t capture information about who scheduled it…".fmt(mName, target));
}
else {
originatingTargets = originatingTargets[originatingKey]; // Could be one target or an array of them
var originatingMethods = value.originatingMethods[originatingKey]; // ditto
var originatingStacks = value.originatingStacks[originatingKey]; // ditto
// Were there multiple originating target/method pairs that
// scheduled this target/method? If so, display them all nicely.
// Otherwise, optimize our output for only one.
if (originatingMethods && SC.typeOf(originatingMethods) === SC.T_ARRAY) {
console.log("Invoking runloop-scheduled method %@ on %@, which was scheduled by multiple target/method pairs:".fmt(mName, target));
var i, len,
originatingTarget,
originatingMethod,
originatingStack;
for (i = 0, len = originatingMethods.length; i < len; ++i) {
originatingTarget = originatingTargets[i];
originatingMethod = originatingMethods[i];
originatingMethod = originatingMethod.displayName || originatingMethod;
originatingStack = originatingStacks[i];
console.log("[%@] originated by target %@, method %@, stack:".fmt(i, originatingTarget, originatingMethod), originatingStack);
}
}
else {
var originatingMethodName = originatingMethods.displayName || originatingMethods;
console.log("Invoking runloop-scheduled method %@ on %@. Originated by target %@, method %@, stack: ".fmt(mName, target, originatingTargets, originatingMethodName), originatingStacks);
}
}
}
m.call(target);
}
// THIS IS THE PORTION THAT DIFFERS FROM THE STANDARD IMPLEMENTATION
}
}
};
SC.Object.prototype.invokeOnce = function(method) {
var originatingTarget = this ;
if (SC.LOG_RUNLOOP_INVOCATIONS) {
originatingStack = SC.getRecentStack();
originatingMethod = originatingStack[0];
}
else {
originatingStack = null;
originatingMethod = arguments.callee.caller;
} SC.RunLoop.currentRunLoop.invokeOnce(this, method, originatingTarget, originatingMethod, originatingStack) ;
return this ;
};
SC.Object.prototype.invokeLast = function(method) {
var originatingTarget = this ;
var originatingStack, originatingMethod;
if (SC.LOG_RUNLOOP_INVOCATIONS) {
originatingStack = SC.getRecentStack();
originatingMethod = originatingStack[0];
}
else {
originatingStack = null;
originatingMethod = arguments.callee.caller;
}
SC.RunLoop.currentRunLoop.invokeLast(this, method, originatingTarget, originatingMethod, originatingStack) ;
return this ;
};
SC.RunLoop.prototype.invokeOnce = function(target, method, originatingTarget, originatingMethod, originatingStack) {
// THIS IS THE PORTION THAT DIFFERS FROM THE STANDARD IMPLEMENTATION
// For debugging convenience, record the originating function if it was
// not provided for us.
if (!originatingTarget) originatingTarget = null; // More obvious when debugging
if (!originatingMethod) {
if (SC.LOG_RUNLOOP_INVOCATIONS) {
originatingStack = SC.getRecentStack();
originatingMethod = originatingStack[0];
}
else {
originatingStack = null;
originatingMethod = arguments.callee.caller;
}
}
// THIS IS THE PORTION THAT DIFFERS FROM THE STANDARD IMPLEMENTATION
// normalize
if (method === undefined) {
method = target; target = this ;
}
if (SC.typeOf(method) === SC.T_STRING) method = target[method];
if (!this._invokeQueue) this._invokeQueue = SC.ObserverSet.create();
this._invokeQueue.add(target, method, null, originatingTarget, originatingMethod, originatingStack); // differs from standard implementation
return this ;
};
SC.RunLoop.prototype.invokeLast = function(target, method, originatingTarget, originatingMethod, originatingStack) {
// THIS IS THE PORTION THAT DIFFERS FROM THE STANDARD IMPLEMENTATION
// For debugging convenience, record the originating function if it was
// not provided for us.
if (!originatingTarget) originatingTarget = null; // More obvious when debugging
if (!originatingMethod) {
if (SC.LOG_RUNLOOP_INVOCATIONS) {
originatingStack = SC.getRecentStack();
originatingMethod = originatingStack[0];
}
else {
originatingStack = null;
originatingMethod = arguments.callee.caller;
}
}
// THIS IS THE PORTION THAT DIFFERS FROM THE STANDARD IMPLEMENTATION
// normalize
if (method === undefined) {
method = target; target = this ;
}
if (SC.typeOf(method) === SC.T_STRING) method = target[method];
if (!this._invokeLastQueue) this._invokeLastQueue = SC.ObserverSet.create();
this._invokeLastQueue.add(target, method, null, originatingTarget, originatingMethod, originatingStack); // differs from standard implementation
return this ;
};
// Will return the recent stack as a hash with numerical keys, for nice
// output in some browser's debuggers. The “recent” stack is capped at 6
// entries.
SC.getRecentStack = function() {
var currentFunction = arguments.callee.caller,
i = 0,
stack = {},
first = YES,
functionName;
while (currentFunction && i < 6) {
// Skip ourselves!
if (first) {
first = NO;
}
else {
functionName = currentFunction.displayName || currentFunction.toString();
stack[i++] = functionName;
}
currentFunction = currentFunction.caller;
}
return stack;
};
};