This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zrx.js
138 lines (117 loc) · 4.27 KB
/
zrx.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
134
135
136
137
138
var Rx = require('rx');
var siren = require('siren');
var Device = require('./device');
var Zrx = module.exports = function(current) {
if (!(this instanceof Zrx)) {
return new Zrx(current);
}
this.client = siren(current);
};
Zrx.prototype.load = function(uri) {
this.client.load(uri);
return this;
};
Zrx.prototype.server =
Zrx.prototype.servers =
Zrx.prototype.peer =
Zrx.prototype.peers = function(name) {
var subject = new Rx.ReplaySubject();
this.client.subscribe(subject);
var server = siren(subject)
.link('http://rels.zettajs.io/server', name)
.catch(Rx.Observable.empty());
return new Zrx(server);
};
Zrx.prototype.query = function(ql) {
return this.transition('query-devices', function(t) {
t.set('ql', ql);
return t.submit();
}).devices();
};
Zrx.prototype.device = Zrx.prototype.devices = function(filter) {
var client;
if (typeof filter === 'string') {
client = this.client.entity(function(entity) {
var device = new Device(entity);
return device.type === filter;
});
} else {
client = this.client.entity(function(entity) {
var device = new Device(entity);
if (!filter) {
return device;
} else {
return filter(device);
}
});
}
return new Zrx(client);
};
Zrx.prototype.toDevice = function() {
var client = this.client
.map(function(env) {
return new Device(env.response.body);
});
return new Zrx(client);
};
Zrx.prototype.transition = function(name, cb) {
var client = this.client.action(name, cb);
return new Zrx(client);
};
// binary streams are not yet supported
Zrx.prototype.stream = function(name) {
var client = this.client
.link('http://rels.zettajs.io/object-stream', name)
.monitor()
.map(JSON.parse);
return new Zrx(client);
};
var subscriptionFns = ['subscribe', 'subscribeOnNext', 'subscribeOnError',
'subscribeOnCompleted', 'subscribeOn'];
subscriptionFns.forEach(function(m) {
Zrx.prototype[m] = function() {
var args = Array.prototype.slice.call(arguments);
return this.client[m].apply(this.client, args);
}
});
var operators = [
'amb', 'and', 'asObservable', 'average', 'buffer',
'bufferWithCount', 'bufferWithTime', 'bufferWithTimeOrCount',
'catch', 'catchError', 'combineLatest', 'concat', 'concatAll',
'concatMap', 'concatMapObserver', 'connect', 'contains',
'controlled', 'count', 'debounce', 'debounceWithSelector',
'defaultIfEmpty', 'delay', 'delayWithSelector', 'dematerialize',
'distinct', 'distinctUntilChanged', 'do', 'doOnNext',
'doOnError', 'doOnCompleted', 'doWhile', 'elementAt',
'elementAtOrDefault', 'every', 'expand', 'filter', 'finally',
'ensure', 'find', 'findIndex', 'first', 'firstOrDefault',
'flatMap', 'flatMapObserver', 'flatMapLatest', 'forkJoin',
'groupBy', 'groupByUntil', 'groupJoin', 'ignoreElements',
'indexOf', 'isEmpty', 'join', 'jortSort', 'jortSortUntil',
'last', 'lastOrDefault', 'let', 'letBind', 'manySelect', 'map',
'max', 'maxBy', 'merge', 'mergeAll', 'min', 'minBy', 'multicast',
'observeOn', 'onErrorResumeNext', 'pairwise', 'partition',
'pausable', 'pausableBuffered', 'pluck', 'publish', 'publishLast',
'publishValue', 'share', 'shareReplay', 'shareValue', 'refCount',
'reduce', 'repeat', 'replay', 'retry', 'sample', 'scan', 'select',
'selectConcat', 'selectConcatObserver', 'selectMany',
'selectManyObserver', 'selectSwitch', 'sequenceEqual', 'single',
'singleOrDefault', 'skip', 'skipLast', 'skipLastWithTime',
'skipUntil', 'skipUntilWithTime', 'skipWhile', 'some', 'startWith',
/*'subscribe', 'subscribeOnNext', 'subscribeOnError',
'subscribeOnCompleted', 'subscribeOn', */ 'sum', 'switch',
'switchLatest', 'take', 'takeLast', 'takeLastBuffer',
'takeLastBufferWithTime', 'takeLastWithTime', 'takeUntil',
'takeUntilWithTime', 'takeWhile', 'tap', 'tapOnNext', 'tapOnError',
'tapOnCompleted', 'throttleFirst', 'throttleWithTimeout',
'timeInterval', 'timeout', 'timeoutWithSelector', 'timestamp',
'toArray', 'toMap', 'toSet', 'transduce', 'where', 'window',
'windowWithCount', 'windowWithTime', 'windowWithTimeOrCount', 'zip'
]
operators.forEach(function(m) {
Zrx.prototype[m] = function() {
var args = Array.prototype.slice.call(arguments);
var client = this.client[m].apply(this.client, args);
return new Zrx(client);
};
});