@@ -35,6 +35,17 @@ interface DatastorePinnedBlock {
35
35
pinnedBy : Uint8Array [ ]
36
36
}
37
37
38
+ /**
39
+ * Callback for updating a {@link DatastorePinnedBlock}'s properties when
40
+ * calling `#updatePinnedBlock`
41
+ *
42
+ * The callback should return `false` to prevent any pinning modifications to
43
+ * the block, and true in all other cases.
44
+ */
45
+ interface WithPinnedBlockCallback {
46
+ ( pinnedBlock : DatastorePinnedBlock ) : boolean
47
+ }
48
+
38
49
const DATASTORE_PIN_PREFIX = '/pin/'
39
50
const DATASTORE_BLOCK_PREFIX = '/pinned-block/'
40
51
const DATASTORE_ENCODING = base36
@@ -82,11 +93,12 @@ export class PinsImpl implements Pins {
82
93
await this . #updatePinnedBlock( cid , ( pinnedBlock : DatastorePinnedBlock ) => {
83
94
// do not update pinned block if this block is already pinned by this CID
84
95
if ( pinnedBlock . pinnedBy . find ( c => uint8ArrayEquals ( c , cid . bytes ) ) != null ) {
85
- return
96
+ return false
86
97
}
87
98
88
99
pinnedBlock . pinCount ++
89
100
pinnedBlock . pinnedBy . push ( cid . bytes )
101
+ return true
90
102
} , options )
91
103
92
104
return cid
@@ -157,7 +169,7 @@ export class PinsImpl implements Pins {
157
169
/**
158
170
* Update the pin count for the CID
159
171
*/
160
- async #updatePinnedBlock ( cid : CID , withPinnedBlock : ( pinnedBlock : DatastorePinnedBlock ) => void , options : AddOptions ) : Promise < void > {
172
+ async #updatePinnedBlock ( cid : CID , withPinnedBlock : WithPinnedBlockCallback , options : AddOptions ) : Promise < void > {
161
173
const blockKey = new Key ( `${ DATASTORE_BLOCK_PREFIX } ${ DATASTORE_ENCODING . encode ( cid . multihash . bytes ) } ` )
162
174
163
175
let pinnedBlock : DatastorePinnedBlock = {
@@ -173,7 +185,10 @@ export class PinsImpl implements Pins {
173
185
}
174
186
}
175
187
176
- withPinnedBlock ( pinnedBlock )
188
+ const shouldContinue = withPinnedBlock ( pinnedBlock )
189
+ if ( ! shouldContinue ) {
190
+ return
191
+ }
177
192
178
193
if ( pinnedBlock . pinCount === 0 ) {
179
194
if ( await this . datastore . has ( blockKey ) ) {
@@ -197,9 +212,10 @@ export class PinsImpl implements Pins {
197
212
yield async ( ) => {
198
213
const cid = await promise ( )
199
214
200
- await this . #updatePinnedBlock( cid , ( pinnedBlock ) : void => {
215
+ await this . #updatePinnedBlock( cid , ( pinnedBlock ) : boolean => {
201
216
pinnedBlock . pinCount --
202
217
pinnedBlock . pinnedBy = pinnedBlock . pinnedBy . filter ( c => uint8ArrayEquals ( c , cid . bytes ) )
218
+ return true
203
219
} , {
204
220
...options ,
205
221
depth : pin . depth
0 commit comments