Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Bug 977692 - [Clock] Refactor Ring View r=millermedeiros
Browse files Browse the repository at this point in the history
  • Loading branch information
mcav committed Apr 7, 2014
1 parent 54c83dc commit 2d8fda1
Show file tree
Hide file tree
Showing 71 changed files with 1,798 additions and 3,392 deletions.
4 changes: 3 additions & 1 deletion apps/clock/.jshintrc
Expand Up @@ -9,6 +9,8 @@
"mozRequestAnimationFrame": false,
"requestAnimationFrame": false,
"cancelAnimationFrame": false,
"Event": false
"Event": false,
"Proxy": false,
"Promise": false
}
}
46 changes: 35 additions & 11 deletions apps/clock/js/alarm.js
Expand Up @@ -26,12 +26,19 @@ define(function(require, exports, module) {
// Alarm Object

function Alarm(config) {
config = config || {};
if (config instanceof Alarm) {
config = config.toSerializable();
}
var econfig = Utils.extend(this.defaultProperties(), config || {});
this.extractProtected(econfig);
Utils.extend(this, econfig);

config = Utils.extend(this.defaultProperties(), config);
this.extractProtected(config);
Utils.extend(this, config);

// Normalize the alarm data. Pre-April-2014 code may have stored
// 'vibrate' and 'sound' as the string "0".
config.sound = (config.sound !== '0' ? config.sound : null);
config.vibrate = (config.vibrate && config.vibrate !== '0');
}

Alarm.prototype = {
Expand Down Expand Up @@ -90,19 +97,25 @@ define(function(require, exports, module) {
// Persisted form

toSerializable: function alarm_toSerializable() {
var retval = {};
var alarm = {};
for (var i in this) {
if (this.hasOwnProperty(i)) {
retval[i] = this[i];
alarm[i] = this[i];
}
}
for (var kv of protectedProperties) {
var prop = kv[0], map = kv[1];
if (map.has(this) && map.get(this) !== undefined) {
retval[prop] = map.get(this);
alarm[prop] = map.get(this);
}
}
return retval;

// Normalize the data. TODO: Perform this normalization immediately
// at the getter/setter level when this class is refactored.
alarm.sound = (alarm.sound !== '0' ? alarm.sound : null);
alarm.vibrate = (alarm.vibrate && alarm.vibrate !== '0');

return alarm;
},

// ---------------------------------------------------------
Expand Down Expand Up @@ -276,10 +289,18 @@ define(function(require, exports, module) {

delete: function alarm_delete(callback) {
this.cancel();
AlarmsDB.deleteAlarm(this.id,
function alarm_innerDelete(err, alarm) {
AlarmsDB.deleteAlarm(this.id, (err, alarm) => {
window.dispatchEvent(new CustomEvent('alarm-removed', {
detail: { alarm: this }
}));
callback(err, this);
}.bind(this));
});
},

_dispatchChangeNotification: function() {
window.dispatchEvent(new CustomEvent('alarm-changed', {
detail: { alarm: this }
}));
},

// ---------------------------------------------------------
Expand All @@ -300,6 +321,7 @@ define(function(require, exports, module) {
save: function alarm_save(callback) {
AlarmsDB.putAlarm(this, function(err, alarm) {
idMap.set(this, alarm.id);
this._dispatchChangeNotification();
callback && callback(err, this);
}.bind(this));
},
Expand All @@ -318,6 +340,7 @@ define(function(require, exports, module) {
var registeredAlarms = registeredAlarmsMap.get(this) || {};
registeredAlarms[type] = ev.target.result;
registeredAlarmsMap.set(this, registeredAlarms);
this._dispatchChangeNotification();
if (callback) {
callback(null, this);
}
Expand All @@ -329,7 +352,7 @@ define(function(require, exports, module) {
};
},

schedule: function alarm_schedule(options, callback) {
schedule: function(options, callback) {
/*
* Schedule
*
Expand All @@ -348,6 +371,7 @@ define(function(require, exports, module) {
* alarm parent.
*
*/

options = options || {}; // defaults
if (typeof options.type === 'undefined') {
options.type = 'normal';
Expand Down
4 changes: 0 additions & 4 deletions apps/clock/js/alarm_manager.js
Expand Up @@ -102,10 +102,6 @@ var AlarmManager = {
request.onerror = function(e) {
console.error('get all alarm fail');
};
},

regUpdateAlarmEnableState: function am_regUpdateAlarmEnableState(handler) {
this._updateAlarmEableStateHandler = handler;
}

};
Expand Down
2 changes: 1 addition & 1 deletion apps/clock/js/alarmsdb.js
Expand Up @@ -122,7 +122,7 @@ var BaseIndexDB = function(objectStoreOptions, upgradeHandler) {
callback && callback({
database: database,
store: storeName,
message: event.message,
message: e.message,
code: request.errorCode
});
};
Expand Down
1 change: 0 additions & 1 deletion apps/clock/js/app.js
Expand Up @@ -87,7 +87,6 @@ var App = {
*/
navigate: function(data, callback) {
var currentIndex = this.panels.indexOf(this.currentPanel);

this.panels.forEach(function(panel, panelIndex) {
if ('#' + panel.fragment === data.hash) {
this.loadPanel(panel, function() {
Expand Down
42 changes: 42 additions & 0 deletions apps/clock/js/async_queue.js
@@ -0,0 +1,42 @@
'use strict';
define(function(require) {

/**
* A serial queue of functions. Call `.push()` to add a function to
* the task; when each function completes (as a result of calling
* 'done()'), the next item in the queue will be executed.
*/
function AsyncQueue() {
this.queue = [];
this.current = null;
}

AsyncQueue.prototype = {
/**
* Add a function to the execution queue. It should accept one
* argument, a 'done' callback.
*/
push: function(fn) {
if (!this.current) {
this._startTask(fn);
} else {
this.queue.push(fn);
}
},

_startTask: function(fn) {
this.current = fn;
fn(this._nextTask.bind(this));
},

_nextTask: function() {
this.current = null;
if (this.queue.length) {
this._startTask(this.queue.shift());
}
}
};

return AsyncQueue;
});

0 comments on commit 2d8fda1

Please sign in to comment.