mobservable
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -290,5 +290,6 @@ /// <reference path="./typings/node-0.10.d.ts" /> | ||
set: function (value) { | ||
if (_this._values[index] !== value) { | ||
var oldValue = _this._values[index]; | ||
if (oldValue !== value) { | ||
_this._values[index] = value; | ||
_this.notifyChildUpdate(index); | ||
_this.notifyChildUpdate(index, oldValue); | ||
} | ||
@@ -313,10 +314,16 @@ }, | ||
var length = this._values.length; | ||
if (index > length) | ||
if ((newItems === undefined || newItems.length === 0) && (deleteCount === 0 || length === 0)) | ||
return []; | ||
if (index === undefined) | ||
index = 0; | ||
else if (index > length) | ||
index = length; | ||
else if (index < 0) | ||
index = Math.max(0, length - index); | ||
if (index === undefined) | ||
return; | ||
if (deleteCount === undefined) | ||
index = Math.max(0, length + index); | ||
if (arguments.length === 1) | ||
deleteCount = length - index; | ||
else if (deleteCount === undefined || deleteCount === null) | ||
deleteCount = 0; | ||
else | ||
deleteCount = Math.max(0, Math.min(deleteCount, length - index)); | ||
if (newItems === undefined) | ||
@@ -330,7 +337,11 @@ newItems = []; | ||
}; | ||
ObservableArray.prototype.notifyChildUpdate = function (index) { | ||
ObservableArray.prototype.notifyChildUpdate = function (index, oldValue) { | ||
this.notifyChanged(); | ||
this.events.emit('change', { object: this, type: 'update', index: index, oldValue: oldValue }); | ||
}; | ||
ObservableArray.prototype.notifySplice = function (index, deleted, added) { | ||
if (deleted.length === 0 && added.length === 0) | ||
return; | ||
this.notifyChanged(); | ||
this.events.emit('change', { object: this, type: 'splice', index: index, addedCount: added.length, removed: deleted }); | ||
}; | ||
@@ -340,3 +351,2 @@ ObservableArray.prototype.notifyChanged = function () { | ||
this.dependencyState.markReady(true); | ||
this.events.emit('change'); | ||
}; | ||
@@ -347,3 +357,3 @@ ObservableArray.prototype.observe = function (listener, fireImmediately) { | ||
if (fireImmediately) | ||
listener(); | ||
listener({ object: this, type: 'splice', index: 0, addedCount: this._values.length, removed: [] }); | ||
this.events.addListener('change', listener); | ||
@@ -368,2 +378,10 @@ return function () { | ||
} | ||
switch (arguments.length) { | ||
case 0: | ||
return []; | ||
case 1: | ||
return this.spliceWithArray(index); | ||
case 2: | ||
return this.spliceWithArray(index, deleteCount); | ||
} | ||
return this.spliceWithArray(index, deleteCount, newItems); | ||
@@ -380,3 +398,3 @@ }; | ||
ObservableArray.prototype.pop = function () { | ||
return this.splice(this._values.length - 1, 1)[0]; | ||
return this.splice(Math.max(this._values.length - 1, 0), 1)[0]; | ||
}; | ||
@@ -383,0 +401,0 @@ ObservableArray.prototype.shift = function () { |
@@ -388,5 +388,6 @@ /// <reference path="./typings/node-0.10.d.ts" /> | ||
set: (value) => { | ||
if (this._values[index] !== value) { | ||
var oldValue = this._values[index]; | ||
if (oldValue !== value) { | ||
this._values[index] = value; | ||
this.notifyChildUpdate(index); | ||
this.notifyChildUpdate(index, oldValue); | ||
} | ||
@@ -412,14 +413,19 @@ }, | ||
var length = this._values.length; | ||
if ((newItems === undefined || newItems.length === 0) && (deleteCount === 0 || length === 0)) | ||
return []; | ||
// yay, splice can deal with strange indexes | ||
if (index > length) | ||
if (index === undefined) | ||
index = 0; | ||
else if (index > length) | ||
index = length; | ||
else if (index < 0) | ||
index = Math.max(0, length - index); | ||
index = Math.max(0, length + index); | ||
// too few arguments? | ||
if (index === undefined) | ||
return; | ||
if (deleteCount === undefined) | ||
if (arguments.length === 1) | ||
deleteCount = length - index; | ||
else if (deleteCount === undefined || deleteCount === null) | ||
deleteCount = 0; | ||
else | ||
deleteCount = Math.max(0, Math.min(deleteCount, length - index)); | ||
if (newItems === undefined) | ||
@@ -436,10 +442,14 @@ newItems = []; | ||
private notifyChildUpdate(index:number) { | ||
private notifyChildUpdate(index:number, oldValue:T) { | ||
this.notifyChanged(); | ||
// TODO: update Array.observe listeners | ||
// conform: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/observe | ||
this.events.emit('change', { object: this, type: 'update', index: index, oldValue: oldValue}); | ||
} | ||
private notifySplice(index:number, deleted:T[], added:T[]) { | ||
if (deleted.length === 0 && added.length === 0) | ||
return; | ||
this.notifyChanged(); | ||
// TODO: update Array.observe listeners | ||
// conform: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/observe | ||
this.events.emit('change', { object: this, type: 'splice', index: index, addedCount: added.length, removed: deleted}); | ||
} | ||
@@ -450,9 +460,7 @@ | ||
this.dependencyState.markReady(true); | ||
this.events.emit('change'); | ||
} | ||
// TODO: eS7 event params | ||
observe(listener:()=>void, fireImmediately=false):Lambda { | ||
observe(listener:(data)=>void, fireImmediately=false):Lambda { | ||
if (fireImmediately) | ||
listener(); // TODO: pass in splice data | ||
listener({ object: this, type: 'splice', index: 0, addedCount: this._values.length, removed: []}); | ||
@@ -488,2 +496,10 @@ this.events.addListener('change', listener); | ||
splice(index:number, deleteCount?:number, ...newItems:T[]):T[] { | ||
switch(arguments.length) { | ||
case 0: | ||
return []; | ||
case 1: | ||
return this.spliceWithArray(index); | ||
case 2: | ||
return this.spliceWithArray(index, deleteCount); | ||
} | ||
return this.spliceWithArray(index, deleteCount, newItems); | ||
@@ -498,3 +514,3 @@ } | ||
pop(): T { | ||
return this.splice(this._values.length - 1, 1)[0]; | ||
return this.splice(Math.max(this._values.length - 1, 0), 1)[0]; | ||
} | ||
@@ -501,0 +517,0 @@ shift(): T { |
{ | ||
"name": "mobservable", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "Changes are coming! Small library for creating observable properties en functions", | ||
@@ -5,0 +5,0 @@ "main": "dist/mobservable.js", |
@@ -243,4 +243,9 @@ # MOBservable | ||
In the near feature this will adhere to the ES7 specs for Array.observe so this class can be used as ES7 array shim. | ||
The events that are being fired adhere to the ES7 specs for Array.observe. The event data will be either a `splice` or `update` event, examples: | ||
```javascript | ||
{ object: <array>, type: "update", index: 2, oldValue: 4 }, | ||
{ object: <array>, type: "splice", index: 1, addedCount: 2, removed: [4,1] }, | ||
``` | ||
### ObservableArray.values() | ||
@@ -247,0 +252,0 @@ |
@@ -12,2 +12,3 @@ | ||
* layout elses, rename properties.js -> observables.js | ||
* tabs to spaces everywhere | ||
* OCD on test files | ||
@@ -17,3 +18,3 @@ * coverage tests | ||
* remove memwatch, make tests smaller? | ||
* drop event emitter, to make lib smaller and stand alone? https://github.com/joyent/node/blob/master/lib/events.js | ||
* drop event emitter, to make lib smaller and stand alone? https://github.com/joyent/node/blob/master/lib/events.js, note: clone listeners before invoking | ||
* array.observe conform | ||
@@ -23,2 +24,4 @@ * test browser compatibility? | ||
* use typescript 1.5 spread operator | ||
* use console.trace() in logging where applicable | ||
* add 'name' as parameter to observable.value, automatically set it when defining properties, use it in warnings / toString | ||
* examples | ||
@@ -34,4 +37,5 @@ * describe properties in readme: | ||
* optimizations | ||
- check if somewhere an array is filled that could be preallocate | ||
- count stale dependencies | ||
- collapse stale / ready notifications whenever possible | ||
- heuristic to make computables non-lazy if used frequently (something like, in computable, if (this.lazyReads > this.computesWithoutObservers) then never-go-to-sleep) | ||
- heuristic to make computables non-lazy if used frequently (something like, in computable, if (this.lazyReads > this.computesWithoutObservers) then never-go-to-sleep) |
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
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
137836
10
2866
264