Created
November 5, 2012 13:28
(Not a really good) use of RACStashSubject
This file contains hidden or 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
- (instancetype)initWithURL:(NSURL *)url type:(NSString *)type { | |
self = [super initWithURL:url type:type]; | |
if (!self) { | |
return nil; | |
} | |
_contentBacking = [RACReplaySubject replaySubjectWithCapacity:1]; | |
NSError *error; | |
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; | |
if (!error) { | |
[_contentBacking sendNext:content]; | |
} else { | |
[_contentBacking sendError:error]; | |
} | |
RACStashSubject *unsavedContent = [RACStashSubject stashSubjectWithLatestValueOnly:YES]; | |
[_contentBacking subscribe:unsavedContent]; | |
@weakify(self); | |
// Autosave every 3 minutes, with a 1 minute leeway | |
// TODO: if the content isn't changed until `unsavedContent` is subscribed to, the next content change will trigger a save. `-[id<RACSubscribable> windowWithStart:close:]` would behave better in that regard | |
[[RACSubscribable interval:180.0 withLeeway:60.0] subscribeNext:^(id x) { | |
// NOTE: this only works because `+[RACSubscribable interval:withLeeway:]` delivers on the main thread, and we deliver the unsaved content on a non-main thread, so `unsavedContentDisposable` is properly set by the time the `subscribeNext:` block is called | |
__block RACDisposable *unsavedContentDisposable = [[unsavedContent deliverOn:fsScheduler()] subscribeNext:^(NSString *x) { | |
@strongify(self); | |
[self saveContent:x]; | |
[unsavedContentDisposable dispose]; | |
}]; | |
}]; | |
return self; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment