Skip to content

Commit

Permalink
Track calls to add/remove file presenter
Browse files Browse the repository at this point in the history
  • Loading branch information
SlayterDev committed Sep 19, 2024
1 parent e1effda commit b4903ed
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions Sources/Configuration/DefaultConfigurationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,18 @@ open class DefaultConfigurationManager: NSObject {
public var fetcher: ConfigurationFetching
public var store: ConfigurationStoring

private var filePresenterCount: Int = 0

public init(fetcher: ConfigurationFetching, store: ConfigurationStoring, defaults: KeyValueStoring) {
self.fetcher = fetcher
self.store = store
self.defaults = defaults
super.init()
NSFileCoordinator.addFilePresenter(self)
addPresenter()
}

deinit {
NSFileCoordinator.removeFilePresenter(self)
removePresenter()
}

public static let queue: DispatchQueue = DispatchQueue(label: "Configuration Manager")
Expand All @@ -94,6 +96,22 @@ open class DefaultConfigurationManager: NSObject {
}
public var lastRefreshCheckTime: Date = Date()

/// Calls to `addFilePresenter` and `removeFilePresenter` must be balanced
/// We'll use `filePresenterCount` to ensure we're properly managing this.
@objc public func addPresenter() {
guard filePresenterCount == 0 else { return }
NSFileCoordinator.addFilePresenter(self)
filePresenterCount += 1
}

@objc public func removePresenter() {
guard filePresenterCount > 0 else { return }
while filePresenterCount > 0 {
NSFileCoordinator.removeFilePresenter(self)
filePresenterCount -= 1
}
}

public func start() {
Logger.config.debug("Starting configuration refresh timer")
refreshTask = Task.periodic(interval: Constants.refreshCheckIntervalSeconds) {
Expand Down

0 comments on commit b4903ed

Please sign in to comment.