Skip to content

Commit d33be53

Browse files
authored
fix: update block events (#58)
Reuse simplified blockstore interface and add block events
1 parent 42308c0 commit d33be53

File tree

1 file changed

+11
-118
lines changed

1 file changed

+11
-118
lines changed

packages/interface/src/blocks.ts

Lines changed: 11 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import type { AbortOptions } from '@libp2p/interfaces'
21
import type { ProgressEvent, ProgressOptions } from 'progress-events'
32
import type { CID } from 'multiformats/cid'
43
import type { BitswapNotifyProgressEvents, BitswapWantProgressEvents } from 'ipfs-bitswap'
5-
import type { AwaitIterable, Await } from './index.js'
64
import type { Blockstore } from 'interface-blockstore'
75

86
export interface Pair {
97
cid: CID
108
block: Uint8Array
119
}
1210

11+
export type HasBlockProgressEvents =
12+
ProgressEvent<'blocks:put:duplicate', CID> |
13+
ProgressEvent<'blocks:put:bitswap:notify', CID> |
14+
ProgressEvent<'blocks:put:blockstore:put', CID> |
15+
BitswapNotifyProgressEvents
16+
1317
export type PutBlockProgressEvents =
1418
ProgressEvent<'blocks:put:duplicate', CID> |
1519
ProgressEvent<'blocks:put:bitswap:notify', CID> |
@@ -43,121 +47,10 @@ export type DeleteBlockProgressEvents =
4347
export type DeleteManyBlocksProgressEvents =
4448
ProgressEvent<'blocks:delete-many:blockstore:delete-many'>
4549

46-
export interface Blocks extends Blockstore {
47-
/**
48-
* Check for the existence of a value for the passed key
49-
*
50-
* @example
51-
* ```js
52-
* const exists = await store.has(CID('bafyfoo'))
53-
*
54-
* if (exists) {
55-
* console.log('it is there')
56-
* } else {
57-
* console.log('it is not there')
58-
* }
59-
*```
60-
*/
61-
has: (key: CID, options?: AbortOptions) => Await<boolean>
62-
63-
/**
64-
* Store the passed block under the passed CID
65-
*
66-
* @example
67-
*
68-
* ```js
69-
* await store.put(CID('bafyfoo'), new Uint8Array([0, 1, 2, 3]))
70-
* ```
71-
*/
72-
put: (key: CID, val: Uint8Array, options?: AbortOptions & ProgressOptions<PutBlockProgressEvents>) => Await<void>
73-
74-
/**
75-
* Store the given key/value pairs
76-
*
77-
* @example
78-
* ```js
79-
* const source = [{ cid: CID('bafyfoo'), block: new Uint8Array([0, 1, 2, 3]) }]
80-
*
81-
* for await (const { key, value } of store.putMany(source)) {
82-
* console.info(`put content for key ${key}`)
83-
* }
84-
* ```
85-
*/
86-
putMany: (
87-
source: AwaitIterable<Pair>,
88-
options?: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents>
89-
) => AwaitIterable<Pair>
90-
91-
/**
92-
* Retrieve the value stored under the given key
93-
*
94-
* @example
95-
* ```js
96-
* const value = await store.get(CID('bafyfoo'))
97-
* console.log('got content: %s', value.toString('utf8'))
98-
* // => got content: datastore
99-
* ```
100-
*/
101-
get: (key: CID, options?: AbortOptions & ProgressOptions<GetBlockProgressEvents>) => Await<Uint8Array>
102-
103-
/**
104-
* Retrieve values for the passed keys
105-
*
106-
* @example
107-
* ```js
108-
* for await (const value of store.getMany([CID('bafyfoo')])) {
109-
* console.log('got content:', new TextDecoder('utf8').decode(value))
110-
* // => got content: datastore
111-
* }
112-
* ```
113-
*/
114-
getMany: (
115-
source: AwaitIterable<CID>,
116-
options?: AbortOptions & ProgressOptions<GetManyBlocksProgressEvents>
117-
) => AwaitIterable<Uint8Array>
118-
119-
/**
120-
* Retrieve all blocks in the blockstore
121-
*
122-
* @example
123-
* ```js
124-
* for await (const value of store.getAll()) {
125-
* console.log('got content:', new TextDecoder('utf8').decode(value))
126-
* // => got content: datastore
127-
* }
128-
* ```
129-
*/
130-
getAll: (
131-
options?: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents>
132-
) => AwaitIterable<Pair>
133-
134-
/**
135-
* Remove the record for the passed key
136-
*
137-
* @example
138-
*
139-
* ```js
140-
* await store.delete(CID('bafyfoo'))
141-
* console.log('deleted awesome content :(')
142-
* ```
143-
*/
144-
delete: (key: CID, options?: AbortOptions & ProgressOptions<DeleteBlockProgressEvents>) => Await<void>
50+
export interface Blocks extends Blockstore<ProgressOptions<HasBlockProgressEvents>,
51+
ProgressOptions<PutBlockProgressEvents>, ProgressOptions<PutManyBlocksProgressEvents>,
52+
ProgressOptions<GetBlockProgressEvents>, ProgressOptions<GetManyBlocksProgressEvents>, ProgressOptions<GetAllBlocksProgressEvents>,
53+
ProgressOptions<DeleteBlockProgressEvents>, ProgressOptions<DeleteManyBlocksProgressEvents>
54+
> {
14555

146-
/**
147-
* Remove values for the passed keys
148-
*
149-
* @example
150-
*
151-
* ```js
152-
* const source = [CID('bafyfoo')]
153-
*
154-
* for await (const key of store.deleteMany(source)) {
155-
* console.log(`deleted content with key ${key}`)
156-
* }
157-
* ```
158-
*/
159-
deleteMany: (
160-
source: AwaitIterable<CID>,
161-
options?: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents>
162-
) => AwaitIterable<CID>
16356
}

0 commit comments

Comments
 (0)