Skip to content

Commit

Permalink
cache plugins fetched from gist storage
Browse files Browse the repository at this point in the history
  • Loading branch information
blurymind committed Aug 5, 2024
1 parent 070339a commit c0a7abe
Showing 1 changed file with 56 additions and 43 deletions.
99 changes: 56 additions & 43 deletions src/public/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,52 +264,65 @@ export var Plugins = function(app) {
});
});

console.log("Get plugins:", app.settings.gistPluginsFile())
// register plugins stored on a gist - todo cache all this
if (app.settings.gistPluginsFile() !== null) {
app.data.storage.getGist(app.settings.gistPluginsFile()).then(({filesInGist}) => {
console.log({ filesInGist });
Object.values(filesInGist).forEach(gistFile => {
console.log({ gistFile });
if (gistFile.language === 'JavaScript') {

try {
app.data.storage
.getContentOrRaw(gistFile.content, gistFile.raw_url)
.then(content => {
console.log({content})//doesnt resolve?
importModuleWeb(content, gistFile.filename).then(
importedPlugin => {
const newPlugin = importedPlugin(pluginApiMethods);
newPlugin.name = newPlugin.name || gistFile.filename;
app.log(
{ newPlugin },
'loaded from ',
gistFile.raw_url
);
if ('dependencies' in newPlugin) {
newPlugin.dependencies.forEach(dependency => {
const scriptEle = document.createElement('script');
scriptEle.setAttribute('src', dependency);
document.body.appendChild(scriptEle);
scriptEle.addEventListener('load', () => {
console.log('File loaded', dependency);
});
const dbStorage = app.data.db;
const cachedPlugins = {};
const loadPluginsFromCacheOrGist = (plugins, shouldCache = true) => {
plugins.forEach(gistFile => {
console.log({ gistFile });

if (gistFile.language === 'JavaScript') {
try {
app.data.storage
.getContentOrRaw(gistFile.content, gistFile.raw_url)
.then(content => {
console.log({content})//doesnt resolve?
importModuleWeb(content, gistFile.filename).then(
importedPlugin => {
const newPlugin = importedPlugin(pluginApiMethods);
newPlugin.name = newPlugin.name || gistFile.filename;
app.log(
{ newPlugin },
'loaded from ',
gistFile.raw_url
);
cachedPlugins[gistFile.filename] = { ...gistFile, content };
if (shouldCache) dbStorage.save("cachedPlugins", cachedPlugins);
if ('dependencies' in newPlugin) {
newPlugin.dependencies.forEach(dependency => {
const scriptEle = document.createElement('script');
scriptEle.setAttribute('src', dependency);
document.body.appendChild(scriptEle);
scriptEle.addEventListener('load', () => {
console.log('File loaded', dependency);
});

scriptEle.addEventListener('error', ev => {
console.log('Error on loading file', ev);
});
scriptEle.addEventListener('error', ev => {
console.log('Error on loading file', ev);
});
}
registerPlugin(newPlugin);
});
}
);
});
} catch (e) {
console.error(gistFile.filename, 'Plugin failed to load', e);
}
registerPlugin(newPlugin);
}
);
});
} catch (e) {
console.error(gistFile.filename, 'Plugin failed to load', e);
}
});
}
});
}
}

dbStorage.getDbValue('cachedPlugins').then(cachedPluginsFromStorage => {
console.log({cachedPluginsFromStorage})
// console.log({cachedPluginsFromStorageParsed: JSON.parse(cachedPluginsFromStorage)})
});
console.log("Get plugins:", app.settings.gistPluginsFile())
// register plugins stored on a gist - todo cache all this
if (app.settings.gistPluginsFile() !== null) {
app.data.storage.getGist(app.settings.gistPluginsFile()).then(({filesInGist}) => {
console.log({ filesInGist });

loadPluginsFromCacheOrGist(Object.values(filesInGist));
});
}
};

0 comments on commit c0a7abe

Please sign in to comment.