export paths from vite.js as required by Platform, reorder ctor params

make it easier for SDK users
This commit is contained in:
Bruno Windels 2021-12-22 17:48:08 +01:00
parent 6247ced7b7
commit 13e77636a9
5 changed files with 27 additions and 30 deletions

View file

@ -38,7 +38,8 @@ import "hydrogen-view-sdk/style.css";
async function main() { async function main() {
const app = document.querySelector<HTMLDivElement>('#app')! const app = document.querySelector<HTMLDivElement>('#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(); const navigation = createNavigation();
platform.setNavigation(navigation); platform.setNavigation(navigation);
const urlRouter = createRouter({ const urlRouter = createRouter({

View file

@ -19,6 +19,6 @@ import {hkdf} from "../../utils/crypto/hkdf";
import {Platform as ModernPlatform} from "./Platform.js"; import {Platform as ModernPlatform} from "./Platform.js";
export function Platform(container, paths) { export function Platform(container, assetPaths, config, options = null) {
return new ModernPlatform(container, paths, {aesjs, hkdf}); return new ModernPlatform(container, assetPaths, config, options, {aesjs, hkdf});
} }

View file

@ -76,12 +76,12 @@ function assetAbsPath(assetPath) {
return assetPath; return assetPath;
} }
async function loadOlmWorker(config) { async function loadOlmWorker(assetPaths) {
const workerPool = new WorkerPool(config.worker, 4); const workerPool = new WorkerPool(assetPaths.worker, 4);
await workerPool.init(); await workerPool.init();
await workerPool.sendAll({ await workerPool.sendAll({
type: "load_olm", type: "load_olm",
path: assetAbsPath(config.olm.legacyBundle) path: assetAbsPath(assetPaths.olm.legacyBundle)
}); });
const olmWorker = new OlmWorker(workerPool); const olmWorker = new OlmWorker(workerPool);
return olmWorker; return olmWorker;
@ -126,9 +126,10 @@ function adaptUIOnVisualViewportResize(container) {
} }
export class Platform { export class Platform {
constructor(container, config, cryptoExtras = null, options = null) { constructor(container, assetPaths, config, options = null, cryptoExtras = null) {
this._config = config;
this._container = container; this._container = container;
this._assetPaths = assetPaths;
this._config = config;
this.settingsStorage = new SettingsStorage("hydrogen_setting_v1_"); this.settingsStorage = new SettingsStorage("hydrogen_setting_v1_");
this.clock = new Clock(); this.clock = new Clock();
this.encoding = new Encoding(); this.encoding = new Encoding();
@ -137,9 +138,9 @@ export class Platform {
this.history = new History(); this.history = new History();
this.onlineStatus = new OnlineStatus(); this.onlineStatus = new OnlineStatus();
this._serviceWorkerHandler = null; this._serviceWorkerHandler = null;
if (config.serviceWorker && "serviceWorker" in navigator) { if (assetPaths.serviceWorker && "serviceWorker" in navigator) {
this._serviceWorkerHandler = new ServiceWorkerHandler(); this._serviceWorkerHandler = new ServiceWorkerHandler();
this._serviceWorkerHandler.registerAndStart(config.serviceWorker); this._serviceWorkerHandler.registerAndStart(assetPaths.serviceWorker);
} }
this.notificationService = new NotificationService(this._serviceWorkerHandler, config.push); this.notificationService = new NotificationService(this._serviceWorkerHandler, config.push);
this.crypto = new Crypto(cryptoExtras); this.crypto = new Crypto(cryptoExtras);
@ -182,7 +183,7 @@ export class Platform {
loadOlm() { loadOlm() {
if (!this._olmPromise) { if (!this._olmPromise) {
this._olmPromise = loadOlm(this._config.olm); this._olmPromise = loadOlm(this._assetPaths.olm);
} }
return this._olmPromise; return this._olmPromise;
} }
@ -194,7 +195,7 @@ export class Platform {
async loadOlmWorker() { async loadOlmWorker() {
if (!window.WebAssembly) { if (!window.WebAssembly) {
if (!this._workerPromise) { if (!this._workerPromise) {
this._workerPromise = loadOlmWorker(this._config); this._workerPromise = loadOlmWorker(this._assetPaths);
} }
return this._workerPromise; return this._workerPromise;
} }
@ -230,7 +231,7 @@ export class Platform {
if (navigator.msSaveBlob) { if (navigator.msSaveBlob) {
navigator.msSaveBlob(blobHandle.nativeBlob, filename); navigator.msSaveBlob(blobHandle.nativeBlob, filename);
} else { } else {
downloadInIframe(this._container, this._config.downloadSandbox, blobHandle, filename, this.isIOS); downloadInIframe(this._container, this._assetPaths.downloadSandbox, blobHandle, filename, this.isIOS);
} }
} }

View file

@ -19,20 +19,14 @@
import {main} from "./main"; import {main} from "./main";
import {Platform} from "./Platform"; import {Platform} from "./Platform";
import configJSON from "./assets/config.json?raw"; import configJSON from "./assets/config.json?raw";
import {olmPaths, downloadSandboxPath, workerPath} from "./sdk/paths/vite"; import assetPaths from "./sdk/paths/vite";
const paths = {
olm: olmPaths,
downloadSandbox: downloadSandboxPath,
worker: workerPath,
...JSON.parse(configJSON)
};
if (import.meta.env.PROD) { if (import.meta.env.PROD) {
paths.serviceWorker = "sw.js"; assetPaths.serviceWorker = "sw.js";
} }
const platform = new Platform( const platform = new Platform(
document.body, document.body,
paths, assetPaths,
null, JSON.parse(configJSON),
{development: import.meta.env.DEV} {development: import.meta.env.DEV}
); );
main(platform); main(platform);

View file

@ -9,11 +9,12 @@ import olmJsPath from "@matrix-org/olm/olm.js?url";
// @ts-ignore // @ts-ignore
import olmLegacyJsPath from "@matrix-org/olm/olm_legacy.js?url"; import olmLegacyJsPath from "@matrix-org/olm/olm_legacy.js?url";
export const olmPaths = { export default {
wasm: olmWasmPath, downloadSandbox: _downloadSandboxPath,
legacyBundle: olmLegacyJsPath, worker: _workerPath,
wasmBundle: olmJsPath, olm: {
wasm: olmWasmPath,
legacyBundle: olmLegacyJsPath,
wasmBundle: olmJsPath,
}
}; };
export const downloadSandboxPath = _downloadSandboxPath;
export const workerPath = _workerPath;