NodeKit icon indicating copy to clipboard operation
NodeKit copied to clipboard

Redesign Observers

Open LastSprint opened this issue 5 years ago • 0 comments

At this moment for creating your own Observer you need to subclass Context and then implement your own logic, bit I think that it's bad approach.

We need redesign this system's part to composition of objects. We should use type erasure technic and replace all classes with boxed structs.

For example like this:

public protocol AsyncIterator {
    associatedtype Value

    func next() -> Value
}

public struct AnyAsyncIterator<Value>: AsyncIterator {

    private let nested: BaseAsyncPager<Value>

    public init<Nested>(nested: Nested) where Nested: AsyncIterator, Nested.Value == Value {
        self.nested = AsyncPagerBox(nested: nested)
    }


    public func next() -> Value {
        return self.nested.next()
    }
}

private class AsyncPagerBox<Value, Nested>: BaseAsyncPager<Value> where Nested: AsyncIterator, Nested.Value == Value {

    let nested: Nested

    init(nested: Nested) {
        self.nested = nested
    }

    override func next() ->Value {
        return self.nested.next()
    }
}

private class BaseAsyncPager<Value>: AsyncIterator {

    func next() -> Value {
        preconditionFailure("\(self.self) \(#function) not implemented")
    }
}

And then we will use observers like this:

func makeProcessing() -> AnyObserver<T> {
    let yourOwn = YourOwnObserver()
    return .init(nested: yourOwn)
}

LastSprint avatar Feb 06 '20 11:02 LastSprint