forked from cory/tildefriends
Move apps/cory/ => apps/. Going to change import and export to support this.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4163 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
82
apps/todo/app.js
Normal file
82
apps/todo/app.js
Normal file
@ -0,0 +1,82 @@
|
||||
import * as tfrpc from '/tfrpc.js';
|
||||
|
||||
let g_db;
|
||||
|
||||
tfrpc.register(async function todo_get_all() {
|
||||
let names = await todo_get_names();
|
||||
let result = [];
|
||||
for (let name of names) {
|
||||
result.push({
|
||||
name: name,
|
||||
items: await todo_get(name),
|
||||
});
|
||||
}
|
||||
return result;
|
||||
});
|
||||
|
||||
async function todo_get_names() {
|
||||
return JSON.parse((await g_db.get('files')) ?? '[]');
|
||||
}
|
||||
|
||||
async function todo_add(list) {
|
||||
let exchanged = false;
|
||||
let tries = 10;
|
||||
while (!exchanged && tries-- > 0) {
|
||||
let original = await g_db.get('files');
|
||||
let names = JSON.parse(original ?? '[]');
|
||||
let set = new Set(names);
|
||||
set.add(list);
|
||||
names = JSON.stringify([...set].sort());
|
||||
exchanged = original === names || await g_db.exchange('files', original, names);
|
||||
}
|
||||
return exchanged;
|
||||
}
|
||||
tfrpc.register(todo_add);
|
||||
|
||||
async function todo_remove(list) {
|
||||
let exchanged = false;
|
||||
let tries = 10;
|
||||
while (!exchanged && tries-- > 0) {
|
||||
let original = await g_db.get('files');
|
||||
let names = JSON.parse(original ?? '[]');
|
||||
let set = new Set(names);
|
||||
set.delete(list);
|
||||
names = JSON.stringify([...set].sort());
|
||||
exchanged = original === names || await g_db.exchange('files', original, names);
|
||||
}
|
||||
await g_db.remove('list:' + list);
|
||||
return exchanged;
|
||||
}
|
||||
tfrpc.register(todo_remove);
|
||||
|
||||
tfrpc.register(async function todo_rename(old_name, new_name) {
|
||||
if (await g_db.get('list:' + new_name)) {
|
||||
throw RuntimeError(`${new_name} already exists.`);
|
||||
}
|
||||
let list = await todo_get(old_name);
|
||||
await todo_set(new_name, list);
|
||||
await todo_add(new_name);
|
||||
await todo_remove(old_name);
|
||||
});
|
||||
|
||||
async function todo_get(list) {
|
||||
try {
|
||||
let value = await g_db.get('list:' + list);
|
||||
return JSON.parse(value ?? '[]');
|
||||
} catch (error) {
|
||||
print(error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async function todo_set(list, value) {
|
||||
await g_db.set('list:' + list, JSON.stringify(value));
|
||||
}
|
||||
tfrpc.register(todo_set);
|
||||
|
||||
async function main() {
|
||||
g_db = await database('todo');
|
||||
await app.setDocument(utf8Decode(getFile('index.html')));
|
||||
}
|
||||
|
||||
main();
|
Reference in New Issue
Block a user