Skip to content

Commit

Permalink
Port callbacks to promise where possible, #104
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Apr 7, 2020
1 parent 58a3946 commit b7b78ea
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 29 deletions.
23 changes: 11 additions & 12 deletions packages/core/lib/datasources/Datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,22 @@ class Datasource extends EventEmitter {

// Initialize the datasource asynchronously
initialize() {
setImmediate(() => {
let done = _.once((error) => {
if (error)
this.emit('error', error);
else {
try {
this._initialize()
.then(() => {
this.initialized = true;
this.emit('initialized');
}
});
try { this._initialize(done); }
catch (error) { done(error); }
});
})
.catch((error) => this.emit('error', error));
}
catch (error) {
this.emit('error', error);
}
}

// Prepares the datasource for querying
_initialize(done) {
done();
async _initialize() {

}

// Checks whether the data source can evaluate the given query
Expand Down
10 changes: 8 additions & 2 deletions packages/core/lib/datasources/MemoryDatasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ class MemoryDatasource extends Datasource {

// Prepares the datasource for querying
_initialize(done) {
let quadStore = this._quadStore = new N3Store();
this._getAllQuads((quad) => { quadStore.addQuad(quad); }, done);
return new Promise((resolve, reject) => {
let quadStore = this._quadStore = new N3Store();
this._getAllQuads((quad) => { quadStore.addQuad(quad); }, (error) => {
if (error)
return reject(error);
return resolve();
});
});
}

// Retrieves all quads in the datasource
Expand Down
15 changes: 9 additions & 6 deletions packages/core/test/datasources/Datasource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ describe('Datasource', () => {
});

describe('A Datasource instance with an initializer', () => {
let datasource, initializedListener, errorListener;
let datasource, initializedListener, errorListener, initResolver, initSpy;
before(() => {
datasource = new Datasource({ dataFactory });
datasource._initialize = sinon.stub();
datasource._initialize = () => new Promise((resolve) => initResolver = resolve);
initSpy = sinon.spy(datasource, '_initialize');
Object.defineProperty(datasource, 'supportedFeatures', {
value: { all: true },
});
Expand All @@ -131,7 +132,7 @@ describe('Datasource', () => {

describe('after construction', () => {
it('should have called the initializer', () => {
datasource._initialize.should.have.been.calledOnce;
initSpy.should.have.been.calledOnce;
});

it('should not be initialized', () => {
Expand All @@ -152,7 +153,7 @@ describe('Datasource', () => {

describe('after the initializer calls the callback', () => {
before(() => {
datasource._initialize.getCall(0).args[0]();
initResolver();
});

it('should be initialized', () => {
Expand Down Expand Up @@ -185,7 +186,8 @@ describe('Datasource', () => {
before(() => {
datasource = new Datasource({ dataFactory });
error = new Error('initializer error');
datasource._initialize = sinon.stub().throws(error);
datasource._initialize = () => { throw error; };
sinon.spy(datasource, '_initialize');
datasource.on('initialized', initializedListener = sinon.stub());
datasource.on('error', errorListener = sinon.stub());
datasource.initialize();
Expand Down Expand Up @@ -216,7 +218,8 @@ describe('Datasource', () => {
before(() => {
datasource = new Datasource({ dataFactory });
error = new Error('initializer error');
datasource._initialize = sinon.stub().callsArgWith(0, error);
datasource._initialize = () => Promise.reject(error);
sinon.spy(datasource, '_initialize');
datasource.on('initialized', initializedListener = sinon.stub());
datasource.on('error', errorListener = sinon.stub());
datasource.initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ class ExternalHdtDatasource extends Datasource {
}

// Prepares the datasource for querying
_initialize(done) {
async _initialize() {
if (this._options.checkFile !== false) {
if (!fs.existsSync(this._hdtFile))
return done(new Error('Not an HDT file: ' + this._hdtFile));
throw new Error('Not an HDT file: ' + this._hdtFile);
if (!fs.existsSync(hdtUtility))
return done(new Error('hdt not found: ' + hdtUtility));
throw new Error('hdt not found: ' + hdtUtility);
}
done();
}

// Writes the results of the query to the given quad stream
Expand Down
7 changes: 2 additions & 5 deletions packages/datasource-hdt/lib/datasources/HdtDatasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ class HdtDatasource extends Datasource {
}

// Loads the HDT datasource
_initialize(done) {
let datasource = this;
hdt.fromFile(this._hdtFile).then((hdtDocument) => {
datasource._hdtDocument = hdtDocument;
}).then(done, done);
async _initialize() {
this._hdtDocument = await hdt.fromFile(this._hdtFile);
}

// Writes the results of the query to the given quad stream
Expand Down

0 comments on commit b7b78ea

Please sign in to comment.