# Combine: Creating a custom Publisher by wrapping another Publisher and operating on it

**URL:** <https://forums.swift.org/t/combine-creating-a-custom-publisher-by-wrapping-another-publisher-and-operating-on-it/48812>\
**Category:** Using Swift\
**Tags:** combine, publisher, backpressure, demand\
**Created:** [May 24, 2021, 5:28pm UTC](https://forums.swift.org/t/combine-creating-a-custom-publisher-by-wrapping-another-publisher-and-operating-on-it/48812 "2021-05-24T17:28:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tourultimate](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@tourultimate](https://forums.swift.org/u/tourultimate)\
**Post date:** [May 24, 2021, 5:28pm UTC](https://forums.swift.org/t/combine-creating-a-custom-publisher-by-wrapping-another-publisher-and-operating-on-it/48812/1 "2021-05-24T17:28:43Z")

</div>

I am trying to create a custom publisher that basically wraps a different publisher and performs a series of operators on it. The reason why I want a custom publisher is because I'm trying to encapsulate some logic for re-use within it and I also need to store some state.

Here is a simplified version of what I'm doing:

```
struct BundledPublisher<State, Output, Failure: Error>: Publisher {
    var wrapped: AnyPublisher<Output, Failure>
    var state: State
    
    func receive<S>(subscriber: S) where S : Subscriber, Failure == S.Failure, Output == S.Input {
        // Start with original publisher
        wrapped
            // Add some operators, not really important what these are
            .delay(for: .seconds(1), scheduler: DispatchQueue.main)
            .dropFirst()
            // forward the receive call
            .receive(subscriber: subscriber)
    }
}

```

My question is if this is a legal, acceptable and functioning workflow? Testing seems to indicate that this approach works correctly. But it's unclear to me if I'm missing something with regards to maybe back pressure or demand.

---

<div class="post-metadata">

**Author:** ![sharplet](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sharplet/32/6184_2.png) [@sharplet](https://forums.swift.org/u/sharplet)\
**Post date:** [May 24, 2021, 5:41pm UTC](https://forums.swift.org/t/combine-creating-a-custom-publisher-by-wrapping-another-publisher-and-operating-on-it/48812/2 "2021-05-24T17:41:29Z")

</div>

This seems fine to me, but the main thing I continue to be unsure about in this context (i.e., wrapping publishers), is whether to call `subscribe()` or `receive(subscriber:)`. I've seen advice on the internet that one should always call `subscribe()`, but I honestly still don't understand why, or what the implications are for not doing so.

---

<div class="post-metadata">

**Author:** ![tourultimate](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@tourultimate](https://forums.swift.org/u/tourultimate)\
**Post date:** [May 24, 2021, 6:18pm UTC](https://forums.swift.org/t/combine-creating-a-custom-publisher-by-wrapping-another-publisher-and-operating-on-it/48812/3 "2021-05-24T18:18:02Z")

</div>

> I've seen advice on the internet that one should always call `subscribe()`

Thanks for the reply @sharplet. Do you have any references to where you've seen that advice?

Thanks!

---

<div class="post-metadata">

**Author:** ![millenomi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/millenomi/32/2922_2.png) [@millenomi](https://forums.swift.org/u/millenomi)\
**Post date:** [May 24, 2021, 6:33pm UTC](https://forums.swift.org/t/combine-creating-a-custom-publisher-by-wrapping-another-publisher-and-operating-on-it/48812/4 "2021-05-24T18:33:05Z")

</div>

I'm going to give a canonical answer here that `subscribe(…)` in Combine has some additional observation logic that we would prefer get triggered before it forwards the subscription over, and that it is preferred over invoking `receive(subscription:)` directly.

---

<div class="post-metadata">

**Author:** ![tourultimate](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@tourultimate](https://forums.swift.org/u/tourultimate)\
**Post date:** [May 24, 2021, 6:45pm UTC](https://forums.swift.org/t/combine-creating-a-custom-publisher-by-wrapping-another-publisher-and-operating-on-it/48812/5 "2021-05-24T18:45:58Z")

</div>

Thank you @millenomi, I appreciate the prompt response! So this is an acceptable way to wrap another publisher then?

```
struct BundledPublisher<State, Output, Failure: Error>: Publisher {
    var wrapped: AnyPublisher<Output, Failure>
    var state: State
    
    func receive<S>(subscriber: S) where S : Subscriber, Failure == S.Failure, Output == S.Input {
        wrapped
            .delay(for: .seconds(1), scheduler: DispatchQueue.main)
            .dropFirst()
            .subscribe(subscriber)
    }
}

```

Is there any documentation that I can refer my colleagues to? Do you have some insider insight into how this is implemented?

---

<div class="post-metadata">

**Author:** ![sharplet](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sharplet/32/6184_2.png) [@sharplet](https://forums.swift.org/u/sharplet)\
**Post date:** [May 24, 2021, 7:04pm UTC](https://forums.swift.org/t/combine-creating-a-custom-publisher-by-wrapping-another-publisher-and-operating-on-it/48812/6 "2021-05-24T19:04:58Z")

</div>

Slightly off-topic, but if this is representative of your real custom publisher, it might be worth making it generic over the _publisher_ type, instead of `Output` and `Failure` individually:

```swift
struct BundledPublisher<Wrapped: Publisher>: Publisher {
  typealias Output = Wrapped.Output
  typealias Failure = Wrapped.Failure

  var wrapped: Wrapped

  // ...
}

```

This would help you avoid intermediate `AnyPublisher`s where they aren't strictly necessary.

---

<div class="post-metadata">

**Author:** ![tourultimate](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@tourultimate](https://forums.swift.org/u/tourultimate)\
**Post date:** [May 24, 2021, 7:05pm UTC](https://forums.swift.org/t/combine-creating-a-custom-publisher-by-wrapping-another-publisher-and-operating-on-it/48812/7 "2021-05-24T19:05:46Z")

</div>

I do see this in the docs, which seems fairly clear that I shouldn't be calling `receive(subscriber:)` directly. Any additional references are welcome. Thank you.

```
/// Attaches the specified subscriber to this publisher.
///
/// Always call this function instead of ``Publisher/receive(subscriber:)``.
/// Adopters of ``Publisher`` must implement ``Publisher/receive(subscriber:)``. The implementation of ``Publisher/subscribe(_:)-4u8kn`` provided by ``Publisher`` calls through to ``Publisher/receive(subscriber:)``.
///
/// - Parameter subscriber: The subscriber to attach to this publisher. After attaching, the subscriber can start to receive values.

```

---

<div class="post-metadata">

**Author:** ![tourultimate](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@tourultimate](https://forums.swift.org/u/tourultimate)\
**Post date:** [May 24, 2021, 7:07pm UTC](https://forums.swift.org/t/combine-creating-a-custom-publisher-by-wrapping-another-publisher-and-operating-on-it/48812/8 "2021-05-24T19:07:45Z")

</div>

> [@sharplet](#):
>
> Slightly off-topic, but if this is representative of your real custom publisher, it might be worth making it generic over the _publisher_ type, instead of `Output` and `Failure` individually:

Good point! Thank you
