-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
175 lines (144 loc) · 4.21 KB
/
test.html
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<html>
<head>
<title>Hyper-path Test Page</title>
<script type="text/javascript" src="build/build.js"></script>
</head>
<body>
<a href="javascript:rootTest()">Root test</a>
<a href="javascript:nestedTest()">Nested test</a>
<a href="javascript:multiTest()">Multi test</a>
<a href="javascript:passedScopeTest()">Passed scope test</a>
<a href="javascript:changedScopeTest()">Changed scope test</a>
<a href="javascript:collectionScopeTest()">Collection scope test</a>
<script type="text/javascript">
var client = require('hyper-path');
var emitter = require('hyper-emitter');
function agent(fn) {
return emitter.get('/api/index.json', fn);
}
agent.get = function(href, fn) {
return emitter.get(href, fn);
}
function rootTest() {
var req = client('.apps', agent)
.on(function(err, apps) {
console.log(arguments);
});
var ref = setInterval(function() {
emitter.refresh('/api/apps.json');
}, 100);
setTimeout(function() {
unsub(req);
}, 1000);
setTimeout(function() {
clearInterval(ref);
}, 2000);
}
function nestedTest() {
var req = client('.apps.0.name', agent)
.on(function(err, name) {
console.log(arguments);
});
var ref = setInterval(function() {
emitter.refresh('/api/apps.json');
}, 100);
setTimeout(function() {
unsub(req);
}, 1000);
setTimeout(function() {
clearInterval(ref);
}, 2000);
}
function multiTest() {
var first = client('.apps.0.name', agent)
.on(function(err, name) {
console.log('first', name);
});
var second = client('.apps.0.name', agent)
.on(function(err, name) {
console.log('second', name);
});
var ref = setInterval(function() {
emitter.refresh('/api/apps.json');
}, 100);
setTimeout(function() {
unsub(first);
}, 1000);
setTimeout(function() {
unsub(second);
}, 2000);
setTimeout(function() {
clearInterval(ref);
}, 3000);
}
function passedScopeTest() {
var req = client('apps.0.name', agent)
.scope({apps: {href: '/api/apps.json'}})
.on(function(err, name) {
console.log(arguments);
});
var appsref = setInterval(function() {
emitter.refresh('/api/apps.json');
}, 300);
var appref = setInterval(function() {
emitter.refresh('/api/app.json');
}, 200);
setTimeout(function() {
unsub(req);
}, 1000);
setTimeout(function() {
clearInterval(appref);
clearInterval(appsref);
}, 2000);
}
function changedScopeTest() {
var req = client('apps.0.name', agent)
.scope({apps: {href: '/api/apps.json'}})
.on(function(err, name) {
console.log(arguments);
});
setTimeout(function() {
req.scope({apps: {href: '/api/other-apps.json'}});
}, 1000);
var appref = setInterval(function() {
emitter.refresh('/api/app.json');
}, 100);
var otherref = setInterval(function() {
emitter.refresh('/api/other-app.json');
}, 200);
setTimeout(function() {
unsub(req);
}, 2000);
setTimeout(function() {
clearInterval(appref);
clearInterval(otherref);
}, 3000);
}
function collectionScopeTest() {
var root = [{href: '/api/app.json'}, {href: '/api/other-app.json'}];
root.href = '/api/apps.json';
var req1 = client('apps.count', agent)
.scope({apps: root})
.on(function(err, count) {
console.log('count should be 2', count);
unsub(req1);
});
var req2 = client('apps.length', agent)
.scope({apps: root})
.on(function(err, count) {
console.log('length should be 2', count);
unsub(req2);
});
var req3 = client('apps.thingy', agent)
.scope({apps: root})
.on(function(err, val) {
console.log('thingy should be undefined', val);
unsub(req3);
});
}
function unsub(req) {
if (req) req.off();
}
</script>
</body>
</html>