Bring Record toward the needs of Syndicate/js Struct
This commit is contained in:
parent
f30b2ae175
commit
9762ad4bfb
|
@ -303,6 +303,24 @@ class Record {
|
||||||
this.fields = fromJS(fields);
|
this.fields = fromJS(fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
equals(other) {
|
||||||
|
return (other instanceof Record) &&
|
||||||
|
Immutable.is(this.label, other.label) &&
|
||||||
|
Immutable.is(this.fields, other.fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
hashCode() {
|
||||||
|
return Immutable.List([this.label, this.fields]).hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
get(index, defaultValue) {
|
||||||
|
return this.fields.get(index, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
set(index, newValue) {
|
||||||
|
return new Record(this.label, this.fields.set(index, newValue));
|
||||||
|
}
|
||||||
|
|
||||||
[PreserveOn](encoder) {
|
[PreserveOn](encoder) {
|
||||||
if (Immutable.is(encoder.shortForms[0], this.label)) {
|
if (Immutable.is(encoder.shortForms[0], this.label)) {
|
||||||
encoder.header(2, 0, this.fields.size);
|
encoder.header(2, 0, this.fields.size);
|
||||||
|
@ -318,11 +336,33 @@ class Record {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Record.makeConstructor = function (label, fieldNames) {
|
||||||
|
const arity = fieldNames.length;
|
||||||
|
const ctor = (...fields) => {
|
||||||
|
if (fields.length !== arity) {
|
||||||
|
throw new Error("Record: cannot instantiate " + (label && label.toString()) +
|
||||||
|
" expecting " + arity + " fields with " + fields.length + " fields");
|
||||||
|
}
|
||||||
|
return new Record(label, fields);
|
||||||
|
};
|
||||||
|
ctor.meta = label;
|
||||||
|
ctor.isClassOf = (v) => ((v instanceof Record) && Immutable.is(label, v.label));
|
||||||
|
return ctor;
|
||||||
|
};
|
||||||
|
|
||||||
class Float {
|
class Float {
|
||||||
constructor(value) {
|
constructor(value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
equals(other) {
|
||||||
|
return (other instanceof Float) && (other.value === this.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
hashCode() {
|
||||||
|
return this.value | 0; // TODO: something better?
|
||||||
|
}
|
||||||
|
|
||||||
[PreserveOn](encoder) {
|
[PreserveOn](encoder) {
|
||||||
encoder.leadbyte(0, 0, 2);
|
encoder.leadbyte(0, 0, 2);
|
||||||
encoder.makeroom(4);
|
encoder.makeroom(4);
|
||||||
|
|
Loading…
Reference in New Issue