Skip to content

Commit 911cfed

Browse files
committed
Dropped debugging handling a layer deeper to catch all teardown/setup methods.
1 parent 510d7c6 commit 911cfed

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

recycle.js

+29-22
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const recycleProp = {
1313
let containers = null; // link-list of cached, unused containers for caches.
1414

1515
export default {
16-
add: function (ClassObject, name, setUp, recycle, mixinMethods, debug) {
16+
add: function (ClassObject, name, setUp, tearDown, mixinMethods, debug) {
1717
var isArray = (ClassObject === Array),
1818
cache = null;
1919

@@ -112,9 +112,9 @@ export default {
112112
}
113113

114114
// Handle object release
115-
if (recycle) {
115+
if (tearDown) {
116116
cache.recycle = function (item, ...args) {
117-
recycle.apply(item, ...args);
117+
tearDown.apply(item, ...args);
118118
this.push(item);
119119
};
120120
} else if (isArray) {
@@ -135,30 +135,37 @@ export default {
135135
cache.recycle = cache.push;
136136
}
137137

138+
// Add debug wrapper if needed
139+
if (debug) {
140+
cache.setUp = function (cacheSetUp, ...args) {
141+
var newObject = cacheSetUp(...args);
142+
143+
if (typeof newObject.recycled === 'undefined') {
144+
Object.defineProperty(newObject, 'recycled', recycleProp);
145+
} else {
146+
newObject.recycled = false;
147+
}
148+
149+
return newObject;
150+
}.bind(cache, cache.setUp.bind(cache));
151+
152+
cache.recycle = function (recycle, instance, ...args) {
153+
if (instance.recycled) {
154+
console.warn('WHOA! I have already been recycled!', instance);
155+
} else {
156+
instance.recycled = true;
157+
recycle(instance, ...args);
158+
}
159+
}.bind(cache, cache.recycle.bind(cache));
160+
}
161+
138162
if (mixinMethods) {
139163
Object.defineProperties(ClassObject, {
140164
setUp: {
141-
value: (debug ? function () {
142-
var newObject = cache.setUp.apply(cache, arguments);
143-
144-
if (typeof newObject.recycled === 'undefined') {
145-
Object.defineProperty(newObject, 'recycled', recycleProp);
146-
} else {
147-
newObject.recycled = false;
148-
}
149-
150-
return newObject;
151-
} : cache.setUp.bind(cache))
165+
value: cache.setUp.bind(cache)
152166
},
153167
recycle: {
154-
value: (debug ? function (instance, ...args) {
155-
if (instance.recycled) {
156-
console.warn('WHOA! I have already been recycled!', instance);
157-
} else {
158-
instance.recycled = true;
159-
cache.recycle(instance, ...args);
160-
}
161-
} : cache.recycle.bind(cache))
168+
value: cache.recycle.bind(cache)
162169
}
163170
});
164171
Object.defineProperty(ClassObject.prototype, 'recycle', {

0 commit comments

Comments
 (0)