2021-06-07 05:08:40 -06:00
|
|
|
//
|
|
|
|
// Callback.swift
|
|
|
|
// VisionCamera
|
|
|
|
//
|
|
|
|
// Created by Marc Rousavy on 07.06.21.
|
|
|
|
// Copyright © 2021 mrousavy. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
/**
|
|
|
|
Represents a callback to JavaScript. Syntax is the same as with Promise.
|
|
|
|
*/
|
|
|
|
class Callback {
|
2021-06-09 06:56:56 -06:00
|
|
|
private var hasCalled = false
|
|
|
|
private let callback: RCTResponseSenderBlock
|
|
|
|
|
2021-06-07 05:08:40 -06:00
|
|
|
init(_ callback: @escaping RCTResponseSenderBlock) {
|
|
|
|
self.callback = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
func reject(error: CameraError, cause: NSError?) {
|
2021-06-09 06:56:56 -06:00
|
|
|
guard !hasCalled else { return }
|
|
|
|
|
2021-06-07 05:08:40 -06:00
|
|
|
callback([NSNull(), makeReactError(error, cause: cause)])
|
2021-06-09 06:56:56 -06:00
|
|
|
hasCalled = true
|
2021-06-07 05:08:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func reject(error: CameraError) {
|
2021-06-09 06:56:56 -06:00
|
|
|
guard !hasCalled else { return }
|
|
|
|
|
2021-06-07 05:08:40 -06:00
|
|
|
reject(error: error, cause: nil)
|
2021-06-09 06:56:56 -06:00
|
|
|
hasCalled = true
|
2021-06-07 05:08:40 -06:00
|
|
|
}
|
|
|
|
|
2021-06-09 06:56:56 -06:00
|
|
|
func resolve(_ value: Any) {
|
|
|
|
guard !hasCalled else { return }
|
|
|
|
|
2021-06-07 05:08:40 -06:00
|
|
|
callback([value, NSNull()])
|
2021-06-09 06:56:56 -06:00
|
|
|
hasCalled = true
|
2021-06-07 05:08:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func resolve() {
|
2021-06-09 06:56:56 -06:00
|
|
|
guard !hasCalled else { return }
|
2021-06-07 05:08:40 -06:00
|
|
|
|
2021-06-09 06:56:56 -06:00
|
|
|
resolve(NSNull())
|
|
|
|
hasCalled = true
|
|
|
|
}
|
2021-06-07 05:08:40 -06:00
|
|
|
}
|