Giter Club home page Giter Club logo

reactivearray's Introduction

ReactiveArray

Carthage compatible Build Status Coverage Status Release

An array class implemented in Swift that can be observed using ReactiveCocoa's Signals.

Installation

Carthage

Add the following to your Cartfile:

github "Wolox/ReactiveArray"

Then run carthage update.

Follow the current instructions in Carthage's README for up to date installation instructions.

You'll also need to add Result.framework, Box.framework and ReactiveCocoa.framework to your Xcode project.

Usage

Array operations

let array = ReactiveArray(elements: [1,2,3])

array.append(4) // => [1,2,3,4]
array.append(5) // => [1,2,3,4,5]

array[0] = 5 // => [5,2,3,4,5]
array.removeAtIndex(4) // => [5,2,3,4]

ReactiveArray conforms to CollectionType and MutableCollectionType which allows you to perform operations like map or filter.

Observing changes

The array can be observed for mutations using the signal or producer properties. Both Signal and SignalProducer will emit Operation values on observation for any of the mutating operations performed to the array.

Operation is an enumeration with three cases. One for each mutating operation: Append, Insert and RemoveElement

Using SignalProducer

Prints an append operation for each element that has already been stored in the array and then the corresponding operation for any new operation performed to the array.

let array = ReactiveArray(elements: [1,2,3])
array.producer |> start(next: { println($0) })
array[0] = 5
array[1] = 4
array.append(2)
array.removeAtIndex(2)

will print the following output:

.Append(value: 1)
.Append(value: 2)
.Append(value: 3)
.Insert(value: 5, atIndex: 0)
.Insert(value: 4, atIndex: 1)
.RemoveElement(atIndex:2)

Using Signal

The signal property will only emit values for operations performed in the array after observation.

let array = ReactiveArray(elements: [1,2,3])
array.signal.observe { println($0) }
array[0] = 5
array[1] = 4
array.append(2)
array.removeAtIndex(2)

will print the following output:

.Insert(value: 5, atIndex: 0)
.Insert(value: 4, atIndex: 1)
.RemoveElement(atIndex:2)

Using observableCount

You can also observe the observableCount property that exposes a producer that emits the current amount of elements each time the array is mutated.

let array = ReactiveArray<String>()
array.observableCount.producer |> start(next: { println($0) })
array.append("Hello")
array.append("World")

will print the following output:

0
1
2

Mirror

Mirror creates a new ReactiveArray by applying a transform operation for each element contained in the original array. It is like map but the returned array will mutate everytime the original array mutates.

let array = ReactiveArray(elements: [1,2,3])
let doubles = array.mirror { $0 * 2 }
doubles.producer |> start(next: { println($0) })
array.append(4)
array.append(5)
array[0] = 6

will print the following output:

.Append(value: 2)
.Append(value: 4)
.Append(value: 6)
.Append(value: 8)
.Append(value: 10)
.Insert(value: 12, atIndex: 0)

Contributing

Setup project

If you want to contribute you need to setup your development environment first.

git clone https://github.com/Wolox/ReactiveArray.git
cd ReactiveArray
script/bootstrap
open ReactiveArray.xcodeproj

Environment dependencies

The following dependencies must be installed in your development machine.

  • XCode & XCode command line tools
  • Homebrew
  • Ruby
  • Bundler (sudoless)
  • Carthage
  • Gcovr
  • SwiftCov

About

This project is maintained by Guido Marucci Blas and it was written by Wolox.

Wolox

License

ReactiveArray is available under the MIT license.

Copyright (c) 2015 Guido Marucci Blas <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

reactivearray's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.