Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Bag with initial elements #609

Merged
merged 7 commits into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
1. New property operator: `filter` (#586, kudos to @iv-mexx)
1. New operator `merge(with:)` (#600, kudos to @ra1028)
1. New operator `map(value:)` (#601, kudos to @ra1028)
1. Bag can be created with the initial elements now (#609, kudos to @ra1028)

# 3.1.0
1. Fixed `schedule(after:interval:leeway:)` being cancelled when the returned `Disposable` is not retained. (#584, kudos to @jjoelson)
Expand Down
18 changes: 14 additions & 4 deletions Sources/Bag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@ public struct Bag<Element> {
fileprivate let value: UInt64
}

fileprivate var elements: ContiguousArray<Element> = []
fileprivate var tokens: ContiguousArray<UInt64> = []
fileprivate var elements: ContiguousArray<Element>
fileprivate var tokens: ContiguousArray<UInt64>

private var nextToken = Token(value: 0)
private var nextToken: Token

public init() {}
public init() {
elements = ContiguousArray()
tokens = ContiguousArray()
nextToken = Token(value: 0)
}

public init<S: Sequence>(_ elements: S) where S.Iterator.Element == Element {
self.elements = ContiguousArray(elements)
self.nextToken = Token(value: UInt64(self.elements.count))
self.tokens = ContiguousArray(0..<nextToken.value)
}

/// Insert the given value into `self`, and return a token that can
/// later be passed to `remove(using:)`.
Expand Down
7 changes: 1 addition & 6 deletions Sources/Disposable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,7 @@ public final class CompositeDisposable: Disposable {
public init<S: Sequence>(_ disposables: S)
where S.Iterator.Element == Disposable
{
var bag: Bag<Disposable> = Bag()

for disposable in disposables {
bag.insert(disposable)
}

let bag = Bag(disposables)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

self.disposables = Atomic(bag)
self.state = UnsafeAtomicState(DisposableState.active)
}
Expand Down
18 changes: 18 additions & 0 deletions Tests/ReactiveSwiftTests/BagSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,23 @@ class BagSpec: QuickSpec {
expect(bag).toNot(contain("bar"))
expect(bag).toNot(contain("buzz"))
}

it("should create bag with initial values") {
let values = ["foo", "bar", "buzz"]
var bag = Bag(values)

let a = bag.insert("fuzz")

expect(bag).to(contain("foo"))
expect(bag).to(contain("bar"))
expect(bag).to(contain("buzz"))
expect(bag).to(contain("fuzz"))

bag.remove(using: a)
expect(bag).to(contain("foo"))
expect(bag).to(contain("bar"))
expect(bag).to(contain("buzz"))
expect(bag).toNot(contain("fuzz"))
}
}
}
18 changes: 18 additions & 0 deletions Tests/ReactiveSwiftTests/DisposableSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ class DisposableSpec: QuickSpec {
disposable.dispose()
expect(simpleDisposable.isDisposed) == false
}

it("should create with initial disposables") {
let disposable1 = AnyDisposable()
let disposable2 = AnyDisposable()
let disposable3 = AnyDisposable()

let compositeDisposable = CompositeDisposable([disposable1, disposable2, disposable3])

expect(disposable1.isDisposed) == false
expect(disposable2.isDisposed) == false
expect(disposable3.isDisposed) == false

compositeDisposable.dispose()

expect(disposable1.isDisposed) == true
expect(disposable2.isDisposed) == true
expect(disposable3.isDisposed) == true
}
}

describe("ScopedDisposable") {
Expand Down