Skip to content

Commit f343cb4

Browse files
committed
feat(plugin): enhance marker tracking configuration and sweep logic
1 parent b6d5add commit f343cb4

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/plugin.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ export class ArtoolkitPlugin {
2424
// Marker state tracking: Map<id, { lastSeen: number, visible: boolean }>
2525
this._markers = new Map();
2626

27-
// configuration
27+
// configuration (defaults)
28+
// lostThreshold: number of frames to consider a marker lost
29+
this.lostThreshold = options.lostThreshold ?? 5; // frames
30+
// frameDurationMs: how many milliseconds to consider a single 'frame' (used to convert lostThreshold -> ms)
31+
// Default 200ms per frame is a conservative default (5 fps). Consumers can adjust to match their capture rate.
32+
this.frameDurationMs = options.frameDurationMs ?? 200;
33+
// sweepIntervalMs: how often to run the lost-marker sweep (ms)
34+
this.sweepIntervalMs = options.sweepIntervalMs ?? 100;
35+
36+
// Worker enabled toggle
2837
this.workerEnabled = options.worker !== false; // default true
29-
this.lostThreshold = options.lostThreshold ?? 5; // frames to consider lost
3038
}
3139

3240
async init(core) {
@@ -48,8 +56,8 @@ export class ArtoolkitPlugin {
4856
await this._startWorker();
4957
}
5058

51-
// start a simple interval to sweep lost markers by frame count (optional)
52-
this._sweepInterval = setInterval(() => this._sweepMarkers(), 100); // adjust as needed
59+
// start a simple interval to sweep lost markers by time computed from frameDurationMs
60+
this._sweepInterval = setInterval(() => this._sweepMarkers(), this.sweepIntervalMs);
5361
return this;
5462
}
5563

@@ -126,7 +134,6 @@ export class ArtoolkitPlugin {
126134
// Browser environment: global Worker exists
127135
if (typeof Worker !== 'undefined') {
128136
// Works in browsers and bundlers that support new URL(...) for workers
129-
// with this line:
130137
this._worker = new Worker(new URL('./worker/worker.js', import.meta.url), { type: 'module' });
131138
} else {
132139
// Node environment: use worker_threads.Worker
@@ -206,17 +213,20 @@ export class ArtoolkitPlugin {
206213
}
207214
}
208215

216+
// sweep markers and emit lost events for markers not seen recently
209217
_sweepMarkers() {
210218
const now = Date.now();
219+
const lostThresholdMs = this.lostThreshold * this.frameDurationMs;
211220
for (const [id, state] of this._markers.entries()) {
212221
const deltaMs = now - (state.lastSeen || 0);
213-
if (deltaMs > (this.lostThreshold * 200)) {
222+
if (deltaMs > lostThresholdMs) {
214223
this._markers.delete(id);
215224
this.core.eventBus.emit('ar:markerLost', { id, timestamp: now });
216225
}
217226
}
218227
}
219228

229+
// public helper to get marker state
220230
getMarkerState(markerId) {
221231
return this._markers.get(markerId) || null;
222232
}

0 commit comments

Comments
 (0)