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