-
Notifications
You must be signed in to change notification settings - Fork 12
/
playground.html
187 lines (135 loc) · 5.71 KB
/
playground.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
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Playground for Hyper Hyper Space</title>
<!--<script src="https://bundle.run/[email protected]"></script>-->
<script src="./dist-browser/hhs.js"></script>
<script>
//globalThis.Buffer = globalThis.Buffer.Buffer
Object.entries(HHS).forEach(([k, v]) => (globalThis[k] = v));
class DocumentSpace extends HashedObject {
constructor() {
super();
this.setRandomId();
this.addDerivedField('contents', new MutableReference());
}
getClassName() {
return 'test/DocumentSpace';
}
init() {
}
async validate(references) {
return this.getId() !== undefined && this.checkDerivedField('contents')
}
setValue(value) {
return this.contents.setValue(value).then(() => { this.getStore().save(this.contents) });
}
getValue() {
return this.contents.getValue();
}
async startSync() {
let resources = this.getResources();
if (resources === undefined) {
throw new Error('Cannot start sync: resources not configured.');
}
if (resources.config?.id === undefined) {
throw new Error('Cannot start sync: local identity has not been defined.');
}
if (resources.store === undefined) {
throw new Error('Cannot start sync: a local store has not been configured.')
}
this._node = new MeshNode(resources);
this._node.broadcast(this);
this._node.sync(this);
this.contents.loadAndWatchForChanges();
}
async stopSync() {
this._node?.stopBroadcast(this);
this._node?.stopSync(this);
}
setResources(resources) {
super.setResources(resources);
this.contents.setResources(resources);
}
addSyncCallback(cb) {
this.contents.addMutationOpCallback(async (mut) => {
if (!(await this._node.expectingMoreOps(this.contents, new Set([mut.hash()]), undefined, this))) {
await this.contents.applyOpFromStore(mut.hash());
cb(this.getValue());
}
});
}
}
HashedObject.registerClass('test/DocumentSpace', DocumentSpace);
let store;
let key;
let id;
let resources;
let init = async () => {
store = new Store(new WorkerSafeIdbBackend('document-space-example'));
key = await RSAKeyPair.generate(1024);
id = Identity.fromKeyPair({name: new RNGImpl().randomHexString(128)}, key);
await store.save(key);
await store.save(id);
resources = await Resources.create({config: {id: id}, store: store});
}
</script>
</head>
<body>
<h1>Playground for Hyper Hyper Space</h1>
<p> If you open the console in this page, you'll find the contents of the
@hyper-hyper-space/core package are avaiable in the gobal scope.</p>
<p> Additionally, this page defines an example DocumentSpace class, that can sync a single javascript
object last-writer-wins style. It also defines an init() function, that will create
a Resources object you can use to initialize HHS (it has a random crypto id and an
IndexedDB-based store).
</p>
<p>
To create a Document Space, do this (paste this to the console in this page):
</p>
<pre>
await init();
let ds = new DocumentSpace();
let space = Space.fromEntryPoint(ds, resources);
await space.entryPoint
console.log(await space.getWordCoding())
ds.setResources(resources)
ds.startSync()
</pre>
<p>
You only need to run the code above once. The 3-word code for your newly created space will be printed on the console, and the space will be persisted to in-browser storage.
</p>
<p>
You can then instantiate this object in another computer / browser / tab (or re-open it in the same browser where you created it) by opening this
page again and then typing this in the console:
</p>
<pre>
await init();
let space = Space.fromWordCode(['your', 'words', 'here!'], resources) // replace 3 words;
let ds = await space.getEntryPoint();
ds.setResources(resources);
await resources.store.save(ds);
ds.startSync();
</pre>
<p>
Remember to replace the 3-code word in the code above by the one you got when creating your space!
You need to keep at least one browser tab open for the space to be available.
</p>
<p>Now you can do, in any of your browser windows:
<pre>
ds.setValue({'myApp': 'state'});
</pre>
<p>
And you can read that back in the rest of them:
</p>
<pre>
ds.getValue();
</pre>
<p>If you were <b>really</b> creating an HHS-based webpage, you'll probably want to use something like these <a href="https://github.com/hyperhyperspace/hyperhyperspace-react">react bindings</a> to tie your web components automatically to the objects in your store!</p>
<p>Or maybe you can get away with a callback whenever state changes, something like this:</p>
<pre>
ds.addSyncCallback((newState) => { 'do something! state has changed!';});
</pre>
<p> Help / feedback: <a href="https://github.com/hyperhyperspace/hyperhyperspace-core">Github</a>, <a href="https://discord.gg/9epr3XrRnW">Discord</a></p>
</body>
</html>