Skip to content

Commit

Permalink
Release 0.0.14
Browse files Browse the repository at this point in the history
  • Loading branch information
changsanjiang committed Dec 27, 2019
1 parent cdec25a commit dda0a32
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 33 deletions.
2 changes: 1 addition & 1 deletion SJRouter.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SJRouter'
s.version = '0.0.13'
s.version = '0.0.14'
s.summary = 'SJRouter.'

# This description is used to generate tags and improve search results.
Expand Down
2 changes: 1 addition & 1 deletion SJRouter/Core/SJRouteHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
NS_ASSUME_NONNULL_BEGIN
typedef void(^SJCompletionHandler)(id _Nullable result, NSError *_Nullable error);
typedef void(^SJRouterUnhandledCallback)(SJRouteRequest *request, UIViewController *topViewController);
typedef void(^SJRouterUnableToGetAnInstanceCallback)(SJRouteRequest *request);
typedef void(^SJRouterUnableToGetAnInstanceCallback)(SJRouteRequest *request, SJCompletionHandler _Nullable handler);

@protocol SJRouteHandler
@optional
Expand Down
4 changes: 4 additions & 0 deletions SJRouter/Core/SJRouteRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ NS_ASSUME_NONNULL_BEGIN
@interface SJRouteRequest(CreateByURL)
- (nullable instancetype)initWithURL:(NSURL *)URL;
@property (nonatomic, strong, readonly, nullable) NSURL *originalURL;

/// 追加参数
- (void)setValue:(nullable id)value forParameterKey:(NSString *)key;
- (void)addParameters:(NSDictionary *)parameters;
@end
NS_ASSUME_NONNULL_END
38 changes: 38 additions & 0 deletions SJRouter/Core/SJRouteRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,44 @@ - (instancetype)_initWithPath:(nullable NSString *)rq parameters:(nullable SJPar
return self;
}

- (void)setValue:(nullable id)value forParameterKey:(NSString *)key {
if ( key.length != 0 ) {
NSMutableDictionary *dictm = _prts ? [_prts mutableCopy] : NSMutableDictionary.new;
dictm[key] = value;
_prts = dictm.copy;

if ( _originalURL != nil ) {
NSURLComponents *components = [[NSURLComponents alloc] initWithURL:_originalURL resolvingAgainstBaseURL:YES];
NSMutableArray<NSURLQueryItem *> *arrm = components.queryItems ? [components.queryItems mutableCopy] : NSMutableArray.new;
__block NSInteger index = NSNotFound;
[arrm enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSURLQueryItem * _Nonnull item, NSUInteger idx, BOOL * _Nonnull stop) {
if ( [item.name isEqualToString:key] ) {
index = idx;
*stop = YES;
}
}];

if ( index == NSNotFound ) index = arrm.count;

if ( value == nil ) {
if ( index < arrm.count ) [arrm removeObjectAtIndex:index];
}
else {
NSURLQueryItem *item = [NSURLQueryItem queryItemWithName:key value:[NSString stringWithFormat:@"%@", value]];
[arrm insertObject:item atIndex:index];
}
components.queryItems = [arrm copy];
_originalURL = components.URL;
}
}
}

- (void)addParameters:(NSDictionary *)parameters {
[parameters enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
[self setValue:obj forParameterKey:key];
}];
}

