forked from processing-js/processing-js
-
Notifications
You must be signed in to change notification settings - Fork 10
/
processing-inspector.js
executable file
·157 lines (142 loc) · 4.77 KB
/
processing-inspector.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
/*
I N S P E C T O R F O R P R O C E S S I N G . J S
Part of the Processing.js project
License : MIT
Web Site : http://processingjs.org
Github Repo. : http://github.com/jeresig/processing-js
Bug Tracking : http://processing-js.lighthouseapp.com
*/
if (typeof ProcessingInspector === "undefined") {
ProcessingInspector = (function() {
var releaseFunctor = "__release__",
monitorCount = "__monitor_count_",
/**
* To prevent magic strings, we
*/
getMonitoredAttrName = function(attr) {
return "__monitored+"+attr+"__";
},
/**
* Determine whether this object owns or inherits
* the indicated attribute property. If it owns
* it, its property descriptor with have a "value"
* property that contains the actual value.
*/
hasValueProperty = function(object, attr, forRelease) {
if (forRelease) {
attr = getMonitoredAttrName(attr);
}
var propDesc = Object.getOwnPropertyDescriptor(object, attr);
if (!propDesc) {
return false;
}
return "value" in Object.getOwnPropertyDescriptor(object, attr);
},
/**
* Get the top-level object that actually holds
* the attribute we're interested in.
*/
getOwner = function(object, attr, forRelease) {
if (!(attr in object)) {
// shortcut: unsupported property
return false;
}
if (hasValueProperty(object, attr, forRelease)) {
// shortcut: property owned by this object
return object;
}
if (object.$super) {
// normal lookup: find owner and return
while (!hasValueProperty(object, attr, forRelease)) {
object = object.$super;
}
return object;
}
// reasonably, we can't get here, but you never know.
return false;
},
/**
* Stop monitoring an object's property.
*/
releaseFunction = function(attr) {
// remove getter/setter again
var monitoredAttr = getMonitoredAttrName(attr);
delete(this[attr]);
var props = {
configurable: true,
enumerable: true,
writable: true,
value: this[monitoredAttr] };
Object.defineProperty(this, attr, props);
delete(this[monitoredAttr]);
};
/**
* We return an object with a monitor() and release() function.
*/
return {
/**
* Start monitoring a specific property on a Pjs object for changes.
*
* @param object The object to monitor
* @param attr the property to monitor changes for
* @param callback a callback function with signature (obj,attr,val) that gets called on changes
*/
monitor: function(object, attr, callback) {
// can we even monitor this object's attribute?
object = getOwner(object, attr, false);
if (object === false) {
return false;
}
// we can. 1) kill off property,
var monitoredAttr = getMonitoredAttrName(attr);
object[monitoredAttr] = object[attr];
delete(object[attr]);
// 2) Lift up this property using a closure,
props = (function(attr, monitorAttr) {
props = {
get : function() { return this[monitoredAttr]; },
set : function(v) {
if (v != this[monitoredAttr]) {
this[monitoredAttr] = v;
callback(object, attr, v);
}
},
configurable : true,
enumerable : true };
return props;
}(attr, monitoredAttr));
Object.defineProperty(object, attr, props);
// 3) (set and) increment the monitored value count,
if (!hasValueProperty(object, monitorCount, false)) {
object[monitorCount] = 0;
}
object[monitorCount]++;
// 4) (re)bind the releasing function.
object[releaseFunctor] = releaseFunction;
},
/**
* We should be able to release monitored objects
* at will, provided they can be released.
*
* @param object The object to monitor
* @param attr the property to monitor changes for
*/
release: function(object, attr) {
// restore the property as "Writable value"
object = getOwner(object, attr, true);
if (object[releaseFunctor]) {
object[releaseFunctor](attr);
object[monitorCount]--;
}
// clean up if this was the last monitored property
if (object[monitorCount] === 0) {
delete(object[releaseFunctor]);
delete(object[monitorCount]);
}
}
};
}());
}
else if (window && window.console && window.console.log) {
window.console.log("ERROR: ProcessingInspector already exists.");
}