This adds a sync mechanism in Ndb.swift to coordinate certain usage of nostrdb.c calls and the need to close nostrdb due to app lifecycle requirements. Furthermore, it fixes the order of operations when re-opening NostrDB, to avoid race conditions where a query uses an older Ndb generation. This sync mechanism allows multiple queries to happen simultaneously (from the Swift-side), while preventing ndb from simultaneously closing during such usages. It also does that while keeping the Ndb interface sync and nonisolated, which keeps the API easy to use from Swift/SwiftUI and allows for parallel operations to occur. If Swift Actors were to be used (e.g. creating an NdbActor), the Ndb.swift interface would change in such a way that it would propagate the need for several changes throughout the codebase, including loading logic in some ViewModels. Furthermore, it would likely decrease performance by forcing Ndb.swift operations to run sequentially when they could run in parallel. Changelog-Fixed: Fixed crashes that happened when the app went into background mode Closes: https://github.com/damus-io/damus/issues/3245 Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
65 lines
2.4 KiB
Swift
65 lines
2.4 KiB
Swift
//
|
||
// MockDamusState.swift
|
||
// damusTests
|
||
//
|
||
// Created by Daniel D’Aquino on 2023-10-13.
|
||
//
|
||
|
||
import Foundation
|
||
@testable import damus
|
||
import EmojiPicker
|
||
|
||
// Generates a test damus state with configurable mock parameters
|
||
@MainActor
|
||
func generate_test_damus_state(
|
||
mock_profile_info: [Pubkey: Profile]?,
|
||
home: HomeModel? = nil,
|
||
addNdbToRelayPool: Bool = true
|
||
) -> DamusState {
|
||
// Create a unique temporary directory
|
||
let ndb = Ndb.test
|
||
let our_pubkey = test_pubkey
|
||
let settings = UserSettingsStore()
|
||
|
||
let profiles: Profiles = {
|
||
guard let mock_profile_info, let profiles: Profiles = MockProfiles(mocked_profiles: mock_profile_info, ndb: ndb) else {
|
||
return Profiles.init(ndb: ndb)
|
||
}
|
||
return profiles
|
||
}()
|
||
|
||
let mutelist_manager = MutelistManager(user_keypair: test_keypair)
|
||
let damus = DamusState(keypair: test_keypair,
|
||
likes: .init(our_pubkey: our_pubkey),
|
||
boosts: .init(our_pubkey: our_pubkey),
|
||
contacts: .init(our_pubkey: our_pubkey),
|
||
contactCards: ContactCardManagerMock(),
|
||
mutelist_manager: mutelist_manager,
|
||
profiles: profiles,
|
||
dms: home?.dms ?? .init(our_pubkey: our_pubkey),
|
||
previews: .init(),
|
||
zaps: .init(our_pubkey: our_pubkey),
|
||
lnurls: .init(),
|
||
settings: settings,
|
||
relay_filters: .init(our_pubkey: our_pubkey),
|
||
relay_model_cache: .init(),
|
||
drafts: .init(),
|
||
events: .init(ndb: ndb),
|
||
bookmarks: .init(pubkey: our_pubkey),
|
||
replies: .init(our_pubkey: our_pubkey),
|
||
wallet: .init(settings: settings),
|
||
nav: .init(),
|
||
music: .init(onChange: {_ in }),
|
||
video: .init(),
|
||
ndb: ndb,
|
||
quote_reposts: .init(our_pubkey: our_pubkey),
|
||
emoji_provider: DefaultEmojiProvider(showAllVariations: false),
|
||
favicon_cache: .init(),
|
||
addNdbToRelayPool: addNdbToRelayPool
|
||
)
|
||
|
||
home?.damus_state = damus
|
||
|
||
return damus
|
||
}
|