Skip to content

Commit

Permalink
fix #40
Browse files Browse the repository at this point in the history
  • Loading branch information
leeluolee committed Jul 7, 2015
1 parent 7e79d66 commit 6b93a28
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 177 deletions.
112 changes: 55 additions & 57 deletions dist/regular.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,14 +1024,13 @@ _.clone = function clone(obj){


_.equals = function(now, old){
if( Array.isArray(now) ){
var splices = ld(now, old||[]);
return splices;
}
var type = typeof now;
if(type === 'number' && typeof old === 'number'&& isNaN(now) && isNaN(old)) return true
return now === old;
}
_.diffArray = function(now, old){
return ld(now, old)
}


//Levenshtein_distance
Expand Down Expand Up @@ -1327,10 +1326,10 @@ walkers.list = function(ast, options){
var indexName = ast.variable + '_index';
var variable = ast.variable;

function update(newValue, splices){
function update(newValue, oldValue, splices){
if(!newValue) {
newValue = [];
splices = _.equals(newValue, splices);
splices = _.diffArray(newValue, oldValue);
}

if(!splices || !splices.length) return;
Expand Down Expand Up @@ -3615,66 +3614,65 @@ var methods = {
_checkSingleWatch: function(watcher, i){
var dirty = false;
if(!watcher) return;
if(watcher.test) { //multi
var result = watcher.test(this);
if(result){
dirty = true;
watcher.fn.apply(this, result)
}
}else{

var now = watcher.get(this);
var last = watcher.last;
var tnow = _.typeOf(now);
var tlast = _.typeOf(last);
var eq = true;

if(tnow === 'object' && watcher.deep){
if(!watcher.last){
eq = false;
}else{
for(var j in now){
if(watcher.last[j] !== now[j]){
eq = false;
break;
}
var now, last, tlast, tnow, eq, diff;

if(!watcher.test){

now = watcher.get(this);
last = watcher.last;
tlast = _.typeOf(last);
tnow = _.typeOf(now);
eq = true, diff;

// !Object
if( !(tnow === 'object' && tlast==='object' && watcher.deep) ){
// Array
if( tnow === 'array' && ( tlast=='undefined' || tlast === 'array') ){
diff = _.diffArray(now, watcher.last || [])
if( tlast !== 'array' || (diff && diff.length) ) dirty = true;
}else{
eq = _.equals( now, last );
if( !eq || watcher.force ){
watcher.force = null;
dirty = true;
}
}
}else{
for(var j in now){
if(last[j] !== now[j]){
dirty = true;
break;
}
if(eq !== false){
for(var n in last){
if(last[n] !== now[n]){
eq = false;
break;
}
}
if(dirty !== true){
for(var n in last){
if(last[n] !== now[n]){
dirty = true;
break;
}
}
}
}else{
eq = _.equals(now, watcher.last);
}
if(eq === false || watcher.force){ // in some case. if undefined, we must force digest.
eq = false;
watcher.force = null;
} else{
// @TODO 是否把多重改掉
var result = watcher.test(this);
if(result){
dirty = true;
watcher.fn.call(this, now, watcher.last);
if(typeof now !== 'object'|| watcher.deep){
watcher.last = _.clone(now);
}else{
watcher.last = now;
}
}else{ // if eq == true
if( _.typeOf(eq) === 'array' && eq.length ){
watcher.last = _.clone(now);
watcher.fn.call(this, now, eq);
dirty = true;
}else{
eq = true;
}
watcher.fn.apply(this, result)
}
// @TODO
if(dirty && watcher.once) this._watchers.splice(i, 1);

return dirty;
}
if(dirty && !watcher.test){
watcher.fn.call(this, now, last, diff)
if(tnow === 'object' && watcher.deep || tnow === 'array'){
watcher.last = _.clone(now);
}else{
watcher.last = now;
}
if(watcher.once) this._watchers.splice(i, 1);
}

return dirty;
},

/**
Expand Down
4 changes: 2 additions & 2 deletions dist/regular.min.js

Large diffs are not rendered by default.

101 changes: 50 additions & 51 deletions src/helper/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,66 +131,65 @@ var methods = {
_checkSingleWatch: function(watcher, i){
var dirty = false;
if(!watcher) return;
if(watcher.test) { //multi
var result = watcher.test(this);
if(result){
dirty = true;
watcher.fn.apply(this, result)
}
}else{

var now = watcher.get(this);
var last = watcher.last;
var tnow = _.typeOf(now);
var tlast = _.typeOf(last);
var eq = true;

if(tnow === 'object' && watcher.deep){
if(!watcher.last){
eq = false;
}else{
for(var j in now){
if(watcher.last[j] !== now[j]){
eq = false;
break;
}
var now, last, tlast, tnow, eq, diff;

if(!watcher.test){

now = watcher.get(this);
last = watcher.last;
tlast = _.typeOf(last);
tnow = _.typeOf(now);
eq = true, diff;

// !Object
if( !(tnow === 'object' && tlast==='object' && watcher.deep) ){
// Array
if( tnow === 'array' && ( tlast=='undefined' || tlast === 'array') ){
diff = _.diffArray(now, watcher.last || [])
if( tlast !== 'array' || (diff && diff.length) ) dirty = true;
}else{
eq = _.equals( now, last );
if( !eq || watcher.force ){
watcher.force = null;
dirty = true;
}
if(eq !== false){
for(var n in last){
if(last[n] !== now[n]){
eq = false;
break;
}
}
}else{
for(var j in now){
if(last[j] !== now[j]){
dirty = true;
break;
}
}
if(dirty !== true){
for(var n in last){
if(last[n] !== now[n]){
dirty = true;
break;
}
}
}
}else{
eq = _.equals(now, watcher.last);
}
if(eq === false || watcher.force){ // in some case. if undefined, we must force digest.
eq = false;
watcher.force = null;
} else{
// @TODO 是否把多重改掉
var result = watcher.test(this);
if(result){
dirty = true;
watcher.fn.call(this, now, watcher.last);
if(typeof now !== 'object'|| watcher.deep){
watcher.last = _.clone(now);
}else{
watcher.last = now;
}
}else{ // if eq == true
if( _.typeOf(eq) === 'array' && eq.length ){
watcher.last = _.clone(now);
watcher.fn.call(this, now, eq);
dirty = true;
}else{
eq = true;
}
watcher.fn.apply(this, result)
}
// @TODO
if(dirty && watcher.once) this._watchers.splice(i, 1);

return dirty;
}
if(dirty && !watcher.test){
watcher.fn.call(this, now, last, diff)
if(tnow === 'object' && watcher.deep || tnow === 'array'){
watcher.last = _.clone(now);
}else{
watcher.last = now;
}
if(watcher.once) this._watchers.splice(i, 1);
}

return dirty;
},

/**
Expand Down
7 changes: 3 additions & 4 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,13 @@ _.clone = function clone(obj){


_.equals = function(now, old){
if( Array.isArray(now) ){
var splices = ld(now, old||[]);
return splices;
}
var type = typeof now;
if(type === 'number' && typeof old === 'number'&& isNaN(now) && isNaN(old)) return true
return now === old;
}
_.diffArray = function(now, old){
return ld(now, old)
}


//Levenshtein_distance
Expand Down
4 changes: 2 additions & 2 deletions src/walkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ walkers.list = function(ast, options){
var indexName = ast.variable + '_index';
var variable = ast.variable;

function update(newValue, splices){
function update(newValue, oldValue, splices){
if(!newValue) {
newValue = [];
splices = _.equals(newValue, splices);
splices = _.diffArray(newValue, oldValue);
}

if(!splices || !splices.length) return;
Expand Down
Loading

0 comments on commit 6b93a28

Please sign in to comment.