Combine

<aside> ๐Ÿ’ก ์‹œ๊ฐ„ ๊ฒฝ๊ณผ์— ๋”ฐ๋ผ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฐ’์„ ๋‚ด๋ณด๋‚ด๋Š” Publisher์™€ ์ด๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” Subscriber๋กœ ์‹œ๊ฐ„ ๊ฒฝ๊ณผ์— ๋”ฐ๋ฅธ ๊ฐ’ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์„ ์–ธ์  Swift API

</aside>

import Combine

Untitled

Untitled

Publisher

protocol Publisher {
    associatedtype Output
    associatedtype Failure : Error
    func receive<S>(subscriber: S) where S : Subscriber, Self.Failure == S.Failure, Self.Output == S.Input
}

Publisher์™€ Subscriber ์‚ฌ์ด์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ ์ˆœ์„œ๊ฐ€ ์ •๋ฆฌ๋˜์–ด์žˆ๋Š” ์‚ฌ์ง„

Publisher์™€ Subscriber ์‚ฌ์ด์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ ์ˆœ์„œ๊ฐ€ ์ •๋ฆฌ๋˜์–ด์žˆ๋Š” ์‚ฌ์ง„

Untitled

  1. Publisher๊ฐ€ Subscriber๋ฅผ ๊ตฌ๋…
    1. Publisher์˜ ์ธ์Šคํ„ด์Šคํ•จ์ˆ˜ย subscribe()๋ฅผ ํ˜ธ์ถœ
  2. Subscriber๋Š” Subscribe๋ฅผ ์ƒ์„ฑํ•ด์„œ Subscriber์—๊ฒŒ ์ œ๊ณต
    1. Publisher์€ ๋‚ด๋ถ€์ ์œผ๋กœ receive()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ
    2. ๊ฒŒ์‹œ์ž๊ฐ€ ๋ฐ›์€ ๊ตฌ๋…์ž์—๊ฒŒ Subscriber์˜ ์ธ์Šคํ„ด์Šค ํ•จ์ˆ˜ receive(subscription: )์„ ํ†ตํ•ด ๊ตฌ๋…์„ ์ „๋‹ฌ
  3. Subscriber๊ฐ€ ๊ฐ’์„ ์š”์ฒญ
    1. Subscription(๊ตฌ๋…)์˜ ์ธ์Šคํ„ด์Šคํ•จ์ˆ˜์ธ request()๋ฅผ ํ˜ธ์ถœ
  4. Publisher๊ฐ€ ๊ฐ’์„ ๋ณด๋ƒ„
    1. Subscriber์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ receive(_ input)๋ฅผ ์ด์šฉํ•ด์„œ ์ „๋‹ฌํ•˜๋ฉฐ ๊ตฌ๋…์ž๋Š” ๊ฐ’์„ ๋ฐ›์Œ
  5. Publisher๊ฐ€ ์™„๋ฃŒ๋ฅผ ๋ณด๋ƒ„
    1. ๊ฒŒ์‹œ์ž๊ฐ€ ๋ชจ๋“  ๊ฐ’์„ ๋ณด๋ƒˆ๋‹ค๋ฉด ํ˜น์€ ์™„๋ฃŒ ์ด๋ฒคํŠธ๋ฅผ receive(completion: )ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ๊ตฌ๋…์ž์—๊ฒŒ ์™„๋ฃŒ์ด๋ฒคํŠธ๋ฅผ ์ „๋‹ฌํ•จ

์˜ˆ์‹œ ์ฝ”๋“œ

Publisher ์ข…๋ฅ˜