forked from Blendman974/faceclaw
Add a simple worker thread to validate worker-thread setup
This commit is contained in:
1 parent
1533c550b4
commit
c83448873e
3 files changed
+60
No files matched your search
@@ -6,8 +6,10 @@ purpose of the file is to pass control to the app’s first module.
|
|||||||
|
|
||||||
import { Application } from '@nativescript/core'
|
import { Application } from '@nativescript/core'
|
||||||
import { registerShareIntentHandler } from './native/share-intents'
|
import { registerShareIntentHandler } from './native/share-intents'
|
||||||
|
import { runWorkerSpike } from './workers/worker-spike'
|
||||||
|
|
||||||
registerShareIntentHandler()
|
registerShareIntentHandler()
|
||||||
|
runWorkerSpike()
|
||||||
|
|
||||||
Application.run({ moduleName: 'app-root' })
|
Application.run({ moduleName: 'app-root' })
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
/**
|
||||||
|
* Trivial echo worker: spike scaffolding for the app-shell architecture.
|
||||||
|
* Validates that worker chunks build under @nativescript/webpack, that
|
||||||
|
* message passing works in both directions, and that a worker isolate can
|
||||||
|
* call Java APIs directly.
|
||||||
|
*/
|
||||||
|
import "@nativescript/core/globals";
|
||||||
|
|
||||||
|
declare const global: any;
|
||||||
|
declare const java: any;
|
||||||
|
|
||||||
|
global.onmessage = (event: { data: any }) => {
|
||||||
|
// Direct Java call from the worker isolate: this runs on the worker's own
|
||||||
|
// JVM-attached thread, not the main thread.
|
||||||
|
const javaThreadName = String(java.lang.Thread.currentThread().getName());
|
||||||
|
global.postMessage({
|
||||||
|
type: "pong",
|
||||||
|
echoed: event.data,
|
||||||
|
workerJavaThreadName: javaThreadName,
|
||||||
|
workerReceivedAtMs: Date.now(),
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
* Worker spike: spawn the echo worker, do one round trip, log the results.
|
||||||
|
* Temporary scaffolding for the app-shell architecture work; exercises the
|
||||||
|
* webpack worker chunk, bidirectional postMessage, and worker Java access.
|
||||||
|
* Results appear in logcat prefixed with "worker-spike:".
|
||||||
|
*/
|
||||||
|
declare const java: any;
|
||||||
|
|
||||||
|
export function runWorkerSpike(): void {
|
||||||
|
const mainJavaThreadName = String(java.lang.Thread.currentThread().getName());
|
||||||
|
console.log(`worker-spike: spawning echo worker (main thread=${mainJavaThreadName})`);
|
||||||
|
|
||||||
|
let worker: Worker;
|
||||||
|
try {
|
||||||
|
worker = new Worker("./echo.worker");
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`worker-spike: failed to spawn worker: ${error}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sentAtMs = Date.now();
|
||||||
|
worker.onmessage = (event: MessageEvent) => {
|
||||||
|
const roundTripMs = Date.now() - sentAtMs;
|
||||||
|
console.log(
|
||||||
|
`worker-spike: reply received in ${roundTripMs}ms: ${JSON.stringify(event.data)}`,
|
||||||
|
);
|
||||||
|
worker.terminate();
|
||||||
|
console.log("worker-spike: worker terminated, spike complete");
|
||||||
|
};
|
||||||
|
worker.onerror = (error) => {
|
||||||
|
console.error(`worker-spike: worker error: ${JSON.stringify(error)}`);
|
||||||
|
worker.terminate();
|
||||||
|
};
|
||||||
|
|
||||||
|
worker.postMessage({ type: "ping", sentAtMs, payload: "hello from main" });
|
||||||
|
}
|
||||||
Reference in new issue
Block a user