-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicro.js
More file actions
73 lines (67 loc) · 1.71 KB
/
Micro.js
File metadata and controls
73 lines (67 loc) · 1.71 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";
var noValue = {};
function Micro(callback){
this.channels = {};
this.parent = [];
this.callback = callback || function identity(x){ return x; };
this.sink = makeSink(this);
}
Micro.noValue = noValue;
Micro.prototype = {
declaredClass: "events/Micro",
noValue: noValue,
on: function on(channelName, callback){
var micro = callback instanceof Micro ? callback : new Micro(callback);
if(this.channels.hasOwnProperty(channelName)){
this.channels[channelName].push(micro);
}else{
this.channels[channelName] = [micro];
}
micro.parent.push(this.channels[channelName]);
return micro;
},
send: function send(value, copy){
value = this.callback(value, this.sink);
if(value !== noValue){
var channel = this.channels["default"];
if(channel){
if(copy){
channel = channel.slice(0);
}
for(var i = 0; i < channel.length; ++i){
channel[i].send(value, copy);
}
}
}
},
release: function release(){
this.parent.forEach(function(parent){
var i = parent.indexOf(this);
parent.splice(i, 1);
});
this.parent = [];
}
};
return Micro;
function makeSink(micro){
return {
send: function(channelName, value, copy){
if(value !== noValue){
var channel = micro.channels[channelName];
if(channel){
if(copy){
channel = channel.slice(0);
}
for(var i = 0; i < channel.length; ++i){
channel[i].send(value, copy);
}
}
}
return noValue;
},
noValue: noValue
};
}
});