airbridge-react-native-sdk
Version:
Airbridge SDK for React Native
100 lines • 4.54 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { NativeEventEmitter, NativeModules } from 'react-native';
import { createInteractor } from '../architecture/Interactor';
import { check } from '../utility/check';
import { log } from '../utility/log';
export const createDependency = () => { };
createDependency.FetchModule = () => ({
emitter: new NativeEventEmitter(NativeModules.FetchInteractor),
interactor: createInteractor(NativeModules.FetchInteractor),
});
export const createFetchModule = () => {
// create dependency
const { emitter, interactor } = createDependency.FetchModule();
// define member
let id = 0;
// define method
const fetchDeviceUUID = (onSuccess, onFailure) => __awaiter(void 0, void 0, void 0, function* () {
if (!check.function(onSuccess)) {
log.unmatchedType('onSuccess', 'function?');
return false;
}
if (!(check.function(onFailure) || check.undefined(onFailure))) {
log.unmatchedType('onFailure', 'function?');
return false;
}
const promiseID = `${id++}`;
return new Promise((onResolve, onReject) => {
const subscription = emitter.addListener('airbridge.fetch', (message) => {
if (message.id !== promiseID) {
return;
}
switch (message.type) {
case 'onResolve':
onResolve(message.value);
break;
case 'onReject':
onReject(Error(message.value));
break;
}
subscription.remove();
});
interactor.fetchDeviceUUID(promiseID, (deviceUUID) => { onSuccess(deviceUUID); }, (message) => { onFailure === null || onFailure === void 0 ? void 0 : onFailure(Error(message)); });
});
});
/**
* Fetch airbridgeGeneratedUUID of SDK.
* @param onSuccess Callback to be invoked when airbridgeGeneratedUUID is successfully handled.
* @param onFailure Callback to be invoked when any error occurs.
*/
const fetchAirbridgeGeneratedUUID = (onSuccess, onFailure) => __awaiter(void 0, void 0, void 0, function* () {
if (!check.function(onSuccess)) {
log.unmatchedType('onSuccess', 'function?');
return false;
}
if (!(check.function(onFailure) || check.undefined(onFailure))) {
log.unmatchedType('onFailure', 'function?');
return false;
}
const promiseID = `${id++}`;
return new Promise((onResolve, onReject) => {
const subscription = emitter.addListener('airbridge.fetch', (message) => {
if (message.id !== promiseID) {
return;
}
switch (message.type) {
case 'onResolve':
onResolve(message.value);
break;
case 'onReject':
onReject(Error(message.value));
break;
}
subscription.remove();
});
interactor.fetchAirbridgeGeneratedUUID(promiseID, (airbridgeGeneratedUUID) => { onSuccess(airbridgeGeneratedUUID); }, (message) => { onFailure === null || onFailure === void 0 ? void 0 : onFailure(Error(message)); });
});
});
const isUninstallTrackingNotification = (notification) => {
if (!check.object(notification)) {
log.unmatchedType('notification', 'object');
return false;
}
return check.defined(notification['airbridge-uninstall-tracking']);
};
// create object
return {
fetchDeviceUUID,
fetchAirbridgeGeneratedUUID,
isUninstallTrackingNotification,
};
};
//# sourceMappingURL=Fetch.js.map