From abb85b3f115e3465f77e75c7e6d345993db09cc3 Mon Sep 17 00:00:00 2001 From: Mo Date: Fri, 22 Apr 2022 13:54:34 -0500 Subject: [PATCH] refactor: improve device interface types (#996) --- app/assets/javascripts/App.tsx | 12 +-- .../Components/ApplicationView/index.tsx | 7 +- .../Components/ComponentView/index.tsx | 4 +- .../Components/ConfirmSignoutModal/index.tsx | 8 +- .../Components/NoteView/NoteView.tsx | 2 +- .../Device/DesktopDeviceInterface.ts | 11 +++ .../javascripts/Device/DesktopSnjsExports.ts | 1 + .../Device/DesktopWebCommunication.ts | 44 +++++++++ .../{ => Device}/StartApplication.ts | 4 +- app/assets/javascripts/Device/WebDevice.ts | 26 +++++ .../WebOrDesktopDevice.ts} | 95 +++++++++++++------ .../Device/WebOrDesktopDeviceInterface.ts | 9 ++ app/assets/javascripts/Services/Bridge.ts | 37 -------- .../javascripts/Services/BrowserBridge.ts | 42 -------- .../javascripts/Services/DesktopManager.ts | 60 ++++++------ .../javascripts/Services/ThemeManager.ts | 8 +- .../javascripts/UIModels/AppState/AppState.ts | 8 +- .../UIModels/AppState/NotesViewState.ts | 15 ++- .../javascripts/UIModels/Application.ts | 35 ++++--- .../javascripts/UIModels/ApplicationGroup.ts | 27 +++--- package.json | 8 +- yarn.lock | 68 ++++++------- 22 files changed, 296 insertions(+), 235 deletions(-) create mode 100644 app/assets/javascripts/Device/DesktopDeviceInterface.ts create mode 100644 app/assets/javascripts/Device/DesktopSnjsExports.ts create mode 100644 app/assets/javascripts/Device/DesktopWebCommunication.ts rename app/assets/javascripts/{ => Device}/StartApplication.ts (62%) create mode 100644 app/assets/javascripts/Device/WebDevice.ts rename app/assets/javascripts/{WebDeviceInterface.ts => Device/WebOrDesktopDevice.ts} (58%) create mode 100644 app/assets/javascripts/Device/WebOrDesktopDeviceInterface.ts delete mode 100644 app/assets/javascripts/Services/Bridge.ts delete mode 100644 app/assets/javascripts/Services/BrowserBridge.ts diff --git a/app/assets/javascripts/App.tsx b/app/assets/javascripts/App.tsx index 0cb0d0f49..a4df70848 100644 --- a/app/assets/javascripts/App.tsx +++ b/app/assets/javascripts/App.tsx @@ -20,15 +20,15 @@ import { IsWebPlatform, WebAppVersion } from '@/Version' import { Runtime, SNLog } from '@standardnotes/snjs' import { render } from 'preact' import { ApplicationGroupView } from './Components/ApplicationGroupView' -import { Bridge } from './Services/Bridge' -import { BrowserBridge } from './Services/BrowserBridge' -import { StartApplication } from './StartApplication' +import { WebDevice } from './Device/WebDevice' +import { StartApplication } from './Device/StartApplication' import { ApplicationGroup } from './UIModels/ApplicationGroup' import { isDev } from './Utils' +import { WebOrDesktopDevice } from './Device/WebOrDesktopDevice' const startApplication: StartApplication = async function startApplication( defaultSyncServerHost: string, - bridge: Bridge, + device: WebOrDesktopDevice, enableUnfinishedFeatures: boolean, webSocketUrl: string, ) { @@ -37,7 +37,7 @@ const startApplication: StartApplication = async function startApplication( const mainApplicationGroup = new ApplicationGroup( defaultSyncServerHost, - bridge, + device, enableUnfinishedFeatures ? Runtime.Dev : Runtime.Prod, webSocketUrl, ) @@ -70,7 +70,7 @@ const startApplication: StartApplication = async function startApplication( if (IsWebPlatform) { startApplication( window.defaultSyncServer, - new BrowserBridge(WebAppVersion), + new WebDevice(WebAppVersion), window.enabledUnfinishedFeatures, window.websocketUrl, ).catch(console.error) diff --git a/app/assets/javascripts/Components/ApplicationView/index.tsx b/app/assets/javascripts/Components/ApplicationView/index.tsx index 9fe5481f4..af0eb42ac 100644 --- a/app/assets/javascripts/Components/ApplicationView/index.tsx +++ b/app/assets/javascripts/Components/ApplicationView/index.tsx @@ -59,7 +59,11 @@ export class ApplicationView extends PureComponent { } async loadApplication() { - this.application.componentManager.setDesktopManager(this.application.getDesktopService()) + const desktopService = this.application.getDesktopService() + if (desktopService) { + this.application.componentManager.setDesktopManager(desktopService) + } + await this.application.prepareForLaunch({ receiveChallenge: async (challenge) => { const challenges = this.state.challenges.slice() @@ -67,6 +71,7 @@ export class ApplicationView extends PureComponent { this.setState({ challenges: challenges }) }, }) + await this.application.launch() } diff --git a/app/assets/javascripts/Components/ComponentView/index.tsx b/app/assets/javascripts/Components/ComponentView/index.tsx index c12bb4183..826649e2e 100644 --- a/app/assets/javascripts/Components/ComponentView/index.tsx +++ b/app/assets/javascripts/Components/ComponentView/index.tsx @@ -168,14 +168,14 @@ export const ComponentView: FunctionalComponent = observer( useEffect(() => { const unregisterDesktopObserver = application .getDesktopService() - .registerUpdateObserver((updatedComponent: SNComponent) => { + ?.registerUpdateObserver((updatedComponent: SNComponent) => { if (updatedComponent.uuid === component.uuid && updatedComponent.active) { requestReload?.(componentViewer) } }) return () => { - unregisterDesktopObserver() + unregisterDesktopObserver?.() } }, [application, requestReload, componentViewer, component.uuid]) diff --git a/app/assets/javascripts/Components/ConfirmSignoutModal/index.tsx b/app/assets/javascripts/Components/ConfirmSignoutModal/index.tsx index 132b42574..ff5fe6c48 100644 --- a/app/assets/javascripts/Components/ConfirmSignoutModal/index.tsx +++ b/app/assets/javascripts/Components/ConfirmSignoutModal/index.tsx @@ -28,8 +28,8 @@ export const ConfirmSignoutModal = observer(({ application, appState }: Props) = const [localBackupsCount, setLocalBackupsCount] = useState(0) useEffect(() => { - application.bridge.localBackupsCount().then(setLocalBackupsCount).catch(console.error) - }, [appState.accountMenu.signingOut, application.bridge]) + application.desktopDevice?.localBackupsCount().then(setLocalBackupsCount).catch(console.error) + }, [appState.accountMenu.signingOut, application.desktopDevice]) return ( @@ -44,6 +44,7 @@ export const ConfirmSignoutModal = observer(({ application, appState }: Props) =

{STRING_SIGN_OUT_CONFIRMATION}

+ {localBackupsCount > 0 && (
@@ -63,13 +64,14 @@ export const ConfirmSignoutModal = observer(({ application, appState }: Props) =
)} +