Skip to content

Commit

Permalink
feat: add isReady method + remove waitFor deps (#18)
Browse files Browse the repository at this point in the history
* feat(SBTree): add isReady method

* feat: moved waitFor to use isReady method

* fix: fslock breaking change
  • Loading branch information
Alex-Werner authored May 18, 2020
1 parent 4924272 commit 71b1453
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 33 deletions.
22 changes: 12 additions & 10 deletions src/adapters/FsAdapter/methods/loadDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ const {each} = require('lodash');
module.exports = async function loadDatabase() {
const job = await this.queue.add('File.read', `${this.path}/sbtree.meta`);
await job.execution();
const db = job.results;
const {
leafs,
tree
} = db;
const db = job.result;
if(db){
const {
leafs,
tree
} = db;

if (tree) {
each(leafs, (leaf, leafName) => {
this.leafs[leafName] = {name: leafName, meta: new LeafMeta(leaf.meta)};
})
if (tree) {
each(leafs, (leaf, leafName) => {
this.leafs[leafName] = {name: leafName, meta: new LeafMeta(leaf.meta)};
})

await this.getParent().loadState(tree);
await this.getParent().loadState(tree);
}
}

this.isReady = true;
Expand Down
4 changes: 3 additions & 1 deletion src/adapters/FsAdapter/methods/ops/autosave.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module.exports = async function autosave(self){
await self.saveDatabase();
}
setTimeout(async ()=>{
await next(self);
if(self.autoSave){
await next(self);
}
}, self.autoSaveInterval)
}
await next(self);
Expand Down
16 changes: 12 additions & 4 deletions src/types/SBTree/SBTree.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const EventEmitter = require('events');
const {MemoryAdapter, FsAdapter} = require('../../adapters');
const {generateTreeId} = require('../../utils/crypto');
const {waitFor} = require('../../utils/fn');
const {each}=require('lodash');
// const SBFTree = require('../SBFTree/SBFTree');

Expand Down Expand Up @@ -35,13 +34,16 @@ class SBTree extends EventEmitter {
exclude:[],
uniques:[],
};

this.state = {
isReady: true
}
this.adapter = (props.adapter) ? parseAdapter(props.adapter) : new MemoryAdapter();
this.isReady = true;

if(this.adapter.name !== 'MemoryAdapter'){
// We will need to sync up first
this.isReady = false;
waitFor(self.adapter,'isReady', ()=> self.isReady = true);
this.state.isReady = false;
self.adapter.on('ready', ()=> self.state.isReady = true);
}

this.order= (props.order) ? props.order : defaultProps.order;
Expand Down Expand Up @@ -83,6 +85,12 @@ class SBTree extends EventEmitter {
order, fillFactor, verbose
}
}
async isReady() {
return new Promise((resolve) => {
if (this.state.isReady) return resolve(true);
this.on('ready', () => resolve(true));
});
}
}


Expand Down
5 changes: 2 additions & 3 deletions src/types/SBTree/methods/deleteDocuments.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const remove = require('../ops/remove');
const {waitFor} = require('../../../utils/fn');

async function deleteDocuments(query){
if(!query || query === {}){
// this would cause to delete all as we would query all.
throw new Error('Invalid query')
}
if(!this.isReady){
await waitFor(this, 'isReady');
if(!this.state.isReady){
await this.isReady();
}

return (await remove.call(this,query));
Expand Down
6 changes: 3 additions & 3 deletions src/types/SBTree/methods/findDocuments.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const query = require('../ops/query');
const {waitFor} = require('../../../utils/fn');

async function findDocuments(params){
if(!this.isReady){
await waitFor(this, 'isReady');
if(!this.state.isReady){
await this.isReady();
}

return (await query.call(this,params));

};
Expand Down
5 changes: 2 additions & 3 deletions src/types/SBTree/methods/getDocument.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const get = require('../ops/get');
const {waitFor} = require('../../../utils/fn');

async function getDocument(identifier){
if(!this.isReady){
await waitFor(this, 'isReady');
if(!this.state.isReady){
await this.isReady();
}

return (await get.call(this,identifier));
Expand Down
5 changes: 2 additions & 3 deletions src/types/SBTree/methods/insertDocuments.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const ObjectId = require('mongo-objectid');
const insert = require('../ops/insert');
const {waitFor} = require('../../../utils/fn');
const {cloneDeep}= require('lodash');

async function insertDocuments(documents) {
// This will wait for SBTree to have isReady = true.
// When so, it will then perform the insertion.
if(!this.isReady){
await waitFor(this, 'isReady');
if(!this.state.isReady){
await this.isReady();
}

if (Array.isArray(documents)) {
Expand Down
5 changes: 2 additions & 3 deletions src/types/SBTree/methods/replaceDocuments.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const replace = require('../ops/replace');
const {waitFor} = require('../../../utils/fn');

async function replaceDocuments(documents){
if(!this.isReady){
await waitFor(this, 'isReady');
if(!this.state.isReady){
await this.isReady();
}
if (Array.isArray(documents)) {
for (const document of documents) {
Expand Down
5 changes: 2 additions & 3 deletions src/types/SBTree/methods/updateDocuments.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const query = require('../ops/query');
const {waitFor} = require('../../../utils/fn');

async function findDocuments(params){
if(!this.isReady){
await waitFor(this, 'isReady');
if(!this.state.isReady){
await this.isReady();
}
return (await query.call(this,params));

Expand Down
11 changes: 11 additions & 0 deletions test/unit/types/SBTree/SBTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ describe('SBTree', () => {
expect(tree.fieldTrees).to.deep.equal({});
expect(tree.size).to.equal(0);
});
it('should provide isReady ', async function () {
const treeNeedReadiness = new SBTree({adapter: {name:'FsAdapter'}});
expect(treeNeedReadiness.state.isReady).to.equal(false);
await treeNeedReadiness.isReady();
expect(treeNeedReadiness.state.isReady).to.equal(true);

// Both of these are done to release any interval working
treeNeedReadiness.adapter.autoSave = false;
treeNeedReadiness.adapter.queue.stop();
});
return;
it('should correctly default', function () {
const t = new SBTree();
expect(t.order).to.equal(511)
Expand Down

0 comments on commit 71b1453

Please sign in to comment.