An array class implemented in Swift that can be observed using ReactiveCocoa's Signals.
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.
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
.
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 four cases. One for each
mutating operation: Append
, Insert
, Update
and RemoveElement
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)
.Update(value: 5, atIndex: 0)
.Update(value: 4, atIndex: 1)
.RemoveElement(atIndex:2)
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:
.Update(value: 5, atIndex: 0)
.Update(value: 4, atIndex: 1)
.RemoveElement(atIndex:2)
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 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)
.Update(value: 12, atIndex: 0)
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
The following dependencies must be installed in your development machine.
- XCode & XCode command line tools
- Homebrew
- Ruby
- Bundler (sudoless)
- Carthage
- Gcovr
- SwiftCov
This project is maintained by Guido Marucci Blas and it was written by Wolox.
ReactiveArray is available under the MIT license.
Copyright (c) 2015 Guido Marucci Blas <guidomb@wolox.com.ar>
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.