Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update ember-concurrency task implementations #7645

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions packages/frontend/app/components/assign-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,16 @@ export default class AssignStudentsComponent extends Component {
return this.args.students.length - this.savedUserIds.length;
}

@restartableTask
*load(element, [school]) {
let cohorts = yield this.store.query('cohort', {
load = restartableTask(async (element, [school]) => {
let cohorts = await this.store.query('cohort', {
filters: {
schools: [school.id],
},
});

//prefetch programYears and programs so that ember data will coalesce these requests.
const programYears = yield all(mapBy(cohorts.slice(), 'programYear'));
yield all(mapBy(programYears.slice(), 'program'));
const programYears = await all(mapBy(cohorts.slice(), 'programYear'));
await all(mapBy(programYears.slice(), 'program'));

cohorts = cohorts.slice();
const allCohorts = [];
Expand All @@ -60,8 +59,8 @@ export default class AssignStudentsComponent extends Component {
id: cohort.id,
model: cohort,
};
const programYear = yield cohort.programYear;
const program = yield programYear.program;
const programYear = await cohort.programYear;
const program = await programYear.program;
obj.title = program.title + ' ' + cohort.title;
obj.startYear = programYear.startYear;
obj.duration = program.duration;
Expand All @@ -74,7 +73,7 @@ export default class AssignStudentsComponent extends Component {
const finalYear = Number(obj.startYear) + Number(obj.duration);
return finalYear > lastYear;
});
}
});

@action
toggleCheck() {
Expand All @@ -93,8 +92,7 @@ export default class AssignStudentsComponent extends Component {
}
}

@dropTask
*save() {
save = dropTask(async () => {
this.savedUserIds = [];
const ids = this.selectedUserIds;
const cohort = this.bestSelectedCohort;
Expand All @@ -109,11 +107,11 @@ export default class AssignStudentsComponent extends Component {

while (studentsToModify.get('length') > 0) {
const parts = studentsToModify.splice(0, 3);
yield all(parts.map((part) => part.save()));
await all(parts.map((part) => part.save()));
this.savedUserIds = [...this.savedUserIds, ...mapBy(parts, 'id')];
}
this.selectedUserIds = [];

this.flashMessages.success('general.savedSuccessfully');
}
});
}
71 changes: 32 additions & 39 deletions packages/frontend/app/components/bulk-new-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,12 @@ export default class BulkNewUsersComponent extends Component {
return this.cohorts.slice().reverse()[0];
}

@restartableTask
*load() {
const user = yield this.currentUser.getModel();
this.primarySchool = yield user.school;
this.schools = yield this.loadSchools.perform();
this.cohorts = yield this.loadCohorts.perform(this.bestSelectedSchool);
}
load = restartableTask(async () => {
const user = await this.currentUser.getModel();
this.primarySchool = await user.school;
this.schools = await this.loadSchools.perform();
this.cohorts = await this.loadCohorts.perform(this.bestSelectedSchool);
});

@action
toggleUserSelection(obj) {
Expand Down Expand Up @@ -144,53 +143,49 @@ export default class BulkNewUsersComponent extends Component {
});
}

@restartableTask
*updateSelectedFile(files) {
updateSelectedFile = restartableTask(async (files) => {
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
if (files.length > 0) {
yield this.parseFile.perform(files[0]);
await this.parseFile.perform(files[0]);
}
} else {
throw new Error(this.intl.t('general.unsupportedBrowserFailure'));
}
}
});

@restartableTask
*setSchool(id) {
setSchool = restartableTask(async (id) => {
this.schoolId = id;
this.cohorts = yield this.loadCohorts.perform(this.bestSelectedSchool);
}
this.cohorts = await this.loadCohorts.perform(this.bestSelectedSchool);
});

