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

SPT-1998 Поддержка отмены таски для Combine #131

Conversation

mrandrewsmith
Copy link
Collaborator

@mrandrewsmith mrandrewsmith commented Apr 17, 2024

Что сделано

  • Созданы кастомные Publisher для AsyncNode и AsyncStreamNode с поддержкой отмены Task. Интерфейс и реализация сделаны по аналогии с URLSession DataTaskPublisher.

Примеры использования

Теперь для AsyncNode и AsyncStreamNode интерфейс один, но для каждого из них будет создан свой Publisher:

ChainConfigurator()
    .build() // <- Возвращает AsyncNode
    .nodeResultPublisher()
    .sink {
    }

ChainConfigurator()
    .build() // <- Возвращает AsyncStreamNode
    .nodeResultPublisher()
    .sink {
    }

При отмене подписки будет вызван cancel у созданной таски:

let cancellable = ChainConfigurator()
    .build()
    .nodeResultPublisher()
    .sink {
    }

cancellable.cancel()

При каждой новой подписке будет создана новая таска с вызовом метода process()

let publisher = ChainConfigurator()
    .build()
    .nodeResultPublisher()

publisher.sink { // <- Создается новая таска и вызывается метод  process
}

publisher.sink { // <- Создается новая таска и вызывается метод  process
}

Шарить результат можно стандартными средствами Combine:

let multicast = ChainConfigurator()
    .build()
    .nodeResultPublisher()
    .multicast { PassthroughSubject() }

multicast.sink { // <- Новая таска не создается
}

multicast.sink { // <- Новая таска не создается
}

multicast.connect() // <- Создается новая таска и вызывается метод  process

@codecov-commenter
Copy link

codecov-commenter commented Apr 17, 2024

Codecov Report

Attention: Patch coverage is 71.95946% with 83 lines in your changes are missing coverage. Please review.

Project coverage is 91.02%. Comparing base (da998b5) to head (f47756f).

Files Patch % Lines
NodeKit/NodeKit/Chains/ChainBuilder.swift 70.70% 29 Missing ⚠️
NodeKit/NodeKit/Chains/ServiceChainProvider.swift 30.76% 27 Missing ⚠️
...odeKit/Core/Node/Async/MergedAsyncStreamNode.swift 0.00% 11 Missing ⚠️
...e/Node/Combine/Subscription/BaseSubscription.swift 70.58% 10 Missing ⚠️
NodeKit/NodeKit/Core/Node/Async/AsyncNode.swift 75.00% 2 Missing ⚠️
NodeKit/NodeKit/Encodings/ParameterEncoding.swift 75.00% 2 Missing ⚠️
...rs/RequestBuildingLayer/URLQueryInjectorNode.swift 50.00% 2 Missing ⚠️
Additional details and impacted files
@@                     Coverage Diff                      @@
##           SPT-1998-example-refresh     #131      +/-   ##
============================================================
- Coverage                     91.39%   91.02%   -0.37%     
============================================================
  Files                            83       87       +4     
  Lines                          1267     1304      +37     
============================================================
+ Hits                           1158     1187      +29     
- Misses                          109      117       +8     
Flag Coverage Δ
tests 91.02% <71.95%> (-0.37%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mrandrewsmith mrandrewsmith force-pushed the SPT-1998-combine-task-cancelation branch 2 times, most recently from be1212c to f3e65e3 Compare May 2, 2024 15:54
@mrandrewsmith mrandrewsmith force-pushed the SPT-1998-example-refresh branch from 5804141 to da998b5 Compare May 7, 2024 04:28
@mrandrewsmith mrandrewsmith force-pushed the SPT-1998-combine-task-cancelation branch from f3e65e3 to f47756f Compare May 7, 2024 04:42
@mrandrewsmith mrandrewsmith deleted the branch SPT-1998-example-refresh May 13, 2024 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants