[ 
https://issues.apache.org/jira/browse/WEEX-294?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16656740#comment-16656740
 ] 

ASF GitHub Bot commented on WEEX-294:
-------------------------------------

Hanks10100 closed pull request #1123: [WEEX-294][iOS]use NSTimer to implement 
setTimeout
URL: https://github.com/apache/incubator-weex/pull/1123
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/ios/sdk/WeexSDK/Sources/Bridge/WXJSCoreBridge.m 
b/ios/sdk/WeexSDK/Sources/Bridge/WXJSCoreBridge.m
index 342c6c0a6f..f80159d56a 100644
--- a/ios/sdk/WeexSDK/Sources/Bridge/WXJSCoreBridge.m
+++ b/ios/sdk/WeexSDK/Sources/Bridge/WXJSCoreBridge.m
@@ -37,6 +37,8 @@
 #import "WXPolyfillSet.h"
 #import "WXAppMonitorProtocol.h"
 #import "JSContext+Weex.h"
+#import "NSTimer+Weex.h"
+#import "WXThreadSafeMutableDictionary.h"
 
 #import <dlfcn.h>
 
@@ -53,10 +55,22 @@ @interface WXJSCoreBridge ()
 @property (nonatomic)  long long intervalTimerId;
 @property (nonatomic, strong)  NSMutableDictionary *callbacks;
 
+@property (nonatomic, strong) WXThreadSafeMutableDictionary *timerDic;
+
 @end
 
 @implementation WXJSCoreBridge
 
+- (void)dealloc {
+    [_timerDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  
_Nonnull obj, BOOL * _Nonnull stop) {
+        NSTimer *timer = (NSTimer *)obj;
+        if (timer.isValid) {
+            [timer invalidate];
+        }
+    }];
+    [_timerDic removeAllObjects];
+}
+
 - (instancetype)init
 {
     self = [super init];
@@ -71,18 +85,12 @@ - (instancetype)init
         _intervalTimerId = 0;
         _intervaltimers = [NSMutableDictionary new];
         _multiContext = NO;
+        _timerDic = [[WXThreadSafeMutableDictionary alloc] init];
 
         __weak typeof(self) weakSelf = self;
         
         [WXBridgeContext mountContextEnvironment:_jsContext];
         
-        _jsContext[@"setTimeout"] = ^(JSValue *function, JSValue *timeout) {
-            // this setTimeout is used by internal logic in JS framework, 
normal setTimeout called by users will call WXTimerModule's method;
-            [weakSelf performSelector: @selector(triggerTimeout:) 
withObject:^() {
-                [function callWithArguments:@[]];
-            } afterDelay:[timeout toDouble] / 1000];
-        };
-        
         _jsContext[@"setTimeoutWeex"] = ^(JSValue *appId, JSValue *ret,JSValue 
*arg ) {
             [weakSelf triggerTimeout:[appId toString] ret:[ret toString] 
arg:[arg toString]];
         };
@@ -105,6 +113,8 @@ - (instancetype)init
         _jsContext[@"extendCallNative"] = ^(JSValue *value ) {
             return [weakSelf extendCallNative:[value toDictionary]];
         };
+        
+        [self registerTimers];
     }
     return self;
 }
@@ -384,7 +394,6 @@ - (void)garbageCollect
 //    JSGarbageCollect(_jsContext.JSGlobalContextRef);
 }
 
-#pragma mark - Public
 -(void)removeTimers:(NSString *)instance
 {
     // remove timers
@@ -404,6 +413,29 @@ -(void)removeTimers:(NSString *)instance
     }
 }
 
+- (void)registerTimers {
+    __weak typeof(self) weakSelf = self;
+    _jsContext[@"setTimeout"] = ^(JSValue *function, JSValue *timeout) {
+        return [weakSelf scheduledTimerWithTimeInterval:[timeout 
toDouble]/1000.f block:^{
+            [function callWithArguments:@[]];
+        } repeats:NO];
+    };
+    
+    _jsContext[@"clearTimeout"] = ^(JSValue *timerId) {
+        [weakSelf clearTimer:[timerId toNumber]];
+    };
+    
+    _jsContext[@"setInterval"] = ^(JSValue *function, JSValue *intervalTime) {
+        return [weakSelf scheduledTimerWithTimeInterval:[intervalTime 
toDouble]/1000.f block:^{
+            [function callWithArguments:@[]];
+        } repeats:YES];
+    };
+    
+    _jsContext[@"clearInterval"] = ^(JSValue *timerId) {
+        [weakSelf clearTimer:[timerId toNumber]];
+    };
+}
+
 #pragma mark - Private
 -(void)addInstance:(NSString *)instance callback:(NSString *)callback
 {
@@ -424,20 +456,13 @@ -(void)addInstance:(NSString *)instance 
callback:(NSString *)callback
     }
 }
 
