From 13e77636a960bb1af05428e9d13a37f21ac0c6b9 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 22 Dec 2021 17:48:08 +0100 Subject: [PATCH] export paths from vite.js as required by Platform, reorder ctor params make it easier for SDK users --- doc/SDK.md | 3 ++- src/platform/web/LegacyPlatform.js | 4 ++-- src/platform/web/Platform.js | 21 +++++++++++---------- src/platform/web/index.html | 14 ++++---------- src/platform/web/sdk/paths/vite.js | 15 ++++++++------- 5 files changed, 27 insertions(+), 30 deletions(-) diff --git a/doc/SDK.md b/doc/SDK.md index 87e7c897..d92b99c6 100644 --- a/doc/SDK.md +++ b/doc/SDK.md @@ -38,7 +38,8 @@ import "hydrogen-view-sdk/style.css"; async function main() { const app = document.querySelector('#app')! - const platform = new Platform(app, assetPaths, { development: import.meta.env.DEV }); + const config = {}; + const platform = new Platform(app, assetPaths, config, { development: import.meta.env.DEV }); const navigation = createNavigation(); platform.setNavigation(navigation); const urlRouter = createRouter({ diff --git a/src/platform/web/LegacyPlatform.js b/src/platform/web/LegacyPlatform.js index fbf74b81..85632bf2 100644 --- a/src/platform/web/LegacyPlatform.js +++ b/src/platform/web/LegacyPlatform.js @@ -19,6 +19,6 @@ import {hkdf} from "../../utils/crypto/hkdf"; import {Platform as ModernPlatform} from "./Platform.js"; -export function Platform(container, paths) { - return new ModernPlatform(container, paths, {aesjs, hkdf}); +export function Platform(container, assetPaths, config, options = null) { + return new ModernPlatform(container, assetPaths, config, options, {aesjs, hkdf}); } diff --git a/src/platform/web/Platform.js b/src/platform/web/Platform.js index 7c73e66f..9de3d4ce 100644 --- a/src/platform/web/Platform.js +++ b/src/platform/web/Platform.js @@ -76,12 +76,12 @@ function assetAbsPath(assetPath) { return assetPath; } -async function loadOlmWorker(config) { - const workerPool = new WorkerPool(config.worker, 4); +async function loadOlmWorker(assetPaths) { + const workerPool = new WorkerPool(assetPaths.worker, 4); await workerPool.init(); await workerPool.sendAll({ type: "load_olm", - path: assetAbsPath(config.olm.legacyBundle) + path: assetAbsPath(assetPaths.olm.legacyBundle) }); const olmWorker = new OlmWorker(workerPool); return olmWorker; @@ -126,9 +126,10 @@ function adaptUIOnVisualViewportResize(container) { } export class Platform { - constructor(container, config, cryptoExtras = null, options = null) { - this._config = config; + constructor(container, assetPaths, config, options = null, cryptoExtras = null) { this._container = container; + this._assetPaths = assetPaths; + this._config = config; this.settingsStorage = new SettingsStorage("hydrogen_setting_v1_"); this.clock = new Clock(); this.encoding = new Encoding(); @@ -137,9 +138,9 @@ export class Platform { this.history = new History(); this.onlineStatus = new OnlineStatus(); this._serviceWorkerHandler = null; - if (config.serviceWorker && "serviceWorker" in navigator) { + if (assetPaths.serviceWorker && "serviceWorker" in navigator) { this._serviceWorkerHandler = new ServiceWorkerHandler(); - this._serviceWorkerHandler.registerAndStart(config.serviceWorker); + this._serviceWorkerHandler.registerAndStart(assetPaths.serviceWorker); } this.notificationService = new NotificationService(this._serviceWorkerHandler, config.push); this.crypto = new Crypto(cryptoExtras); @@ -182,7 +183,7 @@ export class Platform { loadOlm() { if (!this._olmPromise) { - this._olmPromise = loadOlm(this._config.olm); + this._olmPromise = loadOlm(this._assetPaths.olm); } return this._olmPromise; } @@ -194,7 +195,7 @@ export class Platform { async loadOlmWorker() { if (!window.WebAssembly) { if (!this._workerPromise) { - this._workerPromise = loadOlmWorker(this._config); + this._workerPromise = loadOlmWorker(this._assetPaths); } return this._workerPromise; } @@ -230,7 +231,7 @@ export class Platform { if (navigator.msSaveBlob) { navigator.msSaveBlob(blobHandle.nativeBlob, filename); } else { - downloadInIframe(this._container, this._config.downloadSandbox, blobHandle, filename, this.isIOS); + downloadInIframe(this._container, this._assetPaths.downloadSandbox, blobHandle, filename, this.isIOS); } } diff --git a/src/platform/web/index.html b/src/platform/web/index.html index f7aeeed7..0e993992 100644 --- a/src/platform/web/index.html +++ b/src/platform/web/index.html @@ -19,20 +19,14 @@ import {main} from "./main"; import {Platform} from "./Platform"; import configJSON from "./assets/config.json?raw"; - import {olmPaths, downloadSandboxPath, workerPath} from "./sdk/paths/vite"; - const paths = { - olm: olmPaths, - downloadSandbox: downloadSandboxPath, - worker: workerPath, - ...JSON.parse(configJSON) - }; + import assetPaths from "./sdk/paths/vite"; if (import.meta.env.PROD) { - paths.serviceWorker = "sw.js"; + assetPaths.serviceWorker = "sw.js"; } const platform = new Platform( document.body, - paths, - null, + assetPaths, + JSON.parse(configJSON), {development: import.meta.env.DEV} ); main(platform); diff --git a/src/platform/web/sdk/paths/vite.js b/src/platform/web/sdk/paths/vite.js index 6739f8d8..48a17da4 100644 --- a/src/platform/web/sdk/paths/vite.js +++ b/src/platform/web/sdk/paths/vite.js @@ -9,11 +9,12 @@ import olmJsPath from "@matrix-org/olm/olm.js?url"; // @ts-ignore import olmLegacyJsPath from "@matrix-org/olm/olm_legacy.js?url"; -export const olmPaths = { - wasm: olmWasmPath, - legacyBundle: olmLegacyJsPath, - wasmBundle: olmJsPath, +export default { + downloadSandbox: _downloadSandboxPath, + worker: _workerPath, + olm: { + wasm: olmWasmPath, + legacyBundle: olmLegacyJsPath, + wasmBundle: olmJsPath, + } }; - -export const downloadSandboxPath = _downloadSandboxPath; -export const workerPath = _workerPath;