/** * `Transaction` creates a black box that is able to wrap any method such that * certain invariants are maintained before and after the method is invoked * (Even if an exception is thrown while invoking the wrapped method). Whoever * instantiates a transaction can provide enforcers of the invariants at * creation time. The `Transaction` class itself will supply one additional * automatic invariant for you - the invariant that any transaction instance * should not be run while it is already being run. You would typically create a * single instance of a `Transaction` for reuse multiple times, that potentially * is used to wrap several different methods. Wrappers are extremely simple - * they only require implementing two methods. * * invariant词汇的理解释义参考: http://wulfric.me/2017/11/what-is-invariant/ * `Transaction`创建一个黑盒,这样他可以包裹任何方法,以达到在执行这个方法前后维护某些确定的约束( * 即使这个方法抛出了错误)的目标。无论如何,实例化一个事务可以在在创建时提供约束的实施方(需求方)。 * Transaction Class本身提供额外的绝对约束条件给你-任何事务实例的不变量不应该在它已经运行时运行。 * 你一般会创建一个Transaction单例来重用多次,它可能被用来包装不同的方法。Wrappers是非常简单的, * 它仅仅要求继承initialize&&close这两个方法。 * Use cases: * - Preserving the input selection ranges before/after reconciliation. * Restoring selection even in the event of an unexpected error. * - Deactivating events while rearranging the DOM, preventing blurs/focuses, * while guaranteeing that afterwards, the event system is reactivated. * - Flushing a queue of collected DOM mutations to the main UI thread after a * reconciliation takes place in a worker thread. * - Invoking any collected `componentDidUpdate` callbacks after rendering new * content. * - (Future use case): Wrapping particular flushes of the `ReactWorker` queue * to preserve the `scrollTop` (an automatic scroll aware DOM). * - (Future use case): Layout calculations before and after DOM updates. * * 用例: * - 在执行前后保存输入框选择范围(即鼠标选中一个文字高亮区块),即使过程中出错也可以返回这个选择范围 * - 页面重绘前解除事件绑定,防止鼠标焦点进出,当完成了页面重绘恢复事件绑定 * - 在工作线程中进行协调后,将收集的DOM突变队列刷新到主UI线程(批量更新)。 * - render新的内容后 批量执行componentDidUpdate中收集到的回调 * - (未来用例) 保留scrollTop * - (未来用例) 布局计算 */