- (NSString *)description {
return
[NSString stringWithFormat:@"[%@<%p>] {\n \
Expand Down
67 changes: 36 additions & 31 deletions SJRouter/SJRouter.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@

@interface SJRouter()
@property (nonatomic, strong, readonly) NSMutableDictionary<NSString *, id<SJRouteHandler>> *handlersM;
@property (nonatomic, strong, readonly) dispatch_semaphore_t lock;
@end

@implementation SJRouter {
dispatch_group_t _group;
}

@implementation SJRouter
static SEL sel_handler_v1 __deprecated_msg("use `sel_handler_v2`");
static SEL sel_handler_v2;
static SEL sel_instance;
Expand Down Expand Up @@ -61,9 +59,10 @@ + (instancetype)shared {
- (instancetype)init {
self = [super init];
if ( !self ) return nil;
_lock = dispatch_semaphore_create(0);
_handlersM = [NSMutableDictionary new];
_group = dispatch_group_create();
dispatch_group_async(_group, dispatch_get_global_queue(0, 0), ^{

dispatch_async(dispatch_get_global_queue(0, 0), ^{
/// Thanks @yehot, @Potato121
/// https://www.jianshu.com/p/534eccb63974
/// https://github.com/changsanjiang/SJRouter/pull/1
Expand Down Expand Up @@ -112,8 +111,8 @@ - (instancetype)init {
if ( names ) free(names);
}
if ( imgs ) free(imgs);
dispatch_semaphore_signal(self->_lock);
});

return self;
}

Expand Down Expand Up @@ -159,18 +158,18 @@ - (instancetype)init {
///
- (void)instanceWithRequest:(SJRouteRequest *)request completionHandler:(nullable SJCompletionHandler)completionHandler {
if ( request == nil ) return;
dispatch_group_notify(_group, dispatch_get_main_queue(), ^{
__auto_type _Nullable handler = self->_handlersM[request.requestPath];
if ( [(id)handler respondsToSelector:sel_instance] ) {
[handler instanceWithRequest:request completionHandler:completionHandler];
}
else {
dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER);
__auto_type _Nullable handler = self->_handlersM[request.requestPath];
if ( [(id)handler respondsToSelector:sel_instance] ) {
[handler instanceWithRequest:request completionHandler:completionHandler];
}
else {
#ifdef DEBUG
printf("\n(-_-) unable to get an instance: [%s]\n", request.description.UTF8String);
printf("\n(-_-) unable to get an instance: [%s]\n", request.description.UTF8String);
#endif
if ( self->_unableToGetAnInstanceCallback ) self->_unableToGetAnInstanceCallback(request);
}
});
if ( self->_unableToGetAnInstanceCallback ) self->_unableToGetAnInstanceCallback(request, completionHandler);
}
dispatch_semaphore_signal(_lock);
}

///
Expand Down Expand Up @@ -208,37 +207,43 @@ - (void)instanceWithRequest:(SJRouteRequest *)request completionHandler:(nullabl
///
- (void)handleRequest:(SJRouteRequest *)request completionHandler:(nullable SJCompletionHandler)completionHandler {
if ( request == nil ) return;
dispatch_group_notify(_group, dispatch_get_main_queue(), ^{
__auto_type _Nullable handler = self->_handlersM[request.requestPath];
if ( [(id)handler respondsToSelector:sel_handler_v2] ) {
[handler handleRequest:request topViewController:_sj_get_top_view_controller() completionHandler:completionHandler];
}
dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER);
__auto_type _Nullable handler = self->_handlersM[request.requestPath];
if ( [(id)handler respondsToSelector:sel_handler_v2] ) {
[handler handleRequest:request topViewController:_sj_get_top_view_controller() completionHandler:completionHandler];
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
else if ( [(id)handler respondsToSelector:sel_handler_v1] ) {
[(id<SJRouteHandlerDeprecatedMethods>)handler handleRequestWithParameters:request.prts topViewController:_sj_get_top_view_controller() completionHandler:completionHandler];
}
else if ( [(id)handler respondsToSelector:sel_handler_v1] ) {
[(id<SJRouteHandlerDeprecatedMethods>)handler handleRequestWithParameters:request.prts topViewController:_sj_get_top_view_controller() completionHandler:completionHandler];
}
#pragma clang diagnostic pop
else {
else {
#ifdef DEBUG
printf("\n(-_-) Unhandled request: [%s]\n", request.description.UTF8String);
printf("\n(-_-) Unhandled request: [%s]\n", request.description.UTF8String);
#endif
if ( self->_unhandledCallback ) self->_unhandledCallback(request, _sj_get_top_view_controller());
}
});
if ( self->_unhandledCallback ) self->_unhandledCallback(request, _sj_get_top_view_controller());
}
dispatch_semaphore_signal(_lock);
}

///
/// 是否可以处理某个路径
///
- (BOOL)canHandleRoutePath:(NSString *)routePath {
if ( 0 == routePath.length ) return NO;
return _handlersM[routePath] != nil;

dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER);
BOOL res = _handlersM[routePath] != nil;
dispatch_semaphore_signal(_lock);
return res;
}

///
/// 手动添加路由
///
/// 注意: 为保证线程安全应该总是在`+addRoutesToRouter:`中调用该方法
///
- (void)addRoute:(SJRouteObject *)object {
if ( object != nil ) {
for ( NSString *path in object.paths ) {
Expand Down

0 comments on commit dda0a32

Please sign in to comment.