-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
835cd86
commit ff3ae15
Showing
5 changed files
with
119 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 62 additions & 11 deletions
73
android/src/main/java/com/wix/reactnativenotifications/core/JsIOHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,89 @@ | ||
package com.wix.reactnativenotifications.core; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
|
||
import com.facebook.react.ReactApplication; | ||
import com.facebook.react.ReactInstanceManager; | ||
import com.facebook.react.ReactNativeHost; | ||
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 java.util.LinkedList; | ||
|
||
public class JsIOHelper { | ||
|
||
private static final LinkedList<Event> sBackgroundQueue = new LinkedList<Event>(); | ||
private static boolean sJsIsReady = false; | ||
|
||
private final Context mContext; | ||
private final Handler mHandler = new Handler(Looper.getMainLooper()); | ||
|
||
public JsIOHelper(Context context) { | ||
mContext = context; | ||
} | ||
|
||
public boolean sendEventToJS(String eventName, Bundle bundle) { | ||
return sendEventToJS(eventName, Arguments.fromBundle(bundle)); | ||
public void sendEventToJS(String name, Bundle bundle) { | ||
final Event event = new Event(name, Arguments.fromBundle(bundle)); | ||
postEvent(event); | ||
} | ||
|
||
public boolean sendEventToJS(String eventName, String string) { | ||
return sendEventToJS(eventName, (Object) string); | ||
public void sendEventToJS(String name, String string) { | ||
final Event event = new Event(name, string); | ||
postEvent(event); | ||
} | ||
|
||
boolean sendEventToJS(String eventName, Object object) { | ||
final ReactContext reactContext = ((ReactApplication) mContext.getApplicationContext()).getReactNativeHost().getReactInstanceManager().getCurrentReactContext(); | ||
public void consumeBackgroundQueue() { | ||
synchronized (JsIOHelper.class) { | ||
sJsIsReady = true; | ||
|
||
if (reactContext != null && reactContext.hasActiveCatalystInstance()) { | ||
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, object); | ||
return true; | ||
while (!sBackgroundQueue.isEmpty()) { | ||
emitEvent(sBackgroundQueue.pop()); | ||
} | ||
} | ||
} | ||
|
||
private void postEvent(final Event event) { | ||
if (Looper.getMainLooper() == Looper.myLooper()) { | ||
emitEvent(event); | ||
} else { | ||
mHandler.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
emitEvent(event); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
private void emitEvent(Event event) { | ||
final ReactInstanceManager reactInstanceManager = ((ReactApplication) mContext.getApplicationContext()).getReactNativeHost().getReactInstanceManager(); | ||
final ReactContext reactContext = reactInstanceManager.getCurrentReactContext(); | ||
|
||
return false; | ||
synchronized (JsIOHelper.class) { | ||
if (sJsIsReady && reactContext != null && reactContext.hasActiveCatalystInstance()) { | ||
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(event.name, event.object); | ||
} else { | ||
sBackgroundQueue.push(event); | ||
|
||
if (reactContext == null && !reactInstanceManager.hasStartedCreatingInitialContext()) { | ||
reactInstanceManager.createReactContextInBackground(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static final class Event { | ||
final String name; | ||
final Object object; | ||
|
||
Event(String name, Object object) { | ||
this.name = name; | ||
this.object = object; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters