@action-land/component
Version:
Basic interface for a component
38 lines (37 loc) • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const standard_data_structures_1 = require("standard-data-structures");
class ListComponentState {
constructor(baseInit, keys, hashMap) {
this.baseInit = baseInit;
this.keys = keys;
this.hashMap = hashMap;
}
static of(baseInit) {
return new ListComponentState(baseInit, standard_data_structures_1.List.empty(), standard_data_structures_1.HashMap.of());
}
set(k, value = this.baseInit()) {
const items = this.hashMap.has(k) ? this.keys : this.keys.prepend(k);
return new ListComponentState(this.baseInit, items, this.hashMap.set(k, value));
}
get(k) {
return this.hashMap.get(k);
}
fold(s, fn) {
return ListComponentState.fold(this, s, fn);
}
static fold(L, T, fn) {
return L.keys.fold(T, (key, acc) => fn(L.hashMap.get(key).getOrElse(L.baseInit()), key, acc));
}
static toArray(L) {
return L.asArray;
}
has(k) {
return this.hashMap.has(k);
}
get asArray() {
return this.fold(standard_data_structures_1.List.empty(), (S, L, A) => A.prepend([L, S]))
.asArray;
}
}
exports.ListComponentState = ListComponentState;