Created
September 4, 2018 17:17
-
-
Save thiagodelgado111/9103a5d024d3c06b37be4b6f03baf8bc to your computer and use it in GitHub Desktop.
StoreBuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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