IoT Studio可视化搭建平台编辑历史功能的思考与探索
一 背景
二 实现思路
1 页面DSL的维护
PageNode: {
componentName: 'page1',
id: 'page1',
props: {},
children: [
ComponentNode: {
componentName: 'component1',
id: 'component1',
props: {
width: 800,
height: 1000,
color: '#ffffff'
},
children: []
},
ComponentNode: {
componentName: 'component2',
id: 'component2',
props: {},
children: []
},
ComponentNode: {
componentName: 'component3',
id: 'component3',
props: {},
children: []
},
]
}
2 重做与撤销
快照法
实现比较简单,页面信息全量进行深拷贝即可。 历史记录之间的切换灵活。 当页面信息很大时,十分占用存储空间。
指令法
exportabstractclass Operation<T = void> {
/**
* 逆向操作
*/
protectedabstract undo(): T;
/**
* 正向操作
*/
protectedabstract execute(): T;
}
相对快照法,在页面配置复杂时,能节省不少存储空间。 不同的Operation其execute和undo逻辑很可能会不一样,有一定的逻辑开发成本。 跨多个历史记录的重做或撤销,需要执行他们之前所有的execute或undo。例如,上图中如果从O3到O1需要执行2次undo。这一点没有快照法便利。
3 实现细节
Transation
双向链表
Class Manager {
backwardCurrent(): boolean {
if (this._current?.prev) {
this._current.value.operation.undo();
this._current = this._current.prev;
this._validLength -= 1;
returntrue;
}
returnfalse;
}
forwardCurrent(): boolean {
if (this._current?.next) {
this._current.next.value.operation.execute();
this._current = this._current.next;
this._validLength += 1;
returntrue;
}
returnfalse;
}
addAfterCurrent(item: OperationResult<any>) {
if (nextNode) {
nextNode.prev = undefined;
this._length = this._validLength;
}
this._current.next = { value, prev: this._current };
this._current = this._current.next;
}
}
4 总结
三 探索
immutable.js + 快照法
immutable使用持久化数据结构,在使用旧数据创建新数据的时候,会保证旧数据同时可用且不变,同时为了避免深度复制复制所有节点的带来的性能损耗,immutable使用了结构共享,即如果对象树种的一个节点发生变化,只修改这个节点和受他影响的父节点,其他节点则共享。
Git
每次我们运行 git add 和 git commit 命令时,Git 所做的工作实质就是将被改写的文件保存为数据对象, 更新暂存区,记录树对象。
Git 是如何做到这点的?Git 打包对象时,会查找命名及大小相近的文件,并只保存文件不同版本之间的差异内容。 你可以查看包文件,观察它是如何节省空间的。 同样有趣的地方在于,第二个版本完整保存了文件内容,而原始的版本反而是以差异方式保存的——这是因为大部分情况下需要快速访问文件的最新版本。最妙之处是你可以随时重新打包。Git 时常会自动对仓库进行重新打包以节省空间。当然你也可以随时手动执行 git gc 命令来这么做。
Python 脚本入门
Python 脚本入门带着各位运维工程师掌握了在运维过程中需要掌握的核心的 Python 语法和模块;让运维工程师可以通过 Python 来完成自己的脚本的编写。
阅读原文 关键词
就是
文件
方法
对象
版本
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。