Skip to content

Commit 0a9b8cb

Browse files
committed
feat(plugin): improve worker readiness handling and fallback logic
1 parent a2e3036 commit 0a9b8cb

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

examples/simple-marker/index.html

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,35 @@ <h3>Event Log:</h3>
7272
log('Creating ArtoolkitPlugin instance (dist build)…');
7373
plugin = new ArtoolkitPlugin({
7474
worker: true,
75-
cameraParametersUrl: '/examples/simple-marker/data/camera_para.dat',
76-
// minConfidence: 0.6,
75+
cameraParametersUrl: '/examples/simple-marker/data/camera_para.dat'
7776
});
78-
await plugin.init(core);
79-
await plugin.enable();
8077

78+
// Register listeners BEFORE enable to avoid missing early 'ready'
8179
eventBus.on('ar:workerReady', () => {
8280
log('Worker ready');
8381
setStatus('Worker ready. Start camera and then load marker.', 'success');
8482
loadMarkerBtn.disabled = false;
8583
});
86-
8784
eventBus.on('ar:workerError', e => {
8885
log(`workerError: ${JSON.stringify(e)}`);
8986
setStatus('Worker error (see console)', 'error');
9087
});
91-
9288
eventBus.on('ar:getMarker', (e) => console.log('[example] ar:getMarker', e));
9389
eventBus.on('ar:markerFound', d => log(`markerFound: ${JSON.stringify(d)}`));
9490
eventBus.on('ar:markerUpdated', d => log(`markerUpdated: ${JSON.stringify(d)}`));
9591
eventBus.on('ar:markerLost', d => log(`markerLost: ${JSON.stringify(d)}`));
9692

97-
setStatus('Plugin initialized. Waiting for worker…', 'normal');
93+
await plugin.init(core);
94+
await plugin.enable();
95+
96+
// Fallback: if worker became ready during enable, honor it
97+
if (plugin.workerReady) {
98+
log('Worker was already ready (post-enable).');
99+
setStatus('Worker ready. Start camera and then load marker.', 'success');
100+
loadMarkerBtn.disabled = false;
101+
} else {
102+
setStatus('Plugin initialized. Waiting for worker…', 'normal');
103+
}
98104
} catch (err) {
99105
log('Init error: ' + (err && err.message ? err.message : err));
100106
setStatus('Initialization error', 'error');

src/plugin.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export class ArtoolkitPlugin {
3939
// Pending loadMarker requests: Map<requestId, { resolve, reject }>
4040
this._pendingMarkerLoads = new Map();
4141
this._nextLoadRequestId = 0;
42+
this.workerReady = false;
4243
}
4344

4445
async init(core) {
@@ -165,9 +166,13 @@ export class ArtoolkitPlugin {
165166
wasmBaseUrl: this.options.wasmBaseUrl || null
166167
}
167168
});
168-
} catch (e) {
169-
// ignore
170-
}
169+
// Watchdog: if 'ready' wasn’t received shortly, resend a no-op init once
170+
setTimeout(() => {
171+
if (!this.workerReady) {
172+
try { this._worker?.postMessage?.({ type: 'init', payload: {} }); } catch {}
173+
}
174+
}, 500);
175+
} catch (e) {}
171176
}
172177

173178
_stopWorker() {
@@ -197,6 +202,8 @@ export class ArtoolkitPlugin {
197202
const data = ev && ev.data !== undefined ? ev.data : ev;
198203
const { type, payload } = data || {};
199204
if (type === 'ready') {
205+
console.log('[Plugin] Worker ready');
206+
this.workerReady = true;
200207
this.core?.eventBus?.emit('ar:workerReady', {});
201208
} else if (type === 'detectionResult') {
202209
console.log('[Plugin] Received detectionResult:', payload);

0 commit comments

Comments
 (0)