@holochain/client
Version:
A JavaScript client for the Holochain Conductor API
236 lines (235 loc) • 6.25 kB
JavaScript
import flatMap from "lodash-es/flatMap.js";
import { HoloHashMap } from "./holo-hash-map.js";
/**
* A Map of DnaHash to HoloHashMap.
*
* i.e. A Map of DnaHash to a Map of HoloHash to a value.
*
* @param initialEntries - Optional array of `[[DnaHash, HoloHash], value]` to insert into the map.
*
* @public
*/
export class DnaHoloHashMap {
_dnaMap = new HoloHashMap();
constructor(initialEntries) {
if (initialEntries) {
for (const [[dnaHash, key], value] of initialEntries) {
this.set([dnaHash, key], value);
}
}
}
/**
* Gets the value associated with a [DnaHash, HoloHash] key pair.
*
* @param key - Array of [DnaHash, HoloHash]
* @returns The value if found, undefined otherwise
*/
get([dnaHash, key]) {
return this._dnaMap.get(dnaHash)
? this._dnaMap.get(dnaHash)?.get(key)
: undefined;
}
/**
* Checks if a [DnaHash, HoloHash] key pair exists in the map.
*
* @param cellKey - Array of [DnaHash, HoloHash] to check
* @returns True if the key exists, false otherwise
*/
has([dnaHash, key]) {
const map = this._dnaMap.get(dnaHash);
return map ? map.has(key) : false;
}
/**
* Sets a value for a [DnaHash, HoloHash] key pair.
*
* @param key - Tuple of [DnaHash, HoloHash]
* @param value - The value to store
* @returns This map instance for chaining
*/
set([dnaHash, key], value) {
const map = this._dnaMap.get(dnaHash);
if (map === undefined) {
this._dnaMap.set(dnaHash, new HoloHashMap([[key, value]]));
}
else {
map.set(key, value);
}
return this;
}
/**
* Removes all entries from the map.
*/
clear() {
this._dnaMap.clear();
}
/**
* Deletes an entry from the map. If this was the last entry for a DNA, the DNA entry is also removed.
*
* @param key - Array of [DnaHash, HoloHash] to delete
* @returns True if the DNA entry was deleted (last entry for that DNA), false otherwise
*/
delete([dnaHash, key]) {
const map = this._dnaMap.get(dnaHash);
if (map) {
const wasDeleted = map.delete(key);
if (wasDeleted && Array.from(map.keys()).length === 0) {
this._dnaMap.delete(dnaHash);
}
return wasDeleted;
}
else {
return false;
}
}
/**
* Returns all [DnaHash, HoloHash] key pairs in the map.
*
* @returns Array of all key tuples
*/
keys() {
const dnaHashes = Array.from(this._dnaMap.keys());
return flatMap(dnaHashes, (dnaHash) => {
const cell = this._dnaMap.get(dnaHash);
const keys = cell ? Array.from(cell.keys()) : [];
return keys.map((key) => [dnaHash, key]);
});
}
/**
* Returns all values in the map.
*
* @returns Array of all values
*/
values() {
return this.keys().map((dnaKey) => this.get(dnaKey));
}
/**
* Returns all entries as [[DnaHash, HoloHash], value] Arrays.
*
* @returns Array of all entries
*/
entries() {
return this.keys().map((dnaKey) => [dnaKey, this.get(dnaKey)]);
}
/**
* Creates a new DnaHoloHashMap containing only entries that match the filter predicate.
*
* @param fn - Predicate function to test each value
* @returns A new filtered map
*/
filter(fn) {
const entries = this.entries();
const mappedValues = entries.filter(([, v]) => fn(v));
return new DnaHoloHashMap(mappedValues);
}
/**
* Creates a new DnaHoloHashMap with values transformed by the mapping function.
*
* @param fn - Function to transform each value
* @returns A new mapped map
*/
map(fn) {
const entries = this.entries();
const mappedValues = entries.map(([id, v]) => [id, fn(v)]);
return new DnaHoloHashMap(mappedValues);
}
/**
* Returns all HoloHash keys for a specific DNA.
*
* @param dnaHash - The DNA hash to query
* @returns Array of HoloHash keys for this DNA
*/
keysForDna(dnaHash) {
const map = this._dnaMap.get(dnaHash);
return map ? Array.from(map.keys()) : [];
}
/**
* Returns all values for a specific DNA.
*
* @param dnaHash - The DNA hash to query
* @returns Array of values for this DNA
*/
valuesForDna(dnaHash) {
const map = this._dnaMap.get(dnaHash);
return map ? Array.from(map.values()) : [];
}
/**
* Returns all [HoloHash, value] entries for a specific DNA.
*
* @param dnaHash - The DNA hash to query
* @returns Array of entries for this DNA
*/
entriesForDna(dnaHash) {
const map = this._dnaMap.get(dnaHash);
return map ? Array.from(map.entries()) : [];
}
/**
* Removes all entries for a specific DNA.
*
* @param dnaHash - The DNA hash to clear
*/
clearForDna(dnaHash) {
const map = this._dnaMap.get(dnaHash);
if (map !== undefined) {
map.clear();
this._dnaMap.set(dnaHash, map);
}
}
/**
* The number of DNA entries in the map.
*
* @returns The number of unique DNAs
*/
get size() {
return this._dnaMap.size;
}
}
/**
* @public
*/
export class DnaAgentPubKeyMap extends DnaHoloHashMap {
}
/**
* @public
*/
export class DnaDnaHashMap extends DnaHoloHashMap {
}
/**
* @public
*/
export class DnaWasmHashMap extends DnaHoloHashMap {
}
/**
* @public
*/
export class DnaEntryHashMap extends DnaHoloHashMap {
}
/**
* @public
*/
export class DnaActionHashMap extends DnaHoloHashMap {
}
/**
* @public
*/
export class DnaAnyDhtHashMap extends DnaHoloHashMap {
}
/**
* @public
*/
export class DnaExternalHashMap extends DnaHoloHashMap {
}
/**
* @public
*/
export class DnaDhtOpHashMap extends DnaHoloHashMap {
}
/**
* @public
*/
export class DnaWarrantHashMap extends DnaHoloHashMap {
}
/**
* @public
*/
export class CellMap extends DnaAgentPubKeyMap {
}