Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

[WEEX-648][iOS]native batch for dom operations generating from JavaScript #1644

Merged
merged 5 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,18 @@ void WXPerformBlockSyncOnComponentThread(void (^block)(void));
*/
- (void)enumerateComponentsUsingBlock:(void (^)(WXComponent *, BOOL *stop))block;

#pragma mark batch mark

/**
a start native batch tag for a group of UI operation , company with performBatchEnd
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去掉多余的空格,operation -> operations

@see performBatchEnd
*/
- (void)performBatchBegin;

/**
an end native batch tag for a group of UI operation, company with performBatchBegin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operation -> operations

@see performBatchBegin
*/
- (void)performBatchEnd;

@end
57 changes: 54 additions & 3 deletions ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
static NSThread *WXComponentThread;

#define WXAssertComponentExist(component) WXAssert(component, @"component not exists")

#define MAX_DROP_FRAME_FOR_BATCH 200

@implementation WXComponentManager
{
Expand Down Expand Up @@ -991,8 +991,38 @@ - (void) _printFlexComponentFrame:(WXComponent *)component

- (void)_syncUITasks
{
NSArray<dispatch_block_t> *blocks = _uiTaskQueue;
_uiTaskQueue = [NSMutableArray array];
static NSInteger _syncUITaskCount = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_syncUITaskCount不能声明为static,不同的instance有不同的componentManager不能共用一个变量。

NSInteger mismatchBeginIndex = _uiTaskQueue.count;
for (NSInteger i = _uiTaskQueue.count - 1;i >= 0;i --) {
if (_uiTaskQueue[i] == WXPerformUITaskBatchEndBlock) {
_syncUITaskCount = 0;
// clear when find the matches for end and begin tag
break;
}
if (_uiTaskQueue[i] == WXPerformUITaskBatchBeginBlock) {
mismatchBeginIndex = i;
break;
}
}

NSArray<dispatch_block_t> *blocks = nil;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocks声明放到下面if (mismatchBeginIndex > 0)里吧

if (mismatchBeginIndex == _uiTaskQueue.count) {
// here we get end tag or there are not begin and end directives
} else {
_syncUITaskCount ++;
// we only find begin tag but missing end tag,
if (_syncUITaskCount > (MAX_DROP_FRAME_FOR_BATCH)) {
// when the wait times come to MAX_DROP_FRAME_FOR_BATCH, we will pop all the stashed operation for user experience.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stashed operation -> stashed operations

mismatchBeginIndex = _uiTaskQueue.count;
_syncUITaskCount = 0;
}
}

if (mismatchBeginIndex > 0) {
blocks = [_uiTaskQueue subarrayWithRange:NSMakeRange(0, mismatchBeginIndex)];
[_uiTaskQueue removeObjectsInRange:NSMakeRange(0, blocks.count)];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocks.count换成mismatchBeginIndex吧,看起来更对称一些,而且减少一次blocks.count调用。

}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把下面的dispatch_async也放到if (mismatchBeginIndex > 0)这个if里面吧,空任务的时候减少一次给main提交block。

dispatch_async(dispatch_get_main_queue(), ^{
for(dispatch_block_t block in blocks) {
block();
Expand Down Expand Up @@ -1051,6 +1081,27 @@ - (void)enumerateComponentsUsingBlock:(void (^)(WXComponent *, BOOL *stop))block
}
}

static void (^WXPerformUITaskBatchBeginBlock)(void) = ^ () {
#if DEBUG
WXLogDebug(@"directive BatchBeginBlock");
#endif
};
static void (^WXPerformUITaskBatchEndBlock)(void) = ^ () {
#if DEBUG
WXLogDebug(@"directive BatchEndBlock");
#endif
};

- (void)performBatchBegin
{
[self _addUITask:WXPerformUITaskBatchBeginBlock];
}

- (void)performBatchEnd
{
[self _addUITask:WXPerformUITaskBatchEndBlock];
}

- (void)handleDisplayLink {
[self _handleDisplayLink];
}
Expand Down
16 changes: 16 additions & 0 deletions ios/sdk/WeexSDK/Sources/Module/WXDomModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ @implementation WXDomModule
WX_EXPORT_METHOD(@selector(addRule:rule:))
WX_EXPORT_METHOD(@selector(getComponentRect:callback:))
WX_EXPORT_METHOD(@selector(updateComponentData:componentData:callback:))
WX_EXPORT_METHOD(@selector(beginBatchMark))
WX_EXPORT_METHOD(@selector(endBatchMark))

- (void)performBlockOnComponentManager:(void(^)(WXComponentManager *))block
{
Expand Down Expand Up @@ -82,6 +84,20 @@ - (void)performSelectorOnRuleManager:(void(^)(void))block{
});
}

- (void)beginBatchMark
{
[self performBlockOnComponentManager:^(WXComponentManager *manager) {
[manager performBatchBegin];
}];
}

- (void)endBatchMark
{
[self performBlockOnComponentManager:^(WXComponentManager * manager) {
[manager performBatchEnd];
}];
}

- (NSThread *)targetExecuteThread
{
return [WXComponentManager componentThread];
Expand Down