-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from spenceralger/config_transactions
Config transactions
- Loading branch information
Showing
5 changed files
with
141 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
define(function (require) { | ||
return function DelayedUpdaterFactory(Private, $rootScope, Promise, Notifier) { | ||
var notify = new Notifier(); | ||
var _ = require('lodash'); | ||
var angular = require('angular'); | ||
var vals = Private(require('./_vals')); | ||
|
||
return function DelayedUpdater(doc) { | ||
var updater = this; | ||
var queue = []; | ||
var log = {}; | ||
var timer; | ||
|
||
updater.fire = function () { | ||
clearTimeout(timer); | ||
|
||
// only fire once | ||
if (updater.fired) return; | ||
updater.fired = true; | ||
|
||
|
||
var method; | ||
var body; | ||
var updated = []; | ||
var deleted = []; | ||
|
||
// seperate the log into lists | ||
Object.keys(log).forEach(function (key) { | ||
if (log[key] === 'updated') updated.push(key); | ||
else deleted.push(key); | ||
}); | ||
|
||
if (deleted.length) { | ||
method = 'doIndex'; | ||
body = _.clone(vals); | ||
} else { | ||
method = 'doUpdate'; | ||
body = _.pick(vals, updated); | ||
} | ||
|
||
doc[method](vals) | ||
.then(function (resp) { | ||
queue.forEach(function (q) { q.resolve(resp); }); | ||
}, function (err) { | ||
queue.forEach(function (q) { q.reject(err); }); | ||
}); | ||
}; | ||
|
||
updater.update = function (key, val, silentAndLocal) { | ||
var newVal = val; | ||
var oldVal = vals[key]; | ||
|
||
if (angular.equals(newVal, oldVal)) { | ||
return Promise.resolve(); | ||
} | ||
else if (newVal == null) { | ||
delete vals[key]; | ||
log[key] = 'deleted'; | ||
} | ||
else { | ||
vals[key] = newVal; | ||
log[key] = 'updated'; | ||
} | ||
|
||
if (silentAndLocal) return Promise.resolve(); | ||
|
||
var defer = Promise.defer(); | ||
queue.push(defer); | ||
notify.log('config change: ' + key + ': ' + vals[key] + ' -> ' + val); | ||
$rootScope.$broadcast('change:config.' + key, newVal, oldVal); | ||
|
||
// reset the fire timer | ||
clearTimeout(timer); | ||
timer = setTimeout(updater.fire, 200); | ||
return defer.promise; | ||
}; | ||
}; | ||
|
||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
define(function (require) { | ||
return function ConfigValsService() { | ||
return {}; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters