@holochain/client
Version:
A JavaScript client for the Holochain Conductor API
313 lines (312 loc) • 6.72 kB
JavaScript
import { encodeHashToBase64, decodeHashFromBase64 } from "./base64.js";
/**
* A Map of HoloHashB64 to a value.
*
* @param initialEntries - Optional array of [key, value] pairs to insert into the map.
*
* @public
*/
export class HoloHashB64Map extends Map {
constructor(initialEntries) {
super();
if (initialEntries) {
for (const [key, value] of initialEntries) {
this.set(key, value);
}
}
}
}
/**
* @public
*/
export class AgentPubKeyB64Map extends HoloHashB64Map {
}
/**
* @public
*/
export class DnaHashB64Map extends HoloHashB64Map {
}
/**
* @public
*/
export class WasmHashB64Map extends HoloHashB64Map {
}
/**
* @public
*/
export class EntryHashB64Map extends HoloHashB64Map {
}
/**
* @public
*/
export class ActionHashB64Map extends HoloHashB64Map {
}
/**
* @public
*/
export class AnyDhtHashB64Map extends HoloHashB64Map {
}
/**
* @public
*/
export class ExternalHashB64Map extends HoloHashB64Map {
}
/**
* @public
*/
export class DhtOpHashB64Map extends HoloHashB64Map {
}
/**
* @public
*/
export class WarrantHashB64Map extends HoloHashB64Map {
}
/**
* A Map of HoloHash to a value.
*
* @param initialEntries - Optional array of [key, value] pairs to insert into the map.
*
* @public
*/
export class HoloHashMap {
_map;
constructor(initialEntries) {
const encodedEntries = initialEntries?.map(([k, v]) => [this._encodeKey(k), v]);
this._map = new HoloHashB64Map(encodedEntries);
}
/**
* Removes all entries from the map.
*/
clear() {
this._map.clear();
}
/**
* Returns an iterator of values in the map.
*
* @returns An iterator of all values
*/
values() {
return this._map.values();
}
/**
* Checks if a key exists in the map.
*
* @param key - The HoloHash key to check
* @returns True if the key exists, false otherwise
*/
has(key) {
const k = this._encodeKey(key);
return this._map.has(k);
}
/**
* Gets the value associated with a key.
*
* @param key - The HoloHash key
* @returns The value if found, undefined otherwise
*/
get(key) {
const k = this._encodeKey(key);
return this._map.get(k);
}
/**
* Sets a key-value pair in the map.
*
* @param key - The HoloHash key
* @param value - The value to store
* @returns This map instance for chaining
*/
set(key, value) {
const k = this._encodeKey(key);
this._map.set(k, value);
return this;
}
/**
* Deletes an entry from the map.
*
* @param key - The HoloHash key to delete
* @returns True if the entry was deleted, false if it didn't exist
*/
delete(key) {
const k = this._encodeKey(key);
return this._map.delete(k);
}
/**
* Returns an iterator of keys in the map.
*
* @returns An iterator of all HoloHash keys
*/
keys() {
return Array.from(this._map.keys())
.map((key) => this._decodeKey(key))[Symbol.iterator]();
}
/**
* Returns an iterator of [key, value] pairs.
*
* @returns An iterator of all entries
*/
entries() {
return Array.from(this._map.entries())
.map(([key, v]) => [this._decodeKey(key), v])[Symbol.iterator]();
}
/**
* Executes a callback function for each entry in the map.
*
* @param callbackfn - Function to execute for each entry
* @param thisArg - Optional value to use as 'this' when executing the callback
*/
forEach(callbackfn,
// This 'any' is inherited from the Map interface.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
thisArg) {
this._map.forEach((v, k) => {
callbackfn(v, this._decodeKey(k), this);
}, thisArg);
}
[Symbol.iterator]() {
return this.entries();
}
get [Symbol.toStringTag]() {
return this._map[Symbol.toStringTag];
}
/**
* The number of entries in the map.
*
* @returns The size of the map
*/
get size() {
return this._map.size;
}
_encodeKey(key) {
return encodeHashToBase64(key);
}
_decodeKey(encoded) {
return decodeHashFromBase64(encoded);
}
}
/**
* @public
*/
export class AgentPubKeyMap extends HoloHashMap {
}
/**
* @public
*/
export class DnaHashMap extends HoloHashMap {
}
/**
* @public
*/
export class WasmHashMap extends HoloHashMap {
}
/**
* @public
*/
export class EntryHashMap extends HoloHashMap {
}
/**
* @public
*/
export class ActionHashMap extends HoloHashMap {
}
/**
* @public
*/
export class AnyDhtHashMap extends HoloHashMap {
}
/**
* @public
*/
export class ExternalHashMap extends HoloHashMap {
}
/**
* @public
*/
export class DhtOpHashMap extends HoloHashMap {
}
/**
* @public
*/
export class WarrantHashMap extends HoloHashMap {
}
/**
* A HoloHashMap that will fetch and store a value if it is not found in a 'get' call.
*
* @param fetchValue - A function that takes a key and fetches its value.
* @param initialEntries - Optional array of [key, value] pairs to insert into the map.
*
* @public
*/
export class LazyHoloHashMap extends HoloHashMap {
fetchValue;
constructor(fetchValue, initialEntries) {
super(initialEntries);
this.fetchValue = fetchValue;
}
/**
*
* Get a value for a key.
*
* If no value for a key is found, it is fetched, inserted into the Map, then returned.
*
* @param key - HoloHash key
* @returns value if found
*/
get(key) {
const currentVal = super.get(key);
if (currentVal !== undefined) {
return currentVal;
}
else {
const val = this.fetchValue(key);
if (val !== undefined) {
super.set(key, val);
}
return val;
}
}
}
/**
* @public
*/
export class LazyAgentPubKeyMap extends LazyHoloHashMap {
}
/**
* @public
*/
export class LazyDnaHashMap extends LazyHoloHashMap {
}
/**
* @public
*/
export class LazyWasmHashMap extends LazyHoloHashMap {
}
/**
* @public
*/
export class LazyEntryHashMap extends LazyHoloHashMap {
}
/**
* @public
*/
export class LazyActionHashMap extends LazyHoloHashMap {
}
/**
* @public
*/
export class LazyAnyDhtHashMap extends LazyHoloHashMap {
}
/**
* @public
*/
export class LazyExternalHashMap extends LazyHoloHashMap {
}
/**
* @public
*/
export class LazyDhtOpHashMap extends LazyHoloHashMap {
}
/**
* @public
*/
export class LazyWarrantHashMap extends LazyHoloHashMap {
}