Skip to content

Commit

Permalink
简化module, 添加存储
Browse files Browse the repository at this point in the history
  • Loading branch information
念纪 committed Dec 18, 2015
1 parent f1fa5fc commit 52e33e9
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 230 deletions.
108 changes: 94 additions & 14 deletions AppLord/ALContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,119 @@

#import <Foundation/Foundation.h>
#import <AppLord/ALEvent.h>
#import <AppLord/ALContextInfo.h>

@protocol ALModule, ALService;
@class ALTask;
@interface ALContext : NSObject

@property (nonatomic, strong, readonly) ALContextInfo *_Nonnull info;

/**
* Get the singletion
*/
+ (_Nonnull instancetype)sharedContext;

- (__nullable id)findService:( Protocol * _Nonnull )serviceProtocol;
- (__nullable id)findServiceByName:( NSString * _Nonnull )name;
@end

@interface ALContext (Service)

/**
* Regist a service with it's implement class
*/
- (void)registService:(Protocol *_Nonnull)proto withImpl:(Class _Nonnull)implClass;

/**
* Find the service implement of the protocol, return nil if not regist, create instance
* if not create
*/
- (__nullable id)findService:(Protocol *_Nonnull)serviceProtocol;

/**
* Just like `findService`, but you can pass a service's class name
*/
- (__nullable id)findServiceByName:(NSString *_Nonnull)name;

/**
* Figure out if a service is registed
*/
- (BOOL)existService:(NSString *_Nonnull)serviceName;


@end


@interface ALContext (Module)

/**
* regist a module with it's class name
*/
- (void)registModule:(Class _Nonnull)moduleClass;

/**
* init all the module registed
*/
- (void)loadModules;

/**
* Find the module instance
*/
- (__nullable id)findModule:(Class _Nonnull)moduleClass;

@end

@interface ALContext (Event)

/**
* Send a event to observers. you can use this method send message to other modules
*/
- (void)sendEvent:(ALEvent *_Nonnull)event;

/**
* Send a event to observers.
*/
- (void)sendEventWithId:(NSString *_Nonnull)eventId userInfo:(NSDictionary *_Nullable)userInfo;

/**
* add observer for a event by eventId
*/
- (void)addEventObserver:(id _Nonnull)observer forEventId:(NSString *_Nonnull)eventId;
- (void)addEventObserver:(id _Nonnull)observer forEventIdArray:(NSArray *_Nonnull)eventIdArray;

- (void)registService:(Protocol *_Nonnull)proto withImpl:(Class _Nonnull)implClass;
- (void)registModule:(Class _Nonnull)moduleClass;
/**
* remove observer for a event by eventId
*/
- (void)removeEventObserver:(id _Nonnull)observer forEventId:(NSString *_Nonnull)eventId;

@end

@interface ALContext (Task)

/**
* 初始化,在didFinishLaunching中调用
* @param launchOptions 启动参数
* @param launchTasks 启动任务项,格式参考 [{"name":"TestTask1", },...]
* the max concurrent of default operation queue
*/
- (void)setupWithLaunchOptions:(NSDictionary *_Nullable)launchOptions
launchTask:(NSArray *_Nullable)launchTasks;
@property (nonatomic, assign) NSInteger maxConcurrentOperationCount;

/**
* batch run tasks, sync on current thread. can't concurrent,serial
*/
- (void)addSyncTasks:(NSArray<NSOperation *> *_Nonnull)tasks;

/**
* batch run tasks, async on current thread.
*/
- (void)addAsyncTasks:(NSArray<NSOperation *> *_Nonnull)tasks;

/**
* add a task in the default background operation queue.
*/
- (void)addTask:(NSOperation *_Nonnull)task;

@end

@interface ALContext (Object)

FOUNDATION_EXTERN ALContext * _Nonnull ALContextGet();
- (void)setObject:(id _Nonnull)value forKey:(NSString *_Nonnull)key;
- (nullable id)objectForKey:(NSString *_Nonnull)key;

- (nullable NSString *)stringForKey:(NSString *_Nonnull)key;
- (nullable NSDictionary *)dictionaryForKey:(NSString *_Nonnull)key;
- (nullable NSArray *)arrayForKey:(NSString *_Nonnull)key;

@end
Loading

0 comments on commit 52e33e9

Please sign in to comment.