Skip to content

Commit

Permalink
kill interval when controller scope destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwoulfe committed Feb 12, 2020
1 parent 34f65dc commit ce0ba41
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
8 changes: 4 additions & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# version format
version: 1.1.0.{build}
version: 1.2.0.{build}

image: Visual Studio 2017

Expand Down Expand Up @@ -39,9 +39,9 @@ deploy:
appveyor_repo_tag: true

- provider: GitHub
tag: v1.1.0
release: Release 1.1.0
description: "unwatch and just assume the worst"
tag: v1.2.0
release: Release 1.2.0
description: "kill interval when controller scope is destroyed"
force_update: true
auth_token:
secure: Otbl8p8qCwciDqJgSWCyN0Arfs5XS1CwiHcK+r0F6uz9Rxt4gzBFvlc3cjPV3NxR
Expand Down
26 changes: 19 additions & 7 deletions src/Preserver/backoffice/editor.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(() => {

function preserver($interval, $rootScope, $element, editorState, notificationsService) {
function preserver($scope, $interval, $rootScope, $element, editorState, notificationsService) {

const dataKey = `preserver_data_${editorState.current.id}`;

Expand All @@ -17,7 +17,7 @@
/**
* map local values back onto model
*/
const map = model => {
const mapToEditorModel = model => {
for (let m of model) {
let editorVariant = editorState.current.variants.find(x => x.language.culture === m.variant);
if (editorVariant) {
Expand Down Expand Up @@ -57,7 +57,7 @@
alias: t.alias,
properties: t.properties.filter(x => x.editor != 'preserver').map(p => {
return {
alias: p.alias,
alias: p.alias,
value: p.value
}
})
Expand All @@ -71,7 +71,7 @@
return JSON.stringify(model);
};


/**
* hide the property editor
*/
Expand All @@ -97,7 +97,7 @@
*/
$rootScope.$on('preserver.update', (e, data) => {
if (data.id === editorState.current.id) {
map(JSON.parse(fromStore));
mapToEditorModel(JSON.parse(fromStore));
}
});

Expand All @@ -107,12 +107,24 @@
*/
$rootScope.$on('content.saved', (e, data) => localStorage.removeItem(dataKey));


/**
* make sure the interval is killed along with the controller - otherwise continues to fire when changing sections.
*/
$scope.$on('$destroy', () => {
if (angular.isDefined(interval)) {
$interval.cancel(interval);
interval = undefined;
}
});


/**
* smash it into local storage every 10 seconds, if the node is dirty
*/
let interval;
if (editorState.current.variants) {
$interval(() => {
interval = $interval(() => {
if (editorState.current.variants.some(x => x.isDirty)) {
localStorage.setItem(dataKey, getBasicModel(editorState.current));
}
Expand All @@ -122,5 +134,5 @@

angular.module('preserver')
.controller('preserver.editor.controller',
['$interval', '$rootScope', '$element', 'editorState', 'notificationsService', preserver]);
['$scope', '$interval', '$rootScope', '$element', 'editorState', 'notificationsService', preserver]);
})();

0 comments on commit ce0ba41

Please sign in to comment.