tracking: add onceLoaded callback
This commit is contained in:
@@ -42,6 +42,8 @@ export class TrackingManager {
|
|||||||
private _consent: boolean | null = $state(null);
|
private _consent: boolean | null = $state(null);
|
||||||
private _services: Record<string, InternalService<unknown>> = {};
|
private _services: Record<string, InternalService<unknown>> = {};
|
||||||
private _changeCallbacks: Array<(consent: boolean | null) => void> = [];
|
private _changeCallbacks: Array<(consent: boolean | null) => void> = [];
|
||||||
|
private _loadCallbacks: Array<(consent: boolean | null) => void> = [];
|
||||||
|
private _loaded: boolean = false;
|
||||||
private _consentQueue: Array<() => void> = [];
|
private _consentQueue: Array<() => void> = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,6 +82,12 @@ export class TrackingManager {
|
|||||||
log.debug('[TrackingManager] Loaded tracking options from storage:', opts);
|
log.debug('[TrackingManager] Loaded tracking options from storage:', opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run load callbacks
|
||||||
|
this._loadCallbacks.forEach((cb) => {
|
||||||
|
cb(this._consent);
|
||||||
|
});
|
||||||
|
this._loaded = true;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,6 +160,16 @@ export class TrackingManager {
|
|||||||
this.saveOpts();
|
this.saveOpts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a callback to be called only once the tracking state has been
|
||||||
|
* loaded from localStorage. Will be called immediately if already loaded
|
||||||
|
* and with each subsequent load.
|
||||||
|
*/
|
||||||
|
onceLoaded(callback: (consent: boolean | null) => void) {
|
||||||
|
this._loadCallbacks.push(callback);
|
||||||
|
if (this._loaded) callback(this.consent);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a callback to be notified when the tracking permission changes.
|
* Registers a callback to be notified when the tracking permission changes.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user