diff options
author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-07-04 14:46:35 +0300 |
---|---|---|
committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-08-15 13:49:25 +0000 |
commit | c885797be41782bcbfcb3cdb6c55c4098cf03fa4 (patch) | |
tree | 66ab202491277d2e7320f8568d99b6a6bd3ee404 | |
parent | bc0a9b801ab7739fdbafccfbfcbfccdb88b8d682 (diff) | |
download | aconf-c885797be41782bcbfcb3cdb6c55c4098cf03fa4.tar.bz2 aconf-c885797be41782bcbfcb3cdb6c55c4098cf03fa4.tar.xz |
web client: eliminate race condition in validation
-rw-r--r-- | web/client.js | 45 |
1 files changed, 22 insertions, 23 deletions
diff --git a/web/client.js b/web/client.js index ae2546b..7072e4c 100644 --- a/web/client.js +++ b/web/client.js @@ -87,16 +87,11 @@ $(function() { var mpath = join(path, name); var value = data.data[name]; - - var prevTask; - if (mpath in invalid) - prevTask = invalid[mpath][1]; - if (!prevTask) { - prevTask = $.Deferred(); - prevTask.resolve(); - } - - prevTask.always(function() { + + var tn = _.isObject(newValue); + var npv = tn ? mpath : newValue; + + function validate() { var options; if (newValue != null) options = { @@ -119,22 +114,18 @@ $(function() { return; } - var task = objRequest(mpath, options); - - var tn = _.isObject(newValue); - if (tn) newValue = mpath; - invalid[mpath] = [newValue, task]; - - task.done(function() { + objRequest(mpath, options).done(function() { if (!(mpath in changed)) changed[mpath] = [value]; - if (newValue == changed[mpath][0]) + if (!tn && newValue == changed[mpath][0]) delete changed[mpath]; - else changed[mpath][1] = newValue; + else changed[mpath][1] = npv; - delete invalid[mpath]; - - function resolve() { def.resolve(isValid()); } + function resolve() { + var latest = invalid[mpath][1]; + if (latest == def) delete invalid[mpath]; + def.resolve(isValid()); + } if (tn) query(mpath).done(function(data) { if (data.meta.type == "model") @@ -157,7 +148,15 @@ $(function() { else resolve(); }).fail(function(xhr) { def.reject(xhr); }); - }); + } + + var prevTask; + if (mpath in invalid) prevTask = invalid[mpath][1]; + + invalid[mpath] = [npv, def]; + + if (prevTask) prevTask.always(validate); + else validate(); return def; }; |