;; The browser flavour of the Helia block/data stores.
;;
;; lib/log is not ours: it is compiled straight off the classpath from
;; ardegazu-rooms-kit, where the same namespace also serves a Node host, so it
;; may not name a store package at all:
;; `blockstore-idb`/`datastore-idb` exist only in a browser, and the Node side's
;; `blockstore-fs`/`datastore-fs` would drag node:fs into this bundle. lib/log's
;; `open-helia` therefore takes stores that are ALREADY OPEN, and this namespace
;; is the browser side that builds them — the twin of the kit's
;; ardegazu.rooms.node.stores.
;;
;; `open-idb-helia` keeps the store-NAMES signature the app has always called
;; `openHelia` with, so room.cljs and the black-box test helpers change only the
;; function they name. shadow-cljs.edn's :testlib exports it AS `openHelia`,
;; exactly as the kit exports its `open-fs-helia` under that name.
(ns sueta.stores
(:require ["blockstore-idb" :refer (IDBBlockstore)]
["datastore-idb" :refer (IDBDatastore)]
[ardegazu.rooms.lib.log :as log]
[shadow.cljs.modern :refer (js-await)]))
(defn open-idb-stores
"Construct and open the IndexedDB-backed pair from a {blocks, data} object of
NAMES (pass nsDb(\"blocks\")/nsDb(\"data\")). Two apps must not share a
blockstore — bitswap serves every local block to connected peers, and
GC/quota would mix across apps — so derive both from your own storage
namespace. Resolves to #js [blockstore datastore], both open."
[stores]
(let [blockstore (IDBBlockstore. (unchecked-get stores "blocks"))
datastore (IDBDatastore. (unchecked-get stores "data"))]
(js-await [_ (.open ^js blockstore)]
(js-await [_ (.open ^js datastore)]
#js [blockstore datastore]))))
(defn open-idb-helia
"lib/log's open-helia behind the historical store-NAMES signature: open the
IndexedDB stores named by `stores` {blocks, data}, then hand them to Helia."
[libp2p stores]
(js-await [pair (open-idb-stores stores)]
(log/open-helia libp2p (aget pair 0) (aget pair 1))))