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() {
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();
platform.setNavigation(navigation);
const urlRouter = createRouter({

View File

@ -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});
}

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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;