1*aa3c92eaSWill Schurman // Copyright 2015-present 650 Industries. All rights reserved.
2*aa3c92eaSWill Schurman package host.exp.exponent.kernel
3*aa3c92eaSWill Schurman 
4*aa3c92eaSWill Schurman import com.facebook.react.bridge.ReactApplicationContext
5*aa3c92eaSWill Schurman import com.facebook.react.bridge.ReadableMap
6*aa3c92eaSWill Schurman import com.facebook.react.bridge.WritableMap
7*aa3c92eaSWill Schurman import java.util.*
8*aa3c92eaSWill Schurman 
9*aa3c92eaSWill Schurman object ExponentKernelModuleProvider {
10*aa3c92eaSWill Schurman   private var factory: ExponentKernelModuleFactory = object : ExponentKernelModuleFactory {
createnull11*aa3c92eaSWill Schurman     override fun create(reactContext: ReactApplicationContext): ExponentKernelModuleInterface {
12*aa3c92eaSWill Schurman       return ExpoViewKernelModule(reactContext)
13*aa3c92eaSWill Schurman     }
14*aa3c92eaSWill Schurman   }
15*aa3c92eaSWill Schurman 
16*aa3c92eaSWill Schurman   private var instance: ExponentKernelModuleInterface? = null
17*aa3c92eaSWill Schurman 
setFactorynull18*aa3c92eaSWill Schurman   @JvmStatic fun setFactory(factory: ExponentKernelModuleFactory) {
19*aa3c92eaSWill Schurman     this.factory = factory
20*aa3c92eaSWill Schurman   }
21*aa3c92eaSWill Schurman 
newInstancenull22*aa3c92eaSWill Schurman   @JvmStatic fun newInstance(reactContext: ReactApplicationContext): ExponentKernelModuleInterface? {
23*aa3c92eaSWill Schurman     instance = factory.create(reactContext)
24*aa3c92eaSWill Schurman     return instance
25*aa3c92eaSWill Schurman   }
26*aa3c92eaSWill Schurman 
27*aa3c92eaSWill Schurman   @JvmStatic var eventQueue: Queue<KernelEvent> = LinkedList()
queueEventnull28*aa3c92eaSWill Schurman   fun queueEvent(name: String, data: WritableMap, callback: KernelEventCallback) {
29*aa3c92eaSWill Schurman     queueEvent(KernelEvent(name, data, callback))
30*aa3c92eaSWill Schurman   }
31*aa3c92eaSWill Schurman 
queueEventnull32*aa3c92eaSWill Schurman   fun queueEvent(event: KernelEvent) {
33*aa3c92eaSWill Schurman     eventQueue.add(event)
34*aa3c92eaSWill Schurman     instance?.consumeEventQueue()
35*aa3c92eaSWill Schurman   }
36*aa3c92eaSWill Schurman 
37*aa3c92eaSWill Schurman   interface KernelEventCallback {
onEventSuccessnull38*aa3c92eaSWill Schurman     fun onEventSuccess(result: ReadableMap)
39*aa3c92eaSWill Schurman     fun onEventFailure(errorMessage: String?)
40*aa3c92eaSWill Schurman   }
41*aa3c92eaSWill Schurman 
42*aa3c92eaSWill Schurman   interface ExponentKernelModuleFactory {
43*aa3c92eaSWill Schurman     fun create(reactContext: ReactApplicationContext): ExponentKernelModuleInterface
44*aa3c92eaSWill Schurman   }
45*aa3c92eaSWill Schurman 
46*aa3c92eaSWill Schurman   data class KernelEvent(val name: String, val data: WritableMap, val callback: KernelEventCallback)
47*aa3c92eaSWill Schurman }
48