@xysfe/memento-core
Version:
record and replay the web
85 lines (82 loc) • 3.79 kB
JavaScript
import { __awaiter, __generator, __spread } from '../../../node_modules/tslib/tslib.es6.js';
import { decode } from '../../../node_modules/base64-arraybuffer/dist/base64-arraybuffer.es5.js';
var webGLVarMap = new Map();
function variableListFor(ctx, ctor) {
var contextMap = webGLVarMap.get(ctx);
if (!contextMap) {
contextMap = new Map();
webGLVarMap.set(ctx, contextMap);
}
if (!contextMap.has(ctor)) {
contextMap.set(ctor, []);
}
return contextMap.get(ctor);
}
function deserializeArg(imageMap, ctx, preload) {
var _this = this;
return function (arg) { return __awaiter(_this, void 0, Promise, function () {
var args, name, index, name, args, ctor, _a, _b, _c, _d, image, image_1, blobContents, blob, result;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
if (!(arg && typeof arg === 'object' && 'rr_type' in arg)) return [3, 11];
if (preload)
preload.isUnchanged = false;
if (!(arg.rr_type === 'ImageBitmap' && 'args' in arg)) return [3, 3];
return [4, deserializeArg(imageMap, ctx, preload)(arg.args)];
case 1:
args = _e.sent();
return [4, createImageBitmap.apply(null, args)];
case 2: return [2, _e.sent()];
case 3:
if (!('index' in arg)) return [3, 4];
if (preload || ctx === null)
return [2, arg];
name = arg.rr_type, index = arg.index;
return [2, variableListFor(ctx, name)[index]];
case 4:
if (!('args' in arg)) return [3, 6];
name = arg.rr_type, args = arg.args;
ctor = window[name];
_b = (_a = ctor.bind).apply;
_c = [ctor];
_d = [[void 0]];
return [4, Promise.all(args.map(deserializeArg(imageMap, ctx, preload)))];
case 5: return [2, new (_b.apply(_a, _c.concat([__spread.apply(void 0, _d.concat([(_e.sent())]))])))()];
case 6:
if (!('base64' in arg)) return [3, 7];
return [2, decode(arg.base64)];
case 7:
if (!('src' in arg)) return [3, 8];
image = imageMap.get(arg.src);
if (image) {
return [2, image];
}
else {
image_1 = new Image();
image_1.src = arg.src;
imageMap.set(arg.src, image_1);
return [2, image_1];
}
case 8:
if (!('data' in arg && arg.rr_type === 'Blob')) return [3, 10];
return [4, Promise.all(arg.data.map(deserializeArg(imageMap, ctx, preload)))];
case 9:
blobContents = _e.sent();
blob = new Blob(blobContents, {
type: arg.type,
});
return [2, blob];
case 10: return [3, 13];
case 11:
if (!Array.isArray(arg)) return [3, 13];
return [4, Promise.all(arg.map(deserializeArg(imageMap, ctx, preload)))];
case 12:
result = _e.sent();
return [2, result];
case 13: return [2, arg];
}
});
}); };
}
export { deserializeArg, variableListFor };