2019-08-25 02:39:20 -04:00
|
|
|
"use strict";
|
|
|
|
import { Coder } from "./abstract-coder";
|
|
|
|
import { pack, unpack } from "./array";
|
|
|
|
export class TupleCoder extends Coder {
|
|
|
|
constructor(coders, localName) {
|
|
|
|
let dynamic = false;
|
2020-04-25 03:54:54 -04:00
|
|
|
const types = [];
|
2019-08-25 02:39:20 -04:00
|
|
|
coders.forEach((coder) => {
|
|
|
|
if (coder.dynamic) {
|
|
|
|
dynamic = true;
|
|
|
|
}
|
|
|
|
types.push(coder.type);
|
|
|
|
});
|
2020-04-25 03:54:54 -04:00
|
|
|
const type = ("tuple(" + types.join(",") + ")");
|
2019-08-25 02:39:20 -04:00
|
|
|
super("tuple", type, localName, dynamic);
|
|
|
|
this.coders = coders;
|
|
|
|
}
|
|
|
|
encode(writer, value) {
|
|
|
|
return pack(writer, this.coders, value);
|
|
|
|
}
|
|
|
|
decode(reader) {
|
|
|
|
return reader.coerce(this.name, unpack(reader, this.coders));
|
|
|
|
}
|
|
|
|
}
|