plugins: api.subscribeConfig

Massimo Melina committed Jun 7, 2022 at 01:20 UTC bee1e622e2475c56dbaa5feade5efb43f327672e
3 files changed +13 -2
dev-plugins.md
+3
@@ -102,6 +102,9 @@ The `api` object you get as parameter of the `init` contains the following:
102
103 - `setConfig(key: string, value: any)` set config's value set up by using `exports.config`.
104
105 +- `subscribeConfig(key: string, callback: (value: any) => void): Unsubscriber`
106 + will call `callback` with initial value and then at each change.
107 +
108 - `getHfsConfig(key: string): any` similar to getConfig, but retrieves HFS' config instead.
109
110 - `log(...args)` print log in a standard form for plugins.
server/src/plugins.ts
+10 -1
@@ -7,7 +7,7 @@ import pathLib from 'path'
7 import { API_VERSION, COMPATIBLE_API_VERSION, PLUGINS_PUB_URI } from './const'
8 import * as Const from './const'
9 import Koa from 'koa'
10 -import { debounceAsync, Dict, getOrSet, onProcessExit, same, tryJson, wantArray, watchDir } from './misc'
10 +import { Callback, debounceAsync, Dict, getOrSet, onProcessExit, same, tryJson, wantArray, watchDir } from './misc'
11 import { defineConfig, getConfig } from './config'
12 import { DirEntry } from './api.file_list'
13 import { VfsNode } from './vfs'
@@ -232,6 +232,15 @@ export async function rescan() {
232 pluginsConfig.get()?.[id]?.[cfgKey] ?? data.config?.[cfgKey]?.defaultValue,
233 setConfig: (cfgKey: string, value: any) =>
234 setPluginConfig(id, { [cfgKey]: value }),
235 + subscribeConfig(cfgKey: string, cb: Callback<any>) {
236 + let last = this.getConfig(cfgKey)
237 + cb(last)
238 + return pluginsConfig.sub(() => {
239 + const now = this.getConfig(cfgKey)
240 + if (!same(now, last))
241 + cb(last = now)
242 + })
243 + },
244 getHfsConfig: getConfig,
245 })
246 Object.assign(data, res)
todo.md
-1
@@ -3,7 +3,6 @@
3 - use dialogs instead of side-forms on mobile (admin/fs+accounts)
4 - easier deploy on cloud server: --create-admin <password>
5 - admin/fs: sort items
6 -- plugin.api.subscribeConfig
6 - admin/fs: render virtual folders differently
7 - admin/config: hide advanced settings
8 - admin/fs: drag&drop to move items around