Skip to content

Commit

Permalink
adding MYSReady
Browse files Browse the repository at this point in the history
  • Loading branch information
atomkirk committed Oct 4, 2014
1 parent cc8d050 commit b456041
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 0 deletions.
42 changes: 42 additions & 0 deletions MYSDeferred.h
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
124 changes: 124 additions & 0 deletions MYSDeferred.m
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
18 changes: 18 additions & 0 deletions MYSReady.h
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
49 changes: 49 additions & 0 deletions MYSReady.m
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

0 comments on commit b456041

Please sign in to comment.