-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPubSubWithAdd.html
236 lines (186 loc) · 6.22 KB
/
PubSubWithAdd.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1">
<title>PubSub with LDFlex .add()</title>
<script src="https://cdn.jsdelivr.net/npm/solid-auth-client/dist-lib/solid-auth-client.bundle.js">
</script>
<script src="https://cdn.jsdelivr.net/npm/@solid/[email protected]/dist/solid-query-ldflex.bundle.js">
</script>
<script src="https://unpkg.com/@rdfjs/data-model/dist/rdf-data-model.js">
</script>
<style>
body {
background: aliceblue;
padding: 10px;
}
body * {
font-size: 1.1em;
}
input {
width: 100%;
}
pre {
width: 100%;
padding: 5px;
background: black;
color: lightgreen;
height: 250px;
overflow-y: scroll;
}
div#current-labels {
padding: 10px;
border: 2px solid lightgray;
height: 200px;
overflow-y: scroll;
}
</style>
</head>
<body>
<div>
<hr>
<p>
This page views and modifies the Label for a Solid resource by using
<a href="https://github.com/solid/query-ldflex" target="_blank">query-ldflex</a> to read and write the label, and by using WebSocket to listen to changes to the resource.
</p>
<p>
Clicking the <code>Add Label</code> button will write the value of
<code>Next Label</code> to the resource, and will add an 'X' to
<code>Next Label</code> to provide a unique label for subsequent
<code>Add Label</code> clicks.
</p>
<label for="resource">Resource:
<a id="resource-link" target="_blank">View via Data Browser</a>
</label>
<input type="text" id="resource" readonly>
<hr>
<label for="current-labels">
Labels: (
<span id="current-labels-count"></span>
total)
</label>
<div
id="current-labels">
</div>
<hr>
<label for="new-label">Next Sequence Number:</label>
<input type="text" id="next-sequence" readonly>
<label for="new-label">Next Label:</label>
<input type="text" id="next-label" required>
<hr>
<button type="button" id="add-button">Add Label</button>
<button type="button" id="add-n10-button">Add N=10 Labels</button>
<button type="button" id="add-n100-button">Add N=100 Labels</button>
<button type="button" id="add-n1000-button">Add N=1000 Labels</button>
<button type="button" id="reset-button">Reset Labels</button>
<pre id="console"></pre>
</div>
<script>
'use strict';
const rdf = window.rdf;
const literal = rdf.literal;
const resourceId = 'https://doctorbud.solid.community/public/PubSub/statePubSubAdd.ttl';
const wss = 'wss://doctorbud.solid.community';
// const resourceId = 'https://doctorbud.dev.inrupt.net/public/PubSub/statePubSubAdd.ttl';
// const wss = 'wss://doctorbud.dev.inrupt.net';
const consoleElement = document.getElementById('console');
const resourceElement = document.getElementById('resource');
const resourceLinkElement = document.getElementById('resource-link');
const currentElement = document.getElementById('current-labels');
const currentCountElement = document.getElementById('current-labels-count');
const nextSequenceElement = document.getElementById('next-sequence');
const nextLabelElement = document.getElementById('next-label');
const addElement = document.getElementById('add-button');
const addN10Element = document.getElementById('add-n10-button');
const addN100Element = document.getElementById('add-n100-button');
const addN1000Element = document.getElementById('add-n1000-button');
const resetElement = document.getElementById('reset-button');
function log(...args) {
let line = '';
args.forEach(arg => {
line += arg + ' ';
});
line += '\n';
consoleElement.innerText += line;
consoleElement.scrollTop = consoleElement.scrollHeight;
}
function displayCurrent(current) {
currentCountElement.innerText = `${current.length}`;
currentElement.innerText = current.join('\n');
currentElement.scrollTop = currentElement.scrollHeight;
}
async function getLabels(resourceId) {
const resource = solid.data[resourceId];
const labels = [];
for await (const label of resource['http://www.w3.org/2000/01/rdf-schema#label']) {
labels.push(label);
}
return labels
.filter(node => node.termType === 'Literal')
.map(label => label.value);
}
async function restoreState(resourceId) {
await solid.data.clearCache(resourceId);
const current = await getLabels(resourceId);
nextSequenceElement.value = `${(new Date()).toISOString()}|`;
if (nextLabelElement.value.length === 0) {
nextLabelElement.value = '<Message Payload Goes Here>';
}
displayCurrent(current);
}
async function addLabel(resourceId, label) {
try {
// solid.data.clearCache(resourceId);
const resource = solid.data[resourceId];
await resource['http://www.w3.org/2000/01/rdf-schema#label'].add(literal(label));
}
catch (e) {
log('addLabel() exception', typeof e, e, `"${label}"`);
log('Try again after clearing cache');
await solid.data.clearCache(resourceId);
await getLabels(resourceId);
await addLabel(resourceId, label);
}
}
async function addN(resourceId, n) {
for (let i = 0; i < n; ++i) {
await addLabel(resourceId, nextSequenceElement.value + nextLabelElement.value);
nextSequenceElement.value = `${(new Date()).toISOString()}|`;
}
}
addElement.onclick = async () => {
await addN(resourceId, 1);
};
addN10Element.onclick = async () => {
await addN(resourceId, 10);
};
addN100Element.onclick = async () => {
await addN(resourceId, 100);
};
addN1000Element.onclick = async () => {
await addN(resourceId, 1000);
};
resetElement.onclick = async () => {
const resource = solid.data[resourceId];
'' + (await resource['http://www.w3.org/2000/01/rdf-schema#label'].delete());
await restoreState(resourceId);
};
resourceElement.value = resourceId;
resourceLinkElement.href = resourceId;
// Listen to changes via WebSocket
var socket = new WebSocket(wss);
socket.onopen = async function() {
log('onopen');
this.send(`sub ${resourceId}`);
await restoreState(resourceId);
};
socket.onmessage = async function(msg) {
log('onmessage', msg.data);
if (msg.data && msg.data.slice(0, 3) === 'pub') {
await restoreState(resourceId);
}
};
</script>
</body>
</html>