/*
	Copyright (c) 2004-2016, The JS Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/

//>>built
define("dojo/store/Observable",["../_base/lang","../when","../_base/array"],function(w,E,x){var H=function(a){function y(h,f){var d=a[h];d&&(a[h]=function(m,e){var r;"put"===h&&(r=a.getIdentity(m));if(z)return d.apply(this,arguments);z=!0;try{var n=d.apply(this,arguments);E(n,function(u){f("object"==typeof u&&u||m,r,e)});return n}finally{z=!1}})}var v=[],A=0;a=w.delegate(a);a.notify=function(h,f,d){A++;for(var m=v.slice(),e=0,r=m.length;e<r;e++)m[e](h,f,d)};var I=a.query;a.query=function(h,f){f=f||
{};var d=I.apply(this,arguments);if(d&&d.forEach){var m=w.mixin({},f);delete m.start;delete m.count;var e=a.queryEngine&&a.queryEngine(h,m),r=A,n=[],u;d.observe=function(F,J){1==n.push(F)&&v.push(u=function(g,B,t){var K=t&&t.before&&a.getIdentity(t.before);E(d,function(b){var p=b.length!=f.count,l;if(++r!=A)throw Error("Query is out of date, you must observe() the query prior to any data modifications");var q=-1,k=-1;if(void 0!==B){var G=[].concat(b);e&&!g&&(G=e(b));var c=0;for(l=b.length;c<l;c++){var C=
b[c];if(a.getIdentity(C)==B&&!(0>G.indexOf(C))){var L=C;q=c;!e&&g||b.splice(c,1);break}}}if(e){if(g&&(e.matches?e.matches(g):e([g]).length))if(c=-1<q?q:b.length,b.splice(c,0,g),k=x.indexOf(e(b),g),b.splice(c,1),f.start&&0==k||!p&&k==b.length)k=-1;else{if(t&&void 0!==t.before){if(null===t.before)c=b.length;else a:{c=a;p=l=void 0;p=void 0==p?b.length:p;for(l=void 0==l?0:l;l<p;++l)if(c.getIdentity(b[l])===K){c=l;break a}c=-1}-1!==c&&(k=c)}b.splice(k,0,g)}}else g&&(void 0!==B?k=q:f.start||(k=a.defaultIndex||
0,b.splice(k,0,g)));if((-1<q||-1<k)&&(J||!e||q!=k))for(p=n.slice(),c=0;b=p[c];c++)b(g||L,q,k)})});var D={};D.remove=D.cancel=function(){var g=x.indexOf(n,F);-1<g&&(n.splice(g,1),n.length||v.splice(x.indexOf(v,u),1))};return D}}return d};var z;y("put",function(h,f,d){a.notify(h,f,d)});y("add",function(h,f,d){a.notify(h,f,d)});y("remove",function(h){a.notify(void 0,h)});return a};w.setObject("dojo.store.Observable",H);return H});