mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
52 lines
1.3 KiB
Swift
52 lines
1.3 KiB
Swift
//
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import PromiseKit
|
|
|
|
public extension YapDatabaseConnection {
|
|
|
|
@objc
|
|
func readWritePromise(_ block: @escaping (YapDatabaseReadWriteTransaction) -> Void) -> AnyPromise {
|
|
return AnyPromise(readWritePromise(block) as Promise<Void>)
|
|
}
|
|
|
|
func readWritePromise(_ block: @escaping (YapDatabaseReadWriteTransaction) -> Void) -> Promise<Void> {
|
|
return Promise { resolver in
|
|
self.asyncReadWrite(block, completionBlock: { resolver.fulfill(()) })
|
|
}
|
|
}
|
|
|
|
func read(_ block: @escaping (YapDatabaseReadTransaction) throws -> Void) throws {
|
|
var errorToRaise: Error?
|
|
|
|
read { transaction in
|
|
do {
|
|
try block(transaction)
|
|
} catch {
|
|
errorToRaise = error
|
|
}
|
|
}
|
|
|
|
if let errorToRaise = errorToRaise {
|
|
throw errorToRaise
|
|
}
|
|
}
|
|
|
|
func readWrite(_ block: @escaping (YapDatabaseReadWriteTransaction) throws -> Void) throws {
|
|
var errorToRaise: Error?
|
|
|
|
readWrite { transaction in
|
|
do {
|
|
try block(transaction)
|
|
} catch {
|
|
errorToRaise = error
|
|
}
|
|
}
|
|
|
|
if let errorToRaise = errorToRaise {
|
|
throw errorToRaise
|
|
}
|
|
}
|
|
}
|