UNPKG

@akashic/akashic-sandbox

Version:
73 lines (72 loc) 2.98 kB
// 本来であればv3系のg.ScriptAssetをimplementsすべきだが、ビルド時に使用しているakashic-engineはv2系なので一からクラス定義している // game.ejs で参照されるため、未使用の lint エラーを抑止 // eslint-disable-next-line @typescript-eslint/no-unused-vars var SandboxScriptAssetV3 = /** @class */ (function () { function SandboxScriptAssetV3(id, path) { var _this = this; this.type = "script"; this.id = id; this.originalPath = path; this.path = this._assetPathFilter(path); this.onDestroyed = new g.Trigger(); // いきなり読んじゃう var heads = document.getElementsByTagName("head"); var container = (heads.length === 0) ? document.body : heads[0]; var script = document.createElement("script"); script.onload = function () { _this.script = script.text; // TODO: とれない・・ _this.loading = false; }; script.onerror = function () { _this.loading = false; }; this.script = undefined; this.loading = true; // TODO: pathに?が入っていたりするとダメなやつ script.src = this.path + "?id=" + encodeURIComponent(this.id); container.appendChild(script); } SandboxScriptAssetV3.prototype.destroy = function () { this.onDestroyed.fire(this); this.id = undefined; this.originalPath = undefined; this.path = undefined; this.onDestroyed.destroy(); this.onDestroyed = undefined; }; SandboxScriptAssetV3.prototype.destroyed = function () { return this.id === undefined; }; SandboxScriptAssetV3.prototype.inUse = function () { return false; }; // 引数の型はg.ScriptAssetRuntimeValueだが、v2系には無いものなのでanyを指定している SandboxScriptAssetV3.prototype.execute = function (execEnv) { window.gScriptContainer[this.id](execEnv); return execEnv.module.exports; }; SandboxScriptAssetV3.prototype._load = function (loader) { var _this = this; var waitLoader = function () { if (_this.loading) { setTimeout(waitLoader, 100); return; } if (_this.script !== undefined) { loader._onAssetLoad(_this); } else { loader._onAssetError(_this, g.ExceptionFactory.createAssetLoadError("can not load script")); } }; setTimeout(waitLoader, this.loading ? 100 : 0); }; /** * @private */ SandboxScriptAssetV3.prototype._assetPathFilter = function (path) { // 拡張子の補完・読み替えが必要なassetはこれをオーバーライドすればよい。(対応形式が限定されるaudioなどの場合) return path; }; return SandboxScriptAssetV3; }());