/
LinkAddressed.ts
164 lines (144 loc) · 5.24 KB
/
LinkAddressed.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2015-2021 Swim inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {Murmur3, Numbers, Constructors} from "@swim/util";
import type {Output} from "@swim/codec";
import {Item, Attr, AnyValue, Value, Record} from "@swim/structure";
import {AnyUri, Uri} from "@swim/uri";
import {Envelope} from "./Envelope";
export interface LinkAddressedConstructor<E extends LinkAddressed<E>> {
new(node: Uri, lane: Uri, prio: number, rate: number, body: Value): E;
readonly tag: string;
}
export abstract class LinkAddressed<E extends LinkAddressed<E>> extends Envelope {
constructor(node: Uri, lane: Uri, prio: number, rate: number, body: Value) {
super();
Object.defineProperty(this, "node", {
value: node,
enumerable: true,
});
Object.defineProperty(this, "lane", {
value: lane,
enumerable: true,
});
Object.defineProperty(this, "prio", {
value: prio,
enumerable: true,
});
Object.defineProperty(this, "rate", {
value: rate,
enumerable: true,
});
Object.defineProperty(this, "body", {
value: body,
enumerable: true,
});
}
override readonly node!: Uri;
override withNode(node: AnyUri): E {
node = Uri.fromAny(node);
return this.copy(node as Uri, this.lane, this.prio, this.rate, this.body);
}
override readonly lane!: Uri;
override withLane(lane: AnyUri): E {
lane = Uri.fromAny(lane);
return this.copy(this.node, lane as Uri, this.prio, this.rate, this.body);
}
override readonly prio!: number;
override withPrio(prio: number): E {
return this.copy(this.node, this.lane, prio, this.rate, this.body);
}
override readonly rate!: number;
override withRate(rate: number): E {
return this.copy(this.node, this.lane, this.prio, rate, this.body);
}
override readonly body!: Value;
override withBody(body: AnyValue): E {
body = Value.fromAny(body);
return this.copy(this.node, this.lane, this.prio, this.rate, body);
}
protected abstract copy(node: Uri, lane: Uri, prio: number, rate: number, body: Value): E;
override equals(that: unknown): boolean {
if (this === that) {
return true;
} else if (that instanceof LinkAddressed) {
return this.constructor === that.constructor
&& this.node.equals(that.node) && this.lane.equals(that.lane)
&& this.prio === that.prio && this.rate === that.rate
&& this.body.equals(that.body);
}
return false;
}
override hashCode(): number {
return Murmur3.mash(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix(
Constructors.hash(this.constructor), this.node.hashCode()), this.lane.hashCode()),
Numbers.hash(this.prio)), Numbers.hash(this.rate)), this.body.hashCode()));
}
override debug(output: Output): void {
output = output.write(this.constructor.name).write(46/*'.'*/)
.write("create").write(40/*'('*/)
.debug(this.node.toString()).write(", ").debug(this.lane.toString());
if (this.prio !== 0 || this.rate !== 0) {
output = output.write(", ").debug(this.prio).write(", ").debug(this.rate);
}
if (this.body.isDefined()) {
output = output.write(", ").debug(this.body);
}
output = output.write(41/*')'*/);
}
override toValue(): Value {
const header = Record.create(4)
.slot("node", this.node.toString())
.slot("lane", this.lane.toString());
if (this.prio !== 0) {
header.slot("prio", this.prio);
}
if (this.rate !== 0) {
header.slot("rate", this.rate);
}
return Attr.of(this.tag, header).concat(this.body);
}
static override fromValue<E extends LinkAddressed<E>>(this: LinkAddressedConstructor<E>,
value: Value): E | null {
let node: Uri | undefined;
let lane: Uri | undefined;
let prio = 0;
let rate = 0;
const header = value.header(this.tag);
header.forEach(function (header: Item, index: number) {
const key = header.key.stringValue(void 0);
if (key !== void 0) {
if (key === "node") {
node = Uri.parse(header.toValue().stringValue(""));
} else if (key === "lane") {
lane = Uri.parse(header.toValue().stringValue(""));
} else if (key === "prio") {
prio = header.numberValue(prio);
} else if (key === "rate") {
rate = header.numberValue(rate);
}
} else if (header instanceof Value) {
if (index === 0) {
node = Uri.parse(header.stringValue(""));
} else if (index === 1) {
lane = Uri.parse(header.stringValue(""));
}
}
});
if (node !== void 0 && lane !== void 0) {
const body = value.body();
return new this(node, lane, prio, rate, body);
}
return null;
}
}