airbridge-react-native-sdk
Version:
Airbridge SDK for React Native
123 lines • 5.61 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.PlacementModule = () => ({
emitter: new NativeEventEmitter(NativeModules.PlacementInteractor),
interactor: createInteractor(NativeModules.PlacementInteractor),
});
export const createPlacementModule = () => {
// create dependency
const { emitter, interactor } = createDependency.PlacementModule();
// define member
let id = 0;
// define method
const click = (trackingLink, onSuccess, onFailure) => __awaiter(void 0, void 0, void 0, function* () {
if (!check.string(trackingLink)) {
log.unmatchedType('trackingLink', 'string');
return false;
}
if (!(check.function(onSuccess) || check.undefined(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.placement', (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.click(promiseID, trackingLink, () => { onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(); }, (message) => { onFailure === null || onFailure === void 0 ? void 0 : onFailure(Error(message)); });
});
});
const impression = (trackingLink, onSuccess, onFailure) => __awaiter(void 0, void 0, void 0, function* () {
if (!check.string(trackingLink)) {
log.unmatchedType('trackingLink', 'string');
return false;
}
if (!(check.function(onSuccess) || check.undefined(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.placement', (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.impression(promiseID, trackingLink, () => { onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(); }, (message) => { onFailure === null || onFailure === void 0 ? void 0 : onFailure(Error(message)); });
});
});
const createTrackingLink = (channel, option, onSuccess, onFailure) => {
if (!check.string(channel)) {
log.unmatchedType('channel', 'string');
return;
}
if (!(check.undefined(option) || check.object(option))) {
log.unmatchedType('option', 'object');
return;
}
if (!check.function(onSuccess)) {
log.unmatchedType('onSuccess', 'function?');
return;
}
if (!(check.function(onFailure) || check.undefined(onFailure))) {
log.unmatchedType('onFailure', 'function?');
return;
}
interactor.createTrackingLink(channel, option, (airbridgeTrackingLink) => {
if (check.string(airbridgeTrackingLink['qrcodeURL']) &&
check.string(airbridgeTrackingLink['shortURL'])) {
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess({
qrcodeURL: airbridgeTrackingLink['qrcodeURL'],
shortURL: airbridgeTrackingLink['shortURL']
});
}
}, (message) => { onFailure === null || onFailure === void 0 ? void 0 : onFailure(Error(message)); });
};
// create object
return {
click,
impression,
createTrackingLink
};
};
//# sourceMappingURL=Placement.js.map