Skip to content

Redesign Observers #82

Description

@LastSprint

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)
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions