Skip to content

Commit

Permalink
sync mode for div card updating
Browse files Browse the repository at this point in the history
  • Loading branch information
korziuk committed Jan 12, 2024
1 parent 7bf7bd3 commit e245d3c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
4 changes: 4 additions & 0 deletions DivKit/DivKitComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ public final class DivKitComponents {
timerStorage.set(cardId: cardId, timers: divData.timers ?? [])
}

public func flushUpdateActions() {
updateAggregator.flushUpdateActions()
}

private func onVariablesChanged(event: DivVariablesStorage.ChangeEvent) {
switch event.kind {
case let .global(variables):
Expand Down
20 changes: 11 additions & 9 deletions DivKit/RunLoopCardUpdateAggregator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Foundation

final class RunLoopCardUpdateAggregator {
private var enabled: Bool = true
private var scheduled: Bool = false
private var batch: [DivActionURLHandler.UpdateReason] = []
private let updateCardAction: DivKitComponents.UpdateCardAction
private let mainThreadAsyncRunner: MainThreadAsyncRunner
Expand All @@ -27,19 +26,22 @@ final class RunLoopCardUpdateAggregator {
func aggregate(_ reason: DivActionURLHandler.UpdateReason) {
Thread.assertIsMain()
guard enabled else { return }
let notScheduled = batch.isEmpty
batch.append(reason)
if !scheduled {
scheduled = true
if notScheduled {
mainThreadAsyncRunner { [weak self] in
guard let self = self else { return }
if let reasons = NonEmptyArray(self.batch.merge()) {
self.batch = []
self.scheduled = false
self.updateCardAction(reasons)
}
self?.flushUpdateActions()
}
}
}

func flushUpdateActions() {
guard let reasons = NonEmptyArray(self.batch.merge()) else {
return
}
batch = []
updateCardAction(reasons)
}
}

extension Array where Element == DivActionURLHandler.UpdateReason {
Expand Down

0 comments on commit e245d3c

Please sign in to comment.