plugins: watchState third parameter

Massimo Melina committed Mar 22, 2025 at 10:38 UTC f5a69f7d453dba2925e70872d1f9801449f7acc2
2 files changed +9 -6
dev-plugins.md
+4 -4
@@ -334,9 +334,10 @@ The HFS object contains many properties:
334 - `prefixUrl: string` normally an empty string, it will be set in case a [reverse-proxy wants to mount HFS on a path](https://github.com/rejetto/hfs/wiki/Reverse-proxy).
335 - `state: StateObject` [object with many values in it](https://github.com/rejetto/hfs/blob/main/frontend/src/state.ts)
336 - you'll find here some interesting values, like `username` and `loading`.
337 -- `watchState(key: string, callback): function`
337 +- `watchState(key: string, callback, now?: boolean): function`
338 - watch the `key` property of the state object above
339 - `callback(newValue)` will be called at each change
340 + - pass `true` for the third parameter to also call the callback immediately, with current value
341 - use returned callback to stop watching
342 - `useSnapState(): StateObject` React hook version of the `state` object above
343 - `React` whole React object, as for `require('react')` (JSX syntax is not supported here)
@@ -409,9 +410,7 @@ This is a list of available frontend-events, with respective object parameter an
410
411 - `additionalEntryDetails`
412 - you receive each entry of the list, and optionally produce HTML code that will be added in the `entry-details` container.
412 - - parameter `{ entry: DirEntry }`
413 -
414 - The `DirEntry` type is an object with the following properties:
413 + - parameter `{ entry: DirEntry }` current entry. The `DirEntry` type is an object with the following properties:
414 - `name: string` name of the entry.
415 - `ext: string` just the extension part of the name, dot excluded and lowercase.
416 - `isFolder: boolean` true if it's a folder.
@@ -806,6 +805,7 @@ If you want to override a text regardless of the language, use the special langu
805 - removed DirEntry.t
806 - api.setInterval, setTimeout
807 - HFS.Btn
808 + - HFS.watchState added third parameter
809 - 11.6 (v0.56.0)
810 - api.setError
811 - frontend events: afterBreadcrumbs, afterFolderStats, afterFilter
frontend/src/misc.ts
+5 -2
@@ -101,9 +101,12 @@ Object.assign(getHFS(), {
101 misc: { ...cross, ...shared },
102 emit: hfsEvent,
103 onEvent: onHfsEvent,
104 - watchState(k: string, cb: (v: any) => void) {
104 + watchState(k: string, cb: (v: any) => void, callNow=false) {
105 const up = k.split('upload.')[1]
106 - return subscribeKey(up ? uploadState : state as any, up || k, cb, true)
106 + const thisState = up ? uploadState : state as any
107 + if (callNow)
108 + cb(thisState[k])
109 + return subscribeKey(thisState, up || k, cb, true)
110 },
111 customRestCall(name: string, ...rest: any[]) {
112 return apiCall(cross.PLUGIN_CUSTOM_REST_PREFIX + name, ...rest)