-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPool.js
More file actions
73 lines (66 loc) · 1.46 KB
/
Pool.js
File metadata and controls
73 lines (66 loc) · 1.46 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
/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))})
([], function(){
"use strict";
// standard delegator (see Object.create() and dcl/mini.js)
var F = new Function;
function delegate(o){
F.prototype = o;
var t = new F;
F.prototype = null;
return t;
}
// standard mixer (see dcl/mini.js)
function mix(a, b){
for(var n in b){
if(b.hasOwnProperty(n)){
a[n] = b[n];
}
}
}
function Pool(){
this.envs = [];
}
Pool.delegate = delegate;
Pool.mix = mix;
Pool.prototype = {
newEnv: function(){
return this.top ? delegate(this.top) : {};
},
commit: function(env){
this.envs.push(this.top = env);
},
merge: function(env){
if(!this.top){
this.envs.push(this.top = {});
}
mix(this.top, env);
},
getTop: function(){
return this.top;
},
pop: function(){
if(this.top){
var env = this.envs.pop();
var l = this.envs.length;
this.top = l ? this.envs[l - 1] : null;
return env;
}
return null;
}
};
return Pool;
});
// Example of a commit-based exception processing:
//
// var pool = new Pool();
// for(var i = 0; i < TRIES; ++i){
// try{
// var env = pool.newEnv();
// doSomethingWithEnv(env);
// pool.commit(env);
// break; // we are done
// }catch(e){
// // error processing
// }
// // if we are here => retry
// }