Skip to content

Commit

Permalink
Issue #277 : Switch HeaderController to events instead of infinite se…
Browse files Browse the repository at this point in the history
…tTimeout
  • Loading branch information
juliandescottes committed Oct 5, 2015
1 parent 76dbb79 commit aae9655
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/js/Events.js
Expand Up @@ -36,6 +36,8 @@ var Events = {
*/
PISKEL_RESET: 'PISKEL_RESET',
PISKEL_SAVE_STATE: 'PISKEL_SAVE_STATE',
PISKEL_DESCRIPTOR_UPDATED : 'PISKEL_DESCRIPTOR_UPDATED',
PISKEL_SAVED_STATUS_UPDATE : 'PISKEL_SAVED_STATUS_UPDATE',

HISTORY_STATE_SAVED: 'HISTORY_STATE_SAVED',
HISTORY_STATE_LOADED: 'HISTORY_STATE_LOADED',
Expand Down
11 changes: 5 additions & 6 deletions src/js/controller/HeaderController.js
Expand Up @@ -5,12 +5,9 @@
* When embedded in piskelapp.com, the page adds a header containing the name of the currently edited sprite
* This controller will keep the displayed name in sync with the actual piskel name
*/
ns.HeaderController = function (piskelController, savedStatusService, interval) {
ns.HeaderController = function (piskelController, savedStatusService) {
this.piskelController = piskelController;
this.savedStatusService = savedStatusService;
this.interval = interval || 500;

this.updateHeader_ = this.updateHeader_.bind(this);
};

ns.HeaderController.prototype.init = function () {
Expand All @@ -19,6 +16,10 @@
$.subscribe(Events.BEFORE_SAVING_PISKEL, this.onBeforeSavingPiskelEvent_.bind(this));
$.subscribe(Events.AFTER_SAVING_PISKEL, this.onAfterSavingPiskelEvent_.bind(this));

$.subscribe(Events.PISKEL_DESCRIPTOR_UPDATED, this.updateHeader_.bind(this));
$.subscribe(Events.PISKEL_RESET, this.updateHeader_.bind(this));
$.subscribe(Events.PISKEL_SAVED_STATUS_UPDATE, this.updateHeader_.bind(this));

this.updateHeader_();
};

Expand All @@ -35,8 +36,6 @@
} catch (e) {
console.warn('Could not update header : ' + e.message);
}

window.setTimeout(this.updateHeader_, this.interval);
};

ns.HeaderController.prototype.onBeforeSavingPiskelEvent_ = function () {
Expand Down
2 changes: 2 additions & 0 deletions src/js/model/Piskel.js
Expand Up @@ -114,10 +114,12 @@

ns.Piskel.prototype.setDescriptor = function (descriptor) {
this.descriptor = descriptor;
$.publish(Events.PISKEL_DESCRIPTOR_UPDATED);
};

ns.Piskel.prototype.setName = function (name) {
this.descriptor.name = name;
$.publish(Events.PISKEL_DESCRIPTOR_UPDATED);
};

ns.Piskel.prototype.getHash = function () {
Expand Down
5 changes: 4 additions & 1 deletion src/js/service/SavedStatusService.js
Expand Up @@ -32,7 +32,10 @@

ns.SavedStatusService.prototype.updateDirtyStatus = function (status) {
var piskel = this.piskelController.getPiskel();
piskel.isDirty_ = status;
if (piskel.isDirty_ != status) {
piskel.isDirty_ = status;
$.publish(Events.PISKEL_SAVED_STATUS_UPDATE);
}
};

ns.SavedStatusService.prototype.isDirty = function (evt) {
Expand Down

0 comments on commit aae9655

Please sign in to comment.