-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathKeyboardsRegistry.js
72 lines (59 loc) · 2.34 KB
/
KeyboardsRegistry.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
import {AppRegistry} from 'react-native';
import EventEmitterManager from './utils/EventEmitterManager';
import {intersection} from './utils/utils';
/*
* Tech debt: how to deal with multiple registries in the app?
*/
const getKeyboardsWithIDs = (keyboardIDs) => {
return keyboardIDs.map((keyboardId) => {
return {
id: keyboardId,
...KeyboardRegistry.registeredKeyboards[keyboardId].params,
};
});
};
export default class KeyboardRegistry {
static registeredKeyboards = {};
static eventEmitter = new EventEmitterManager();
static registerKeyboard = (componentID, generator, params = {}) => {
if (typeof generator !== 'function') {
console.error(`KeyboardRegistry.registerKeyboard: ${componentID} you must register a generator function`);//eslint-disable-line
return;
}
KeyboardRegistry.registeredKeyboards[componentID] = {generator, params, componentID};
AppRegistry.registerComponent(componentID, generator);
};
static getKeyboard = (componentID) => {
const res = KeyboardRegistry.registeredKeyboards[componentID];
if (!res || !res.generator) {
console.error(`KeyboardRegistry.getKeyboard: ${componentID} used but not yet registered`);//eslint-disable-line
return undefined;
}
return res.generator();
};
static getKeyboards = (componentIDs = []) => {
const validKeyboardIDs = intersection(componentIDs, Object.keys(KeyboardRegistry.registeredKeyboards));
return getKeyboardsWithIDs(validKeyboardIDs);
};
static getAllKeyboards = () => {
return getKeyboardsWithIDs(Object.keys(KeyboardRegistry.registeredKeyboards));
};
static addListener = (globalID, callback) => {
KeyboardRegistry.eventEmitter.listenOn(globalID, callback);
};
static notifyListeners = (globalID, args) => {
KeyboardRegistry.eventEmitter.emitEvent(globalID, args);
};
static removeListeners = (globalID) => {
KeyboardRegistry.eventEmitter.removeListeners(globalID);
};
static onItemSelected = (globalID, args) => {
KeyboardRegistry.notifyListeners(`${globalID}.onItemSelected`, args);
};
static requestShowKeyboard = (globalID) => {
KeyboardRegistry.notifyListeners('onRequestShowKeyboard', {keyboardId: globalID});
};
static toggleExpandedKeyboard = (globalID) => {
KeyboardRegistry.notifyListeners('onToggleExpandedKeyboard', {keyboardId: globalID});
};
}