You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.3 KiB
83 lines
2.3 KiB
import { idb, nests } from "./common"
|
|
|
|
const nest = nests.make()
|
|
|
|
async function init() {
|
|
const extNest = demon.require("internal/nest")
|
|
if (!window.__demon) { // this *shouldn't* be required but if we magically run into a race condition it's better to be safe
|
|
window.__demon = {}
|
|
}
|
|
if (!(await idb.get("demon"))) {
|
|
await idb.set("demon", {
|
|
status: {
|
|
|
|
},
|
|
plugins: {
|
|
|
|
}
|
|
})
|
|
}
|
|
const currdemon = await idb.get("demon")
|
|
currdemon.status = {}
|
|
Object.keys(currdemon.plugins).forEach(key => {
|
|
const plug = currdemon.plugins[key]
|
|
const exports = (0, eval)(plug.initialize)
|
|
const ret = exports.onStart()
|
|
nest.store[plug.meta.name].ctx = ret
|
|
currdemon.status[plug.meta.name] = {
|
|
running: true
|
|
}
|
|
})
|
|
extNest.store.pluginsList = currdemon.plugins
|
|
await idb.set("demon", currdemon)
|
|
}
|
|
|
|
async function add(iife, meta) {
|
|
const currdemon = await idb.get("demon")
|
|
currdemon.plugins[meta.name] = {
|
|
initialize: iife,
|
|
meta: meta
|
|
}
|
|
extNest.store.pluginsList = currdemon.plugins
|
|
await idb.set("demon", currdemon)
|
|
}
|
|
|
|
async function del(name) {
|
|
const currdemon = await idb.get("demon")
|
|
if (!!currdemon.plugins[name])
|
|
if (currdemon.status[name].running) {
|
|
const ctx = nest.store[name].ctx;
|
|
(0, eval)(currdemon.plugins[name].initialize).onStop(ctx)
|
|
}
|
|
delete currdemon.status[name]
|
|
delete currdemon.plugins[name]
|
|
extNest.store.pluginsList = currdemon.plugins
|
|
await idb.set("demon", currdemon)
|
|
}
|
|
|
|
async function toggle(name) {
|
|
const currdemon = await idb.get("demon")
|
|
if (!!currdemon.plugins[name]) {
|
|
if (currdemon.status[name]?.running) {
|
|
console.log(currdemon.status[name].ctx)
|
|
const ctx = nest.store[name].ctx;
|
|
(0, eval)(currdemon.plugins[name].initialize).onStop(ctx)
|
|
currdemon.status[name].running = false
|
|
}
|
|
else {
|
|
const ret = (0, eval)(currdemon.plugins[name].initialize).onStart()
|
|
nest.store[name].ctx = ret
|
|
currdemon.status[name] = {
|
|
running: true
|
|
}
|
|
}
|
|
}
|
|
await idb.set("demon", currdemon)
|
|
}
|
|
|
|
export default {
|
|
init,
|
|
add,
|
|
del,
|
|
toggle
|
|
} |