Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

wip: use content-decider to control what you replicate #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class BlockService {
*
* @param {IPFSRepo} ipfsRepo
*/
constructor (ipfsRepo) {
constructor (ipfsRepo, decider) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is optional can it please go in an options object?

this._repo = ipfsRepo
this._bitswap = null
this._decider = decider
}

/**
Expand Down Expand Up @@ -56,7 +57,8 @@ class BlockService {
* @param {Block} block
* @returns {Promise}
*/
put (block) {
async put (block) {
if (this._decider && await !this._decider(block)) return
if (this.hasExchange()) {
return this._bitswap.put(block)
} else {
Expand All @@ -70,7 +72,11 @@ class BlockService {
* @param {Array<Block>} blocks
* @returns {Promise}
*/
putMany (blocks) {
async putMany (blocks) {
if (this._decider) {
blocks = blocks.map(b => this._decider(b).then(allowed => allowed ? block : null))
blocks = (await Promise.all(blocks)).filter(x => x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: .filter(x => x) can also be written as .filter(Boolean).

}
if (this.hasExchange()) {
return this._bitswap.putMany(blocks)
} else {
Expand All @@ -85,6 +91,7 @@ class BlockService {
* @returns {Promise<Block>}
*/
get (cid) {
if (this._decider && !this._decider(cid)) throw new Error("Self-care error, this block is bad for you")
if (this.hasExchange()) {
return this._bitswap.get(cid)
} else {
Expand Down