-- (void)triggerTimeout:(void(^)(void))block
-{
-    block();
-}
-
 - (void)callBack:(NSDictionary *)dic
 {
     if([dic objectForKey:@"ret"] && [_timers containsObject:[dic 
objectForKey:@"ret"]]) {
         [[WXSDKManager bridgeMgr] callBack:[dic objectForKey:@"appId"] 
funcId:[dic objectForKey:@"ret"]  params:[dic objectForKey:@"arg"] 
keepAlive:NO];
     }
-
 }
 
-
 - (void)callBackInterval:(NSDictionary *)dic
 {
     if(dic[@"function"] && [dic objectForKey:@"appId"] && [_intervaltimers 
objectForKey:[dic objectForKey:@"appId"]]){
@@ -525,4 +550,26 @@ -(id)extendCallNative:(NSDictionary *)dict
     return @(-1);
 }
 
+- (NSNumber *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval 
block:(void(^)(void))block repeats:(BOOL)repeats {
+    NSTimer *timer = [NSTimer wx_scheduledTimerWithTimeInterval:interval 
block:block repeats:repeats];
+    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
+    _intervalTimerId = _intervalTimerId + 1; // timerId must auto-increment.
+    long long timerId = _intervalTimerId;
+    [self.timerDic setObject:timer forKey:@(timerId)];
+    
+    return @(timerId);
+}
+
+- (void)clearTimer:(NSNumber *)timerId {
+    NSTimer *timer = [self.timerDic objectForKey:timerId];
+    if (!timer) {
+        return;
+    }
+    
+    if (timer.isValid) {
+        [timer invalidate];
+        [self.timerDic removeObjectForKey:timerId];
+    }
+}
+
 @end
diff --git a/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m 
b/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
index 50de3488d7..5b6479a52e 100644
--- a/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
+++ b/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
@@ -159,6 +159,10 @@ - (instancetype)init
         [[NSNotificationCenter defaultCenter] 
postNotificationName:WX_INSTANCE_JSCONTEXT_CREATE_NOTIFICATION 
object:instanceContext];
     }
     
+    if ([_instanceJavaScriptContext 
respondsToSelector:@selector(registerTimers)]) {
+        [_instanceJavaScriptContext registerTimers];
+    }
+    
     return _instanceJavaScriptContext;
 }
 
diff --git a/ios/sdk/WeexSDK/Sources/Protocol/WXBridgeProtocol.h 
b/ios/sdk/WeexSDK/Sources/Protocol/WXBridgeProtocol.h
index e8dc9be552..6ded659869 100644
--- a/ios/sdk/WeexSDK/Sources/Protocol/WXBridgeProtocol.h
+++ b/ios/sdk/WeexSDK/Sources/Protocol/WXBridgeProtocol.h
@@ -64,6 +64,11 @@ typedef void (^WXJSCallNativeComponent)(NSString 
*instanceId, NSString *componen
 - (void)resetEnvironment;
 
 @optional
+/**
+ * Register timer.
+ */
+- (void)registerTimers;
+
 /**
  * Remove instance's timer.
  */


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> use NSTimer to implement setTimeout
> -----------------------------------
>
>                 Key: WEEX-294
>                 URL: https://issues.apache.org/jira/browse/WEEX-294
>             Project: Weex
>          Issue Type: Improvement
>          Components: iOS
>    Affects Versions: 0.18
>            Reporter: XuYouyang
>            Assignee: XuYouyang
>            Priority: Major
>
> Now setTimeout used by internal logic in JS framework call WXTimerModule's 
> method. It maybe cause useless communications .So this task is to use NSTimer 
> to implement setTimeout.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to