From a1056f70178e09acd87e96f2fc50d988feeed48a Mon Sep 17 00:00:00 2001 From: Peter Date: Wed, 29 Apr 2015 20:23:13 +0300 Subject: [PATCH] Added take --- SSignalKit.xcodeproj/project.pbxproj | 10 +++++++ SSignalKit/SSignal+Take.h | 7 +++++ SSignalKit/SSignal+Take.m | 40 ++++++++++++++++++++++++++++ SSignalKit/SSignalKit.h | 1 + 4 files changed, 58 insertions(+) create mode 100644 SSignalKit/SSignal+Take.h create mode 100644 SSignalKit/SSignal+Take.m diff --git a/SSignalKit.xcodeproj/project.pbxproj b/SSignalKit.xcodeproj/project.pbxproj index 8d264f799c..f065ddfdbb 100644 --- a/SSignalKit.xcodeproj/project.pbxproj +++ b/SSignalKit.xcodeproj/project.pbxproj @@ -86,6 +86,9 @@ D089E0341AC48EA7009A744B /* SThreadPoolTask.h in Headers */ = {isa = PBXBuildFile; fileRef = D089E0301AC48EA7009A744B /* SThreadPoolTask.h */; settings = {ATTRIBUTES = (Public, ); }; }; D089E0391AC56981009A744B /* SThreadPoolQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = D089E02E1AC48EA7009A744B /* SThreadPoolQueue.m */; }; D089E03A1AC56981009A744B /* SThreadPoolTask.m in Sources */ = {isa = PBXBuildFile; fileRef = D089E02D1AC48EA7009A744B /* SThreadPoolTask.m */; }; + D0FC5B431AF140E600F353AB /* SSignal+Take.h in Headers */ = {isa = PBXBuildFile; fileRef = D0FC5B411AF140E600F353AB /* SSignal+Take.h */; settings = {ATTRIBUTES = (Public, ); }; }; + D0FC5B441AF140E600F353AB /* SSignal+Take.m in Sources */ = {isa = PBXBuildFile; fileRef = D0FC5B421AF140E600F353AB /* SSignal+Take.m */; }; + D0FC5B451AF141F800F353AB /* SSignal+Take.m in Sources */ = {isa = PBXBuildFile; fileRef = D0FC5B421AF140E600F353AB /* SSignal+Take.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -171,6 +174,8 @@ D089E02E1AC48EA7009A744B /* SThreadPoolQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SThreadPoolQueue.m; sourceTree = ""; }; D089E02F1AC48EA7009A744B /* SThreadPoolQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SThreadPoolQueue.h; sourceTree = ""; }; D089E0301AC48EA7009A744B /* SThreadPoolTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SThreadPoolTask.h; sourceTree = ""; }; + D0FC5B411AF140E600F353AB /* SSignal+Take.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "SSignal+Take.h"; sourceTree = ""; }; + D0FC5B421AF140E600F353AB /* SSignal+Take.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "SSignal+Take.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -258,6 +263,8 @@ D0445E191A7C2D7300267924 /* SSignal+Single.m */, D0445E1A1A7C2D7300267924 /* SSignal+Timing.h */, D0445E1B1A7C2D7300267924 /* SSignal+Timing.m */, + D0FC5B411AF140E600F353AB /* SSignal+Take.h */, + D0FC5B421AF140E600F353AB /* SSignal+Take.m */, D0445E1C1A7C2D7300267924 /* SSubscriber.h */, D0445E1D1A7C2D7300267924 /* SSubscriber.m */, D0445E1E1A7C2D7300267924 /* SThreadPool.h */, @@ -323,6 +330,7 @@ D0445E321A7C2D7300267924 /* SSignal+Accumulate.h in Headers */, D0445E361A7C2D7300267924 /* SSignal+Combine.h in Headers */, D0445E2A1A7C2D7300267924 /* SDisposable.h in Headers */, + D0FC5B431AF140E600F353AB /* SSignal+Take.h in Headers */, D0445E421A7C2D7300267924 /* SSignal+SideEffects.h in Headers */, D0445E3E1A7C2D7300267924 /* SSignal+Meta.h in Headers */, D0445E2B1A7C2D7300267924 /* SEvent.h in Headers */, @@ -475,6 +483,7 @@ D0445E471A7C2D7300267924 /* SSignal+Timing.m in Sources */, D0445E311A7C2D7300267924 /* SSignal.m in Sources */, D087632B1A839EDC00632240 /* SDisposableSet.m in Sources */, + D0FC5B441AF140E600F353AB /* SSignal+Take.m in Sources */, D089E0311AC48EA7009A744B /* SThreadPoolTask.m in Sources */, D0445E491A7C2D7300267924 /* SSubscriber.m in Sources */, D0445E431A7C2D7300267924 /* SSignal+SideEffects.m in Sources */, @@ -517,6 +526,7 @@ D0445E7B1A7C447D00267924 /* SSignal+Accumulate.m in Sources */, D0445E851A7C447D00267924 /* SSignal+Timing.m in Sources */, D0445E7A1A7C447D00267924 /* SSignal.m in Sources */, + D0FC5B451AF141F800F353AB /* SSignal+Take.m in Sources */, D087632C1A839EE800632240 /* SDisposableSet.m in Sources */, D0445E861A7C447D00267924 /* SSubscriber.m in Sources */, D0445E831A7C447D00267924 /* SSignal+SideEffects.m in Sources */, diff --git a/SSignalKit/SSignal+Take.h b/SSignalKit/SSignal+Take.h new file mode 100644 index 0000000000..796db95af9 --- /dev/null +++ b/SSignalKit/SSignal+Take.h @@ -0,0 +1,7 @@ +#import + +@interface SSignal (Take) + +- (SSignal *)take:(NSUInteger)count; + +@end diff --git a/SSignalKit/SSignal+Take.m b/SSignalKit/SSignal+Take.m new file mode 100644 index 0000000000..af0a38d6f0 --- /dev/null +++ b/SSignalKit/SSignal+Take.m @@ -0,0 +1,40 @@ +#import "SSignal+Take.h" + +#import "SAtomic.h" + +@implementation SSignal (Take) + +- (SSignal *)take:(NSUInteger)count +{ + return [[SSignal alloc] initWithGenerator:^id(SSubscriber *subscriber) + { + SAtomic *counter = [[SAtomic alloc] initWithValue:@(0)]; + return [self startWithNext:^(id next) + { + __block bool passthrough = false; + __block bool complete = false; + [counter modify:^id(NSNumber *currentCount) + { + NSUInteger updatedCount = [currentCount unsignedIntegerValue] + 1; + if (updatedCount <= count) + passthrough = true; + if (updatedCount == count) + complete = true; + return @(updatedCount); + }]; + + if (passthrough) + [subscriber putNext:next]; + if (complete) + [subscriber putCompletion]; + } error:^(id error) + { + [subscriber putError:error]; + } completed:^ + { + [subscriber putCompletion]; + }]; + }]; +} + +@end diff --git a/SSignalKit/SSignalKit.h b/SSignalKit/SSignalKit.h index 3021283400..f3910b50fb 100644 --- a/SSignalKit/SSignalKit.h +++ b/SSignalKit/SSignalKit.h @@ -35,5 +35,6 @@ FOUNDATION_EXPORT const unsigned char SSignalKitVersionString[]; #import #import #import +#import #import #import