From e272adcc3f32e31fe7668551453b8e34bc823c3e Mon Sep 17 00:00:00 2001 From: vnugent Date: Wed, 22 Nov 2023 15:07:08 -0500 Subject: feature and internal api polish --- extension/src/features/identity-api.ts | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'extension/src/features/identity-api.ts') diff --git a/extension/src/features/identity-api.ts b/extension/src/features/identity-api.ts index d7db5ff..a8ac4e6 100644 --- a/extension/src/features/identity-api.ts +++ b/extension/src/features/identity-api.ts @@ -13,7 +13,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -import { Endpoints, useServerApi } from "./server-api"; +import { Endpoints } from "./server-api"; import { NostrPubKey, Watchable } from "./types"; import { type FeatureApi, @@ -26,8 +26,9 @@ import { import { AppSettings } from "./settings"; import { shallowRef, watch } from "vue"; import { useSession } from "@vnuge/vnlib.browser"; -import { set, useToggle, watchOnce } from "@vueuse/core"; +import { set, useToggle } from "@vueuse/core"; import { defer, isArray } from "lodash"; +import { waitForChange, waitForChangeFn } from "./util"; export interface IdentityApi extends FeatureApi, Watchable { createIdentity: (identity: NostrPubKey) => Promise @@ -41,13 +42,13 @@ export interface IdentityApi extends FeatureApi, Watchable { export const useIdentityApi = (): IFeatureExport => { return{ background: ({ state }: BgRuntime) =>{ - const { execRequest } = useServerApi(state); + const { execRequest } = state.useServerApi(); const { loggedIn } = useSession(); //Get the current selected key const selectedKey = shallowRef(); const allKeys = shallowRef([]); - const [ onTriggered , triggerChange ] = useToggle() + const [ onKeyUpdateTriggered , triggerKeyUpdate ] = useToggle() const keyLoadWatchLoop = async () => { while(true){ @@ -62,7 +63,7 @@ export const useIdentityApi = (): IFeatureExport => { } //Wait for changes to trigger a new key-load - await new Promise((resolve) => watchOnce([onTriggered, loggedIn] as any, () => resolve(null))) + await waitForChange([loggedIn, onKeyUpdateTriggered]) } } @@ -74,16 +75,18 @@ export const useIdentityApi = (): IFeatureExport => { return { //Identity is only available in options context createIdentity: optionsOnly(async (id: NostrPubKey) => { - await execRequest(Endpoints.CreateId, id) - triggerChange() + const newKey = await execRequest(Endpoints.CreateId, id) + triggerKeyUpdate() + return newKey }), updateIdentity: optionsOnly(async (id: NostrPubKey) => { - await execRequest(Endpoints.UpdateId, id) - triggerChange() + const updated = await execRequest(Endpoints.UpdateId, id) + triggerKeyUpdate() + return updated }), deleteIdentity: optionsOnly(async (key: NostrPubKey) => { await execRequest(Endpoints.DeleteKey, key); - triggerChange() + triggerKeyUpdate() }), selectKey: popupAndOptionsOnly((key: NostrPubKey): Promise => { set(selectedKey, key); @@ -95,10 +98,8 @@ export const useIdentityApi = (): IFeatureExport => { getPublicKey: (): Promise => { return Promise.resolve(selectedKey.value); }, - waitForChange: () => { - return new Promise((resolve) => watchOnce([selectedKey, loggedIn, onTriggered] as any, () => resolve())) - } - } + waitForChange: waitForChangeFn([selectedKey, loggedIn, allKeys]) + } }, foreground: exportForegroundApi([ 'createIdentity', -- cgit