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

Redesign Observers #82

Open
LastSprint opened this issue Feb 6, 2020 · 0 comments
Open

Redesign Observers #82

LastSprint opened this issue Feb 6, 2020 · 0 comments

Comments

@LastSprint
Copy link
Contributor

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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants