Skip to content

Commit

Permalink
improvement: Use new test waiters
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Jun 23, 2020
1 parent 1b619f4 commit 2bbc722
Show file tree
Hide file tree
Showing 4 changed files with 874 additions and 71 deletions.
63 changes: 13 additions & 50 deletions addon/services/indexed-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { Promise } from 'rsvp';
import { later } from '@ember/runloop';
import { typeOf as getTypeOf } from '@ember/utils';
import { A as array } from '@ember/array';
import {
registerWaiter,
unregisterWaiter,
} from 'ember-indexeddb/utils/test-waiter';
import { task, timeout } from 'ember-concurrency';
import { log } from 'ember-indexeddb/utils/log';
import Dexie from 'dexie';
import { buildWaiter, waitForPromise } from '@ember/test-waiters';

const testWaiter = buildWaiter('ember-indexeddb');

/**
* This service allows interacting with an IndexedDB database.
Expand Down Expand Up @@ -116,10 +115,7 @@ export default class IndexedDbService extends Service {
return get(this, 'db');
}

let testWaiter = function () {
return false;
};
registerWaiter(testWaiter);
let testWaiterToken = testWaiter.beginAsync();

let db = new Dexie(get(this, 'databaseName'));

Expand All @@ -130,7 +126,7 @@ export default class IndexedDbService extends Service {

yield openDb(db);

unregisterWaiter(testWaiter);
testWaiter.endAsync(testWaiterToken);

return db;
})
Expand Down Expand Up @@ -378,10 +374,7 @@ export default class IndexedDbService extends Service {
return;
}

let testWaiter = function () {
return false;
};
registerWaiter(testWaiter);
let testWaiterToken = testWaiter.beginAsync();

// Ensure the db is open
yield openDb(db);
Expand All @@ -392,7 +385,7 @@ export default class IndexedDbService extends Service {

set(this, 'db', null);

unregisterWaiter(testWaiter);
testWaiter.endAsync(testWaiterToken);
})
dropDatabaseTask;

Expand Down Expand Up @@ -650,40 +643,16 @@ export default class IndexedDbService extends Service {
* @private
*/
_addToPromiseQueue(promise) {
let wrappedPromise = waitForPromise(promise);

let promiseQueue = get(this, '_promiseQueue');
promiseQueue.pushObject(promise);
promiseQueue.pushObject(wrappedPromise);

let removeObject = () => {
promiseQueue.removeObject(promise);
promiseQueue.removeObject(wrappedPromise);
};
promise.finally(removeObject);
return promise;
}

/**
* Register the test waiter.
* This waiter checks if there are no promises left in the _promiseQueue.
*
* @method _registerTestWaiter
* @private
*/
_registerTestWaiter() {
let testWaiter = () => {
return get(this, '_promiseQueue.length') === 0;
};
registerWaiter(testWaiter);
set(this, '_testWaiter', testWaiter);
}

/**
* This removes the test waiter.
*
* @method _unregisterTestWaiter
* @private
*/
_unregisterTestWaiter() {
let testWaiter = get(this, '_testWaiter');
unregisterWaiter(testWaiter);
wrappedPromise.finally(removeObject);
return wrappedPromise;
}

constructor() {
Expand All @@ -694,12 +663,6 @@ export default class IndexedDbService extends Service {
} catch (e) {
this._supportsCompoundIndices = false;
}

this._registerTestWaiter();
}

willDestroy() {
this._unregisterTestWaiter();
}
}

Expand Down
18 changes: 0 additions & 18 deletions addon/utils/test-waiter.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"dexie": "^3.0.1",
"ember-auto-import": "^1.5.3",
"ember-cli-babel": "^7.18.0",
"ember-concurrency": "^1.1.6"
"ember-concurrency": "^1.1.6",
"ember-test-waiters": "^2.1.2"
},
"devDependencies": {
"@ember/optional-features": "^1.3.0",
Expand Down
Loading

0 comments on commit 2bbc722

Please sign in to comment.