Latest0.2.4
Homepagehttps://github.com/derkis/Reactor
LicenseMIT
Platformsios 7.0, requires ARC
Authors

Reactor in an Objective-C library that provides mechanisms for writing transparently reactive code. It’s based on the Tracker
library from Meteor.js, which you can view the source for here.

How does it work?

An example reactive function using Reactor looks something like this:

[MTRReactor autorun:^(MTRComputation *computation) {
    self.counterLabel.text = @(self.counter).description;
}];

Which re-runs automagically as the counter property updates:

for(int i=0 ; i<100 ; i++) {
    self.counter++;
}

Seems like there’s a wizard behind the curtain, right? There is! Let’s get a good look at his underthings. Reactor has two core components, MTRComputation and MTRDependency, that are linked up on the sly by MTRReactor to provide reactivity.

Computations

An MTRComputation is created every time you call -autorun:—it’s an object that encapsulates the block you pass to
the same. Only one computation can run at a time, and the running computation is known as the

"current computation"

The block passed to -autorun: is called immediately to update the view, but it’s also used to infer what
this new computation’s dependencies are. How does that happen?

Dependencies

Here’s the wizard’s torso (it’s what he uses to wave hello):

- (NSInteger)counter
{
    [self.counterDependency depend];
    return _counter;
}

This bit was left off the original example. In the accessor for counter we’re secretly calling -depend another
property, counterDependency, which we created earlier:

self.counterDependency = [MTRDependency new]

When you call -depend on a dependency, Reactor looks at the current computation and adds it as a dependent of your
dependency. The legs, the powertrain:

- (void)setCounter:(NSInteger)counter
{
    _counter = counter;
    [self.counterDependency changed];
}

When -changed is called on a dependency, all of its dependent computations will be re-run. This updates your UI, re-establishes the dependent relationships, and kicks-off the cycle all over again. It’s not that magical, eh? With legs and a torso you’re as capable as any wizard.

Declarative Reactivity

Creating all those dependencies is pretty tedious. Worse, if you override the setter and getter for a property to trigger a dependency its storage isn’t @synthesized anymore and you have to do it manually.

Enter MTRReactive. Annotate any of your classes with this protocol, and all its properties become implicitly reactive:

@interface Person : NSObject <MTRReactive>
@property (copy  , nonatomic) NSString *name;
@property (assign, nonatomic) NSInteger age;
@end

If you want to whitelist/blacklist certain properties, you can implement either +reactiveProperties: or +nonreactiveProperties:, respectively.

There are a few caveats to keep in mind:

  • Just because your superclass adopts MTRReactive doesn’t mean your properties are also reactive. Every class that wants reactivity must adopt the protocol independently.
  • Properties which don’t have a setter won’t be reactive, as there’s no way to invalidate its dependency.

You can then react to your objects’ properties in computations like normal:

[MTRReactor autorun:^(MTRComputation *computation) {
    self.ageLabel = @(person.age).description;
}];

Latest podspec

{
    "name": "Reactor",
    "version": "0.2.4",
    "summary": "Transparent reactivity based on Meteor.Tracker",
    "description": "                   Reactor provides mechanisms for writing transparently reactive code. It's based on the Tracker library n                   from [Meteor.js](https://www.meteor.com/), which you can view the source for [here](https://github.com/meteor/meteor/blob/devel/packages/tracker/tracker.js).n",
    "homepage": "https://github.com/derkis/Reactor",
    "authors": {
        "Ty Cobb": "[email protected]"
    },
    "license": {
        "type": "MIT",
        "file": "License.txt"
    },
    "platforms": {
        "ios": "7.0"
    },
    "requires_arc": true,
    "source": {
        "git": "https://github.com/derkis/Reactor.git",
        "tag": "v0.2.4"
    },
    "source_files": "Reactor/*.{h,m}",
    "public_header_files": "Reactor/*.h"
}

Pin It on Pinterest

Share This