Skip to content

Commit

Permalink
feat: pin cache fn getAllPlugins
Browse files Browse the repository at this point in the history
  • Loading branch information
tachibana-shin committed Dec 10, 2023
1 parent 18142af commit 9d86f8f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/composables/use-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function useModule<R>(module: () => R): R {
return module()
}
57 changes: 35 additions & 22 deletions src/stores/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,44 @@ export const usePluginStore = defineStore("plugin", () => {
"remove plugin": [string]
}>()

async function getAllPlugins() {
const files = await Filesystem.readdir({
path: "plugins",
directory: Directory.External
})
.then(({ files }) => files)
.catch(() => [])

return Promise.all(
files.map(async (file) => {
const meta = await Filesystem.readFile({
path: `plugins/${file.name}`,
directory: Directory.External,
encoding: Encoding.UTF8
}).then(({ data }) => {
const plugin = JSON.parse(data)
delete plugin.plugin
const getAllPlugins = useModule(() => {
async function _getAllPlugins() {
const files = await Filesystem.readdir({
path: "plugins",
directory: Directory.External
})
.then(({ files }) => files)
.catch(() => [])

return Promise.all(
files.map(async (file) => {
const meta = await Filesystem.readFile({
path: `plugins/${file.name}`,
directory: Directory.External,
encoding: Encoding.UTF8
}).then(({ data }) => {
const plugin = JSON.parse(data)
delete plugin.plugin

return plugin as Omit<PackageDisk, "plugin">
})

return plugin as Omit<PackageDisk, "plugin">
return meta
})
).then((res) => res.filter(Boolean))
}

return meta
})
).then((res) => res.filter(Boolean))
}
let promiseGetAllPlugins: ReturnType<typeof _getAllPlugins>
async function getAllPlugins() {
if (promiseGetAllPlugins) return promiseGetAllPlugins

promiseGetAllPlugins = _getAllPlugins()

return promiseGetAllPlugins
}

return getAllPlugins
})

async function installPlugin(source: string, devMode: boolean) {
const [packageMjs, pluginMjs] = await Promise.all([
Expand Down

0 comments on commit 9d86f8f

Please sign in to comment.