1410be7427
Chrome 61 (Karoo WebView) cannot parse dynamic import() — a SyntaxError at parse time prevents loadPyodide from ever being defined. Fix: fetch pyodide.js as text, replace every import( with a __loadScript( shim that uses <script> tag injection, then inject via Blob URL. The Blob script is never pre-scanned for module syntax so the patch is invisible to the parser. Also: expose waitForEngine() from extractActivity so callers can await engine readiness before batching files — manual scan now shows "Preparing extraction engine…" instead of flooding with N individual failures.
110 lines
3.5 KiB
TypeScript
110 lines
3.5 KiB
TypeScript
import { createRef } from 'react';
|
|
import type WebView from 'react-native-webview';
|
|
import type { WebViewMessageEvent } from 'react-native-webview';
|
|
|
|
export type ExtractionResult = {
|
|
id: string;
|
|
detail: object;
|
|
timeseries: object | null;
|
|
geojson: object | null;
|
|
sourceHash: string;
|
|
};
|
|
|
|
type Pending = {
|
|
resolve: (r: ExtractionResult) => void;
|
|
reject: (e: Error) => void;
|
|
onStatus: (msg: string) => void;
|
|
};
|
|
|
|
export const pyodideRef = createRef<WebView>();
|
|
|
|
const pending = new Map<string, Pending>();
|
|
let reqCounter = 0;
|
|
let isExtracting = false;
|
|
|
|
// Engine readiness — tracked so callers can wait before batching files.
|
|
let _engineReady = false;
|
|
let _engineError: string | null = null;
|
|
const _engineResolvers: Array<() => void> = [];
|
|
const _engineRejecters: Array<(e: Error) => void> = [];
|
|
|
|
export function waitForEngine(timeoutMs = 300_000): Promise<void> {
|
|
if (_engineReady) return Promise.resolve();
|
|
if (_engineError) return Promise.reject(new Error(_engineError));
|
|
return new Promise((resolve, reject) => {
|
|
const timer = setTimeout(() => {
|
|
reject(new Error('Extraction engine timed out — check network and Bincio instance URL'));
|
|
}, timeoutMs);
|
|
_engineResolvers.push(() => { clearTimeout(timer); resolve(); });
|
|
_engineRejecters.push((e) => { clearTimeout(timer); reject(e); });
|
|
});
|
|
}
|
|
|
|
export function handleWebViewMessage(e: WebViewMessageEvent): void {
|
|
let msg: Record<string, unknown>;
|
|
try { msg = JSON.parse(e.nativeEvent.data); } catch { return; }
|
|
|
|
const reqId = msg.reqId as string | undefined;
|
|
const p = reqId ? pending.get(reqId) : undefined;
|
|
|
|
switch (msg.type) {
|
|
case 'pyodide_ready':
|
|
_engineReady = true;
|
|
_engineResolvers.splice(0).forEach(fn => fn());
|
|
break;
|
|
case 'init_error':
|
|
_engineError = msg.message as string;
|
|
_engineRejecters.splice(0).forEach(fn => fn(new Error(_engineError!)));
|
|
break;
|
|
case 'result':
|
|
if (p) {
|
|
pending.delete(reqId!);
|
|
p.resolve({
|
|
id: msg.id as string,
|
|
detail: msg.detail as object,
|
|
timeseries: (msg.timeseries as object | null) ?? null,
|
|
geojson: (msg.geojson as object | null) ?? null,
|
|
sourceHash: msg.sourceHash as string,
|
|
});
|
|
}
|
|
break;
|
|
case 'error':
|
|
if (p) {
|
|
pending.delete(reqId!);
|
|
p.reject(new Error(msg.message as string));
|
|
}
|
|
break;
|
|
case 'progress':
|
|
p?.onStatus(msg.msg as string);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// wheelBase64 is the bincio .whl file pre-fetched by the React Native side
|
|
// (native networking supports HTTP on local network; WKWebView does not).
|
|
export function extractFile(
|
|
filename: string,
|
|
base64: string,
|
|
wheelBase64: string,
|
|
wheelFilename: string,
|
|
onStatus: (msg: string) => void = () => {},
|
|
): Promise<ExtractionResult> {
|
|
if (isExtracting) return Promise.reject(new Error('Another extraction is already in progress'));
|
|
|
|
const webview = pyodideRef.current;
|
|
if (!webview) return Promise.reject(new Error('Extraction engine not ready — restart the app'));
|
|
|
|
isExtracting = true;
|
|
const reqId = String(++reqCounter);
|
|
const args = JSON.stringify({ reqId, filename, base64, wheelBase64, wheelFilename });
|
|
|
|
return new Promise<ExtractionResult>((resolve, reject) => {
|
|
pending.set(reqId, {
|
|
resolve: (r) => { isExtracting = false; resolve(r); },
|
|
reject: (e) => { isExtracting = false; reject(e); },
|
|
onStatus,
|
|
});
|
|
webview.injectJavaScript(`window._bincioExtract(${args}); true;`);
|
|
});
|
|
}
|