stateshot icon indicating copy to clipboard operation
stateshot copied to clipboard

有几个小错误

Open julyL opened this issue 6 years ago • 3 comments

const history = new History()
const state = { a: 1, b: 2 }
history.pushSync({ ...state })
history.pushSync({ ...state, ...{ a: 2 } })
history.pushSync({ ...state, ...{ a: 3 } });
history.undo();
history.undo();
let r1 = history.get();    // { a: 1, b: 2, children: undefined }
history.pushSync({ ...state, ...{ c: 1 } });
let r2 = history.get();
let r3 = history.length;   // 3  应该等于2
history.undo();
let r4 = history.get();    // { a: 1, b: 2, children: undefined }

存在的几点问题

  1. 默认的defaultRule会使得get方法返回值中带有children字段
  2. history.length值应该是this.$records.filter(record => record).length,r3应该为2

julyL avatar Oct 14 '19 07:10 julyL

@julyL @doodlewind 问题的第二点,我实测应该是对的。

xiaoluoboding avatar Jan 02 '20 09:01 xiaoluoboding

@xiaoluoboding 框架内部通过维护 $records数组和$index 来记录状态 前3次执行pushSync, 内部会向$records存入3个操作,2次undo,改变$index-- 第三次pushSync会将 $record[2]置为null ,pushSync内部代码如下

    for (let i = this.$index + 1; i < this.$records.length; i++) {
      this.$records[i] = null
    }

第三次pushSync之后,实际上$records里面只存储了2个有效操作 image 但是length的取值并没有过滤null值

  get length() {
    return Math.min(this.$records.length, this.maxLength)
  }

julyL avatar Jan 10 '20 04:01 julyL

谢谢你的定位,近期刚好也有其他维护需求,待我这顺便处理下哈

doodlewind avatar Jan 10 '20 14:01 doodlewind