@ -1,86 +1,85 @@
//TODO: separate currently running status from enabled on reload
//ugh am i gonna remake the internal plugin format again? (yes)
/ * [ p l u g i n n a m e ] : {
enabled : Bool , //doubles as our running status (my explanation is too complex to fit in this comment so get fucked)
iife : String , //we can't store functions in iDB? well fuck you, I'll make plugin devs package to an iife string
desc : String //self-descriptory
//TODO: should we be saving this in a separate metadata object?
} * /
import { idb , nests } from "./common" ;
import logger from "./utils/logger"
const ctxNest = nests . make ( ) ; //Plugin context nest (I would create this in index, but it's not a good idea to expose that)
const nest = nests . make ( ) ;
let pluginNest ;
const pluginEval = ( iife ) => ( 0 , eval ) ( iife ) //defined as a separate function in case we want to do more things on plugin eval later
async function savePlugin ( eve , { path , value } ) {
logger . debug ( [ "Plugins" ] , ` Got ${ eve } event for plugin manager's nest with path ${ path } and val ${ value } ` )
await idb . set ( "demon" , {
plugins : pluginNest . ghost . plugins
} )
}
async function init ( ) {
const extNest = demon . summon ( "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 = { } ;
}
pluginNest = demon . summon ( "internal/nest" ) //we don't have access to the global yet outside of the function
if ( ! ( await idb . get ( "demon" ) ) ) {
await idb . set ( "demon" , {
status : { } ,
plugins : { }
} ) ;
plugins : { } //we need to pre-create this i guess?
} )
}
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 ;
extNest . store . pluginsStatus = currdemon . status ;
await idb . set ( "demon" , currdemon ) ;
pluginNest . store . plugins = ( await idb . get ( "demon" ) ) . plugins //we do this before we setup our hook to avoid any potential conflicts
pluginNest . on ( nests . Events . SET , savePlugin )
pluginNest . on ( nests . Events . DELETE , savePlugin )
Object . keys ( pluginNest . ghost . plugins ) . forEach ( ( ele ) => {
if ( pluginNest . ghost . plugins [ ele ] . enabled ) {
const exports = pluginEval ( pluginNest . ghost . plugins [ ele ] . iife )
const ctx = exports . onStart ( ) //actually starts plugin
ctxNest . store [ ele ] = ctx
pluginNest . store . plugins [ ele ] . enabled = true //for clarity
} else {
pluginNest . store . plugins [ ele ] . enabled = false //for clarity
}
} )
}
async function add ( iife , meta ) {
const extNest = demon . summon ( "internal/nest" ) ;
const currdemon = await idb . get ( "demon" ) ;
const exports = ( 0 , eval ) ( iife ) ;
if ( ! ! exports . meta ) {
meta = exports . meta ;
function add ( iife ) {
const exports = pluginEval ( iife )
logger . debug ( [ "Plugins" ] , ` Adding ${ exports . meta . name } ` )
pluginNest . store . plugins [ exports . meta . name ] = {
iife ,
enabled : false ,
desc : exports . meta . desc
}
currdemon . plugins [ meta . name ] = {
initialize : iife ,
meta : meta
} ;
extNest . store . pluginsList = currdemon . plugins ;
extNest . store . pluginsStatus = currdemon . status ;
await idb . set ( "demon" , currdemon ) ;
}
async function del ( name ) {
const extNest = demon . summon ( "internal/nest" ) ;
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 ;
extNest . store . pluginsStatus = currdemon . status ;
await idb . set ( "demon" , currdemon ) ;
function del ( name ) {
if ( ! ! pluginNest . ghost . plugins [ name ] ) {
logger . debug ( [ "Plugins" ] , ` Removing ${ name } ` )
delete pluginNest . store . plugins [ name ]
} else {
throw new Error ( "Can't delete plugin that doesn't exist. lol" )
}
}
async function toggle ( name ) {
const extNest = demon . summon ( "internal/nest" ) ;
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 ;
function toggle ( name ) {
if ( ! ! pluginNest . ghost . plugins [ name ] ) {
if ( pluginNest . ghost . plugins [ name ] . enabled ) {
logger . debug ( [ "Plugins" ] , ` Disabling ${ name } ` )
const exports = pluginEval ( pluginNest . ghost . plugins [ name ] . iife )
exports . onStop ( ctxNest . store [ name ] )
pluginNest . store . plugins [ name ] . enabled = false
} else {
const ret = ( 0 , eval ) ( currdemon . plugins [ name ] . initialize ) . onStart ( ) ;
nest . store [ name ] . ctx = ret ;
currdemon . status [ name ] = {
running : true
} ;
logger . debug ( [ "Plugins" ] , ` Enabling ${ name } ` )
const exports = pluginEval ( pluginNest . ghost . plugins [ name ] . iife )
const ctx = exports . onStart ( )
ctxNest . store [ name ] = ctx
pluginNest . store . plugins [ name ] . enabled = true
}
}
extNest . store . pluginsList = currdemon . plugins ;
extNest . store . pluginsStatus = currdemon . status ;
await idb . set ( "demon" , currdemon ) ;
}
export { init , add , del , toggle } ;