Skip to content

Instantly share code, notes, and snippets.

@thiagodelgado111
Created September 4, 2018 17:17
Show Gist options
  • Save thiagodelgado111/9103a5d024d3c06b37be4b6f03baf8bc to your computer and use it in GitHub Desktop.
Save thiagodelgado111/9103a5d024d3c06b37be4b6f03baf8bc to your computer and use it in GitHub Desktop.
StoreBuilder
class StoreBuilder {
constructor(StoreClass, ipfs, peerId, address) {
if (!StoreClass) throw new Error('Store class to build not defined')
if (!ipfs) throw new Error('IPFS is required')
if (!peerId) throw new Error('peerId is required')
if (!address) throw new Error('Store address is required')
this.StoreClass = StoreClass
this.ipfs = ipfs
this.peerId = peerId
this.address = address
this.options = {}
}
onClose(onClose) {
this.options = Object.assign({}, this.options, {
onClose,
})
return this
}
onInit(onInit) {
this.options = Object.assign({}, this.options, {
onInit,
})
return this
}
onWrite(onWrite) {
this.options = Object.assign({}, this.options, {
onWrite,
})
return this
}
accessController(accessController, keystore, key) {
this.options = Object.assign({}, this.options, {
accessController,
keystore,
key,
})
return this
}
flags({ create, localOnly, replicate }) {
this.options = Object.assign({}, this.options, {
create,
localOnly,
replicate,
})
return this
}
replication({ replicate, referenceCount, replicationConcurrency }) {
this.options = Object.assign({}, this.options, {
referenceCount,
replicationConcurrency,
})
return this
}
cache(cache) {
this.options = Object.assign({}, this.options, {
cache,
})
return this
}
index(IndexingStrategy) {
this.options = Object.assign({}, this.options, {
IndexingStrategy,
})
return this
}
build(builderOptions) {
// Use options to get any specific argument. Once we expose the builder, it won't
// be necessary anymore
const { StoreClass, address, ipfs, options, peerId } = this
return new StoreClass(ipfs, peerId, address, Object.assign({}, options, builderOptions))
}
}
module.exports = StoreBuilder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment