You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 前面省略assign(MyTransaction.prototype,Transaction.Mixin,{getTransactionWrappers: function(){return[{initialize: function(e){// 让这里抛出错误dosthundefined();console.log('init first');},close: function(){console.log('close first');}},{initialize: function(e){console.log('init second');},close: function(){console.log('close second');}}];}});varmyTransaction=newMyTransaction();myTransaction.perform(dosth,this);// init second// close second// Uncaught ReferenceError: dosthundefined is not defined
从上面的例子,我们可以发现,如果wrapper出现错误,则忽略该wrapper以及被包裹函数。
// 前面省略functiondosth(){dosthundefined();console.log('dosth');}// 中间省略myTransaction.perform(dosth,this);// init first// init second// close first// close second// Uncaught ReferenceError: dosthundefined is not defined
How to use?
从上面的例子,我们可以发现,在被包裹的函数运行前,会先运行wrappers的初始化方法,然后运行被包裹函数,最后运行wrappers结束方法。
从上面的例子,我们可以发现,如果wrapper出现错误,则忽略该wrapper以及被包裹函数。
这个例子我们可以发现,如果被包裹函数出错,不会影响wrappers的运行。
具体实现请参考:https://github.com/facebook/react/blob/master/src/utils/Transaction.js ,比较简单。
Why we need Transaction?
由于
虚拟DOM
和DOM
两者是分别更新的,而两者是需要保持强一致性的,否则虚拟DOM中用户已经付款了,但页面却不显示用户付款了这就悲剧了!!!那么哪里最有可能不可预知的错误,导致两者不一致呢?
回想一下同步逻辑:
JSX + 数据 -> 虚拟DOM -> 算出差异 -> 根据差异更新
错误最后可能出现在:
而Transaction正是为了解决
虚拟DOM
和DOM
同步的一致性而生的。The text was updated successfully, but these errors were encountered: