-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunserver.js
More file actions
157 lines (138 loc) · 5.94 KB
/
Copy pathunserver.js
File metadata and controls
157 lines (138 loc) · 5.94 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var Unserver = (function(){
var http={DEFAULTS:{async:!0,contentType:"text/plain",data:null,headers:{},method:"GET",onerror:function(){},onload:function(){},onreadystatechange:function(){},ontimeout:function(){},url:"./",props:[]},DEFAULT_PROPS:["onload","onerror","onreadystatechange","ontimeout"],request:function(e){for(var e=this.options(e),t=new XMLHttpRequest,n=0;n<e.props.length;n++){var r=e.props[n];t[r]=e[r]}t.timeout=2000;t.open(e.method,e.url,e.async);for(var r in e.headers)e.headers.hasOwnProperty(r)&&t.setRequestHeader(r,e.headers[r]);return t.setRequestHeader("Content-Type",e.contentType),t.send(e.data),t},options:function(e){for(var t={},n=this.DEFAULTS,r=Object.keys(e).concat(Object.keys(n)),o=0;o<r.length;o++){var s=r[o];t[s]=e[s]||n[s]}return t.props=t.props.concat(this.DEFAULT_PROPS),t},get:function(e){return this.request(e)},post:function(e){return e.method="POST",this.request(e)},put:function(e){return e.method="PUT",this.request(e)}};
var Watcher = (function(){
function ctor(tag, property){
var listeners = [];
function addListener(onNewValue){
if (typeof(onNewValue) !== "function")
throw new TypeError();
listeners.push(onNewValue);
}
function newValue(value){
listeners.forEach(function(x){x(value);});
}
function key(){
return JSON.stringify({t:tag, p:property});
}
return {
addListener: addListener,
newValue: newValue,
tag: tag,
property: property,
key: key
}
}
ctor.computeKey = function(tag,property){
return JSON.stringify({t:tag, p:property});
}
return ctor;
})();
function ctor(unserverUrl){
var _interval = 1000;
var _url = unserverUrl;
var watchedTags = {};
var watchedProperties = {};
var errorWatcher = new Watcher();
var poll = (function(callback){
var isRunning = false;
function loop(){
if(isRunning){
callback();
setTimeout(loop, _interval);
}
}
function start(interval){
if(!isRunning){
_interval = interval;
setTimeout(loop, _interval);
isRunning = true;
}
}
function stop(){
isRunning = false;
}
return{
start:start,
stop:stop
}
})(updateAllTags);
function updateAllTags(){
for (wt in watchedTags){
(function(t){
http.get({
url: _url + '/tags/' + watchedTags[t].tag,
onload: function() {
var responseJson = JSON.parse(this.responseText);
var tagData = responseJson.data;
if (tagData){
watchedTags[t].newValue(tagData);
for(p in tagData){
var pk = Watcher.computeKey(watchedTags[t].tag, p);
if (watchedProperties.hasOwnProperty(pk)){
watchedProperties[pk].newValue(tagData[p]);
}
}
}
},
onerror: function(e) {
errorWatcher.newValue(e);
}
});
})(wt);
}
}
function setProperty(propertyPath, value, callback, error){
var tag = propertyPath.split('.')[0];
var property = propertyPath.split('.')[1];
http.put({
url: _url + '/tags/' + tag + '/properties/' + property,
data: JSON.stringify({value:value}),
contentType: 'application/json',
onload: function(){
callback && callback();
},
onerror: function(e){
error && error();
},
ontimeout: function(){
error && error();
}
});
}
function watchTag(tag, onNewValue){
var tagWatcherKey = Watcher.computeKey(tag);
if(!watchedTags.hasOwnProperty(tagWatcherKey)){
watchedTags[tagWatcherKey] = new Watcher(tag);
}
onNewValue && watchedTags[tagWatcherKey].addListener(onNewValue);
}
function watchProperty(propertyPath, onNewValue){
var tag = propertyPath.split('.')[0];
var property = propertyPath.split('.')[1];
var pk = Watcher.computeKey(tag, property);
if(!watchedProperties.hasOwnProperty(pk)){
watchedProperties[pk] = new Watcher(tag, property);
}
watchedProperties[pk].addListener(onNewValue);
watchTag(tag);
}
function watchError(listener){
listener && errorWatcher.addListener(listener);
}
function unwatchProperty(tag, property){
delete watchedProperties[Watcher.computeKey(tag,property)];
}
return{
startPolling: function(intervalMs){
poll.start(intervalMs);
},
stopPolling: function(){poll.stop();},
watchTag:watchTag,
watchProperty:watchProperty,
watchError: watchError,
unwatchProperty:unwatchProperty,
setProperty: setProperty,
}
}
return ctor;
})();