Skip to content

Instantly share code, notes, and snippets.

@jonsterling
Last active December 23, 2015 18:49
Show Gist options
  • Select an option

  • Save jonsterling/6678080 to your computer and use it in GitHub Desktop.

Select an option

Save jonsterling/6678080 to your computer and use it in GitHub Desktop.
// RACCommand α β === Kleisli RACSignal α β === α -> RACSignal β
// We can pullback along the kleisli arrow to change the input to a command.
// Likewise, we can pushout along the arrow to change the output to a command.
RACSignal *validationSignal = self.form.validationSignal.replayLast;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
doneButton.rac_command = [self.viewModel.doneCommand ys_pullback:^RACSignal *(id input) {
return [validationSignal take:1];
}];
// There are two alternate approaches. The first one I did was to make a
// dummy command with `RACSignal.empty` as its signal block, and then
// sample the proper input from its execution signal, and then use that
// to execute the real command which requires input. This is a bit
// messy.
RACCommand *doneCommand = self.viewModel.doneCommand;
RACCommand *dummyCommand = [[RACCommand alloc] initWithEnabled:doneCommand.enabled signalBlock:^RACSignal *(id input) {
return [RACSignal empty];
}];
[[self.form.validationSignal
sample:dummyCommand.executionSignals]
subscribeNext:^(id value) {
[doneCommand execute:value];
}];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
doneButton.rac_command = doneCommand;
// Another option is to do basically the same thing as the pullback, but
// inline without any help:
RACSignal *validationSignal = self.form.validationSignal.replayLast;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
RACCommand *doneCommand = self.viewModel.doneCommand;
doneButton.rac_command = [[RACCommand alloc] initWithEnabled:doneCommand.enabled signalBlock:^RACSignal *(id input) {
return [[validationSignal take:1] flattenMap:^RACSignal *(id validatedData) {
return [doneCommand execute:validatedData];
}];
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment