Skip to content

Commit

Permalink
Removing concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
bakerac4 committed Apr 3, 2019
1 parent 8b011c3 commit 8d9fbaf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 88 deletions.
60 changes: 46 additions & 14 deletions addon/mixins/controller-pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,70 @@ import { readOnly, or } from '@ember/object/computed';
import { tryInvoke } from '@ember/utils';
import { reject } from 'rsvp';
import { A } from '@ember/array';
import { task } from 'ember-concurrency';
import { math, divide, string, bool } from 'ember-awesome-macros';
import { buildQueryParams } from 'gavant-pagination/utils/query-params';

export default Mixin.create({
router: service(),
loadingBar: service(),
sort: A(),
hasMore: true,
limit: 10,
isLoadingPage: false,

isLoadingRoute: bool(string.match('router.currentRouteName', /loading$/)),
isLoadingModels: or('loadModelsTask.isRunning', 'isLoadingRoute'),
offset: readOnly('model.length'),
pagesLoaded: math.ceil(divide('model.length', 'limit')),

loadModelsTask: task(function * (reset, params) {
get(this, 'loadingBar').start();
async _loadModels(reset, params) {
this.isLoadingPage = true;
if(reset) {
this.clearModels();
}

const offset = get(this, 'offset');
const limit = get(this, 'limit');
const queryParams = this.buildQueryParams(this, params, offset, limit);
const result = yield this.fetchModels(queryParams);
const models = result.toArray();
const queryParams = buildQueryParams(this, params, offset, limit);
let models = [];
try {
const result = await this.fetchModels(queryParams);
models = result.toArray();

setProperties(this, {
metadata: get(result, 'meta'),
hasMore: get(models, 'length') >= limit
});

setProperties(this, {
metadata: get(result, 'meta'),
hasMore: get(models, 'length') >= limit
});
tryInvoke(get(this, 'model'), 'pushObjects', [models]);
} catch(errors) {
reject(errors);
}

tryInvoke(get(this, 'model'), 'pushObjects', [models]);
get(this, 'loadingBar').stop();
return models;
}).restartable(),
},

// loadModelsTask: task(function * (reset, params) {
// get(this, 'loadingBar').start();
// if(reset) {
// this.clearModels();
// }
//
// const offset = get(this, 'offset');
// const limit = get(this, 'limit');
// const queryParams = this.buildQueryParams(this, params, offset, limit);
// const result = yield this.fetchModels(queryParams);
// const models = result.toArray();
//
// setProperties(this, {
// metadata: get(result, 'meta'),
// hasMore: get(models, 'length') >= limit
// });
//
// tryInvoke(get(this, 'model'), 'pushObjects', [models]);
// get(this, 'loadingBar').stop();
// return models;
// }).restartable(),

//override this method if more complex logic is necessary to retrieve the records
//it should return a promise, which resolves to an array-like object (such as a DS.RecordArray)
Expand All @@ -50,7 +78,11 @@ export default Mixin.create({
},

loadModels(reset, params) {
return get(this, 'loadModelsTask').perform(reset, params);
if (!get(this, 'isLoadingPage')) {
return this._loadModels(reset, params);
} else {
return;
}
},

clearModels() {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"ember-cli-template-lint": "^1.0.0-beta.1",
"ember-cli-typescript-blueprints": "^2.0.0",
"ember-cli-uglify": "^2.1.0",
"ember-concurrency": "^0.9.0",
"ember-data": "^3.8.0",
"ember-disable-prototype-extensions": "^1.1.3",
"ember-export-application-global": "^2.0.0",
Expand Down
73 changes: 0 additions & 73 deletions types/ember-concurrency/index.d.ts

This file was deleted.

0 comments on commit 8d9fbaf

Please sign in to comment.