139 lines
4.4 KiB
JavaScript
139 lines
4.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Forwarder = void 0;
|
|
exports.forward = forward;
|
|
const impl_1 = require("./impl");
|
|
const interface_1 = require("./interface");
|
|
const registry_1 = require("../registry");
|
|
const utils_1 = require("../utils");
|
|
function forward(value, into, registry = registry_1.GlobalRegistry) {
|
|
const forwarder = new Forwarder(value);
|
|
return (0, impl_1.deserialize)(forwarder, into, registry);
|
|
}
|
|
class ForwardMapAccess extends interface_1.MapAccess {
|
|
constructor(entries) {
|
|
super();
|
|
Object.defineProperty(this, "_entries", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: void 0
|
|
});
|
|
Object.defineProperty(this, "index", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: -1
|
|
});
|
|
this._entries = entries;
|
|
}
|
|
static fromObject(value) {
|
|
return new this(Object.entries(value));
|
|
}
|
|
nextKey(seed) {
|
|
this.index += 1;
|
|
if (this.index >= this._entries.length) {
|
|
return utils_1.IterResult.Done();
|
|
}
|
|
else {
|
|
const key = this._entries[this.index][0];
|
|
const value = seed != null ? seed(new Forwarder(key)) : key;
|
|
return utils_1.IterResult.Next(value);
|
|
}
|
|
}
|
|
nextValue(seed) {
|
|
if (this.index >= this._entries.length) {
|
|
return utils_1.IterResult.Done();
|
|
}
|
|
else {
|
|
const value = this._entries[this.index][1];
|
|
const deser = seed != null ? seed(value) : value;
|
|
return utils_1.IterResult.Next(deser);
|
|
}
|
|
}
|
|
}
|
|
class ForwardIterableAccess extends interface_1.IterableAccess {
|
|
constructor(elements) {
|
|
super();
|
|
Object.defineProperty(this, "elements", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: void 0
|
|
});
|
|
Object.defineProperty(this, "index", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: -1
|
|
});
|
|
this.elements = elements;
|
|
}
|
|
nextElement(seed) {
|
|
this.index += 1;
|
|
if (this.index >= this.elements.length) {
|
|
return utils_1.IterResult.Done();
|
|
}
|
|
else {
|
|
const element = this.elements[this.index];
|
|
const deser = seed != null ? seed(new Forwarder(element)) : element;
|
|
return utils_1.IterResult.Next(deser);
|
|
}
|
|
}
|
|
}
|
|
class Forwarder {
|
|
constructor(value) {
|
|
Object.defineProperty(this, "value", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: void 0
|
|
});
|
|
this.value = value;
|
|
}
|
|
deserializeAny(visitor) {
|
|
switch (typeof this.value) {
|
|
case 'string': return this.deserializeString(visitor);
|
|
case 'number': return this.deserializeNumber(visitor);
|
|
case 'bigint': return this.deserializeBigInt(visitor);
|
|
case 'boolean': return this.deserializeBoolean(visitor);
|
|
case 'symbol': return this.deserializeSymbol(visitor);
|
|
case 'undefined': return this.deserializeNull(visitor);
|
|
case 'object': {
|
|
switch (true) {
|
|
case Array.isArray(this.value): return this.deserializeIterable(visitor);
|
|
default: return this.deserializeObject(visitor);
|
|
}
|
|
}
|
|
case 'function': return this.deserializeFunction(visitor);
|
|
}
|
|
}
|
|
deserializeBoolean(visitor) {
|
|
return visitor.visitBoolean(this.value);
|
|
}
|
|
deserializeNumber(visitor) {
|
|
return visitor.visitNumber(this.value);
|
|
}
|
|
deserializeBigInt(visitor) {
|
|
return visitor.visitBigInt(this.value);
|
|
}
|
|
deserializeString(visitor) {
|
|
return visitor.visitString(this.value);
|
|
}
|
|
deserializeSymbol(visitor) {
|
|
return visitor.visitSymbol(this.value);
|
|
}
|
|
deserializeNull(visitor) {
|
|
return visitor.visitNull();
|
|
}
|
|
deserializeObject(visitor) {
|
|
return visitor.visitObject(ForwardMapAccess.fromObject(this.value));
|
|
}
|
|
deserializeIterable(visitor) {
|
|
return visitor.visitIterable(new ForwardIterableAccess(this.value));
|
|
}
|
|
deserializeFunction(_visitor) {
|
|
throw new Error('Method not implemented.');
|
|
}
|
|
}
|
|
exports.Forwarder = Forwarder;
|