UNPKG

@orbitdb/feed-db

Version:

Feed database type for orbit-db.

73 lines 2.02 kB
import { Database } from "@orbitdb/core"; const type = "feed"; const Feed = () => async ({ ipfs, identity, address, name, access, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically, onUpdate, }) => { const database = await Database({ ipfs, identity, address, name, access, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically, onUpdate, }); const { add, remove, iterator, all } = FeedApi({ database }); return { ...database, type, add, remove, iterator, all, }; }; Feed.type = type; export const FeedApi = ({ database }) => { const add = async (value) => { return database.addOperation({ op: "ADD", key: null, value }); }; const remove = async (hash) => { return database.addOperation({ op: "DEL", key: null, value: hash }); }; const iterator = async function* ({ amount, } = {}) { const vals = {}; let count = 0; for await (const entry of database.log.traverse()) { const { op, value } = entry.payload; const { hash } = entry; if (op === "ADD" && !vals[hash]) { count++; const hash = entry.hash; vals[hash] = true; yield { value, hash }; } else if (op === "DEL" && !vals[value]) { vals[value] = true; } if (amount !== undefined && count >= amount) { break; } } }; const all = async () => { const values = []; for await (const entry of iterator()) { values.unshift(entry); } return values; }; return { add, remove, iterator, all, }; }; FeedApi.type = type; export default Feed; //# sourceMappingURL=feed.js.map