@restartableTask
*parseFile(file) {
const proposedUsers = yield this.getFileContents(file);
const existingUsernames = yield this.existingUsernames();
parseFile = restartableTask(async (file) => {
const proposedUsers = await this.getFileContents(file);
const existingUsernames = await this.existingUsernames();
const filledOutUsers = proposedUsers.map((obj) => {
obj.existingUsernames = existingUsernames;

return obj;
});
this.validUsers = yield filter(filledOutUsers, async (obj) => {
this.validUsers = await filter(filledOutUsers, async (obj) => {
return await obj.isValid();
});

this.selectedUsers = this.validUsers;
this.proposedUsers = filledOutUsers;
}
});

@dropTask
*save() {
save = dropTask(async () => {
this.savedUserIds = [];
const nonStudentMode = this.nonStudentMode;
const selectedSchool = this.bestSelectedSchool;
const selectedCohort = this.bestSelectedCohort;
const roles = yield this.store.findAll('user-role');
const roles = await this.store.findAll('user-role');
const studentRole = findById(roles.slice(), '4');

const proposedUsers = this.selectedUsers;

const validUsers = yield filter(proposedUsers, async (obj) => {
const validUsers = await filter(proposedUsers, async (obj) => {
return obj.isValid();
});

Expand Down Expand Up @@ -244,9 +239,9 @@ export default class BulkNewUsersComponent extends Component {
try {
parts = records.splice(0, 10);
const users = mapBy(parts, 'user');
yield all(users.map((user) => user.save()));
await all(users.map((user) => user.save()));
const authentications = mapBy(parts, 'authentication');
yield all(authentications.map((auth) => auth.save()));
await all(authentications.map((auth) => auth.save()));
} catch (e) {
const userErrors = parts.filter((obj) => obj.user.get('isError'));
const authenticationErrors = parts.filter(
Expand Down Expand Up @@ -274,29 +269,27 @@ export default class BulkNewUsersComponent extends Component {
this.validUsers = [];
this.selectedUsers = [];
this.proposedUsers = [];
}
});

@restartableTask
*loadSchools() {
const schools = yield this.store.findAll('school', { reload: true });
loadSchools = restartableTask(async () => {
const schools = await this.store.findAll('school', { reload: true });
return filter(schools.slice(), async (school) => {
return this.permissionChecker.canCreateUser(school);
});
}
});

@restartableTask
*loadCohorts(school) {
const cohorts = yield this.store.query('cohort', {
loadCohorts = restartableTask(async (school) => {
const cohorts = await this.store.query('cohort', {
filters: {
schools: [school.id],
},
});

//prefetch programYears and programs so that ember data will coalesce these requests.
const programYears = yield all(mapBy(cohorts.slice(), 'programYear'));
yield all(mapBy(programYears.slice(), 'program'));
const programYears = await all(mapBy(cohorts.slice(), 'programYear'));
await all(mapBy(programYears.slice(), 'program'));

const objects = yield all(
const objects = await all(
cohorts.slice().map(async (cohort) => {
const obj = {
id: cohort.get('id'),
Expand All @@ -317,7 +310,7 @@ export default class BulkNewUsersComponent extends Component {
const finalYear = parseInt(obj.startYear, 10) + parseInt(obj.duration, 10);
return finalYear > lastYear;
});
}
});
}

@validatable
Expand Down
7 changes: 3 additions & 4 deletions packages/frontend/app/components/competency-title-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ export default class CompetencyTitleEditorComponent extends Component {
this.title = this.args.competency.title;
}

@dropTask
*save() {
save = dropTask(async () => {
this.addErrorDisplayFor('title');
const isValid = yield this.isValid('title');
const isValid = await this.isValid('title');
if (!isValid) {
return false;
}
this.args.competency.set('title', this.title);
this.removeErrorDisplayFor('title');
}
});
}
18 changes: 8 additions & 10 deletions packages/frontend/app/components/connection-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,43 +49,41 @@ export default class ConnectionStatusComponent extends Component {
}
}

@restartableTask
*changeConnectionState(isOnline) {
changeConnectionState = restartableTask(async (isOnline) => {
this.timer = 5;
this.multiplier = 1;
this.stopAttemptingToReconnect = false;
this.isOnline = isOnline;
if (!isOnline) {
yield this.reconnect.perform();
await this.reconnect.perform();
} else {
this.reconnect.cancelAll();
}
}
});

@restartableTask
*reconnect(force) {
reconnect = restartableTask(async (force) => {
if (navigator.onLine) {
this.changeConnectionState.perform(true);
}
if (force) {
this.unableToReconnect = true;
this.timer = 5;
yield timeout(2000);
await timeout(2000);
this.unableToReconnect = false;
} else if (this.timer > 1) {
this.unableToReconnect = false;
this.timer = this.timer - 1;
} else {
if (!this.stopAttemptingToReconnect) {
this.unableToReconnect = true;
yield timeout(2000);
await timeout(2000);
}
const newMultiplier = this.multiplier < 8 ? this.multiplier * 2 : 10;
this.multiplier = newMultiplier;
this.timer = 5 * newMultiplier;
}

yield timeout(1000);
await timeout(1000);
this.reconnect.perform();
}
});
}
18 changes: 8 additions & 10 deletions packages/frontend/app/components/courses/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,23 @@ export default class CoursesListComponent extends Component {
return this.intl.t(translation);
}

@task
*unlockCourse(course) {
const permission = yield this.permissionChecker.canUnlockCourse(course);
unlockCourse = task(async (course) => {
const permission = await this.permissionChecker.canUnlockCourse(course);
this.startSavingCourse(course.id);
if (permission) {
yield this.args.unlock(course);
await this.args.unlock(course);
this.stopSavingCourse(course.id);
}
}
});

@task
*lockCourse(course) {
const permission = yield this.permissionChecker.canUpdateCourse(course);
lockCourse = task(async (course) => {
const permission = await this.permissionChecker.canUpdateCourse(course);
this.startSavingCourse(course.id);
if (permission) {
yield this.args.lock(course);
await this.args.lock(course);
this.stopSavingCourse(course.id);
}
}
});

@action
confirmRemoval(course) {
Expand Down
16 changes: 7 additions & 9 deletions packages/frontend/app/components/courses/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,18 @@ export default class CoursesNewComponent extends Component {
}
}

@restartableTask
*load() {
this.academicYearCrossesCalendarYearBoundaries = yield this.iliosConfig.itemFromConfig(
load = restartableTask(async () => {
this.academicYearCrossesCalendarYearBoundaries = await this.iliosConfig.itemFromConfig(
'academicYearCrossesCalendarYearBoundaries',
);
if (this.args.currentYear && this.years.includes(parseInt(this.args.currentYear.id, 10))) {
this.setYear(this.args.currentYear.id);
}
}
});

@dropTask
*saveCourse() {
saveCourse = dropTask(async () => {
this.addErrorDisplayFor('title');
const isValid = yield this.isValid();
const isValid = await this.isValid();
if (!isValid) {
return false;
}
Expand All @@ -69,6 +67,6 @@ export default class CoursesNewComponent extends Component {
school: this.args.currentSchool,
year: this.selectedYear,
});
yield this.args.save(course);
}
await this.args.save(course);
});
}
16 changes: 7 additions & 9 deletions packages/frontend/app/components/courses/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,22 @@ export default class CoursesRootComponent extends Component {
return defaultYear;
}

@dropTask
*removeCourse(course) {
const courses = (yield this.selectedSchool.courses).slice();
removeCourse = dropTask(async (course) => {
const courses = (await this.selectedSchool.courses).slice();
courses.splice(courses.indexOf(course), 1);
this.selectedSchool.set('courses', courses);
yield course.destroyRecord();
await course.destroyRecord();
this.deletedCourse = course;
if (this.newCourse === course) {
this.newCourse = null;
}
}
});

@dropTask
*saveNewCourse(newCourse) {
saveNewCourse = dropTask(async (newCourse) => {
newCourse.setDatesBasedOnYear();
this.newCourse = yield newCourse.save();
this.newCourse = await newCourse.save();
this.showNewCourseForm = false;
}
});

@action
lockCourse(course) {
Expand Down
Loading
Loading