Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
Stop waiting for previous result when a new save, run or route change…
Browse files Browse the repository at this point in the history
… occur
  • Loading branch information
prasmussen committed Jul 28, 2013
1 parent 03fc794 commit 31376b8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
13 changes: 11 additions & 2 deletions app/scripts/controllers/compose.js
Expand Up @@ -17,6 +17,7 @@ ComposeController.resolve = {
};

function ComposeController($scope, $rootScope, $routeParams, $location, Snippets, Runs, Utils, Segue, Url, settings, helloWorld, dbInfo) {
var waitingForResult;
var language = $routeParams.lang;
$scope.language = language;

Expand All @@ -43,6 +44,10 @@ function ComposeController($scope, $rootScope, $routeParams, $location, Snippets
}

$scope.run = function(code) {
if (waitingForResult) {
waitingForResult.stop();
}

$scope.result = {stdout: "Running..."};

// Check if result already exists in the database
Expand All @@ -61,19 +66,23 @@ function ComposeController($scope, $rootScope, $routeParams, $location, Snippets
};

$scope.save = function(name, code) {
if (waitingForResult) {
waitingForResult.stop();
}

Snippets.create(language, name, code)
.success(saveSuccess)
.error(saveError);
};

function waitForResult(doc) {
var stop = Runs.onResult(doc._id, dbInfo.update_seq, function(doc) {
waitingForResult = Runs.onResult(doc._id, dbInfo.update_seq, function(doc) {
$scope.result = doc.result;
});

// Stop listening for result if we leave the view
$rootScope.$on("$routeChangeStart", function() {
stop();
waitingForResult.stop();
});
}

Expand Down
18 changes: 16 additions & 2 deletions app/scripts/controllers/snippet.js
Expand Up @@ -14,7 +14,8 @@ SnippetController.resolve = {
}
};

function SnippetController($scope, $routeParams, $location, Utils, Segue, Snippets, Runs, Url, snippet, settings, dbInfo) {
function SnippetController($scope, $rootScope, $routeParams, $location, Utils, Segue, Snippets, Runs, Url, snippet, settings, dbInfo) {
var waitingForResult;
var language = $routeParams.lang;
$scope.language = language;

Expand All @@ -40,6 +41,10 @@ function SnippetController($scope, $routeParams, $location, Utils, Segue, Snippe
};

$scope.run = function(code) {
if (waitingForResult) {
waitingForResult.stop();
}

$scope.result = {stdout: "Running..."};

// Check if result already exists in the database
Expand All @@ -58,15 +63,24 @@ function SnippetController($scope, $routeParams, $location, Utils, Segue, Snippe
};

$scope.save = function(name, code) {
if (waitingForResult) {
waitingForResult.stop();
}

Snippets.update(snippet._id, language, name, code)
.success(saveSuccess)
.error(saveError);
};

function waitForResult(doc) {
Runs.onResult(doc._id, dbInfo.update_seq, function(doc) {
waitingForResult = Runs.onResult(doc._id, dbInfo.update_seq, function(doc) {
$scope.result = doc.result;
});

// Stop listening for result if we leave the view
$rootScope.$on("$routeChangeStart", function() {
waitingForResult.stop();
});
}

function saveSuccess(data) {
Expand Down

0 comments on commit 31376b8

Please sign in to comment.