All Projects → wuyunfeng → Lightweightrunloop A Reactor Style Nsrunloop

wuyunfeng / Lightweightrunloop A Reactor Style Nsrunloop

NSRunLoop Reactor Style Implementation: Using BSD kqueue implements iOS/Mac NSRunLoop and RunLoop-Relative Foundation such as perform selector(or delay some times) on other thread , Timer, URLConnection ,LWStream(LWInputStream、LWOutputStream) , LWPort(LWSocketPort) etc.

Projects that are alternatives of or similar to Lightweightrunloop A Reactor Style Nsrunloop

Ngx Filter Pipe
𝗩 Angular 5+ pipeline for array filtering.
Stars: ✭ 129 (-34.18%)
Mutual labels:  pipe
Browserselect
Browser Select is a utility to dynamically select the browser you want instead of just having one default for all links.
Stars: ✭ 154 (-21.43%)
Mutual labels:  select
Vue Treeselect
A multi-select component with nested options support for Vue.js
Stars: ✭ 2,347 (+1097.45%)
Mutual labels:  select
Cjlabel
A drop-in replacement for UILabel that supports NSAttributedString, rich text, display any view, links, select copy and more
Stars: ✭ 140 (-28.57%)
Mutual labels:  select
React Ds
🔥 React Drag To Select component (tiny, touch friendly, and no dependencies!)
Stars: ✭ 153 (-21.94%)
Mutual labels:  select
El Tree Select
基于element-ui2.x扩展下拉树
Stars: ✭ 159 (-18.88%)
Mutual labels:  select
React Select Me
Fast 🐆. Lightweight 🐜. Configurable 🐙. Easy to use 🦄. Give it a shot 👉🏼
Stars: ✭ 119 (-39.29%)
Mutual labels:  select
Ppipe
pipes values through functions, an alternative to using the proposed pipe operator ( |> ) for ES
Stars: ✭ 192 (-2.04%)
Mutual labels:  pipe
Fluids
Fluid dynamics component of Chemical Engineering Design Library (ChEDL)
Stars: ✭ 154 (-21.43%)
Mutual labels:  pipe
Flutter smart select
SmartSelect allows you to easily convert your usual form select or dropdown into dynamic page, popup dialog, or sliding bottom sheet with various choices input such as radio, checkbox, switch, chips, or even custom input. Supports single and multiple choice.
Stars: ✭ 179 (-8.67%)
Mutual labels:  select
Pipetools
Functional plumbing for Python
Stars: ✭ 143 (-27.04%)
Mutual labels:  pipe
Metago
MetaGo provides fast cursor movement/selection for keyboard focused users in vscode
Stars: ✭ 151 (-22.96%)
Mutual labels:  select
Dropkick
A JavaScript plugin for creating beautiful, accessible, and painless custom dropdowns.
Stars: ✭ 1,946 (+892.86%)
Mutual labels:  select
Ng2 Search Filter
Angular 2 / Angular 4 / Angular 5 custom pipe npm module to make a search filter on any input, 🔥 100K+ downloads
Stars: ✭ 137 (-30.1%)
Mutual labels:  pipe
Selectize.js
Selectize is the hybrid of a textbox and <select> box. It's jQuery based, and it has autocomplete and native-feeling keyboard navigation; useful for tagging, contact lists, etc.
Stars: ✭ 12,744 (+6402.04%)
Mutual labels:  select
Lessmd
A small markdown viewer/converter for unix terminal.
Stars: ✭ 122 (-37.76%)
Mutual labels:  pipe
Vue Ui For Pc
基于Vue2.x的一套PC端UI组件,包括了Carousel 跑马灯、Cascader 级联、Checkbox 多选框、Collapse 折叠面板、DatePicker 日期选择、Dialog 对话框、Form 表单、Input 输入框、InputNumber 数字输入框、Layer 弹窗层、Loading 加载、Menu 菜单、Page 分页、Progress 进度条、Radio 单选框、SelectDropDown 仿select、Switch 开关、Table 表格、Tabs 标签页、Textarea 文本框、Tooltip 文字提示、BackTop 返回顶部、steps 步骤条、Transfer 穿梭框、Tree 树形、Upload 文件上传、Lazy 图片懒加载、Loading 加载、Pagination 分页等等
Stars: ✭ 156 (-20.41%)
Mutual labels:  select
Vue Cool Select
Select with autocomplete, slots, bootstrap and material design themes.
Stars: ✭ 195 (-0.51%)
Mutual labels:  select
Pipeline.rs
☔️ => ⛅️ => ☀️
Stars: ✭ 188 (-4.08%)
Mutual labels:  pipe
Pipe
🎷 B3log 分布式社区的 Go 博客端节点系统,欢迎加入下一代社区网络。B3log distributed community blog-end node, welcome to join the next generation community network.
Stars: ✭ 169 (-13.78%)
Mutual labels:  pipe

