-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// FHSPromise.h | ||
// Firehose | ||
// | ||
// Created by Adam Kirk on 2/22/14. | ||
// Copyright (c) 2014 Mysterious Trousers. All rights reserved. | ||
// | ||
|
||
|
||
typedef NS_ENUM(NSUInteger, FHSPromiseState) { | ||
FHSPromiseStatePending, | ||
FHSPromiseStateResolved, | ||
FHSPromiseStateRejected | ||
}; | ||
|
||
|
||
typedef void (^FHSPromiseHandler)(void); | ||
|
||
|
||
@interface MYSDeferred : NSObject | ||
|
||
@property (nonatomic, assign, readonly) FHSPromiseState state; | ||
|
||
+ (instancetype)sharedDeferrerForActionName:(NSString *)actionName; | ||
|
||
- (MYSDeferred *)always:(FHSPromiseHandler)handler; | ||
|
||
- (MYSDeferred *)done:(FHSPromiseHandler)handler; | ||
|
||
- (MYSDeferred *)fail:(FHSPromiseHandler)handler; | ||
|
||
//- (FHSPromise *)then:(void (^)(id object))handler; | ||
|
||
//- (FHSPromise *)progress:(void (^)(id object))handler; | ||
|
||
//- (FHSPromise *)notify:(void (^)(id object))handler; | ||
|
||
- (void)reject; | ||
|
||
- (void)resolve; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// | ||
// FHSPromise.m | ||
// Firehose | ||
// | ||
// Created by Adam Kirk on 2/22/14. | ||
// Copyright (c) 2014 Mysterious Trousers. All rights reserved. | ||
// | ||
|
||
#import "MYSDeferred.h" | ||
|
||
|
||
@interface MYSDeferred () | ||
@property (nonatomic, assign) FHSPromiseState state; | ||
@property (nonatomic, copy ) NSMutableArray *alwaysHandlers; | ||
@property (nonatomic, copy ) NSMutableArray *doneHandlers; | ||
@property (nonatomic, copy ) NSMutableArray *failHandlers; | ||
@end | ||
|
||
|
||
@implementation MYSDeferred | ||
|
||
- (id)init | ||
{ | ||
self = [super init]; | ||
if (self) { | ||
_state = FHSPromiseStatePending; | ||
_alwaysHandlers = [NSMutableArray new]; | ||
_doneHandlers = [NSMutableArray new]; | ||
_failHandlers = [NSMutableArray new]; | ||
} | ||
return self; | ||
} | ||
|
||
+ (instancetype)sharedDeferrerForActionName:(NSString *)actionName | ||
{ | ||
static NSMutableDictionary *__deferrers; | ||
|
||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
__deferrers = [NSMutableDictionary new]; | ||
}); | ||
|
||
if (!__deferrers[actionName]) { | ||
__deferrers[actionName] = [MYSDeferred new]; | ||
} | ||
|
||
return __deferrers[actionName]; | ||
} | ||
|
||
- (MYSDeferred *)always:(FHSPromiseHandler)handler | ||
{ | ||
if (handler) { | ||
if (self.state == FHSPromiseStateRejected || self.state == FHSPromiseStateResolved) { | ||
handler(); | ||
} | ||
else { | ||
[self.alwaysHandlers addObject:handler]; | ||
} | ||
} | ||
return self; | ||
} | ||
|
||
- (MYSDeferred *)done:(FHSPromiseHandler)handler | ||
{ | ||
if (handler) { | ||
if (self.state == FHSPromiseStateResolved) { | ||
handler(); | ||
} | ||
else { | ||
[self.doneHandlers addObject:handler]; | ||
} | ||
} | ||
return self; | ||
} | ||
|
||
- (MYSDeferred *)fail:(FHSPromiseHandler)handler | ||
{ | ||
if (handler) { | ||
if (self.state == FHSPromiseStateRejected) { | ||
handler(); | ||
} | ||
else { | ||
[self.failHandlers addObject:handler]; | ||
} | ||
} | ||
return self; | ||
} | ||
|
||
//- (FHSPromise *)then:(void (^)(id object))handler | ||
//{ | ||
// return self; | ||
//} | ||
|
||
//- (FHSPromise *)progress:(void (^)(id object))handler | ||
//{ | ||
// | ||
//} | ||
|
||
//- (FHSPromise *)notify:(void (^)(id object))handler | ||
//{ | ||
// | ||
//} | ||
|
||
- (void)reject | ||
{ | ||
self.state = FHSPromiseStateRejected; | ||
for (FHSPromiseHandler handler in [self.failHandlers arrayByAddingObjectsFromArray:self.alwaysHandlers]) { | ||
handler(); | ||
[self.failHandlers removeObject:handler]; | ||
[self.alwaysHandlers removeObject:handler]; | ||
} | ||
} | ||
|
||
- (void)resolve | ||
{ | ||
self.state = FHSPromiseStateResolved; | ||
for (FHSPromiseHandler handler in [self.doneHandlers arrayByAddingObjectsFromArray:self.alwaysHandlers]) { | ||
handler(); | ||
[self.doneHandlers removeObject:handler]; | ||
[self.alwaysHandlers removeObject:handler]; | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// MYSReady.h | ||
// FirehoseChat | ||
// | ||
// Created by Adam Kirk on 10/4/14. | ||
// Copyright (c) 2014 Adam Kirk. All rights reserved. | ||
// | ||
|
||
|
||
@interface MYSReady : NSObject | ||
|
||
+ (instancetype)sharedReadyForActionName:(NSString *)actionName; | ||
|
||
@property (nonatomic, strong) id object; | ||
|
||
@property (nonatomic, strong) void (^actionBlock)(id object); | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// MYSReady.m | ||
// FirehoseChat | ||
// | ||
// Created by Adam Kirk on 10/4/14. | ||
// Copyright (c) 2014 Adam Kirk. All rights reserved. | ||
// | ||
|
||
#import "MYSReady.h" | ||
|
||
|
||
@implementation MYSReady | ||
|
||
+ (instancetype)sharedReadyForActionName:(NSString *)actionName | ||
{ | ||
static NSMutableDictionary *__objects; | ||
|
||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
__objects = [NSMutableDictionary new]; | ||
}); | ||
|
||
if (!__objects[actionName]) { | ||
__objects[actionName] = [MYSReady new]; | ||
} | ||
|
||
return __objects[actionName]; | ||
} | ||
|
||
- (void)setObject:(id)object | ||
{ | ||
_object = object; | ||
if (self.actionBlock) { | ||
self.actionBlock(object); | ||
_object = nil; | ||
} | ||
} | ||
|
||
- (void)setActionBlock:(void (^)(id object))actionBlock | ||
{ | ||
_actionBlock = actionBlock; | ||
if (self.object) { | ||
actionBlock(self.object); | ||
_object = nil; | ||
} | ||
} | ||
|
||
|
||
@end |