Skip to content

Commit

Permalink
fix: update block events (#58)
Browse files Browse the repository at this point in the history
Reuse simplified blockstore interface and add block events
  • Loading branch information
achingbrain authored Mar 20, 2023
1 parent 42308c0 commit d33be53
Showing 1 changed file with 11 additions and 118 deletions.
129 changes: 11 additions & 118 deletions packages/interface/src/blocks.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import type { AbortOptions } from '@libp2p/interfaces'
import type { ProgressEvent, ProgressOptions } from 'progress-events'
import type { CID } from 'multiformats/cid'
import type { BitswapNotifyProgressEvents, BitswapWantProgressEvents } from 'ipfs-bitswap'
import type { AwaitIterable, Await } from './index.js'
import type { Blockstore } from 'interface-blockstore'

export interface Pair {
cid: CID
block: Uint8Array
}

export type HasBlockProgressEvents =
ProgressEvent<'blocks:put:duplicate', CID> |
ProgressEvent<'blocks:put:bitswap:notify', CID> |
ProgressEvent<'blocks:put:blockstore:put', CID> |
BitswapNotifyProgressEvents

export type PutBlockProgressEvents =
ProgressEvent<'blocks:put:duplicate', CID> |
ProgressEvent<'blocks:put:bitswap:notify', CID> |
Expand Down Expand Up @@ -43,121 +47,10 @@ export type DeleteBlockProgressEvents =
export type DeleteManyBlocksProgressEvents =
ProgressEvent<'blocks:delete-many:blockstore:delete-many'>

export interface Blocks extends Blockstore {
/**
* Check for the existence of a value for the passed key
*
* @example
* ```js
* const exists = await store.has(CID('bafyfoo'))
*
* if (exists) {
* console.log('it is there')
* } else {
* console.log('it is not there')
* }
*```
*/
has: (key: CID, options?: AbortOptions) => Await<boolean>

/**
* Store the passed block under the passed CID
*
* @example
*
* ```js
* await store.put(CID('bafyfoo'), new Uint8Array([0, 1, 2, 3]))
* ```
*/
put: (key: CID, val: Uint8Array, options?: AbortOptions & ProgressOptions<PutBlockProgressEvents>) => Await<void>

/**
* Store the given key/value pairs
*
* @example
* ```js
* const source = [{ cid: CID('bafyfoo'), block: new Uint8Array([0, 1, 2, 3]) }]
*
* for await (const { key, value } of store.putMany(source)) {
* console.info(`put content for key ${key}`)
* }
* ```
*/
putMany: (
source: AwaitIterable<Pair>,
options?: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents>
) => AwaitIterable<Pair>

/**
* Retrieve the value stored under the given key
*
* @example
* ```js
* const value = await store.get(CID('bafyfoo'))
* console.log('got content: %s', value.toString('utf8'))
* // => got content: datastore
* ```
*/
get: (key: CID, options?: AbortOptions & ProgressOptions<GetBlockProgressEvents>) => Await<Uint8Array>

/**
* Retrieve values for the passed keys
*
* @example
* ```js
* for await (const value of store.getMany([CID('bafyfoo')])) {
* console.log('got content:', new TextDecoder('utf8').decode(value))
* // => got content: datastore
* }
* ```
*/
getMany: (
source: AwaitIterable<CID>,
options?: AbortOptions & ProgressOptions<GetManyBlocksProgressEvents>
) => AwaitIterable<Uint8Array>

/**
* Retrieve all blocks in the blockstore
*
* @example
* ```js
* for await (const value of store.getAll()) {
* console.log('got content:', new TextDecoder('utf8').decode(value))
* // => got content: datastore
* }
* ```
*/
getAll: (
options?: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents>
) => AwaitIterable<Pair>

/**
* Remove the record for the passed key
*
* @example
*
* ```js
* await store.delete(CID('bafyfoo'))
* console.log('deleted awesome content :(')
* ```
*/
delete: (key: CID, options?: AbortOptions & ProgressOptions<DeleteBlockProgressEvents>) => Await<void>
export interface Blocks extends Blockstore<ProgressOptions<HasBlockProgressEvents>,
ProgressOptions<PutBlockProgressEvents>, ProgressOptions<PutManyBlocksProgressEvents>,
ProgressOptions<GetBlockProgressEvents>, ProgressOptions<GetManyBlocksProgressEvents>, ProgressOptions<GetAllBlocksProgressEvents>,
ProgressOptions<DeleteBlockProgressEvents>, ProgressOptions<DeleteManyBlocksProgressEvents>
> {

/**
* Remove values for the passed keys
*
* @example
*
* ```js
* const source = [CID('bafyfoo')]
*
* for await (const key of store.deleteMany(source)) {
* console.log(`deleted content with key ${key}`)
* }
* ```
*/
deleteMany: (
source: AwaitIterable<CID>,
options?: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents>
) => AwaitIterable<CID>
}

0 comments on commit d33be53

Please sign in to comment.