From 0f227492dcbd7dd1de7c0c0990d3c4d2ea4e2e4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20de=20Giessen?= Date: Mon, 31 Dec 2018 10:02:19 +0100 Subject: [PATCH] Translated design docs to English (#19) * Renamed original documentation files * Add translated English design docs --- README.md | 2 - README.zh_CN.md | 8 +-- docs/observer.md | 120 ++++++++++++++++------------------ docs/observer.zh_CN.md | 122 +++++++++++++++++++++++++++++++++++ docs/replay.md | 53 +++++++-------- docs/replay.zh_CN.md | 47 ++++++++++++++ docs/sandbox.md | 35 +++++----- docs/sandbox.zh_CN.md | 45 +++++++++++++ docs/serialization.md | 52 ++++++++------- docs/serialization.zh_CN.md | 125 ++++++++++++++++++++++++++++++++++++ 10 files changed, 464 insertions(+), 145 deletions(-) create mode 100644 docs/observer.zh_CN.md create mode 100644 docs/replay.zh_CN.md create mode 100644 docs/sandbox.zh_CN.md create mode 100644 docs/serialization.zh_CN.md diff --git a/README.md b/README.md index 8ede55eb..18dadcc2 100644 --- a/README.md +++ b/README.md @@ -44,8 +44,6 @@ rrweb is mainly composed of 3 parts: ## Internal Design -*Since the design docs were originally written in Chinese, we do not have the English version yet, but it would be available as soon as possible.* - - [serialization](./docs/serialization.md) - [incremental snapshot](./docs/observer.md) - [replay](./docs/replay.md) diff --git a/README.zh_CN.md b/README.zh_CN.md index 9458e755..c63580e8 100644 --- a/README.zh_CN.md +++ b/README.zh_CN.md @@ -45,10 +45,10 @@ rrweb 主要由 3 部分组成: ## Internal Design -- [序列化](./docs/serialization.md) -- [增量快照](./docs/observer.md) -- [回放](./docs/replay.md) -- [沙盒](./docs/sandbox.md) +- [序列化](./docs/serialization.zh_CN.md) +- [增量快照](./docs/observer.zh_CN.md) +- [回放](./docs/replay.zh_CN.md) +- [沙盒](./docs/sandbox.zh_CN.md) ## Contribute Guide diff --git a/docs/observer.md b/docs/observer.md index 4b1f8525..6b9a27be 100644 --- a/docs/observer.md +++ b/docs/observer.md @@ -1,42 +1,42 @@ -# 增量快照 +# Incremental snapshots +After completing a full snapshot, we need to record events that change the state. -在完成一次全量快照之后,我们就需要基于当前视图状态观察所有可能对视图造成改动的事件,在 rrweb 中我们已经观察了以下事件(将不断增加): +Right now, rrweb records the following events (we will expand upon this): -- DOM 变动 - - 节点创建、销毁 - - 节点属性变化 - - 文本变化 -- 鼠标移动 -- 鼠标交互 - - mouse up、mouse down - - click、double click、context menu - - focus、blur - - touch start、touch move、touch end -- 页面或元素滚动 -- 视窗大小改变 -- 输入 +- DOM changes + - Node creation, deletion + - Node attribute changes + - Text changes +- Mouse movement +- Mouse interaction + - mouse up, mouse down + - click, double click, context menu + - focus, blur + - touch start, touch move, touch end +- Page or element scrolling +- Window size changes +- Input ## Mutation Observer +Since we don't execute any JavaScript during replay, we instead need to record all changes scripts make to the document. -由于我们在回放时不会执行所有的 JavaScript 脚本,所以例如这样的场景我们需要完整记录才能够实现回放: +Consider this example: +> User clicks a button. A dropdown menu appears. User selects the first item. The dropdown menu disappears. -> 点击 button,出现 dropdown menu,选择第一项,dropdown menu 消失 +During replay, the dropdown menu does not automatically appear after the "click button" is executed, because the original JavaScript is not part of the recording. Thus, we need to record the creation of the dropdown menu DOM nodes, the selection of the first item, and subsequent deletion of the dropdown menu DOM nodes. This is the most difficult part. -回放时,在“点击 button”执行之后 dropdown menu 不会自动出现,因为已经没有 JavaScript 脚本为我们执行这件事,所以我们需要记录 dropdown menu 相关的 DOM 节点的创建以及后续的销毁,这也是录制中的最大难点。 +Fortunately, modern browsers have provided us with a very powerful API which can do exactly this: [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver). -好在现代浏览器已经给我们提供了非常强大的 API —— [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) 用来完成这一功能。 +This documentation does not explain the basic usages of MutationObserver, but only focusses on aspects in particular relevant to rrweb. -此处我们不具体讲解 MutationObserver 的基本使用方式,只专注于在 rrweb 中我们需要做哪些特殊处理。 +The first thing to understand is that MutationObserver uses a **Bulk Asynchronous** callback. Specifically, there will be a single callback after a series of DOM changes occur, and it is passed an array of multiple mutation records. -首先要了解 MutationObserver 的触发方式为**批量异步**回调,具体来说就是会在一系列 DOM 变化发生之后将这些变化一次性回调,传出的是一个 mutation 记录数组。 +This mechanism is not problematic for normal use, because we do not only have the mutation record, but we can also directly access the DOM object of the mutated node as well as any parent, child and sibling nodes. -这一机制在常规使用时不会有问题,因为从 mutation 记录中我们可以获取到变更节点的 JS 对象,可以做很多等值比较以及访问父子、兄弟节点等操作来保证我们可以精准回放一条 mutation 记录。 +However in rrweb, since we have a serialization process, we need more sophisticated soluation to be able to deal with various scenarios. -但是在 rrweb 中由于我们有序列化的过程,我们就需要更多精细的判断来应对各种场景。 - -### 新增节点 - -例如以下两种操作会生成相同的 DOM 结构,但是产生不同的 mutation 记录: +### Add node +For example, the following two operations generate the same DOM structure, but produce a different set of mutation records: ``` body @@ -44,58 +44,52 @@ body n2 ``` -1. 创建节点 n1 并 append 在 body 中,再创建节点 n2 并 append 在 n1 中。 -2. 创建节点 n1、n2,将 n2 append 在 n1 中,再将 n1 append 在 body 中。 +1. Create node n1 and append it as child of body, then create node n2 and append it as child of n1. +2. Create nodes n1 and n2, then append n2 as child to of n1, then append n1 as child of body. -第 1 种情况将产生两条 mutation 记录,分别为增加节点 n1 和增加节点 n2;第 2 种情况则只会产生一条 mutation 记录,即增加节点 n1。 +In the first case, two mutation records will be generated, namely adding node n1 and adding node n2; in the second case, only one mutation record will be generated, that is, node n1 (including children) is added. -**注意**,在第一种情况下虽然 n1 append 时还没有子节点,但是由于上述的批量异步回调机制,当我们处理 mutation 记录时获取到的 n1 是已经有子节点 n2 的状态。 +**Note**: In the first case, although n1 has no child node when it is added, due to the above-mentioned batch asynchronous callback mechanism, when we receive the mutation record and process the n1 node the it already has the child node n2 in the DOM. -受第二种情况的限制,我们在处理新增节点时必须遍历其所有子孙节点,才能保证所有新增节点都被记录,但是这一策略应用在第一种情况中就会导致 n2 被作为新增节点记录两次,回放时就会产生与原页面不一致的 DOM 结构。 +Due to the second case, when processing new nodes we must traverse all its descendants to ensure that all new nodes are recorded, however this strategy will cause n2 to be (incorrectly) recorded during the first record. Then, when processing the second record, adding a the node for a second time will result in a DOM structure that is inconsistent with the original page during replay. -因此,在处理一次回调中的多个 mutation 记录时我们需要“惰性”处理新增节点,即在遍历每条 mutation 记录遇到新增节点时先收集,再在全部 mutation 遍历完毕之后对收集的新增节点进行去重操作,保证不遗漏节点的同时每个节点只被记录一次。 +Therefore, when dealing with multiple mutation records in a callback, we need to "lazely" process the newly added nodes, that is, first collect all raw, unprocessed nodes when we go through each mutation record, and then after we've been through all the mutation records we determine the the order nodes were added to the DOM. When new these nodes are added we perform deduplication to ensure that each node is only recorded once and we check no nodes were missed. -在[序列化设计](./serialization.md)中已经介绍了我们需要维护一个 `id -> Node` 的映射,因此当出现新增节点时,我们需要将新节点序列化并加入映射中。但由于我们为了去重新增节点,选择在所有 mutation 记录遍历完毕之后才进行序列化,在以下示例中就会出现问题: +We already introduced in the [serialization design document](./serialization.md) that we need to maintain a mapping of `id -> Node`, so when new nodes appear, we need to serialize the new nodes and add them to the map. But since we want to perform deduplication, and thus only serialize after all the mutation records have been processed, some problems may arise, as demonstrated in the following example: -1. mutation 记录1,新增节点 n1。我们暂不处理,等待最终去重后序列化。 -2. mutation 记录2,n1 新增属性 a1。我们试图将它记录成一次增量快照,但会发现无法从映射中找到 n1 对应的 id,因为此时它还未被序列化。 +1. mutation record 1, add node n1. We will not serialize it yet, since we are waiting for the final deduplication. +2. mutation record 2, n1 added attribute a1. We tried to record it as an incremental snapshot, but we found that we couldn't find the id for n1 from the map because it was not serialized yet. -由此可见,由于我们对新增节点进行了延迟序列化的处理,所有 mutation 记录也都需要先收集,再新增节点去重并序列化之后再做处理。 +As you can see, since we have delayed serialization of the newly added nodes, all mutation records also need to be processed first, and only then the new nodes can be de-duplicated without causing trouble. -### 移除节点 +### Remove node +When processing mutation records, we may encounter a removed node that has not yet been serialized. That indicates that it is a newly added node, and the "add node" mutation record is also somewhere in the mutation records we received. We label these nodes as "dropped nodes". -在处理移除节点时,我们需要做以下处理: +There are two cases we need to handle here: +1. Since the node was removed already, there is no need to replay it, and thus we remove it from the newly added node pool. +2. This also applies to descendants of the dropped node, thus when processing newly added nodes we need to check if it has a dropped node as an ancestor. -1. 移除的节点还未被序列化,则说明是在本次 callback 中新增的节点,无需记录,并且从新增节点池中将其移除。 -2. 上步中在一次 callback 中被新增又移除的节点我们将其称为 dropped node,用于最终处理新增节点时判断节点的父节点是否已经 drop。 +### Attribute change +Although MutationObserver is an asynchronous batch callback, we can still assume that the time interval between mutations occurring in a callback is extremely short, so we can optimize the size of the incremental snapshot by overwriting some data when recording the DOM property changes. -### 属性变化覆盖写 +For example, resizing a `