web: memory stability++

Call the explicit memory deletion—binded in function `delete()`, which is highly recommended by the Memory management in Emscripten guideline.

The function will guarantee that the WASM module is cleaned up from the memory.

see: https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html#memory-management

Additionally, WASM module's initialization part has been refactored to correspond to the change.
This commit is contained in:
Jinny You 2024-04-23 11:16:28 +09:00 committed by Hermet Park
parent 08fe14280d
commit f960c04474

View file

@ -30,11 +30,9 @@ import { THORVG_VERSION } from './version';
type LottieJson = Map<PropertyKey, any>;
type TvgModule = any;
let _tvg: TvgModule;
let _module: any;
(async () => {
_module = await Module();
_tvg = new _module.TvgLottieAnimation();
})();
// Define library version
@ -279,29 +277,21 @@ export class LottiePlayer extends LitElement {
private _timer?: ReturnType<typeof setInterval>;
private _observer?: IntersectionObserver;
constructor() {
super();
this._init();
}
private async _init(): Promise<void> {
if (!_tvg) {
// throw new Error('ThorVG has not loaded');
if (!_module) {
//NOTE: ThorVG Module has not loaded
return;
}
this._TVG = _tvg;
}
private _delayedLoad(): void {
if (!_tvg || !this._timer) {
if (!this._timer) {
//NOTE: ThorVG Module has loaded, but called this function again
return;
}
clearInterval(this._timer);
this._timer = undefined;
this._TVG = _tvg;
this._TVG = new _module.TvgLottieAnimation();
if (this.src) {
this.load(this.src, this.mimeType);
@ -317,7 +307,7 @@ export class LottiePlayer extends LitElement {
this._observer.observe(this);
if (!this._TVG) {
this._timer = setInterval(this._delayedLoad.bind(this), 100);
this._timer = setInterval(this._init.bind(this), 100);
return;
}
@ -537,6 +527,7 @@ export class LottiePlayer extends LitElement {
return;
}
this._TVG.delete();
this._TVG = null;
this.currentState = PlayerState.Destroyed;