From 8f0f69f144c8edf64e38dbb768935fcd89719ec4 Mon Sep 17 00:00:00 2001 From: Denis Bunakalya Date: Mon, 27 Feb 2023 06:03:03 +0300 Subject: [PATCH] Make visibleIf async in SpecialNavModel (#2684) Signed-off-by: Denis Bunakalya --- models/chunter/src/plugin.ts | 2 +- models/workbench/src/plugin.ts | 2 +- plugins/chunter-resources/src/index.ts | 2 +- .../src/components/Navigator.svelte | 35 +++++++++++-------- plugins/workbench-resources/src/index.ts | 2 +- plugins/workbench/src/index.ts | 2 +- 6 files changed, 26 insertions(+), 19 deletions(-) diff --git a/models/chunter/src/plugin.ts b/models/chunter/src/plugin.ts index 4029b3f777..3c296214cd 100644 --- a/models/chunter/src/plugin.ts +++ b/models/chunter/src/plugin.ts @@ -90,7 +90,7 @@ export default mergeIds(chunterId, chunter, { Random: '' as Ref }, function: { - ChunterBrowserVisible: '' as Resource<(spaces: Space[]) => boolean> + ChunterBrowserVisible: '' as Resource<(spaces: Space[]) => Promise> }, filter: { CommentsFilter: '' as Resource<(tx: DisplayTx, _class?: Ref) => boolean>, diff --git a/models/workbench/src/plugin.ts b/models/workbench/src/plugin.ts index 8b44c73fa7..8f406bc511 100644 --- a/models/workbench/src/plugin.ts +++ b/models/workbench/src/plugin.ts @@ -32,6 +32,6 @@ export default mergeIds(workbenchId, workbench, { HiddenApplication: '' as IntlString }, function: { - HasArchiveSpaces: '' as Resource<(spaces: Space[]) => boolean> + HasArchiveSpaces: '' as Resource<(spaces: Space[]) => Promise> } }) diff --git a/plugins/chunter-resources/src/index.ts b/plugins/chunter-resources/src/index.ts index 8f3545981c..e23ce9498a 100644 --- a/plugins/chunter-resources/src/index.ts +++ b/plugins/chunter-resources/src/index.ts @@ -187,7 +187,7 @@ export async function DeleteMessageFromSaved (message: ChunterMessage): Promise< export const userSearch = writable('') -export function chunterBrowserVisible (spaces: Space[]): boolean { +export async function chunterBrowserVisible (spaces: Space[]): Promise { return false } diff --git a/plugins/workbench-resources/src/components/Navigator.svelte b/plugins/workbench-resources/src/components/Navigator.svelte index 3b6ba0bbbc..cd8dcc1d61 100644 --- a/plugins/workbench-resources/src/components/Navigator.svelte +++ b/plugins/workbench-resources/src/components/Navigator.svelte @@ -107,20 +107,27 @@ requestIndex: number ): Promise<[Map, number]> { const result = new Map() - const promises = specials.map(async (sp) => { - const pos = sp.position ?? 'top' - let visible = true - if (sp.visibleIf !== undefined) { - const f = await getResource(sp.visibleIf) - visible = f(spaces) - } - if (visible) { - const list = result.get(pos) ?? [] - list.push(sp) - result.set(pos, list) - } - }) - await Promise.all(promises) + + const spHandlers = await Promise.all( + specials.map(async (sp) => { + const pos = sp.position ?? 'top' + let visible = true + if (sp.visibleIf !== undefined) { + const f = await getResource(sp.visibleIf) + visible = await f(spaces) + } + + return () => { + if (visible) { + const list = result.get(pos) ?? [] + list.push(sp) + result.set(pos, list) + } + } + }) + ) + spHandlers.forEach((spHandler) => spHandler()) + return [result, requestIndex] } diff --git a/plugins/workbench-resources/src/index.ts b/plugins/workbench-resources/src/index.ts index aeff72fb9c..cb2f6b12e7 100644 --- a/plugins/workbench-resources/src/index.ts +++ b/plugins/workbench-resources/src/index.ts @@ -23,7 +23,7 @@ import SpecialView from './components/SpecialView.svelte' import WorkbenchApp from './components/WorkbenchApp.svelte' import { doNavigate } from './utils' -function hasArchiveSpaces (spaces: Space[]): boolean { +async function hasArchiveSpaces (spaces: Space[]): Promise { return spaces.find((sp) => sp.archived) !== undefined } export { default as SpaceBrowser } from './components/SpaceBrowser.svelte' diff --git a/plugins/workbench/src/index.ts b/plugins/workbench/src/index.ts index e63cf00f48..44e7ded845 100644 --- a/plugins/workbench/src/index.ts +++ b/plugins/workbench/src/index.ts @@ -79,7 +79,7 @@ export interface SpecialNavModel { componentProps?: Record // If not top and bottom, position will be sorted alphabetically. position?: 'top' | 'bottom' | string // undefined == 'top - visibleIf?: Resource<(spaces: Space[]) => boolean> + visibleIf?: Resource<(spaces: Space[]) => Promise> // If defined, will be used to find spaces for visibleIf spaceClass?: Ref> }