-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
fission.js
112 lines (92 loc) · 2.98 KB
/
fission.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
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
let publicLink = null;
let username, goToLobby, wnfs;
async function init() {
// we work with the webnative library through the `state` variable returned from this function
await webnative.initialize({
permissions: {
// there is also an alternate 'Apps' directory for which you can claim permission, see https://guide.fission.codes/developers/webnative/auth
fs: {
private: [webnative.path.directory('todos')]
}
}
}).then(async state => {
switch (state.scenario) {
case webnative.Scenario.AuthSucceeded:
case webnative.Scenario.Continuation:
// authorized
username = state.username;
wnfs = state.fs;
break;
case webnative.Scenario.NotAuthorised:
case webnative.Scenario.AuthCancelled:
// not authorized
goToLobby = function () {
webnative.redirectToLobby(state.permissions);
};
break;
}
}).catch(error => {
switch (error) {
// private mode and non-localhost:3000 may cause issues
case webnative.InitialisationError.UnsupportedBrowser:
window.alert('Unsupported browser.')
break;
case webnative.InitialisationError.InsecureContext:
window.alert('Insecure context.')
break;
}
})
}
async function restoreSession() {
// wait for library ready event
await init();
try {
if (!username)
return false;
return {
name: username,
url: username,
};
} catch (error) {
alert(error.message);
return false;
}
}
function getLoginUrl() {
return 'TO_BE_DETERMINED';
}
function performLogin(loginUrl) {
goToLobby();
}
async function performLogout() {
await webnative.leave({
withoutRedirect: true,
});
}
async function performTaskCreation(description) {
const url = Date.now().toString(36).toLowerCase();
const item = {
url,
description,
};
await wnfs.write(webnative.path.file('private', 'todos', url), JSON.stringify(item));
await wnfs.publish();
return item;
}
async function performTaskUpdate(taskUrl, done) {
const item = JSON.parse(await wnfs.cat(webnative.path.file('private', 'todos', taskUrl)));
await wnfs.write(webnative.path.file('private', 'todos', item.url), JSON.stringify(Object.assign(item, {
done,
})));
await wnfs.publish();
return item;
}
async function performTaskDeletion(taskUrl) {
await wnfs.rm(webnative.path.file('private', 'todos', taskUrl))
await wnfs.publish();
}
async function loadTasks() {
return (await Promise.all(Object.keys(await wnfs.ls(webnative.path.directory('private', 'todos'))).map(function (e) {
return wnfs.cat(webnative.path.file('private', 'todos', e));
}))).map(JSON.parse);
}