LightWeightRunLoop

Implement iOS/Mac NSRunLoop and some Runloop-Relative Fundation API such as perform selector(or delay some times) on other thread , Timer, URLConnection, LWStream(LWInputStream、LWOutputStream), LWPort(LWSocketPort)etc..

IO Event-Driver-Model

Main Features

  • NSObject - postSelector:onThread:withObject:afterDelay:modes (equal to NSObject performSelector: withObject:afterDelay:inModes:)etc.

  • LWTimer - NSTimer, nanosecond

  • LWURLConnection - NSURLConnection

  • LWInputStream - File relative Stream

  • LWOutputStream - File relative Stream

  • LWPort - LWSocketPort(Leader-Follower pattern socket)

Future Features & To-Do

  • Refactor project

Get Started

Each NSThread object, excluding the application’s main thread, can own an LWRunLoop object. You can get the current thread’s LWRunLoop, through the class method currentLWRunLoop. Subsequently code snippet shows how configure LWRunLoop for NSThread and make the NSThread _lwRunLoopThread entering into Event-Driver-Mode:

 NSThread *_lwRunLoopThread = [[NSThread alloc] initWithTarget:self selector:@selector(lightWeightRunloopThreadEntryPoint:) object:nil];
    - (void)lightWeightRunloopThreadEntryPoint:(id)data {
@autoreleasepool {
    LWRunLoop *looper = [LWRunLoop currentLWRunLoop];
    [looper run];
    
    //or
    [[LWRunLoop currentLWRunLoop] run];
    }
}

To enqueue a selector to be performed on a different thread than your own && schedule a selector to be executed at some point in the future

you can use the category of NSObject(post)

  • -(void)postSelector:(SEL)aSelector onThread:(NSThread *)thread withObject:(id)arg;

  • -(void)postSelector:(SEL)aSelector onThread:(NSThread *)thread withObject:(id)arg afterDelay:(NSInteger)delay

  • -(void)postSelector:(SEL)aSelector onThread:(NSThread *)thread withObject:(id)arg afterDelay:(NSInteger)delay modes:(NSArray<NSString *> modes)

lwrunloop modes are:

NSString * const  LWDefaultRunLoop = @"LWDefaultRunLoop";
NSString * const  LWRunLoopCommonModes = @"LWRunLoopCommonModes";
NSString * const  LWRunLoopModeReserve1 = @"LWRunLoopModeReserve1";
NSString * const  LWRunLoopModeReserve2 = @"LWRunLoopModeReserve2";
NSString * const  LWTrackingRunLoopMode = @"LWTrackingRunLoopMode";

you can switch runloop mode at any time you can like this:

	    LWRunLoop *runLoop = [_lwModeRunLoopThread looper];
        [runLoop changeRunLoopMode:LWRunLoopModeReserve2];

you can use the category of NSObject(post):

  [self postSelector:@selector(execute) onThread:_lwRunLoopThread withObject:nil];
  [self postSelector:@selector(execute) onThread:_lwRunLoopThread withObject:nil afterDelay:5000];
  [self postSelector:@selector(execute) onThread:_lwRunLoopThread withObject:nil afterDelay:5000 modes:@[LWDefaultRunLoop]];

You use the LWTimer class to create timer objects or, more simply, timers.

