Skip to content
This repository has been archived by the owner on Nov 23, 2022. It is now read-only.

Commit

Permalink
feat(lib): store top level cache.get
Browse files Browse the repository at this point in the history
  • Loading branch information
0x77dev committed Jan 27, 2022
1 parent 6a3d154 commit e7a55ff
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/lib/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,21 @@ export class Store {
await Promise.all([
drain(this.ipfs.dht.put(this.getDHTKey(key), value.bytes, { minPeers: 0 } as any)),
this.pubsub.publish('$store', { key, value: value.toString() })
])
]).catch(console.warn)
}

public async set<T = any>(key: string, data: T): Promise<Shard<T>> {
const shard = await Shard.create(this.ipfs, data, { namespace: key, store: this })

this.cache.set(key, shard.cid)
await this.emitUpdate(key, shard.cid).catch(console.warn)
await this.emitUpdate(key, shard.cid)

return shard
}

public async getFromDHT<T = any>(key: string, timeout = 1000): Promise<Shard<T>> {
// eslint-disable-next-line no-async-promise-executor
return new Promise<Shard>(async (resolve, reject) => {
const cached = this.cache.get(key)
if (cached) return resolve(Shard.from(this.ipfs, cached, { namespace: key, store: this }))

const interval = setTimeout(() => reject('timeout'), timeout)

for await (const event of this.ipfs.dht.get(this.getDHTKey(key))) {
Expand All @@ -150,10 +147,13 @@ export class Store {
return Shard.from(this.ipfs, CID.parse(value), { namespace: key, store: this })
}

public async get<T = any>(key: string, timeout = 1000): Promise<Shard<T>> {
public async get<T = any>(key: string, timeout = 1000): Promise<Shard<T> | void> {
const cached = this.cache.get(key)
if (cached) return Shard.from(this.ipfs, cached, { namespace: key, store: this })

return Promise.race([
this.getFromPubSub(key, timeout),
this.getFromDHT(key, timeout)
this.getFromPubSub(key, timeout).catch(console.warn),
this.getFromDHT(key, timeout).catch(console.warn)
])
}
}

0 comments on commit e7a55ff

Please sign in to comment.