Skip to content

Instantly share code, notes, and snippets.

@Coneko
Created November 5, 2012 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Coneko/4017177 to your computer and use it in GitHub Desktop.
Save Coneko/4017177 to your computer and use it in GitHub Desktop.
(Not a really good) use of RACStashSubject
- (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