mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			53 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			53 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Swift
		
	
| 
											5 years ago
										 | // | ||
|  | //  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 | ||
|  |         } | ||
|  |     } | ||
|  | } |