React Native 模块 – 存储 Java 类的最佳实践

huangapple go评论65阅读模式
英文:

React Native module - Best practice to store java class

问题

我正在使用Kotlin构建一个React Native模块。我有一个外部的Java SDK,可以在多个协议/网络/服务器上发现外围设备。

有一个类似这样的Discovery类:

class Discovery(params: Params) {

  fun start() {
    // ...
  }

  fun stop() {
    // ...
  }

}

我想要将**startDiscovery()stopDiscovery()**函数传递给React Bridge。

客户端可以在多个协议/服务器上搜索许多设备,因此可能需要同时实例化许多Discovery类,并在需要时停止其中一些。类似于发现对象池的概念。

因此,我想将已实例化对象的引用传递给JavaScript,以便在每次需要调用另一个方法时可以将其传递给我。但是React Bridge不允许将Java对象传递给JavaScript。有没有什么好的模式可以实现这样的功能呢?

英文:

I'm building a React Native module with Kotlin. I have an external Java SDK that can discover peripherals on multiple protocols / networks / servers.

There is a Discovery class like this :

class Discovery(params: Params) {

  fun start() {
    // ...
  }

  fun stop() {
    // ...
  }

}

I want to pass a startDiscovery() and stopDiscovery() functions to React Bridge.

The client can search for many devices on multiple protocols / servers / ... at the same time. So it would require the instantiation of many Discovery class at the same time and stop some of them if necessary. Some sort of a Pool of discoveries.

So I would like to pass a reference to the instantiated object to Javascript so it can give back to me each time it want to call another method. But React Bridges doesn't allow to pass Java objets to JavaScript. Is there any good pattern to do so ?

答案1

得分: 1

public class DummyModule extends ReactContextBaseJavaModule {
    MyDummyClass dummy; // this context

    public DummyModule(final ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "DummyModule";
    }

    @ReactMethod
    public void startMyClass() {
        this.dummy = new MyDummyClass();
    }

    @ReactMethod
    public void fooActionClass() {
        if (this.dummy != null) {
            this.dummy.fooAction();
        }
    }
}

在您的 JavaScript 代码中:

import { NativeModules } from 'react-native';
const dummyModule = NativeModules.DummyModule;

dummyModule.startMyClass();
// 确保在类实例化时调用操作。
dummyModule.fooActionClass();
英文:

Just try to make async actions in java so the thread not get stuck and you don't lose performance(return promises if you have async actions).

public class DummyModule extends ReactContextBaseJavaModule {
MyDummyClass dummy // this context

public DummyModule(final ReactApplicationContext reactContext){
super(reactContext);
}

@Override
    // getName is required to define the name of the module represented in
    // JavaScript
    public String getName() {
        return "DummyModule";
    }
@ReactMethod
    public void startMyClass() {
       this.dummy = new MyDummyClass();
    }

@ReactMethod
    public void fooActionClass() {
       if(this.dummy != null){
           this.dummy.fooAction();
       }
    }
}

In your javascript code

import { NativeModules } from 'react-native';
const dummyModule = NativeModules.DummyModule;

dummyModule.startMyClass();
// Make sure that u call the action when the class is instanciated.
dummyModule.fooActionClass();

huangapple
  • 本文由 发表于 2020年9月8日 16:21:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63789924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定