DEV Community

Rene (Renetik) Dohan
Rene (Renetik) Dohan

Posted on • Updated on

Renetik iOS - Event & Property

https://github.com/renetik/renetik-ios-event

Documentation

Framework to enjoy, improve and speed up your application development while writing readable code.
Used as library in many projects and improving it while developing new projects.
I am open for Hire or investment in my mobile app music production & perfromance project Renetik Instruments www.renetik.com.

Installation

You can install using swift package manager for now using latest released version or master.

Examples

/**
 * Simple event use cases
 */
final class EventTests: XCTestCase {

    func testListen() throws {
        let event = event()
        var count = 0
        event.listen { count += 1 }
        event.fire()
        event.fire()
        XCTAssertEqual(count, 2)
    }

    func testArgListen() throws {
        let event: CSEvent<Int> = event()
        var count = 0
        event.listen { count += $0 }
        event.fire(2)
        event.fire(3)
        XCTAssertEqual(count, 5)
    }

    func testListenOnce() throws {
        let event = event()
        var count = 0
        event.listenOnce { count += 1 }
        event.fire()
        event.fire()
        XCTAssertEqual(count, 1)
    }

    func testArgListenOnce() throws {
        let event = event()
        var count = 0
        event.listenOnce { count += 1 }
        event.fire()
        event.fire()
        XCTAssertEqual(count, 1)
    }

    func testEventCancel() throws {
        let event = event()
        var count = 0
        event.listen { registration, _ in
            count += 1
            if count == 2 { registration.cancel() }
        }
        event.fire()
        event.fire()
        event.fire()
        XCTAssertEqual(count, 2)
    }

    func testStringEventCancel() throws {
        let event: CSEvent<String> = event()
        var value: String? = nil
        event.listen {
            $0.cancel()
            value = $1
        }
        event.fire("first")
        XCTAssertEqual("first", value)
        event.fire("second")
    }

    func testEventPause() throws {
        let event = event()
        var count = 0
        let registration = event.listen { count += 1 }
        registration.pause { event.fire() }
        XCTAssertEqual(count, 0)
        event.fire()
        XCTAssertEqual(count, 1)
    }
}
Enter fullscreen mode Exit fullscreen mode
/**
 * Simple event property use cases
 */
class EventPropertyTests: XCTestCase {

    func testOnChange() throws {
        let property = property("initial")
        var count = 0
        property.onChange { count += 1 }
        property.value = "second"
        property.value = "third"
        XCTAssertEqual(count, 2)
        XCTAssertEqual("third", property.value)
    }

    func testOnApply() throws {
        var count = 0
        let property = property("initial") { _ in count += 1 }.apply()
        property.value = "second"
        property.value = "third"
        XCTAssertEqual(count, 3)
        XCTAssertEqual("third", property.value)
    }

    func testArgListen() throws {
        var count = 0
        let property = property(0) { count += 1 }
        property.value += 2
        property.value += 3
        XCTAssertEqual(5, property.value)
        XCTAssertEqual(2, count)
    }

    func testEquals() throws {
        var count = 0
        let property = property(""){ count += 1 }
        property.value = "second"
        property.value = "second"
        XCTAssertEqual(count, 1)
        XCTAssertEqual("second", property.value)
    }

    func testOnChangeOnce() throws {
        var count = 0
        let property = property("")
        property.onChangeOnce { count += 1 }
        property.value = "second"
        property.value = "third"
        XCTAssertEqual(count, 1)
        XCTAssertEqual("third", property.value)
    }

    func testEventCancel() throws {
        var count = 0
        let property = property(0)
        property.onChange { registration, value in
            count += value
            if count > 2 { registration.cancel() }
        }
        property.value = 1
        property.value = 2
        property.value = 3
        XCTAssertEqual(count, 3)
    }

    func testEventPause() throws {
        var count = 0
        let property = property(0)
        let registration = property.onChange { count += $1 }
        registration.pause { property.value = 1 }
        XCTAssertEqual(count, 0)
        property.value = 2
        XCTAssertEqual(count, 2)
    }
}
Enter fullscreen mode Exit fullscreen mode
/**
 * Event unregister after owner niled
 */
final class EventOwnerEventTest: XCTestCase {

    func testUnregisteredAfterNilled() throws {
        var owner: CSEventOwner? = CSEventOwnerBase()
        let event = event()
        var count = 0
        owner!.register(event.listen { count += 1 })
        event.fire()
        event.fire()
        XCTAssertEqual(count, 2)
        owner = nil
        event.fire()
        XCTAssertEqual(count, 2)
    }
}
Enter fullscreen mode Exit fullscreen mode
/**
 * Event property unregister after owner niled
 */
class EventOwnerPropertyTest: XCTestCase {
    class SomeClass: CSEventOwnerBase {
        let prop = property("initial value")
        init(arugment: SomeClass? = nil) {
            super.init()
            arugment?.also {
                register($0.prop.onChange { [unowned self] in prop.value = $0 })
            }
        }
    }
    func testUnregisteredAfterNilled() throws {
        let class1 = SomeClass()
        var class2: SomeClass? = SomeClass(arugment: class1)
        let class3 = SomeClass(arugment: class2)
        XCTAssertEqual(class3.prop.value, "initial value")
        class1.prop.value = "first value"
        XCTAssertEqual(class3.prop.value, "first value")
        class2 = nil
        class1.prop.value = "second value"
        XCTAssertEqual(class3.prop.value, "first value")
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)