-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
60 lines (50 loc) · 1.35 KB
/
index.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
'use strict';
import {
NativeAppEventEmitter,
DeviceEventEmitter,
NativeModules,
Platform
} from 'react-native';
let NearbyNative = NativeModules.Nearby;
class Nearby {
/**
* example config:
* const nearbyConfig: {
* message: `Hello there from user:${userId}`
* }
*/
constructor (config) {
const { message } = config;
if (typeof message === 'undefined')
throw 'Please provide a message in your config object.';
this.nearby = NearbyNative;
this.handlers = {};
this.nearbyHelperHandlers = {};
this.Emitter = Platform.OS === 'ios' ? NativeAppEventEmitter : DeviceEventEmitter;
this.deviceEventSubscription = this.Emitter.addListener(
'nearbySubscribe', this._handleEvent.bind(this)
);
this.nearby.init(config.message);
}
_handleEvent(event) {
if (this.handlers.hasOwnProperty(event.method)) {
this.handlers[event.method](
(event.hasOwnProperty('message')) ? event.message : null
);
}
if (this.nearbyHelperHandlers) this.nearbyHelperHandlers(event);
}
/**
* onFound: Nearby user found
*/
onFound(event, handler) {
this.handlers[event] = handler;
}
/**
* onHelperEvents: userful callback that come back from the Nearby Native Module
*/
onHelperEvents(handler) {
this.nearbyHelperHandlers = handler;
}
}
module.exports = Nearby;