<commit_before>declare var NSMakeRange; // not recognized by platform-declarations

import { CLog, CLogTypes } from '../bluetooth.common';
import { Bluetooth, CBUUIDToString, toArrayBuffer } from '../bluetooth.ios';
import { CBPeripheralDelegateImpl } from './CBPeripheralDelegateImpl';

/**
 * @link - https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerdelegate
 * The CBCentralManagerDelegate protocol defines the methods that a delegate of a CBCentralManager object must adopt.
 * The optional methods of the protocol allow the delegate to monitor the discovery, connectivity, and retrieval of peripheral devices.
 * The only required method of the protocol indicates the availability of the central manager, and is called when the central manager’s state is updated.
 */
export class CBCentralManagerDelegateImpl extends NSObject implements CBCentralManagerDelegate {
    static ObjCProtocols = [CBCentralManagerDelegate];

    private _owner: WeakRef<Bluetooth>;

    private _callback: (result?) => void;

    static new(): CBCentralManagerDelegateImpl {
        return super.new() as CBCentralManagerDelegateImpl;
    }

    public initWithCallback(owner: WeakRef<Bluetooth>, callback: (result?) => void): CBCentralManagerDelegateImpl {
        this._owner = owner;
        CLog(CLogTypes.info, `CBCentralManagerDelegateImpl.initWithCallback ---- this._owner: ${this._owner}`);
        this._callback = callback;
        return this;
    }

    /**
     * Invoked when a connection is successfully created with a peripheral.
     * This method is invoked when a call to connect(_:options:) is successful.
     * You typically implement this method to set the peripheral’s delegate and to discover its services.
     * @param central [CBCentralManager] - The central manager providing this information.
     * @param peripheral [CBPeripheral] - The peripheral that has been connected to the system.
     */
    public centralManagerDidConnectPeripheral(central: CBCentralManager, peripheral: CBPeripheral) {
        CLog(CLogTypes.info, `----- CBCentralManagerDelegateImpl centralManager:didConnectPeripheral: ${peripheral}`);
        const UUID = peripheral.identifier.UUIDString;
        // find the peri in the array and attach the delegate to that
        const peri = this._owner.get().findPeripheral(UUID);
        CLog(CLogTypes.info, `----- CBCentralManagerDelegateImpl centralManager:didConnectPeripheral: cached perio: ${peri}`);

        const cb = this._owner.get()._connectCallbacks[UUID];
        delete this._owner.get()._connectCallbacks[UUID];
        const delegate = CBPeripheralDelegateImpl.new().initWithCallback(this._owner, cb);
        CFRetain(delegate);
        peri.delegate = delegate;

        CLog(CLogTypes.info, "----- CBCentralManagerDelegateImpl centralManager:didConnectPeripheral, let's discover service");
        this._owner.get().onPeripheralConnected(peripheral);
        peri.discoverServices(null);
    }
<commit_msg>cleanup<commit_after>declare var NSMakeRange; // not recognized by platform-declarations

import { CLog, CLogTypes } from '../bluetooth.common';
import { Bluetooth, CBUUIDToString, toArrayBuffer } from '../bluetooth.ios';
import { CBPeripheralDelegateImpl } from './CBPeripheralDelegateImpl';

/**
 * @link - https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerdelegate
 * The CBCentralManagerDelegate protocol defines the methods that a delegate of a CBCentralManager object must adopt.
 * The optional methods of the protocol allow the delegate to monitor the discovery, connectivity, and retrieval of peripheral devices.
 * The only required method of the protocol indicates the availability of the central manager, and is called when the central manager’s state is updated.
 */
export class CBCentralManagerDelegateImpl extends NSObject implements CBCentralManagerDelegate {
    static ObjCProtocols = [CBCentralManagerDelegate];

    private _owner: WeakRef<Bluetooth>;

    private _callback: (result?) => void;

    static new(): CBCentralManagerDelegateImpl {
        return super.new() as CBCentralManagerDelegateImpl;
    }

    public initWithCallback(owner: WeakRef<Bluetooth>, callback: (result?) => void): CBCentralManagerDelegateImpl {
        this._owner = owner;
        CLog(CLogTypes.info, `CBCentralManagerDelegateImpl.initWithCallback ---- this._owner: ${this._owner}`);
        this._callback = callback;
        return this;
    }

    /**
     * Invoked when a connection is successfully created with a peripheral.
     * This method is invoked when a call to connect(_:options:) is successful.
     * You typically implement this method to set the peripheral’s delegate and to discover its services.
     * @param central [CBCentralManager] - The central manager providing this information.
     * @param peripheral [CBPeripheral] - The peripheral that has been connected to the system.
     */
    public centralManagerDidConnectPeripheral(central: CBCentralManager, peripheral: CBPeripheral) {
        CLog(CLogTypes.info, `----- CBCentralManagerDelegateImpl centralManager:didConnectPeripheral: ${peripheral}`);
        const UUID = peripheral.identifier.UUIDString;
        // find the peri in the array and attach the delegate to that
        // const peri = this._owner.get().findPeripheral(UUID);
        // CLog(CLogTypes.info, `----- CBCentralManagerDelegateImpl centralManager:didConnectPeripheral: cached perio: ${peripheral}`);

        const cb = this._owner.get()._connectCallbacks[UUID];
        delete this._owner.get()._connectCallbacks[UUID];
        const delegate = CBPeripheralDelegateImpl.new().initWithCallback(this._owner, cb);
        // CFRetain(delegate);
        peripheral.delegate = delegate;

        CLog(CLogTypes.info, "----- CBCentralManagerDelegateImpl centralManager:didConnectPeripheral, let's discover service");
        this._owner.get().onPeripheralConnected(peripheral);
        peripheral.discoverServices(null);
    }

{'fraction_non_alphanumeric': 0.09144001334890706, 'fraction_numerical': 0.0, 'mean_word_length': 4.028523489932886, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '3210959', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1497, 'n_words': 466}