-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ios.js
74 lines (59 loc) · 2.34 KB
/
index.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Copyright (c) 2018, HouseRater LLC.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule react-native-ui-native
*/
import React from "react";
import { NativeModules } from "react-native";
const BatchedBridge = require('BatchedBridge');
const RNUINativeManager = NativeModules.RNUINativeManager;
class RNUINativeHandler {
constructor(name) {
this._RNUINativeName = name;
if (this.registerEventListeners) {
this.registerEventListeners();
}
}
buildEmitter(eventName) {
return (data) => {
const fullEventName = `${this._RNUINativeName}.${eventName}`;
RNUINativeManager.emitEvent(fullEventName, JSON.stringify(data));
};
}
}
const _registeredHandlers = {};
class RNUINative {
static Handler = RNUINativeHandler;
static registerHandler(name, registerFn) {
const HandlerClass = registerFn();
const handler = new HandlerClass(name);
_registeredHandlers[name] = handler;
return { name, handler };
}
static async loadData(handleId, responseId, ...args) {
try {
// We assume native callers will always supply `Handler.functionName()` with the empty parenthesis
// (even when arguments are passed in via native code). This is a paradigm that makes it easier to run
// Project-Find operations, but is not following any specific programming convention :)
const [, handlerName, actionName ] = handleId.match(/([\w\s-]+)\.([\w\s-]+)\(\)/);
const handler = _registeredHandlers[handlerName];
if (!handler) {
throw new Error(`Unregistered handler "${handlerName}"`);
}
const actionFn = handler[actionName];
if (!actionFn) {
throw new Error(`Unregistered action "${handlerName}.${actionName}()"`);
}
const response = await actionFn.call(handler, ...args);
RNUINativeManager.loadDataComplete(JSON.stringify(response), responseId, null);
} catch (err) {
RNUINativeManager.loadDataComplete(null, responseId, err.message);
}
}
}
BatchedBridge.registerCallableModule('RNUINative', RNUINative);
export default RNUINative;