Skip to content

Commit

Permalink
feat: improve bridge and exported types
Browse files Browse the repository at this point in the history
  • Loading branch information
arielsvg committed Nov 10, 2020
1 parent f3b4503 commit 016b730
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 49 deletions.
25 changes: 15 additions & 10 deletions app/assets/javascripts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,16 @@ import {

import { trusted } from './filters';
import { isDev } from './utils';
import { Bridge, BrowserBridge } from './services/bridge';
import { BrowserBridge } from './services/browserBridge';
import { startErrorReporting } from './services/errorReporting';
import { alertDialog } from './services/alertService';
import { StartApplication } from './startApplication';
import { Bridge } from './services/bridge';

if (__WEB__) {
startApplication((window as any)._default_sync_server, new BrowserBridge());
} else {
(window as any).startApplication = startApplication;
}

async function startApplication(defaultSyncServerHost: string, bridge: Bridge) {
const startApplication: StartApplication = async function startApplication(
defaultSyncServerHost: string,
bridge: Bridge
) {
notifyBetaPeriodEnd();

SNLog.onLog = console.log;
Expand All @@ -77,7 +76,7 @@ async function startApplication(defaultSyncServerHost: string, bridge: Bridge) {
.config(configRoutes)
.constant('bridge', bridge)
.constant('defaultSyncServerHost', defaultSyncServerHost)
.constant('appVersion', __VERSION__);
.constant('appVersion', bridge.appVersion);

// Controllers
angular
Expand Down Expand Up @@ -149,7 +148,7 @@ async function startApplication(defaultSyncServerHost: string, bridge: Bridge) {
angular.element(document).ready(() => {
angular.bootstrap(document, ['app']);
});
}
};

function notifyBetaPeriodEnd() {
if (window.location.hostname === 'app-beta.standardnotes.org') {
Expand All @@ -163,3 +162,9 @@ function notifyBetaPeriodEnd() {
});
}
}

if (__WEB__) {
startApplication((window as any)._default_sync_server, new BrowserBridge(__VERSION__));
} else {
(window as any).startApplication = startApplication;
}
46 changes: 9 additions & 37 deletions app/assets/javascripts/services/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,23 @@
import { PurePayload, Environment } from "snjs";
/**
* This file will be imported by desktop, so we make sure imports are not
* carrying too much code with them that's not tree-shakeable.
*/
import { Environment } from 'snjs/lib/platforms';
export { Environment };

/** Platform-specific (i-e Electron/browser) behavior is handled by a Bridge object. */
export interface Bridge {
environment: Environment,
readonly appVersion: string;
environment: Environment;

getKeychainValue(): Promise<unknown>;
setKeychainValue(value: any): Promise<void>;
clearKeychainValue(): Promise<void>;

extensionsServerHost?: string;
syncComponents(payloads: PurePayload[]): void;
syncComponents(payloads: unknown[]): void;
onMajorDataChange(): void;
onInitialDataLoad(): void;
onSearch(text?: string): void;
downloadBackup(): void;
}

const KEYCHAIN_STORAGE_KEY = 'keychain';

export class BrowserBridge implements Bridge {
environment = Environment.Web;

async getKeychainValue(): Promise<unknown> {
const value = localStorage.getItem(KEYCHAIN_STORAGE_KEY);
if (value) {
return JSON.parse(value);
}
}

async setKeychainValue(value: any): Promise<void> {
localStorage.setItem(KEYCHAIN_STORAGE_KEY, JSON.stringify(value));
}

async clearKeychainValue(): Promise<void> {
localStorage.removeItem(KEYCHAIN_STORAGE_KEY);
}

/** No-ops */

syncComponents() {
}
onMajorDataChange() {
}
onInitialDataLoad() {
}
onSearch() {
}
downloadBackup() {
}
}
32 changes: 32 additions & 0 deletions app/assets/javascripts/services/browserBridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Bridge } from "./bridge";
import { Environment } from 'snjs';

const KEYCHAIN_STORAGE_KEY = 'keychain';

export class BrowserBridge implements Bridge {
constructor(public appVersion: string) {}
environment = Environment.Web;

async getKeychainValue(): Promise<unknown> {
const value = localStorage.getItem(KEYCHAIN_STORAGE_KEY);
if (value) {
return JSON.parse(value);
}
}

async setKeychainValue(value: any): Promise<void> {
localStorage.setItem(KEYCHAIN_STORAGE_KEY, JSON.stringify(value));
}

async clearKeychainValue(): Promise<void> {
localStorage.removeItem(KEYCHAIN_STORAGE_KEY);
}

/** No-ops */

syncComponents() {}
onMajorDataChange() {}
onInitialDataLoad() {}
onSearch() {}
downloadBackup() {}
}
6 changes: 6 additions & 0 deletions app/assets/javascripts/startApplication.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Bridge } from "./services/bridge";

export type StartApplication = (
defaultSyncServerHost: string,
bridge: Bridge
) => Promise<void>;
3 changes: 2 additions & 1 deletion dist/@types/app/assets/javascripts/app.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {};
import { Bridge } from './services/bridge';
export declare type StartApplication = (defaultSyncServerHost: string, bridge: Bridge) => Promise<void>;
5 changes: 4 additions & 1 deletion dist/@types/app/assets/javascripts/services/bridge.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PurePayload, Environment } from "snjs";
import { PurePayload, Environment } from 'snjs';
/** Platform-specific (i-e Electron/browser) behavior is handled by a Bridge object. */
export interface Bridge {
readonly appVersion: string;
environment: Environment;
getKeychainValue(): Promise<unknown>;
setKeychainValue(value: any): Promise<void>;
Expand All @@ -13,6 +14,8 @@ export interface Bridge {
downloadBackup(): void;
}
export declare class BrowserBridge implements Bridge {
appVersion: string;
constructor(appVersion: string);
environment: Environment;
getKeychainValue(): Promise<unknown>;
setKeychainValue(value: any): Promise<void>;
Expand Down

0 comments on commit 016b730

Please sign in to comment.