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 all 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 operations, company with performBatchEnd
@see performBatchEnd
*/
- (void)performBatchBegin;

/**
an end native batch tag for a group of UI operations, company with performBatchBegin
@see performBatchBegin
*/
- (void)performBatchEnd;

@end
67 changes: 59 additions & 8 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 All @@ -64,6 +64,7 @@ @implementation WXComponentManager

pthread_mutex_t _propertyMutex;
pthread_mutexattr_t _propertMutexAttr;
NSUInteger _syncUITaskCount;
}

+ (instancetype)sharedManager
Expand All @@ -80,7 +81,7 @@ - (instancetype)initWithWeexInstance:(id)weexInstance
{
if (self = [self init]) {
_weexInstance = weexInstance;

_syncUITaskCount = 0;
_indexDict = [NSMapTable strongToWeakObjectsMapTable];
_fixedComponents = [NSMutableArray wx_mutableArrayUsingWeakReferences];
_uiTaskQueue = [NSMutableArray array];
Expand Down Expand Up @@ -991,13 +992,42 @@ - (void) _printFlexComponentFrame:(WXComponent *)component

- (void)_syncUITasks
{
NSArray<dispatch_block_t> *blocks = _uiTaskQueue;
_uiTaskQueue = [NSMutableArray array];
dispatch_async(dispatch_get_main_queue(), ^{
for(dispatch_block_t block in blocks) {
block();
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;
}
}

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 operations for user experience.
mismatchBeginIndex = _uiTaskQueue.count;
_syncUITaskCount = 0;
}
}

if (mismatchBeginIndex > 0) {
NSArray<dispatch_block_t> *blocks = [_uiTaskQueue subarrayWithRange:NSMakeRange(0, mismatchBeginIndex)];
[_uiTaskQueue removeObjectsInRange:NSMakeRange(0, mismatchBeginIndex)];
if (blocks.count) {
dispatch_async(dispatch_get_main_queue(), ^{
for(dispatch_block_t block in blocks) {
block();
}
});
}
}
}

#pragma mark Fixed
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