Issue #277 : Switch HeaderController to events instead of infinite setTimeout

This commit is contained in:
jdescottes 2015-10-01 01:07:29 +02:00
parent 669d7a21cb
commit b859857b2d
4 changed files with 13 additions and 7 deletions

View File

@ -36,6 +36,8 @@ var Events = {
*/ */
PISKEL_RESET: 'PISKEL_RESET', PISKEL_RESET: 'PISKEL_RESET',
PISKEL_SAVE_STATE: 'PISKEL_SAVE_STATE', 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_SAVED: 'HISTORY_STATE_SAVED',
HISTORY_STATE_LOADED: 'HISTORY_STATE_LOADED', HISTORY_STATE_LOADED: 'HISTORY_STATE_LOADED',

View File

@ -5,12 +5,9 @@
* When embedded in piskelapp.com, the page adds a header containing the name of the currently edited sprite * 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 * 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.piskelController = piskelController;
this.savedStatusService = savedStatusService; this.savedStatusService = savedStatusService;
this.interval = interval || 500;
this.updateHeader_ = this.updateHeader_.bind(this);
}; };
ns.HeaderController.prototype.init = function () { ns.HeaderController.prototype.init = function () {
@ -19,6 +16,10 @@
$.subscribe(Events.BEFORE_SAVING_PISKEL, this.onBeforeSavingPiskelEvent_.bind(this)); $.subscribe(Events.BEFORE_SAVING_PISKEL, this.onBeforeSavingPiskelEvent_.bind(this));
$.subscribe(Events.AFTER_SAVING_PISKEL, this.onAfterSavingPiskelEvent_.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_(); this.updateHeader_();
}; };
@ -35,8 +36,6 @@
} catch (e) { } catch (e) {
console.warn('Could not update header : ' + e.message); console.warn('Could not update header : ' + e.message);
} }
window.setTimeout(this.updateHeader_, this.interval);
}; };
ns.HeaderController.prototype.onBeforeSavingPiskelEvent_ = function () { ns.HeaderController.prototype.onBeforeSavingPiskelEvent_ = function () {

View File

@ -114,10 +114,12 @@
ns.Piskel.prototype.setDescriptor = function (descriptor) { ns.Piskel.prototype.setDescriptor = function (descriptor) {
this.descriptor = descriptor; this.descriptor = descriptor;
$.publish(Events.PISKEL_DESCRIPTOR_UPDATED);
}; };
ns.Piskel.prototype.setName = function (name) { ns.Piskel.prototype.setName = function (name) {
this.descriptor.name = name; this.descriptor.name = name;
$.publish(Events.PISKEL_DESCRIPTOR_UPDATED);
}; };
ns.Piskel.prototype.getHash = function () { ns.Piskel.prototype.getHash = function () {

View File

@ -32,7 +32,10 @@
ns.SavedStatusService.prototype.updateDirtyStatus = function (status) { ns.SavedStatusService.prototype.updateDirtyStatus = function (status) {
var piskel = this.piskelController.getPiskel(); 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) { ns.SavedStatusService.prototype.isDirty = function (evt) {