-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
59 lines (52 loc) · 1.52 KB
/
app.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
/*
Singleton class for the app
Elements can import this to get access to app state
Also handles coordinating between the player and the other UI elements
*/
import Table from "./lib/storage.js";
import Emitter from "./lib/emitter.js";
class Radio extends Emitter {
constructor() {
super();
this.feeds = new Table("feeds");
this.on("sync-export", () => this.syncUp());
this.on("sync-import", () => this.syncDown());
}
async read(key) {
var v = localStorage.getItem(key);
if (!v) return null;
return JSON.parse(v);
}
async write(key, value) {
var v = JSON.stringify(value);
localStorage.setItem(key, v);
}
async syncUp() {
var feeds = await this.feeds.getAll();
var body = JSON.stringify(feeds);
var post = await fetch("/fetch-key-repeat/new", {
method: "POST",
body,
mode: "cors"
});
var { key } = await post.json();
if (!key) {
return alert("Unable to sync with the copy/paste server!");
}
alert(`Got a sync key: ${key}`);
}
async syncDown() {
var key = prompt("Sync key?");
if (!key) return;
var request = await fetch(`/fetch-key-repeat/${key}`);
var json = await request.json();
if (!json instanceof Array) return alert("Unable to parse feed array!");
for (var feed of json) {
var legacy = typeof feed == "string";
var url = legacy ? feed : feed.url;
var data = legacy ? { url, subscribed: Date.now() } : feed;
await this.feeds.set(url, data);
}
}
}
export default new Radio();