-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPubSubWithSet.html
175 lines (142 loc) · 4.63 KB
/
PubSubWithSet.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
<!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 .set()</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;
}
</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>Change Label</code> button will write the value of
<code>New Label</code> to the resource, and will add an 'X' to
<code>New Label</code> to provide a unique label for subsequent
<code>Change 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-label">Current Label:</label>
<input type="text" id="current-label" readonly>
<hr>
<label for="new-label">New Label:</label>
<input type="text" id="new-label" required>
<button type="button" id="change-button">Change Label</button>
<button type="button" id="change-10-button">Change Label N=10 times</button>
<hr>
<pre id="console"></pre>
</div>
<script>
const rdf = window.rdf;
const literal = rdf.literal;
const resourceId = 'https://doctorbud.solid.community/public/PubSub/statePubSubSet.ttl';
const wss = 'wss://doctorbud.solid.community';
// const resourceId = 'https://doctorbud.dev.inrupt.net/public/PubSub/statePubSubSet.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-label');
const newElement = document.getElementById('new-label');
const changeElement = document.getElementById('change-button');
const change10Element = document.getElementById('change-10-button');
function log(...args) {
let line = '';
args.forEach(arg => {
line += arg + ' ';
});
line += '\n';
consoleElement.innerText += line;
consoleElement.scrollTop = consoleElement.scrollHeight;
}
async function getLabel(resourceId) {
return (await solid.data[resourceId]['http://www.w3.org/2000/01/rdf-schema#label']);
}
async function restoreState(resourceId) {
solid.data.clearCache(resourceId);
let current = await getLabel(resourceId);
if (typeof current === 'undefined') {
current = '';
}
currentElement.value = current;
newElement.value = current + 'X';
}
async function setLabel(resourceId, label) {
try {
// solid.data.clearCache(resourceId);
const resource = solid.data[resourceId];
await resource['http://www.w3.org/2000/01/rdf-schema#label'].set(literal(label));
}
catch (e) {
log('setLabel() exception', typeof e, e, `"${label}"`);
log('Try again after clearing cache');
await restoreState(resourceId);
// await solid.data.clearCache(resourceId);
// await getLabel(resourceId);
// await setLabel(resourceId, label);
}
}
async function setNLabels(resourceId, label, n) {
for (let i = 0; i < n; ++i) {
const iLabel = label + i;
await setLabel(resourceId, iLabel);
restoreState(resourceId);
}
}
changeElement.onclick = async () => {
await setNLabels(resourceId, newElement.value, 1);
};
change10Element.onclick = async () => {
await setNLabels(resourceId, newElement.value, 10);
};
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}`);
restoreState(resourceId);
};
socket.onmessage = async function(msg) {
log('onmessage', msg.data);
if (msg.data && msg.data.slice(0, 3) === 'pub') {
restoreState(resourceId);
}
};
</script>
</body>
</html>