Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有几个小错误 #20

Open
julyL opened this issue Oct 14, 2019 · 3 comments
Open

有几个小错误 #20

julyL opened this issue Oct 14, 2019 · 3 comments

Comments

@julyL
Copy link

julyL commented Oct 14, 2019

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 julyL changed the title 有几个小问题 有几个小错误 Oct 14, 2019
@xiaoluoboding
Copy link

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

@julyL
Copy link
Author

julyL commented Jan 10, 2020

@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)
  }

@doodlewind
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants