mobservable
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -760,2 +760,21 @@ var __extends = this.__extends || function (d, b) { | ||
})(); | ||
mobservable.mobservableStatic.ObserverMixin = { | ||
componentWillMount: function () { | ||
var baseRender = this.render; | ||
this.render = function () { | ||
var _this = this; | ||
if (this._watchDisposer) | ||
this._watchDisposer(); | ||
var _a = mobservable.mobservableStatic.watch(function () { return baseRender.call(_this); }, function () { | ||
_this.forceUpdate(); | ||
}), rendering = _a[0], disposer = _a[1]; | ||
this._watchDisposer = disposer; | ||
return rendering; | ||
}; | ||
}, | ||
componentWillUnmount: function () { | ||
if (this._watchDisposer) | ||
this._watchDisposer(); | ||
} | ||
}; | ||
function quickDiff(current, base) { | ||
@@ -762,0 +781,0 @@ if (!base.length) |
@@ -1,1 +0,1 @@ | ||
var __extends=this.__extends||function(a,b){function c(){this.constructor=a}for(var d in b)b.hasOwnProperty(d)&&(a[d]=b[d]);c.prototype=b.prototype,a.prototype=new c},mobservable;!function(a){function b(b,c){return Array.isArray(b)?new l(b):"function"==typeof b?a.mobservableStatic.computed(b,c):a.mobservableStatic.primitive(b)}function c(a,b){if(!b.length)return[a,[]];if(!a.length)return[[],b];for(var c=[],d=[],e=0,f=0,g=a.length,h=!1,i=0,j=0,k=b.length,l=!1,m=!1;!m&&!h;){if(!l){if(g>e&&k>i&&a[e]===b[i]){if(e++,i++,e===g&&i===k)return[c,d];continue}f=e,j=i,l=!0}j+=1,f+=1,j>=k&&(m=!0),f>=g&&(h=!0),h||a[f]!==b[i]?m||b[j]!==a[e]||(d.push.apply(d,b.slice(i,j)),i=j+1,e++,l=!1):(c.push.apply(c,a.slice(e,f)),e=f+1,i++,l=!1)}return c.push.apply(c,a.slice(e)),d.push.apply(d,b.slice(i)),[c,d]}function d(a){console&&console.warn("[WARNING:mobservable] "+a)}function e(a){var b=!1;return function(){return b?void 0:(b=!0,a.apply(this,arguments))}}a.mobservableStatic=function(a,c){return b(a,c)},a.mobservableStatic.value=b,a.mobservableStatic.primitive=a.mobservableStatic.reference=function(a){return new g(a).createGetterSetter()},a.mobservableStatic.computed=function(a,b){return new h(a,b).createGetterSetter()},a.mobservableStatic.array=function(a){return new l(a)},a.mobservableStatic.props=function o(b,o,c){switch(arguments.length){case 0:throw new Error("Not enough arguments");case 1:return a.mobservableStatic.props(b,b);case 2:for(var d in o)a.mobservableStatic.props(b,d,o[d]);break;case 3:var e=Array.isArray(c),f=a.mobservableStatic.value(c,b);Object.defineProperty(b,o,{get:e?function(){return f}:f,set:e?function(a){f.replace(a)}:f,enumerable:!0,configurable:!1})}return b},a.mobservableStatic.observable=function(b,c,d){var e=d?d.value:null;"function"==typeof e?(delete d.value,delete d.writable,d.get=function(){return a.mobservableStatic.props(this,c,e),this[c]},d.set=function(){throw console.trace(),new Error("It is not allowed to reassign observable functions")}):Object.defineProperty(b,c,{configurable:!1,enumberable:!0,get:function(){return a.mobservableStatic.props(this,c,void 0),this[c]},set:function(b){a.mobservableStatic.props(this,c,b)}})},a.mobservableStatic.toPlainValue=function p(a){if(a){if(a instanceof Array)return a.slice();if(a instanceof g)return a.get();if("function"==typeof a&&a.impl){if(a.impl instanceof g)return a();if(a.impl instanceof l)return a().slice()}else if("object"==typeof a){var b={};for(var c in a)b[c]=p(a[c]);return b}}return a},a.mobservableStatic.observeProperty=function(b,c,f,i){if(void 0===i&&(i=!1),!b||!c||void 0===b[c])throw new Error("Object '"+b+"' has no property '"+c+"'.");if(!f||"function"!=typeof f)throw new Error("Third argument to mobservable.observeProperty should be a function");var j=b[c];if(j instanceof g||j instanceof l)return j.observe(f,i);if(j.impl&&(j.impl instanceof g||j instanceof l))return j.impl.observe(f,i);var k=new h(function(){return b[c]},b),m=k.observe(f,i);return a.mobservableStatic.debugLevel&&0===k.dependencyState.observing.length&&d("mobservable.observeProperty: property '"+c+"' of '"+b+" doesn't seem to be observable. Did you define it as observable?"),e(function(){m(),k.dependencyState.dispose()})},a.mobservableStatic.watch=function q(a,b){var q=new i(a,b);return[q.value,function(){return q.dispose()}]},a.mobservableStatic.batch=function(a){return n.batch(a)},a.mobservableStatic.debugLevel=0;var f,g=function(){function a(a){this._value=a,this.changeEvent=new m,this.dependencyState=new j(this)}return a.prototype.set=function(a){if(a!==this._value){var b=this._value;this.dependencyState.markStale(),this._value=a,this.dependencyState.markReady(!0),this.changeEvent.emit(a,b)}},a.prototype.get=function(){return this.dependencyState.notifyObserved(),this._value},a.prototype.observe=function(a,b){var c=this;void 0===b&&(b=!1),this.dependencyState.setRefCount(1),b&&a(this.get(),void 0);var d=this.changeEvent.on(a);return e(function(){c.dependencyState.setRefCount(-1),d()})},a.prototype.createGetterSetter=function(){var a=this,b=this,c=function(a){return arguments.length>0?void b.set(a):b.get()};return c.observe=function(b,c){return a.observe(b,c)},c.impl=this,c.toString=function(){return a.toString()},c},a.prototype.toString=function(){return"Observable["+this._value+"]"},a}(),h=function(b){function c(a,c){if(b.call(this,void 0),this.func=a,this.scope=c,this.isComputing=!1,this.hasError=!1,"function"!=typeof a)throw new Error("ComputedObservable requires a function")}return __extends(c,b),c.prototype.get=function(){if(this.isComputing)throw new Error("Cycle detected");var b=this.dependencyState;if(b.isSleeping?j.trackingStack.length>0?(b.wakeUp(),b.notifyObserved()):this.compute():b.notifyObserved(),b.hasCycle)throw new Error("Cycle detected");if(this.hasError)throw a.mobservableStatic.debugLevel&&(console.trace(),d(this+": rethrowing caught exception to observer: "+this._value+(this._value.cause||""))),this._value;return this._value},c.prototype.set=function(a){throw new Error(this.toString()+": A computed observable does not accept new values!")},c.prototype.compute=function(){var a;try{if(this.isComputing)throw new Error("Cycle detected");this.isComputing=!0,a=this.func.call(this.scope),this.hasError=!1}catch(b){this.hasError=!0,console.error(this+"Caught error during computation: ",b),b instanceof Error?a=b:(a=new Error("MobservableComputationError"),a.cause=b)}if(this.isComputing=!1,a!==this._value){var c=this._value;return this._value=a,this.changeEvent.emit(a,c),!0}return!1},c.prototype.toString=function(){return"ComputedObservable["+this.func.toString()+"]"},c}(g),i=function(){function a(a,b){this.expr=a,this.onInvalidate=b,this.dependencyState=new j(this),this.didEvaluate=!1,this.dependencyState.computeNextState()}return a.prototype.compute=function(){return this.didEvaluate?(this.dispose(),this.onInvalidate()):(this.didEvaluate=!0,this.value=this.expr()),!1},a.prototype.dispose=function(){this.dependencyState.dispose()},a}();!function(a){a[a.STALE=0]="STALE",a[a.PENDING=1]="PENDING",a[a.READY=2]="READY"}(f||(f={}));var j=function(){function b(a){this.owner=a,this.state=f.READY,this.isSleeping=!0,this.hasCycle=!1,this.observing=[],this.prevObserving=null,this.observers=[],this.dependencyChangeCount=0,this.dependencyStaleCount=0,this.isDisposed=!1,this.externalRefenceCount=0,this.isComputed=void 0!==a.compute}return b.prototype.setRefCount=function(a){var b=this.externalRefenceCount+=a;0===b?this.tryToSleep():b===a&&this.wakeUp()},b.prototype.addObserver=function(a){this.observers[this.observers.length]=a},b.prototype.removeObserver=function(a){var b=this.observers,c=b.indexOf(a);-1!==c&&(b.splice(c,1),0===b.length&&this.tryToSleep())},b.prototype.markStale=function(){this.state===f.READY&&(this.state=f.STALE,this.notifyObservers())},b.prototype.markReady=function(a){this.state!==f.READY&&(this.state=f.READY,this.notifyObservers(a))},b.prototype.notifyObservers=function(a){void 0===a&&(a=!1);for(var b=this.observers.slice(),c=b.length,d=0;c>d;d++)b[d].notifyStateChange(this,a)},b.prototype.tryToSleep=function(){if(!this.isSleeping&&this.isComputed&&0===this.observers.length&&0===this.externalRefenceCount){for(var a=0,b=this.observing.length;b>a;a++)this.observing[a].removeObserver(this);this.observing=[],this.isSleeping=!0}},b.prototype.wakeUp=function(){this.isSleeping&&this.isComputed&&(this.isSleeping=!1,this.state=f.PENDING,this.computeNextState())},b.prototype.notifyStateChange=function(a,b){var c=this;a.state===f.STALE?1===++this.dependencyStaleCount&&this.markStale():(b&&(this.dependencyChangeCount+=1),0===--this.dependencyStaleCount&&(this.state=f.PENDING,n.schedule(function(){c.dependencyChangeCount>0?c.computeNextState():c.markReady(!1),c.dependencyChangeCount=0})))},b.prototype.computeNextState=function(){this.trackDependencies();var a=this.owner.compute();this.bindDependencies(),this.markReady(a)},b.prototype.trackDependencies=function(){this.prevObserving=this.observing,b.trackingStack[b.trackingStack.length]=[]},b.prototype.bindDependencies=function(){this.observing=b.trackingStack.pop(),this.isComputed&&0===this.observing.length&&a.mobservableStatic.debugLevel>1&&!this.isDisposed&&(console.trace(),d("You have created a function that doesn't observe any values, did you forget to make its dependencies observable?"));var e=c(this.observing,this.prevObserving),f=e[0],g=e[1];this.prevObserving=null;for(var h=0,i=g.length;i>h;h++)g[h].removeObserver(this);this.hasCycle=!1;for(var h=0,i=f.length;i>h;h++)this.isComputed&&f[h].findCycle(this)?(this.hasCycle=!0,this.observing.splice(this.observing.indexOf(f[h]),1),f[h].hasCycle=!0):f[h].addObserver(this)},b.prototype.notifyObserved=function(){var a=b.trackingStack,c=a.length;if(c>0){var d=a[c-1],e=d.length;d[e-1]!==this&&d[e-2]!==this&&(d[e]=this)}},b.prototype.findCycle=function(a){var b=this.observing;if(-1!==b.indexOf(a))return!0;for(var c=b.length,d=0;c>d;d++)if(b[d].findCycle(a))return!0;return!1},b.prototype.dispose=function(){if(this.observers.length)throw new Error("Cannot dispose DNode; it is still being observed");for(var a=this.observing.length,b=0;a>b;b++)this.observing[b].removeObserver(this);this.observing=[],this.isDisposed=!0},b.trackingStack=[],b}(),k=function(){function a(){}return a}();k.prototype=[];var l=function(a){function b(b){a.call(this),Object.defineProperties(this,{dependencyState:{enumerable:!1,value:new j(this)},_values:{enumerable:!1,value:b?b.slice():[]},changeEvent:{enumerable:!1,value:new m}}),b&&b.length&&this.updateLength(0,b.length)}return __extends(b,a),Object.defineProperty(b.prototype,"length",{get:function(){return this.dependencyState.notifyObserved(),this._values.length},set:function(a){if("number"!=typeof a||0>a)throw new Error("Out of range: "+a);var b=this._values.length;a!==b&&(a>b?this.spliceWithArray(b,0,new Array(a-b)):this.spliceWithArray(a,b-a))},enumerable:!0,configurable:!0}),b.prototype.updateLength=function(a,c){if(0>c)for(var d=a+c;a>d;d++)delete this[d];else if(c>0){a+c>b.OBSERVABLE_ARRAY_BUFFER_SIZE&&b.reserveArrayBuffer(a+c);for(var d=a,e=a+c;e>d;d++)Object.defineProperty(this,""+d,b.ENUMERABLE_PROPS[d])}},b.prototype.spliceWithArray=function(a,b,c){var d=this._values.length;if(!(void 0!==c&&0!==c.length||0!==b&&0!==d))return[];void 0===a?a=0:a>d?a=d:0>a&&(a=Math.max(0,d+a)),b=1===arguments.length?d-a:void 0===b||null===b?0:Math.max(0,Math.min(b,d-a)),void 0===c&&(c=[]);var e=c.length-b,f=(g=this._values).splice.apply(g,[a,b].concat(c));return this.updateLength(d,e),this.notifySplice(a,f,c),f;var g},b.prototype.notifyChildUpdate=function(a,b){this.notifyChanged(),this.changeEvent.emit({object:this,type:"update",index:a,oldValue:b})},b.prototype.notifySplice=function(a,b,c){(0!==b.length||0!==c.length)&&(this.notifyChanged(),this.changeEvent.emit({object:this,type:"splice",index:a,addedCount:c.length,removed:b}))},b.prototype.notifyChanged=function(){this.dependencyState.markStale(),this.dependencyState.markReady(!0)},b.prototype.observe=function(a,b){return void 0===b&&(b=!1),b&&a({object:this,type:"splice",index:0,addedCount:this._values.length,removed:[]}),this.changeEvent.on(a)},b.prototype.clear=function(){return this.splice(0)},b.prototype.replace=function(a){return this.spliceWithArray(0,this._values.length,a)},b.prototype.values=function(){return this.dependencyState.notifyObserved(),this._values.slice()},b.prototype.toJSON=function(){return this.dependencyState.notifyObserved(),this._values.slice()},b.prototype.clone=function(){return this.dependencyState.notifyObserved(),new b(this._values)},b.prototype.find=function(a,b,c){void 0===c&&(c=0),this.dependencyState.notifyObserved();for(var d=this._values,e=d.length,f=c;e>f;f++)if(a.call(b,d[f],f,this))return d[f];return null},b.prototype.splice=function(a,b){for(var c=[],d=2;d<arguments.length;d++)c[d-2]=arguments[d];switch(this.sideEffectWarning("splice"),arguments.length){case 0:return[];case 1:return this.spliceWithArray(a);case 2:return this.spliceWithArray(a,b)}return this.spliceWithArray(a,b,c)},b.prototype.push=function(){for(var a=[],b=0;b<arguments.length;b++)a[b-0]=arguments[b];return this.sideEffectWarning("push"),this.spliceWithArray(this._values.length,0,a),this._values.length},b.prototype.pop=function(){return this.sideEffectWarning("pop"),this.splice(Math.max(this._values.length-1,0),1)[0]},b.prototype.shift=function(){return this.sideEffectWarning("shift"),this.splice(0,1)[0]},b.prototype.unshift=function(){for(var a=[],b=0;b<arguments.length;b++)a[b-0]=arguments[b];return this.sideEffectWarning("unshift"),this.spliceWithArray(0,0,a),this._values.length},b.prototype.reverse=function(){return this.sideEffectWarning("reverse"),this.replace(this._values.reverse())},b.prototype.sort=function(a){return this.sideEffectWarning("sort"),this.replace(this._values.sort.apply(this._values,arguments))},b.prototype.remove=function(a){this.sideEffectWarning("remove");var b=this._values.indexOf(a);return b>-1?(this.splice(b,1),!0):!1},b.prototype.toString=function(){return this.wrapReadFunction("toString",arguments)},b.prototype.toLocaleString=function(){return this.wrapReadFunction("toLocaleString",arguments)},b.prototype.concat=function(){return this.wrapReadFunction("concat",arguments)},b.prototype.join=function(a){return this.wrapReadFunction("join",arguments)},b.prototype.slice=function(a,b){return this.wrapReadFunction("slice",arguments)},b.prototype.indexOf=function(a,b){return this.wrapReadFunction("indexOf",arguments)},b.prototype.lastIndexOf=function(a,b){return this.wrapReadFunction("lastIndexOf",arguments)},b.prototype.every=function(a,b){return this.wrapReadFunction("every",arguments)},b.prototype.some=function(a,b){return this.wrapReadFunction("some",arguments)},b.prototype.forEach=function(a,b){return this.wrapReadFunction("forEach",arguments)},b.prototype.map=function(a,b){return this.wrapReadFunction("map",arguments)},b.prototype.filter=function(a,b){return this.wrapReadFunction("filter",arguments)},b.prototype.reduce=function(a,b){return this.wrapReadFunction("reduce",arguments)},b.prototype.reduceRight=function(a,b){return this.wrapReadFunction("reduceRight",arguments)},b.prototype.wrapReadFunction=function(a,c){var d=Array.prototype[a];return(b.prototype[a]=function(){return this.dependencyState.notifyObserved(),d.apply(this._values,arguments)}).apply(this,c)},b.prototype.sideEffectWarning=function(a){j.trackingStack.length>0&&d("[Mobservable.Array] The method array."+a+" should not be used inside observable functions since it has side-effects")},b.createArrayBufferItem=function(a){var c={enumerable:!1,configurable:!1,set:function(b){if(a<this._values.length){var c=this._values[a];c!==b&&(this._values[a]=b,this.notifyChildUpdate(a,c))}else{if(a!==this._values.length)throw new Error("ObservableArray: Index out of bounds, "+a+" is larger than "+this.values.length);this.push(b)}},get:function(){return a<this._values.length?(this.dependencyState.notifyObserved(),this._values[a]):void 0}};Object.defineProperty(b.prototype,""+a,c),c.enumerable=!0,c.configurable=!0,b.ENUMERABLE_PROPS[a]=c},b.reserveArrayBuffer=function(a){for(var c=b.OBSERVABLE_ARRAY_BUFFER_SIZE;a>=c;c++)b.createArrayBufferItem(c);b.OBSERVABLE_ARRAY_BUFFER_SIZE=a},b.OBSERVABLE_ARRAY_BUFFER_SIZE=0,b.ENUMERABLE_PROPS=[],b}(k);l.reserveArrayBuffer(1e3);var m=function(){function a(){this.listeners=[]}return a.prototype.emit=function(){var a=this.listeners.slice(),b=a.length;switch(arguments.length){case 0:for(var c=0;b>c;c++)a[c]();break;case 1:for(var d=arguments[0],c=0;b>c;c++)a[c](d);break;default:for(var c=0;b>c;c++)a[c].apply(null,arguments)}},a.prototype.on=function(a){var b=this;return this.listeners.push(a),e(function(){var c=b.listeners.indexOf(a);-1!==c&&b.listeners.splice(c,1)})},a.prototype.once=function(a){var b=this.on(function(){b(),a.apply(this,arguments)});return b},a}();a.mobservableStatic.SimpleEventEmitter=m;var n=function(){function a(){}return a.schedule=function(b){a.inBatch<1?b():a.tasks[a.tasks.length]=b},a.runPostBatchActions=function(){for(var b=0;a.tasks.length;)try{for(;b<a.tasks.length;b++)a.tasks[b]();a.tasks=[]}catch(c){console.error("Failed to run scheduled action, the action has been dropped from the queue: "+c,c),a.tasks.splice(0,b+1)}},a.batch=function(b){a.inBatch+=1;try{return b()}finally{0===--a.inBatch&&(a.inBatch+=1,a.runPostBatchActions(),a.inBatch-=1)}},a.inBatch=0,a.tasks=[],a}();a.mobservableStatic.quickDiff=c,a.mobservableStatic.stackDepth=function(){return j.trackingStack.length}}(mobservable||(mobservable={})),function(a,b){"function"==typeof define&&define.amd?define("mobservable",[],function(){return b()}):"object"==typeof exports?module.exports=b():a.mobservable=b()}(this,function(){return mobservable.mobservableStatic}); | ||
var __extends=this.__extends||function(a,b){function c(){this.constructor=a}for(var d in b)b.hasOwnProperty(d)&&(a[d]=b[d]);c.prototype=b.prototype,a.prototype=new c},mobservable;!function(a){function b(b,c){return Array.isArray(b)?new l(b):"function"==typeof b?a.mobservableStatic.computed(b,c):a.mobservableStatic.primitive(b)}function c(a,b){if(!b.length)return[a,[]];if(!a.length)return[[],b];for(var c=[],d=[],e=0,f=0,g=a.length,h=!1,i=0,j=0,k=b.length,l=!1,m=!1;!m&&!h;){if(!l){if(g>e&&k>i&&a[e]===b[i]){if(e++,i++,e===g&&i===k)return[c,d];continue}f=e,j=i,l=!0}j+=1,f+=1,j>=k&&(m=!0),f>=g&&(h=!0),h||a[f]!==b[i]?m||b[j]!==a[e]||(d.push.apply(d,b.slice(i,j)),i=j+1,e++,l=!1):(c.push.apply(c,a.slice(e,f)),e=f+1,i++,l=!1)}return c.push.apply(c,a.slice(e)),d.push.apply(d,b.slice(i)),[c,d]}function d(a){console&&console.warn("[WARNING:mobservable] "+a)}function e(a){var b=!1;return function(){return b?void 0:(b=!0,a.apply(this,arguments))}}a.mobservableStatic=function(a,c){return b(a,c)},a.mobservableStatic.value=b,a.mobservableStatic.primitive=a.mobservableStatic.reference=function(a){return new g(a).createGetterSetter()},a.mobservableStatic.computed=function(a,b){return new h(a,b).createGetterSetter()},a.mobservableStatic.array=function(a){return new l(a)},a.mobservableStatic.props=function o(b,o,c){switch(arguments.length){case 0:throw new Error("Not enough arguments");case 1:return a.mobservableStatic.props(b,b);case 2:for(var d in o)a.mobservableStatic.props(b,d,o[d]);break;case 3:var e=Array.isArray(c),f=a.mobservableStatic.value(c,b);Object.defineProperty(b,o,{get:e?function(){return f}:f,set:e?function(a){f.replace(a)}:f,enumerable:!0,configurable:!1})}return b},a.mobservableStatic.observable=function(b,c,d){var e=d?d.value:null;"function"==typeof e?(delete d.value,delete d.writable,d.get=function(){return a.mobservableStatic.props(this,c,e),this[c]},d.set=function(){throw console.trace(),new Error("It is not allowed to reassign observable functions")}):Object.defineProperty(b,c,{configurable:!1,enumberable:!0,get:function(){return a.mobservableStatic.props(this,c,void 0),this[c]},set:function(b){a.mobservableStatic.props(this,c,b)}})},a.mobservableStatic.toPlainValue=function p(a){if(a){if(a instanceof Array)return a.slice();if(a instanceof g)return a.get();if("function"==typeof a&&a.impl){if(a.impl instanceof g)return a();if(a.impl instanceof l)return a().slice()}else if("object"==typeof a){var b={};for(var c in a)b[c]=p(a[c]);return b}}return a},a.mobservableStatic.observeProperty=function(b,c,f,i){if(void 0===i&&(i=!1),!b||!c||void 0===b[c])throw new Error("Object '"+b+"' has no property '"+c+"'.");if(!f||"function"!=typeof f)throw new Error("Third argument to mobservable.observeProperty should be a function");var j=b[c];if(j instanceof g||j instanceof l)return j.observe(f,i);if(j.impl&&(j.impl instanceof g||j instanceof l))return j.impl.observe(f,i);var k=new h(function(){return b[c]},b),m=k.observe(f,i);return a.mobservableStatic.debugLevel&&0===k.dependencyState.observing.length&&d("mobservable.observeProperty: property '"+c+"' of '"+b+" doesn't seem to be observable. Did you define it as observable?"),e(function(){m(),k.dependencyState.dispose()})},a.mobservableStatic.watch=function q(a,b){var q=new i(a,b);return[q.value,function(){return q.dispose()}]},a.mobservableStatic.batch=function(a){return n.batch(a)},a.mobservableStatic.debugLevel=0;var f,g=function(){function a(a){this._value=a,this.changeEvent=new m,this.dependencyState=new j(this)}return a.prototype.set=function(a){if(a!==this._value){var b=this._value;this.dependencyState.markStale(),this._value=a,this.dependencyState.markReady(!0),this.changeEvent.emit(a,b)}},a.prototype.get=function(){return this.dependencyState.notifyObserved(),this._value},a.prototype.observe=function(a,b){var c=this;void 0===b&&(b=!1),this.dependencyState.setRefCount(1),b&&a(this.get(),void 0);var d=this.changeEvent.on(a);return e(function(){c.dependencyState.setRefCount(-1),d()})},a.prototype.createGetterSetter=function(){var a=this,b=this,c=function(a){return arguments.length>0?void b.set(a):b.get()};return c.observe=function(b,c){return a.observe(b,c)},c.impl=this,c.toString=function(){return a.toString()},c},a.prototype.toString=function(){return"Observable["+this._value+"]"},a}(),h=function(b){function c(a,c){if(b.call(this,void 0),this.func=a,this.scope=c,this.isComputing=!1,this.hasError=!1,"function"!=typeof a)throw new Error("ComputedObservable requires a function")}return __extends(c,b),c.prototype.get=function(){if(this.isComputing)throw new Error("Cycle detected");var b=this.dependencyState;if(b.isSleeping?j.trackingStack.length>0?(b.wakeUp(),b.notifyObserved()):this.compute():b.notifyObserved(),b.hasCycle)throw new Error("Cycle detected");if(this.hasError)throw a.mobservableStatic.debugLevel&&(console.trace(),d(this+": rethrowing caught exception to observer: "+this._value+(this._value.cause||""))),this._value;return this._value},c.prototype.set=function(a){throw new Error(this.toString()+": A computed observable does not accept new values!")},c.prototype.compute=function(){var a;try{if(this.isComputing)throw new Error("Cycle detected");this.isComputing=!0,a=this.func.call(this.scope),this.hasError=!1}catch(b){this.hasError=!0,console.error(this+"Caught error during computation: ",b),b instanceof Error?a=b:(a=new Error("MobservableComputationError"),a.cause=b)}if(this.isComputing=!1,a!==this._value){var c=this._value;return this._value=a,this.changeEvent.emit(a,c),!0}return!1},c.prototype.toString=function(){return"ComputedObservable["+this.func.toString()+"]"},c}(g),i=function(){function a(a,b){this.expr=a,this.onInvalidate=b,this.dependencyState=new j(this),this.didEvaluate=!1,this.dependencyState.computeNextState()}return a.prototype.compute=function(){return this.didEvaluate?(this.dispose(),this.onInvalidate()):(this.didEvaluate=!0,this.value=this.expr()),!1},a.prototype.dispose=function(){this.dependencyState.dispose()},a}();!function(a){a[a.STALE=0]="STALE",a[a.PENDING=1]="PENDING",a[a.READY=2]="READY"}(f||(f={}));var j=function(){function b(a){this.owner=a,this.state=f.READY,this.isSleeping=!0,this.hasCycle=!1,this.observing=[],this.prevObserving=null,this.observers=[],this.dependencyChangeCount=0,this.dependencyStaleCount=0,this.isDisposed=!1,this.externalRefenceCount=0,this.isComputed=void 0!==a.compute}return b.prototype.setRefCount=function(a){var b=this.externalRefenceCount+=a;0===b?this.tryToSleep():b===a&&this.wakeUp()},b.prototype.addObserver=function(a){this.observers[this.observers.length]=a},b.prototype.removeObserver=function(a){var b=this.observers,c=b.indexOf(a);-1!==c&&(b.splice(c,1),0===b.length&&this.tryToSleep())},b.prototype.markStale=function(){this.state===f.READY&&(this.state=f.STALE,this.notifyObservers())},b.prototype.markReady=function(a){this.state!==f.READY&&(this.state=f.READY,this.notifyObservers(a))},b.prototype.notifyObservers=function(a){void 0===a&&(a=!1);for(var b=this.observers.slice(),c=b.length,d=0;c>d;d++)b[d].notifyStateChange(this,a)},b.prototype.tryToSleep=function(){if(!this.isSleeping&&this.isComputed&&0===this.observers.length&&0===this.externalRefenceCount){for(var a=0,b=this.observing.length;b>a;a++)this.observing[a].removeObserver(this);this.observing=[],this.isSleeping=!0}},b.prototype.wakeUp=function(){this.isSleeping&&this.isComputed&&(this.isSleeping=!1,this.state=f.PENDING,this.computeNextState())},b.prototype.notifyStateChange=function(a,b){var c=this;a.state===f.STALE?1===++this.dependencyStaleCount&&this.markStale():(b&&(this.dependencyChangeCount+=1),0===--this.dependencyStaleCount&&(this.state=f.PENDING,n.schedule(function(){c.dependencyChangeCount>0?c.computeNextState():c.markReady(!1),c.dependencyChangeCount=0})))},b.prototype.computeNextState=function(){this.trackDependencies();var a=this.owner.compute();this.bindDependencies(),this.markReady(a)},b.prototype.trackDependencies=function(){this.prevObserving=this.observing,b.trackingStack[b.trackingStack.length]=[]},b.prototype.bindDependencies=function(){this.observing=b.trackingStack.pop(),this.isComputed&&0===this.observing.length&&a.mobservableStatic.debugLevel>1&&!this.isDisposed&&(console.trace(),d("You have created a function that doesn't observe any values, did you forget to make its dependencies observable?"));var e=c(this.observing,this.prevObserving),f=e[0],g=e[1];this.prevObserving=null;for(var h=0,i=g.length;i>h;h++)g[h].removeObserver(this);this.hasCycle=!1;for(var h=0,i=f.length;i>h;h++)this.isComputed&&f[h].findCycle(this)?(this.hasCycle=!0,this.observing.splice(this.observing.indexOf(f[h]),1),f[h].hasCycle=!0):f[h].addObserver(this)},b.prototype.notifyObserved=function(){var a=b.trackingStack,c=a.length;if(c>0){var d=a[c-1],e=d.length;d[e-1]!==this&&d[e-2]!==this&&(d[e]=this)}},b.prototype.findCycle=function(a){var b=this.observing;if(-1!==b.indexOf(a))return!0;for(var c=b.length,d=0;c>d;d++)if(b[d].findCycle(a))return!0;return!1},b.prototype.dispose=function(){if(this.observers.length)throw new Error("Cannot dispose DNode; it is still being observed");for(var a=this.observing.length,b=0;a>b;b++)this.observing[b].removeObserver(this);this.observing=[],this.isDisposed=!0},b.trackingStack=[],b}(),k=function(){function a(){}return a}();k.prototype=[];var l=function(a){function b(b){a.call(this),Object.defineProperties(this,{dependencyState:{enumerable:!1,value:new j(this)},_values:{enumerable:!1,value:b?b.slice():[]},changeEvent:{enumerable:!1,value:new m}}),b&&b.length&&this.updateLength(0,b.length)}return __extends(b,a),Object.defineProperty(b.prototype,"length",{get:function(){return this.dependencyState.notifyObserved(),this._values.length},set:function(a){if("number"!=typeof a||0>a)throw new Error("Out of range: "+a);var b=this._values.length;a!==b&&(a>b?this.spliceWithArray(b,0,new Array(a-b)):this.spliceWithArray(a,b-a))},enumerable:!0,configurable:!0}),b.prototype.updateLength=function(a,c){if(0>c)for(var d=a+c;a>d;d++)delete this[d];else if(c>0){a+c>b.OBSERVABLE_ARRAY_BUFFER_SIZE&&b.reserveArrayBuffer(a+c);for(var d=a,e=a+c;e>d;d++)Object.defineProperty(this,""+d,b.ENUMERABLE_PROPS[d])}},b.prototype.spliceWithArray=function(a,b,c){var d=this._values.length;if(!(void 0!==c&&0!==c.length||0!==b&&0!==d))return[];void 0===a?a=0:a>d?a=d:0>a&&(a=Math.max(0,d+a)),b=1===arguments.length?d-a:void 0===b||null===b?0:Math.max(0,Math.min(b,d-a)),void 0===c&&(c=[]);var e=c.length-b,f=(g=this._values).splice.apply(g,[a,b].concat(c));return this.updateLength(d,e),this.notifySplice(a,f,c),f;var g},b.prototype.notifyChildUpdate=function(a,b){this.notifyChanged(),this.changeEvent.emit({object:this,type:"update",index:a,oldValue:b})},b.prototype.notifySplice=function(a,b,c){(0!==b.length||0!==c.length)&&(this.notifyChanged(),this.changeEvent.emit({object:this,type:"splice",index:a,addedCount:c.length,removed:b}))},b.prototype.notifyChanged=function(){this.dependencyState.markStale(),this.dependencyState.markReady(!0)},b.prototype.observe=function(a,b){return void 0===b&&(b=!1),b&&a({object:this,type:"splice",index:0,addedCount:this._values.length,removed:[]}),this.changeEvent.on(a)},b.prototype.clear=function(){return this.splice(0)},b.prototype.replace=function(a){return this.spliceWithArray(0,this._values.length,a)},b.prototype.values=function(){return this.dependencyState.notifyObserved(),this._values.slice()},b.prototype.toJSON=function(){return this.dependencyState.notifyObserved(),this._values.slice()},b.prototype.clone=function(){return this.dependencyState.notifyObserved(),new b(this._values)},b.prototype.find=function(a,b,c){void 0===c&&(c=0),this.dependencyState.notifyObserved();for(var d=this._values,e=d.length,f=c;e>f;f++)if(a.call(b,d[f],f,this))return d[f];return null},b.prototype.splice=function(a,b){for(var c=[],d=2;d<arguments.length;d++)c[d-2]=arguments[d];switch(this.sideEffectWarning("splice"),arguments.length){case 0:return[];case 1:return this.spliceWithArray(a);case 2:return this.spliceWithArray(a,b)}return this.spliceWithArray(a,b,c)},b.prototype.push=function(){for(var a=[],b=0;b<arguments.length;b++)a[b-0]=arguments[b];return this.sideEffectWarning("push"),this.spliceWithArray(this._values.length,0,a),this._values.length},b.prototype.pop=function(){return this.sideEffectWarning("pop"),this.splice(Math.max(this._values.length-1,0),1)[0]},b.prototype.shift=function(){return this.sideEffectWarning("shift"),this.splice(0,1)[0]},b.prototype.unshift=function(){for(var a=[],b=0;b<arguments.length;b++)a[b-0]=arguments[b];return this.sideEffectWarning("unshift"),this.spliceWithArray(0,0,a),this._values.length},b.prototype.reverse=function(){return this.sideEffectWarning("reverse"),this.replace(this._values.reverse())},b.prototype.sort=function(a){return this.sideEffectWarning("sort"),this.replace(this._values.sort.apply(this._values,arguments))},b.prototype.remove=function(a){this.sideEffectWarning("remove");var b=this._values.indexOf(a);return b>-1?(this.splice(b,1),!0):!1},b.prototype.toString=function(){return this.wrapReadFunction("toString",arguments)},b.prototype.toLocaleString=function(){return this.wrapReadFunction("toLocaleString",arguments)},b.prototype.concat=function(){return this.wrapReadFunction("concat",arguments)},b.prototype.join=function(a){return this.wrapReadFunction("join",arguments)},b.prototype.slice=function(a,b){return this.wrapReadFunction("slice",arguments)},b.prototype.indexOf=function(a,b){return this.wrapReadFunction("indexOf",arguments)},b.prototype.lastIndexOf=function(a,b){return this.wrapReadFunction("lastIndexOf",arguments)},b.prototype.every=function(a,b){return this.wrapReadFunction("every",arguments)},b.prototype.some=function(a,b){return this.wrapReadFunction("some",arguments)},b.prototype.forEach=function(a,b){return this.wrapReadFunction("forEach",arguments)},b.prototype.map=function(a,b){return this.wrapReadFunction("map",arguments)},b.prototype.filter=function(a,b){return this.wrapReadFunction("filter",arguments)},b.prototype.reduce=function(a,b){return this.wrapReadFunction("reduce",arguments)},b.prototype.reduceRight=function(a,b){return this.wrapReadFunction("reduceRight",arguments)},b.prototype.wrapReadFunction=function(a,c){var d=Array.prototype[a];return(b.prototype[a]=function(){return this.dependencyState.notifyObserved(),d.apply(this._values,arguments)}).apply(this,c)},b.prototype.sideEffectWarning=function(a){j.trackingStack.length>0&&d("[Mobservable.Array] The method array."+a+" should not be used inside observable functions since it has side-effects")},b.createArrayBufferItem=function(a){var c={enumerable:!1,configurable:!1,set:function(b){if(a<this._values.length){var c=this._values[a];c!==b&&(this._values[a]=b,this.notifyChildUpdate(a,c))}else{if(a!==this._values.length)throw new Error("ObservableArray: Index out of bounds, "+a+" is larger than "+this.values.length);this.push(b)}},get:function(){return a<this._values.length?(this.dependencyState.notifyObserved(),this._values[a]):void 0}};Object.defineProperty(b.prototype,""+a,c),c.enumerable=!0,c.configurable=!0,b.ENUMERABLE_PROPS[a]=c},b.reserveArrayBuffer=function(a){for(var c=b.OBSERVABLE_ARRAY_BUFFER_SIZE;a>=c;c++)b.createArrayBufferItem(c);b.OBSERVABLE_ARRAY_BUFFER_SIZE=a},b.OBSERVABLE_ARRAY_BUFFER_SIZE=0,b.ENUMERABLE_PROPS=[],b}(k);l.reserveArrayBuffer(1e3);var m=function(){function a(){this.listeners=[]}return a.prototype.emit=function(){var a=this.listeners.slice(),b=a.length;switch(arguments.length){case 0:for(var c=0;b>c;c++)a[c]();break;case 1:for(var d=arguments[0],c=0;b>c;c++)a[c](d);break;default:for(var c=0;b>c;c++)a[c].apply(null,arguments)}},a.prototype.on=function(a){var b=this;return this.listeners.push(a),e(function(){var c=b.listeners.indexOf(a);-1!==c&&b.listeners.splice(c,1)})},a.prototype.once=function(a){var b=this.on(function(){b(),a.apply(this,arguments)});return b},a}();a.mobservableStatic.SimpleEventEmitter=m;var n=function(){function a(){}return a.schedule=function(b){a.inBatch<1?b():a.tasks[a.tasks.length]=b},a.runPostBatchActions=function(){for(var b=0;a.tasks.length;)try{for(;b<a.tasks.length;b++)a.tasks[b]();a.tasks=[]}catch(c){console.error("Failed to run scheduled action, the action has been dropped from the queue: "+c,c),a.tasks.splice(0,b+1)}},a.batch=function(b){a.inBatch+=1;try{return b()}finally{0===--a.inBatch&&(a.inBatch+=1,a.runPostBatchActions(),a.inBatch-=1)}},a.inBatch=0,a.tasks=[],a}();a.mobservableStatic.ObserverMixin={componentWillMount:function(){var b=this.render;this.render=function(){var c=this;this._watchDisposer&&this._watchDisposer();var d=a.mobservableStatic.watch(function(){return b.call(c)},function(){c.forceUpdate()}),e=d[0],f=d[1];return this._watchDisposer=f,e}},componentWillUnmount:function(){this._watchDisposer&&this._watchDisposer()}},a.mobservableStatic.quickDiff=c,a.mobservableStatic.stackDepth=function(){return j.trackingStack.length}}(mobservable||(mobservable={})),function(a,b){"function"==typeof define&&define.amd?define("mobservable",[],function(){return b()}):"object"==typeof exports?module.exports=b():a.mobservable=b()}(this,function(){return mobservable.mobservableStatic}); |
@@ -39,2 +39,7 @@ /** GENERATED FILE */ | ||
SimpleEventEmitter: new()=> ISimpleEventEmitter; | ||
ObserverMixin: { | ||
componentWillMount(); | ||
componentWillUnmount(); | ||
} | ||
} | ||
@@ -41,0 +46,0 @@ |
@@ -38,2 +38,7 @@ /** | ||
SimpleEventEmitter: new()=> ISimpleEventEmitter; | ||
ObserverMixin: { | ||
componentWillMount(); | ||
componentWillUnmount(); | ||
} | ||
} | ||
@@ -970,2 +975,22 @@ | ||
mobservableStatic.ObserverMixin = { | ||
componentWillMount: function() { | ||
var baseRender = this.render; | ||
this.render = function() { | ||
if (this._watchDisposer) | ||
this._watchDisposer(); | ||
var[rendering, disposer] = mobservableStatic.watch(() => baseRender.call(this), () => { | ||
this.forceUpdate(); | ||
}); | ||
this._watchDisposer = disposer; | ||
return rendering; | ||
} | ||
}, | ||
componentWillUnmount: function() { | ||
if (this._watchDisposer) | ||
this._watchDisposer(); | ||
} | ||
} | ||
/** | ||
@@ -972,0 +997,0 @@ * Given a new and an old list, tries to determine which items are added or removed |
{ | ||
"name": "mobservable", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Changes are coming! Small library for creating observable properties, arrays and functions", | ||
@@ -5,0 +5,0 @@ "main": "dist/mobservable.js", |
@@ -16,2 +16,3 @@ # MOBservable | ||
[Fiddle demo: MOBservable + JQuery](http://jsfiddle.net/mweststrate/vxn7qgdw) | ||
[Fiddle demo: MOBservable + React](https://jsfiddle.net/mweststrate/46vL0phw) | ||
@@ -146,2 +147,32 @@ ## Example: Observable values and functions | ||
## Example: ObserverMixin for react components | ||
MOBservable ships with a mixin that can be used to subscribe React components to observables automatically, so that model changes are processed transparently. | ||
The full JSX example can be found in this [fjsiddle]() | ||
```javascript | ||
function Article(name, price) { | ||
mobservable.props(this, { | ||
name: name, | ||
price: price | ||
}); | ||
} | ||
var ArticleView = React.createClass({ | ||
mixins: [mobservable.ObserverMixin], | ||
render: function() { | ||
return (<li> | ||
<span>{this.props.article.name}</span> | ||
<span className="price">{this.props.article.price}</span> | ||
</li>); | ||
} | ||
}); | ||
var book = new Article("Orthodoxy, G.K. Chesterton", 19.95); | ||
React.render(<ArticleView article={book} />, document.body); | ||
book.price = 15.95; // Triggers automatically a re-render of the ArticleView | ||
``` | ||
# Processing observables | ||
@@ -396,2 +427,12 @@ | ||
### mobservable.ObserverMixin | ||
The observer mixin can be used in [React](https://facebook.github.io/react/index.html) components. | ||
This mixin basically turns the `.render` function of the component into an observable function, and makes sure that the component itself becomes an observer of that function, | ||
so that the component is re-rendered each time an observable has changed. | ||
In general, this mixin combines very well with the [React PureRender mixin](https://facebook.github.io/react/docs/pure-render-mixin.html) if observable objects or arrays are passed into the component. | ||
This allows for React apps that perform well in apps with large amount of complex data, while avoiding the need to manage a lot of subscriptions. | ||
See the [above example](#example_observermixin_for_react_components) or the [JSFiddle demo: MOBservable + React](https://jsfiddle.net/mweststrate/46vL0phw) | ||
### mobservable.debugLevel | ||
@@ -398,0 +439,0 @@ |
20
todo.md
@@ -12,3 +12,3 @@ | ||
- computable's without observers should stay 'awake' after a computation (e.g. being inspected by .get), | ||
but then go to sleep if one of its dependencies has changed, so that subsequent reads on a computable are cheap even if there are no observers | ||
but then go to sleep if one of its dependencies has changed, so that subsequent reads on a computable are cheap even if there are no observers -> But, what about GC? this creates pending references until invalidation | ||
- check if somewhere an array is filled that could be preallocate | ||
@@ -49,4 +49,8 @@ - collapse stale / ready notifications whenever possible | ||
* ~~no source map for dist!~~ | ||
* fiddle demo | ||
* update apidocs | ||
* ~~fiddle demo~~ | ||
* ~~update apidocs~~ | ||
* react mixin, https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750 | ||
* react fiddle demo | ||
* ~~test with webmodeler~~ | ||
* blog posts | ||
@@ -59,7 +63,11 @@ 0.5 | ||
* ~~minify~~ | ||
* react mixin, https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750 | ||
* react fiddle demo | ||
* mobservable.struct (ALWAYS compare deep equal, use defensive copy so that changes in object are detected) | ||
* mobservable.struct (ALWAYS compare deep equal, use defensive copy so that changes in object are detected). Wrap in Struct() object? | ||
* mobservable.computedStruct | ||
* observable annotation on methods should define a method instead of a property (e.g. order.total() instead of order.total) | ||
* update fiddle to master | ||
0.6 | ||
* implement .liveFilter, .liveMap, .liveSlice, .liveSort | ||
* add `thisArg` to all methods that accept a function | ||
Later | ||
@@ -66,0 +74,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
High entropy strings
Supply chain riskContains high entropy strings. This could be a sign of encrypted data, leaked secrets or obfuscated code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2103323
21
16549
525
2
9
2