A timer waits until a certain time interval has elapsed and then fires, sending a specified message to a target object.

  • +(LWTimer *)scheduledLWTimerWithTimeInterval:(NSTimeInterval)interval target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

  • +(LWTimer *)timerWithTimeInterval:(NSTimeInterval)interval target:(id)aTarget selector:( SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;

fire the LWTimer using - (void)fire and invalidate the LWTimer using - (void)invalidate

For example:

run the - (void)genernateLWTimer on _lwRunLoopThread:

[self postSelector:@selector(genernateLWTimer) onThread:_lwRunLoopThread withObject:nil];

- (void)genernateLWTimer
{
    _count = 0;
    LWTimer *timer = [LWTimer timerWithTimeInterval:1000 target:self selector:@selector(bindLWTimerWithSelector:) userInfo:nil repeats:YES];
[timer fire];
   //gTimer = [LWTimer scheduledLWTimerWithTimeInterval:2000 target:self selector:@selector(bindLWTimerWithSelector:) userInfo:nil repeats:YES];
}

the selector for LWTimer to be executed:

- (void)bindLWTimerWithSelector:(LWTimer *)timer
{
    _count++;
    NSLog(@"* [ LWTimer : %@ performSelector: ( %@ ) on Thread : %@ ] *", [self class], NSStringFromSelector(_cmd), [NSThread currentThread].name);
    if (_count == 4) {
        [timer invalidate];
    }
}   

An LWURLConnection object lets you load the contents of a URL by providing a URL request object.

Step1: Perform LWURLConnection on _lwRunLoopThread:

- (void)executeURLConnection:(UIButton *)button
{
   [self postSelector:@selector(performURLConnectionOnRunLoopThread) onThread:_lwRunLoopThread withObject:nil];
}

Step2: Create LWURLConnection, schedule LWURLConnection to _lwRunLoopThread such as following code snippet:

- (void)performURLConnectionOnRunLoopThread
{
    NSLog(@"[%@ %@]", [self class], NSStringFromSelector(_cmd));
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.8:8888/post.php"]];
   request.HTTPMethod = @"POST";
   NSString *content = @"name=john&address=beijing&mobile=140005";
   request.HTTPBody = [content dataUsingEncoding:NSUTF8StringEncoding];
   LWURLConnection *conn = [[LWURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
   [conn scheduleInRunLoop:_lwRunLoopThread.looper];
   [conn start];
}

Step3: Implement the delegate methods on Receiver:

@protocol LWURLConnectionDataDelegate <NSObject>

- (void)lw_connection:(LWURLConnection * _Nonnull)connection didReceiveData:(NSData * _Nullable)data;
- (void)lw_connection:(LWURLConnection * _Nonnull)connection didFailWithError:(NSError * _Nullable)error;
- (void)lw_connectionDidFinishLoading:(LWURLConnection * _Nonnull)connection;
@end

Step4: You can use LWURLResponse to format Http response

LWURLResponse *response = [[LWURLResponse alloc] initWithData:_responseData];

LWInputStream & LWOutputStream (File Relative)

Initilize LWInputStream or LWOutputStream :
	  _lwInputStream = [LWInputStream inputStreamWithFileAtPath:filePath];
	  _lwInputStream.delegate = self;
	  [_lwInputStream scheduleInRunLoop:[_thread looper] forMode:LWDefaultRunLoop];
	  [_lwInputStream open];

or

      _lwOutputStream = [LWOutputStream outputStreamToFileAtPath:filePath append:YES];
	  _lwOutputStream.delegate = self;
      [_lwOutputStream scheduleInRunLoop:[_thread looper] forMode:LWDefaultRunLoop];
      [_lwOutputStream open];
Then read or write data on the selector of delegate,such as:
 switch (eventCode) {
        case LWStreamEventOpenCompleted:
             break;
        case LWStreamEventHasBytesAvailable:
        {
            uint8_t buffer[1];
            NSUInteger len = [(LWInputStream *)aStream read:buffer maxLength:sizeof(buffer)];
            [_inputStreamData appendBytes:buffer length:len];
        }
            break;
        case LWStreamEventEndEncountered:
        {
            NSString *content = [[NSString alloc] initWithData:_inputStreamData encoding:NSUTF8StringEncoding];
            NSLog(@"content = %@", content);
            [(LWInputStream *)aStream close];
        }
            break;
        default:
            break;
    }

or

switch (eventCode) {
        case LWStreamEventOpenCompleted:
            break;
        case LWStreamEventHasSpaceAvailable:
        {
            uint8_t buffer[50] = "abcdefghijklmnopqrstuvwxyz";
            NSUInteger len = [(LWOutputStream *)aStream write:buffer maxLength:50];
            [_inputStreamData appendBytes:buffer length:len];
            [(LWOutputStream *)aStream close];
        }
            break;
        case LWStreamEventEndEncountered:
        {
        }
            break;

LWPort & LWSocketPort (Socket Relative)

current the message should be :
[data length] + [data]

for example(the data hello length is 5):

5hello
Initilize _lwPortRunLoopThread :
_lwPortRunLoopThread = [[NSThread alloc] initWithTarget:self selector:@selector(portThreadEntryPoint:) object:nil];
_lwPortRunLoopThread.name = @"lwPortLoopThread";
[_lwPortRunLoopThread start];
provide portThreadEntryPoint:
- (void)portThreadEntryPoint:(id)data
{
    @autoreleasepool {
        LWRunLoop *looper = [LWRunLoop currentLWRunLoop];
        _leaderPort = [[LWSocketPort alloc] initWithTCPPort:8082];
        _leaderPort.delegate = self;
        _worker = [[WorkerClass alloc] init];
        [looper addPort:_leaderPort forMode:LWDefaultRunLoop];
        [looper runMode:LWDefaultRunLoop];
    }
}
Send message from leader to followers :
	-(void)performFollowerToLeader:(UIButton *)button
	{
	    [NSThread detachNewThreadSelector:@selector(launchThreadWithPort:) toTarget:_worker withObject:_leaderPort];
	}
send message from followers to leader:
	- (void)performLeaderToFollower:(UIButton *)buttton
	{
	    // wake up _lwPortRunLoopThread and send data from leader to follower
	    [self postSelector:@selector(actualPerfomLeaderToFolloer) onThread:_lwPortRunLoopThread withObject:nil];
	}
	- (void)actualPerfomLeaderToFolloer
	{
	    NSString *content = @"This_Is_A_Leader_To_Follower_Message_Data";
	    int length = (int)[content length];
	    NSMutableData *data = [[NSMutableData alloc] init];
	    [data appendBytes:&length length:sizeof(int)];
	    [data appendData:[content dataUsingEncoding:NSUTF8StringEncoding]];
	    LWPortMessage *messge = [[LWPortMessage alloc] initWithSendPort:_leaderPort receivePort:_worker.localPort components:data];
	    [messge sendBeforeDate:0];
	}
the worker should like:
	- (void)launchThreadWithPort:(LWPort *)port
	{
	    @autoreleasepool {
	        [self send:port];
	    }
	}
	- (void)send:(LWPort *)port
	{
		    [NSThread currentThread].name = @"workerPortLoopThread";
		    _distantPort = (LWSocketPort *)port;
		    _localPort = [[LWSocketPort alloc] initWithTCPPort:8082];
		    _localPort.delegate = self;
		    [_localPort setType:LWSocketPortRoleTypeFollower];
		    NSString *content = @"This_Is_A_Follower_To_Leader_Message_Data";
		    int length = (int)[content length];
		    NSMutableData *data = [[NSMutableData alloc] init];
		    [data appendBytes:&length length:sizeof(int)];
		    [data appendData:[content dataUsingEncoding:NSUTF8StringEncoding]];
		    LWPortMessage *messge = [[LWPortMessage alloc] initWithSendPort:_localPort receivePort:_distantPort components:data];
		    [messge sendBeforeDate:0];
		    LWRunLoop *_currentRunLoop = [[NSThread currentThread] looper];
		    [_currentRunLoop addPort:_localPort forMode:LWDefaultRunLoop];
		    [_currentRunLoop runMode:LWDefaultRunLoop];
	}

Links

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].