Doric bridge

This commit is contained in:
pengfei.zhou
2019-07-29 20:23:00 +08:00
parent 4a5acaa781
commit a17a21954a
15 changed files with 383 additions and 212 deletions

View File

@@ -16,6 +16,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)createContext:(DoricContext *)context script:(NSString *)script source:(NSString *)source;
- (void)destroyContext:(DoricContext *)context;
- (DoricContext *)getContext:(NSString *)contextId;
@end
NS_ASSUME_NONNULL_END

View File

@@ -44,6 +44,14 @@ - (void)createContext:(DoricContext *)context script:(NSString *)script source:(
});
}
- (DoricContext *)getContext:(NSString *)contextId {
__block DoricContext *context;
dispatch_sync(self.mapQueue, ^{
NSValue *value = [self.doricContextMap valueForKey:contextId];
context = value.nonretainedObjectValue;
});
return context;
}
- (void)destroyContext:(DoricContext *)context {
NSString *contextId = context.contextId;

View File

@@ -0,0 +1,16 @@
//
// DoricModalPlugin.h
// Doric
//
// Created by pengfei.zhou on 2019/7/29.
//
#import <Foundation/Foundation.h>
#import "DoricNativePlugin.h"
NS_ASSUME_NONNULL_BEGIN
@interface DoricModalPlugin : DoricNativePlugin
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,20 @@
//
// DoricModalPlugin.m
// Doric
//
// Created by pengfei.zhou on 2019/7/29.
//
#import "DoricModalPlugin.h"
#import "DoricRegistry.h"
@implementation DoricModalPlugin
- (void)toast:(NSString *)message promise:(DoricPromise *)promise {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"toast:%@",message);
});
}
@end

View File

@@ -7,6 +7,9 @@
#import <Foundation/Foundation.h>
#import "DoricComponent.h"
#import "DoricPromise.h"
#import "DoricRegistry.h"
NS_ASSUME_NONNULL_BEGIN
@interface DoricNativePlugin : DoricComponent

View File

@@ -0,0 +1,21 @@
//
// DoricPromise.h
// Doric
//
// Created by pengfei.zhou on 2019/7/29.
//
#import <Foundation/Foundation.h>
#import "DoricContext.h"
NS_ASSUME_NONNULL_BEGIN
@interface DoricPromise : NSObject
- (instancetype)initWithContext: (DoricContext *)context callbackId:(NSString *)callbackId;
- (void)resolve:(id) result;
- (void)reject:(id) result;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,34 @@
//
// DoricPromise.m
// Doric
//
// Created by pengfei.zhou on 2019/7/29.
//
#import "DoricPromise.h"
#import "DoricConstant.h"
@interface DoricPromise()
@property (nonatomic,strong) DoricContext *context;
@property (nonatomic,strong) NSString *callbackId;
@end
@implementation DoricPromise
- (instancetype)initWithContext:(DoricContext *)context callbackId:(NSString *)callbackId {
if(self = [super init]){
_context = context;
_callbackId = callbackId;
}
return self;
}
- (void)resolve:(id)result {
[self.context.driver invokeDoricMethod:DORIC_BRIDGE_RESOLVE,self.context.contextId,self.callbackId,result,nil];
}
- (void)reject:(id)result {
[self.context.driver invokeDoricMethod:DORIC_BRIDGE_REJECT,self.context.contextId,self.callbackId,result,nil];
}
@end

View File

@@ -11,7 +11,14 @@ NS_ASSUME_NONNULL_BEGIN
@interface DoricRegistry : NSObject
- (NSString *)acquireJSBundle:(NSString *)bundleName;
- (NSString *)acquireJSBundle:(NSString *)name;
- (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name;
- (void)registerNativePlugin:(Class)pluginClass withName:(NSString *)name;
- (Class)acquireNativePlugin:(NSString *)name;
@end

View File

@@ -6,7 +6,45 @@
//
#import "DoricRegistry.h"
#import "DoricModalPlugin.h"
#import <objc/runtime.h>
@interface DoricRegistry ()
@property (nonatomic,strong) NSMutableDictionary *bundles;
@property (nonatomic,strong) NSMutableDictionary *plugins;
@end
@implementation DoricRegistry
- (instancetype)init {
if(self = [super init]){
_bundles = [[NSMutableDictionary alloc] init];
_plugins = [[NSMutableDictionary alloc] init];
[self innerRegister];
}
return self;
}
- (void)innerRegister {
[self registerNativePlugin:DoricModalPlugin.class withName:@"modal"];
}
- (void)registerJSBundle:(NSString *)bundle withName:(NSString *)name {
[self.bundles setObject:bundle forKey:name];
}
- (NSString *)acquireJSBundle:(NSString *)name {
return [self.bundles objectForKey:name];
}
- (void)registerNativePlugin:(Class)pluginClass withName:(NSString *)name {
[self.plugins setObject:pluginClass forKey:name];
}
- (Class)acquireNativePlugin:(NSString *)name {
return [self.plugins objectForKey:name];
}
@end