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

Feature submissions store #1443

Merged
merged 6 commits into from
Aug 10, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Using SubmissionsStore
  • Loading branch information
flbulgarelli committed Aug 3, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit cbbf016652a2dec0252042e61e8d97875e8f37fb
48 changes: 19 additions & 29 deletions app/assets/javascripts/mumuki_laboratory/application/bridge.js
Original file line number Diff line number Diff line change
@@ -22,13 +22,7 @@
* @typedef {{content?: {solution: object}, result?: SubmissionResult}} SubmissionAndResult
*/

/** @module mumuki.bridge */
mumuki.bridge = {};
mumuki.bridge.Laboratory = (() => {
/**
* @type {SubmissionAndResult}
*/
var lastSubmission = {};
mumuki.bridge = (() => {

class Laboratory {
// ==========
@@ -53,25 +47,26 @@ mumuki.bridge.Laboratory = (() => {
* Sends a solution object
*
* @param {Submission} submission the submission object
* @returns {JQuery.Promise<SubmissionResult>}
*/
_submitSolution(submission) {
if(this._lastSubmissionFinishedSuccessfully() && this._sameAsLastSolution(submission)){
return $.Deferred().resolve(lastSubmission.result);
const lastSubmission = mumuki.SubmissionsStore.getCachedResultFor(mumuki.currentExerciseId, submission);
if(lastSubmission){
return $.Deferred().resolve(lastSubmission);
} else {
return this._sendNewSolution(submission);
return this._sendNewSolution(submission).done(function (result) {
mumuki.SubmissionsStore.setLastSubmission(mumuki.currentExerciseId, this._buildLastSubmission(submission, result));
});
}
}

_asString(json){
return JSON.stringify(json);
}

_sameAsLastSolution(newSolution){
return this._asString(lastSubmission.content) === this._asString(newSolution);
}

_lastSubmissionFinishedSuccessfully() {
return lastSubmission.result && lastSubmission.result.status !== 'aborted';
/**
* @param {Submission} submission
* @param {SubmissionResult} result
* @returns {SubmissionAndResult}
*/
_buildLastSubmission(submission, result) {
return { content: {solution: submission.solution}, result: result }
}

/**
@@ -84,10 +79,7 @@ mumuki.bridge.Laboratory = (() => {
url: window.location.origin + window.location.pathname + '/solutions' + window.location.search,
data: submission
});

return $.ajax(request).then((result) => this._preRenderResult(result)).done(function (result) {
lastSubmission = { content: {solution: submission.solution}, result: result };
});
return $.ajax(request).then((result) => this._preRenderResult(result));
}

/**
@@ -102,9 +94,7 @@ mumuki.bridge.Laboratory = (() => {
}
}

mumuki.load(() => {
lastSubmission = {};
});

return Laboratory;
return {
Laboratory
};
})();
Original file line number Diff line number Diff line change
@@ -27,6 +27,12 @@
window.localStorage.setItem(this._keyFor(exerciseId), this._asString(submission));
}

/**
*
* @param {number} exerciseId
* @param {*} newSolution
* @returns {SubmissionResult}
*/
getCachedResultFor(exerciseId, newSolution) {
const lastSubmission = this.getLastSubmission(exerciseId);
if (!lastSubmission