@@ -13,7 +13,7 @@ const recycleProp = {
13
13
let containers = null ; // link-list of cached, unused containers for caches.
14
14
15
15
export default {
16
- add : function ( ClassObject , name , setUp , recycle , mixinMethods , debug ) {
16
+ add : function ( ClassObject , name , setUp , tearDown , mixinMethods , debug ) {
17
17
var isArray = ( ClassObject === Array ) ,
18
18
cache = null ;
19
19
@@ -112,9 +112,9 @@ export default {
112
112
}
113
113
114
114
// Handle object release
115
- if ( recycle ) {
115
+ if ( tearDown ) {
116
116
cache . recycle = function ( item , ...args ) {
117
- recycle . apply ( item , ...args ) ;
117
+ tearDown . apply ( item , ...args ) ;
118
118
this . push ( item ) ;
119
119
} ;
120
120
} else if ( isArray ) {
@@ -135,30 +135,37 @@ export default {
135
135
cache . recycle = cache . push ;
136
136
}
137
137
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
+
138
162
if ( mixinMethods ) {
139
163
Object . defineProperties ( ClassObject , {
140
164
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 )
152
166
} ,
153
167
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 )
162
169
}
163
170
} ) ;
164
171
Object . defineProperty ( ClassObject . prototype , 'recycle' , {
0 commit comments