Event Mecanism based on JsbBridge

An eventify system based on JsbBridge

JsbBridgeWrapper is an eventify system based on JsbBridge, which is more convenient and easier to use. Developers can trigger multiple events without manually implementing one. But it is not mthread safe. If you’re working on a complex situation, it is still recommended to implement the eventify system by yourself.

JsbBridgeWrapper interface introduction

Same as jsbBridge, jsbBridgeWrapper is declared in jsb.d.ts. Its usage is more intuitive and easy to understand.

  1. /**
  2. * Listener for jsbBridgeWrapper's event.
  3. * It takes one argument as string which is transferred by jsbBridge.
  4. */
  5. export type OnNativeEventListener = (arg: string) => void;
  6. export namespace jsbBridgeWrapper {
  7. /** If there's no event registered, the wrapper will create one */
  8. export function addNativeEventListener(eventName: string, listener: OnNativeEventListener);
  9. /**
  10. * Dispatch the event registered on Objective-C, Java etc.
  11. * No return value in JS to tell you if it works.
  12. */
  13. export function dispatchEventToNative(eventName: string, arg?: string);
  14. /**
  15. * Remove all listeners relative.
  16. */
  17. export function removeAllListenersForEvent(eventName: string);
  18. /**
  19. * Remove the listener specified
  20. */
  21. export function removeNativeEventListener(eventName: string, listener: OnNativeEventListener);
  22. /**
  23. * Remove all events, use it carefully!
  24. */
  25. export function removeAllListeners();
  26. }

OnNativeEventListener is the type of callback to register here, yet you can also code with anonymous function, for example:

  1. // When event ‘A’ is triggered, the function ‘this.A’ will be applied
  2. jsb.jsbBridgeWrapper.addNativeEventListener("A", (usr: string) => {
  3. this.A(usr);
  4. });

Platform implementations

JsbBridgeWrapper has different implementations on different platforms.

As for Objective-C and Java, you will see JsbBridgeWrapper with a similar declaration.

  • Objective-C:

    1. // In Objective-C
    2. typedef void (^OnScriptEventListener)(NSString*);
    3. @interface JsbBridgeWrapper : NSObject
    4. /**
    5. * Get the instance of JsbBridgetWrapper
    6. */
    7. + (instancetype)sharedInstance;
    8. /**
    9. * Add a listener to specified event, if the event does not exist, the wrapper will create one. Concurrent listener will be ignored
    10. */
    11. - (void)addScriptEventListener:(NSString*)eventName listener:(OnScriptEventListener)listener;
    12. /**
    13. * Remove listener for specified event, concurrent event will be deleted. Return false only if the event does not exist
    14. */
    15. - (bool)removeScriptEventListener:(NSString*)eventName listener:(OnScriptEventListener)listener;
    16. /**
    17. * Remove all listener for event specified.
    18. */
    19. - (void)removeAllListenersForEvent:(NSString*)eventName;
    20. /**
    21. * Remove all event registered. Use it carefully!
    22. */
    23. - (void)removeAllListeners;
    24. /**
    25. * Dispatch the event with argument, the event should be registered in javascript, or other script language in future.
    26. */
    27. - (void)dispatchEventToScript:(NSString*)eventName arg:(NSString*)arg;
    28. /**
    29. * Dispatch the event which is registered in javascript, or other script language in future.
    30. */
    31. - (void)dispatchEventToScript:(NSString*)eventName;
    32. @end
  • JAVA or Huawei HarmonyOS:

    1. // In JAVA
    2. public class JsbBridgeWrapper {
    3. public interface OnScriptEventListener {
    4. void onScriptEvent(String arg);
    5. }
    6. /**
    7. * Add a listener to specified event, if the event does not exist, the wrapper will create one. Concurrent listener will be ignored
    8. */
    9. public void addScriptEventListener(String eventName, OnScriptEventListener listener);
    10. /**
    11. * Remove listener for specified event, concurrent event will be deleted. Return false only if the event does not exist
    12. */
    13. public boolean removeScriptEventListener(String eventName, OnScriptEventListener listener);
    14. /**
    15. * Remove all listener for event specified.
    16. */
    17. public void removeAllListenersForEvent(String eventName);
    18. /**
    19. * Remove all event registered. Use it carefully!
    20. */
    21. public void removeAllListeners() {
    22. this.eventMap.clear();
    23. }
    24. /**
    25. * Dispatch the event with argument, the event should be registered in javascript, or other script language in future.
    26. */
    27. public void dispatchEventToScript(String eventName, String arg);
    28. /**
    29. * Dispatch the event which is registered in javascript, or other script language in future.
    30. */
    31. public void dispatchEventToScript(String eventName);
    32. }

Basic usage

Register JS event, and using Objective-C/JAVA to trigger JavaScript event

We can still do with the simple demand: Change the label content with native callback result. once the native event is triggered, it will return the specified text to JavaScript. Before that, we should add an event listener for changeLabelContent.

Register JS event

  1. public changeLabelContent(user: string): void {
  2. console.log("Hello " + user + " I'm K");
  3. this.labelForContent!.string = "Hello " + user + " ! I'm K";
  4. }
  5. jsb.jsbBridgeWrapper.addNativeEventListener("changeLabelContent", (usr: string) => {
  6. this.changeLabelContent(usr);
  7. });

Dispatch event to JS

Once changeLabelContent event is triggered, label content will change to composition of specified strings. Before that, we should register the native event.

  • Objective-C code sample:

    1. // Objective-C
    2. JsbBridgeWrapper* m = [JsbBridgeWrapper sharedInstance];
    3. OnScriptEventListener requestLabelContent = ^void(NSString* arg){
    4. JsbBridgeWrapper* m = [JsbBridgeWrapper sharedInstance];
    5. [m dispatchEventToScript:@"changeLabelContent" arg:@"Charlotte"];
    6. };
    7. [m addScriptEventListener:@"requestLabelContent" listener:requestLabelContent];
  • JAVA code sample:

    1. // JAVA
    2. JsbBridgeWrapper jbw = JsbBridgeWrapper.getInstance();
    3. jbw.addScriptEventListener("requestLabelContent", arg ->{
    4. System.out.print("@JAVA: here is the argument transport in" + arg);
    5. jbw.dispatchEventToScript("changeLabelContent","Charlotte");
    6. });

    Note: You can also use anonymous function in JAVA too.

Dispatch event to native

The return value here is set to a const value, when JsbBridgeWrapper in Objective-C/JAVA receives the event notification of requestLabelContent, it will trigger changeLabelContent with a return value.

The final step, add a button to start our test. the code example is as follows:

  1. // Button click event for SAY HELLO
  2. public sayHelloBtn() {
  3. jsb.jsbBridgeWrapper.dispatchEventToNative("requestLabelContent");
  4. }

The effect is same as the test case of JsbBridge, click the SAY HELLO button, the label content will become a welcome message. Also, example case of JsbBridge is updated in repository.