<commit_before>
'use strict';

import { NativeModules, NativeEventEmitter, NetInfo, Platform } from 'react-native';
import invariant from 'invariant';

const RNOneSignal = NativeModules.OneSignal;

const eventBroadcastNames = [
    'OneSignal-remoteNotificationReceived',
    'OneSignal-remoteNotificationOpened',
    'OneSignal-idsAvailable',
    'OneSignal-emailSubscription'
];

var oneSignalEventEmitter;

var _eventNames = [ "received", "opened", "ids", "emailSubscription"];

var _notificationHandler = new Map();
var _notificationCache = new Map();
var _listeners = [];

if (RNOneSignal != null) {
    oneSignalEventEmitter = new NativeEventEmitter(RNOneSignal);

    for(var i = 0; i < eventBroadcastNames.length; i++) {
        var eventBroadcastName = eventBroadcastNames[i];
        var eventName = _eventNames[i];

        _listeners[eventName] = handleEventBroadcast(eventName, eventBroadcastName)
    }
}

function handleEventBroadcast(type, broadcast) {
    return oneSignalEventEmitter.addListener(
        broadcast, (notification) => {
            // Check if we have added listener for this type yet
            // Cache the result first if we have not.
            var handler = _notificationHandler.get(type);

            if (handler) {
                handler(notification);
            } else {
                _notificationCache.set(type, notification);
            }
        }
    );
}

function checkIfInitialized() {
    return RNOneSignal != null;
}

export default class OneSignal {
    static addEventListener(type, handler) {
        if (!checkIfInitialized()) return;

        // Listen to events of notification received, opened, device registered and IDSAvailable.

        invariant(
            type === 'received' || type === 'opened' || type === 'ids' || type == 'emailSubscription',
            'OneSignal only supports `received`, `opened`, and `ids` events'
        );

        _notificationHandler.set(type, handler);

        if (type == 'opened') {
            RNOneSignal.didSetNotificationOpenedHandler();
        }

        // Check if there is a cache for this type of event
        var cache = _notificationCache.get(type);
        if (handler && cache) {
            handler(cache);
            _notificationCache.delete(type);
        }
    }

    static removeEventListener(type, handler) {
        if (!checkIfInitialized()) return;

        invariant(
            type === 'received' || type === 'opened' || type === 'ids' || type == 'emailSubscription',
            'OneSignal only supports `received`, `opened`, and `ids` events'
        );

        _notificationHandler.delete(type);
    }

    static clearListeners() {
        if (!checkIfInitialized()) return;

        for(var i = 0; i < _eventNames.length; i++) {
            _listeners[_eventNames].remove();
        }
    }

    static registerForPushNotifications() {
        if (!checkIfInitialized()) return;

        if (Platform.OS === 'ios') {
            RNOneSignal.registerForPushNotifications();
        } else {
            console.log("This function is not supported on Android");
        }
    }

    static promptForPushNotificationsWithUserResponse(callback) {
        if (!checkIfInitialized()) return;

        if (Platform.OS === 'ios') {
            invariant(
                typeof callback === 'function',
                'Must provide a valid callback'
            );
            RNOneSignal.promptForPushNotificationsWithUserResponse(callback);
        } else {
            console.log("This function is not supported on Android");
        }
    }

    static requestPermissions(permissions) {
        if (!checkIfInitialized()) return;

        var requestedPermissions = {};
        if (Platform.OS === 'ios') {
            if (permissions) {
                requestedPermissions = {
                    alert: !!permissions.alert,
                    badge: !!permissions.badge,
                    sound: !!permissions.sound
                };
            } else {
                requestedPermissions = {
                    alert: true,
                    badge: true,
                    sound: true
                };
            }
            RNOneSignal.requestPermissions(requestedPermissions);
        } else {
            console.log("This function is not supported on Android");
        }
    }

    static configure() {
        if (!checkIfInitialized()) return;

        RNOneSignal.configure();
    }

    static init(appId, iOSSettings) {
        if (!checkIfInitialized()) return;

        if (Platform.OS == 'ios') {
            RNOneSignal.initWithAppId(appId, iOSSettings);
        } else {
            RNOneSignal.init(appId);
        }
    }

