@awayjs/graphics
Version:
AwayJS graphics classes
78 lines (77 loc) • 2.5 kB
JavaScript
import * as Tess2TS from 'tess2-ts';
import createTess2Wasm from 'tess2-wasm';
var TessAsyncService = /** @class */ (function () {
function TessAsyncService() {
this.name = 'tessWasm';
this.status = 'pending';
this.module = null;
}
Object.defineProperty(TessAsyncService, "instance", {
get: function () {
return this._instance || (this._instance = new TessAsyncService());
},
enumerable: false,
configurable: true
});
TessAsyncService.prototype.tesselate = function (options) {
try {
var proc = this._process = new this.module();
proc.addContours(options.contours);
return proc.tesselate(options);
}
catch (e) {
this.status = 'error';
throw e;
}
};
TessAsyncService.prototype.dispose = function () {
if (!this._process) {
return;
}
this._process.dispose();
this._process = null;
};
TessAsyncService.prototype.init = function () {
var _this = this;
if (this.module) {
return Promise.resolve();
}
if (!self.WebAssembly) {
console.warn('[Tess Wasm] Wasm not supported');
return Promise.reject('[Tess Wasm] Wasm not supported');
}
return createTess2Wasm()
.then(function (_a) {
var Tess = _a.Tess;
_this.module = Tess;
_this.status = 'done';
}, function (err) {
_this.status = 'error';
console.warn('[Tess Wasm] Error while spawn TessWasm', err);
});
};
return TessAsyncService;
}());
export { TessAsyncService };
var Tess2Provider = /** @class */ (function () {
function Tess2Provider() {
}
Tess2Provider.tesselate = function (options) {
if (TessAsyncService.instance.status === 'pending') {
console.debug('[GraphicsFactoryFills] WASM Tess not loaded, JS will used.');
return Tess2TS.tesselate(options);
}
try {
return TessAsyncService.instance.tesselate(options);
}
catch (e) {
console.warn('[GraphicsFactoryFills] Tess2Wasm crash, downgrade to JS', e.message, options);
return Tess2TS.tesselate(options);
}
};
Tess2Provider.dispose = function () {
TessAsyncService.instance.dispose();
};
return Tess2Provider;
}());
export { Tess2Provider };