-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMicro.js
133 lines (111 loc) · 3.16 KB
/
Micro.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
/* 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";
// ice is "heya-ice/assert"
// ice = ice.specialize(module);
// Based on Max' micro-deferred: https://gist.github.com/MaxMotovilov/4750596
function Micro(val){
// ice.assert(!(val instanceof Micro), "Attempt to improperly construct a promise");
if (val instanceof Micro) { throw new Error("Attempt to improperly construct a promise"); }
if(val instanceof Callback){
this.parent = val; // State W: Weak promise
}else if(arguments.length){
this.value = val; // State R: Resolved promise
}else{
this.chain = []; // State P': Regular, independent promise
}
}
Micro.prototype = {
declaredClass: "heya-async/Micro",
rebind: function(val, adapter){
if(!(val instanceof Micro)){
return false;
}
if("value" in val || !val.addCallback(adapter || doNothing)){
this.value = adapter ? adapter(val.value) : val.value; // Enter transitional state
}else{
(this.parent = val.chain[val.chain.length - 1]).promise = this;
}
return true;
},
resolve: function(val, isEvent){
// ASSERT( state == P )
// ice.assert(!("value" in this) && this.chain, "Promise cannot be resolved");
if ("value" in this || !this.chain) { throw new Error("Promise cannot be resolved"); }
if(!isEvent && this.rebind(val)){
if("value" in this){
val = this.value; // Exit transitional state
delete this.value;
}else{
return;
}
}
for(var i = 0; i < this.chain.length; ++i){
this.chain[i].resolve(val, isEvent);
}
if(!isEvent){
// state: P/P' -> R
this.value = val;
delete this.chain;
delete this.parent;
}
},
then: function(cb){
if("value" in this || !this.addCallback(cb)){
var value = cb(this.value);
return value instanceof Micro ? value : new Micro(value);
}
return new Micro(this.chain[this.chain.length - 1]);
},
done: function(cb){
if("value" in this || !this.addCallback(cb)){
cb(this.value);
}
},
addCallback: function(cb){
if(!this.chain){
// ice.assert("parent" in this, "Malformed promise state");
if (!("parent" in this)) { throw new Error("Malformed promise state"); }
if("value" in this.parent){
// state: W -> R
this.value = this.parent.value;
delete this.parent;
return false;
}
// state: W -> P
this.parent.promise = this;
this.chain = [];
}
this.chain.push(new Callback(cb, this));
return true;
},
cancel: function(arg){
if(this.parent){
delete this.parent.promise;
if(this.parent.parent.chain.length == 1){
this.parent.parent.cancel(arg);
}
}
}
};
function Callback(cb, parent){
this.callback = cb;
this.parent = parent;
}
Callback.prototype = {
resolve: function(val, isEvent){
val = this.callback(val);
delete this.parent;
if(this.promise){
this.promise.resolve(val, isEvent);
if(!isEvent){
delete this.promise;
}
}else if(!isEvent){
this.value = val;
}
}
};
return Micro;
function doNothing(v){ return v; }
});