package com.xcodetemplateproject;

import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import javax.annotation.Nullable;

/**
 * 原生发送事件给JS
 */
public class JSEvents {
    private volatile static JSEvents instance;

    private JSEvents() {
    }

    public static JSEvents getInstance() {
        if (instance == null) {
            synchronized (JSEvents.class) {
                if (instance == null) {
                    instance = new JSEvents();
                }
            }
        }
        return instance;
    }


    private void sendEvent(ReactContext mReactContext, String eventName, @Nullable WritableMap params) {
        mReactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
    }

    public void sendMsg(ReactContext mReactContext) {
        //发送事件给JS
        WritableMap wm = Arguments.createMap();
        wm.putString("key", "this is a writeable message");
        sendEvent(mReactContext, "MessageEvent", wm);
    }
}