    static checkPermissions(callback) {
        if (!checkIfInitialized()) return;

        if (Platform.OS === 'ios') {
            invariant(
                typeof callback === 'function',
                'Must provide a valid callback'
            );
            RNOneSignal.checkPermissions(callback);
        } else {
            console.log("This function is not supported on Android");
        }<commit_msg>Deprecated the configure method  (#765)<commit_after>
'use strict';

import { NativeModules, NativeEventEmitter, NetInfo, Platform } from 'react-native';
import invariant from 'invariant';

const RNOneSignal = NativeModules.OneSignal;

const eventBroadcastNames = [
    'OneSignal-remoteNotificationReceived',
    'OneSignal-remoteNotificationOpened',
    'OneSignal-idsAvailable',
    'OneSignal-emailSubscription'
];

var oneSignalEventEmitter;

var _eventNames = [ "received", "opened", "ids", "emailSubscription"];

var _eventTypeHandler = new Map();
var _notificationCache = new Map();
var _listeners = [];

if (RNOneSignal != null) {
    oneSignalEventEmitter = new NativeEventEmitter(RNOneSignal);

    for(var i = 0; i < eventBroadcastNames.length; i++) {
        var eventBroadcastName = eventBroadcastNames[i];
        var eventName = _eventNames[i];

        _listeners[eventName] = handleEventBroadcast(eventName, eventBroadcastName)
    }
}

function handleEventBroadcast(type, broadcast) {
    return oneSignalEventEmitter.addListener(
        broadcast, (notification) => {
            // Check if we have added listener for this type yet
            // Cache the result first if we have not.
            var handler = _eventTypeHandler.get(type);

            if (handler) {
                handler(notification);
            } else {
                _notificationCache.set(type, notification);
            }
        }
    );
}

function checkIfInitialized() {
    return RNOneSignal != null;
}

export default class OneSignal {
    static addEventListener(type, handler) {
        if (!checkIfInitialized()) return;

        // Listen to events of notification received, opened, device registered and IDSAvailable.

        invariant(
            type === 'received' || type === 'opened' || type === 'ids' || type == 'emailSubscription',
            'OneSignal only supports `received`, `opened`, and `ids` events'
        );

        _eventTypeHandler.set(type, handler);

        if (type == 'opened') {
            RNOneSignal.didSetNotificationOpenedHandler();
        }

        // triggers ids event
        if (type == 'ids') {
            RNOneSignal.configure();
        }

        // Check if there is a cache for this type of event
        var cache = _notificationCache.get(type);
        if (handler && cache) {
            handler(cache);
            _notificationCache.delete(type);
        }
    }

    static removeEventListener(type, handler) {
        if (!checkIfInitialized()) return;

        invariant(
            type === 'received' || type === 'opened' || type === 'ids' || type == 'emailSubscription',
            'OneSignal only supports `received`, `opened`, and `ids` events'
        );

        _eventTypeHandler.delete(type);
    }

    static clearListeners() {
        if (!checkIfInitialized()) return;

        for(var i = 0; i < _eventNames.length; i++) {
            _listeners[_eventNames].remove();
        }
    }

    static registerForPushNotifications() {
        if (!checkIfInitialized()) return;

        if (Platform.OS === 'ios') {
            RNOneSignal.registerForPushNotifications();
        } else {
            console.log("This function is not supported on Android");
        }
    }

    static promptForPushNotificationsWithUserResponse(callback) {
        if (!checkIfInitialized()) return;

        if (Platform.OS === 'ios') {
            invariant(
                typeof callback === 'function',
                'Must provide a valid callback'
            );
            RNOneSignal.promptForPushNotificationsWithUserResponse(callback);
        } else {
            console.log("This function is not supported on Android");
        }
    }

    static requestPermissions(permissions) {
        if (!checkIfInitialized()) return;

        var requestedPermissions = {};
        if (Platform.OS === 'ios') {
            if (permissions) {
                requestedPermissions = {
                    alert: !!permissions.alert,
                    badge: !!permissions.badge,
                    sound: !!permissions.sound
                };
            } else {
                requestedPermissions = {
                    alert: true,
                    badge: true,
                    sound: true
                };
            }
            RNOneSignal.requestPermissions(requestedPermissions);
        } else {
            console.log("This function is not supported on Android");
        }
    }

    /* deprecated */
    static configure() {
        console.warn("OneSignal: the `configure` method has been deprecated. The `ids` event is now triggered automatically.");
    }

    static init(appId, iOSSettings) {
        if (!checkIfInitialized()) return;

        if (Platform.OS == 'ios') {
            RNOneSignal.initWithAppId(appId, iOSSettings);
        } else {
            RNOneSignal.init(appId);
        }
    }

    static checkPermissions(callback) {
        if (!checkIfInitialized()) return;

        if (Platform.OS === 'ios') {
            invariant(
                typeof callback === 'function',
                'Must provide a valid callback'
            );
            RNOneSignal.checkPermissions(callback);
        } else {
            console.log("This function is not supported on Android");
        }
{'fraction_non_alphanumeric': 0.10865191146881288, 'fraction_numerical': 0.0006706908115358819, 'mean_word_length': 2.0817832890463537, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2413690', 'n_tokens_mistral': 2711, 'n_tokens_neox': 2618, 'n_words': 670}