<commit_before>//
//  Unsubscribe.swift
//  CuspExample
//
//  Created by keyOfVv on 2/17/16.
//  Copyright © 2016 com.keyang. All rights reserved.
//

import Foundation

// MARK: UnsubscribeRequest

/// request of unsubscribe value update of specific characteristic
internal class UnsubscribeRequest: PeripheralOperationRequest {

	// MARK: Stored Properties

	/// a CBCharacteristic object of which the value update to be unsubscribed
	internal var characteristic: Characteristic!

	// MARK: Initializer

	fileprivate override init() {
		super.init()
	}

	/**
	Convenient initializer

	- parameter characteristic: a CBCharacteristic object of which the value update to be unsubscribed
	- parameter success:        a closure called when unsubscription succeed
	- parameter failure:        a closure called when unsubscription failed

	- returns: a UnsubscribeRequest instance
	*/
	internal convenience init(characteristic: Characteristic, success: ((Response?) -> Void)?, failure: ((CuspError?) -> Void)?) {
		self.init()
        self.characteristic = characteristic
        self.success        = success
        self.failure        = failure
	}

	override internal var hash: Int {
		return characteristic.uuid.uuidString.hashValue
	}

	override internal func isEqual(_ object: Any?) -> Bool {
		if let other = object as? UnsubscribeRequest {
			return self.hashValue == other.hashValue
		}
		return false
	}
}

// MARK: Communicate
extension Peripheral {

	/**
	Unsubscribe value update of specific characteristic on specific peripheral
	取消订阅指定从设备的指定特征的数值变化

	- parameter characteristic: a CBCharacteristic object of which the value update to be unsubscribed. 待取消订阅数值更新的特征.
	- parameter success:        a closure called when unsubscription succeed. 取消订阅成功时执行的闭包.
	- parameter failure:        a closure called when unsubscription failed. 取消订阅失败时执行的闭包.
	*/
	public func unsubscribe(_ characteristic: Characteristic, success: ((Response?) -> Void)?, failure: ((CuspError?) -> Void)?) {
		// 0. check if ble is available
		if let error = Cusp.central.assertAvailability() {
			failure?(error)
			return
		}
		// 1. create req
		let req = UnsubscribeRequest(characteristic: characteristic, success: success, failure: failure)
		// 2. add req
		self.requestQ.async(execute: { () -> Void in
			self.unsubscribeRequests.insert(req)
		})
		// 3. unsubscribe characteristic
		self.operationQ.async(execute: { () -> Void in
			self.core.setNotifyValue(false, for: characteristic)
		})
		// 4. set time out closure
		self.operationQ.asyncAfter(deadline: DispatchTime.now() + Double(req.timeoutPeriod)) { () -> Void in
			if req.timedOut {
				DispatchQueue.main.async(execute: { () -> Void in
					failure?(CuspError.timedOut)
				})
				// since req timed out, don't need it any more
				self.requestQ.async(execute: { () -> Void in
					self.unsubscribeRequests.remove(req)
				})
			}
		}
	}
}





<commit_msg>added new unsubscribe method<commit_after>//
//  Unsubscribe.swift
//  CuspExample
//
//  Created by keyOfVv on 2/17/16.
//  Copyright © 2016 com.keyang. All rights reserved.
//

import Foundation

// MARK: UnsubscribeRequest

/// request of unsubscribe value update of specific characteristic
internal class UnsubscribeRequest: PeripheralOperationRequest {

	// MARK: Stored Properties

	/// a CBCharacteristic object of which the value update to be unsubscribed
	internal var characteristic: Characteristic!

	// MARK: Initializer

	fileprivate override init() {
		super.init()
	}

	/**
	Convenient initializer

	- parameter characteristic: a CBCharacteristic object of which the value update to be unsubscribed
	- parameter success:        a closure called when unsubscription succeed
	- parameter failure:        a closure called when unsubscription failed

	- returns: a UnsubscribeRequest instance
	*/
	internal convenience init(characteristic: Characteristic, success: ((Response?) -> Void)?, failure: ((CuspError?) -> Void)?) {
		self.init()
        self.characteristic = characteristic
        self.success        = success
        self.failure        = failure
	}

	override internal var hash: Int {
		return characteristic.uuid.uuidString.hashValue
	}

	override internal func isEqual(_ object: Any?) -> Bool {
		if let other = object as? UnsubscribeRequest {
			return self.hashValue == other.hashValue
		}
		return false
	}
}

// MARK: Communicate
extension Peripheral {

	/**
	Unsubscribe value update of specific characteristic on specific peripheral
	取消订阅指定从设备的指定特征的数值变化

	- parameter characteristic: a CBCharacteristic object of which the value update to be unsubscribed. 待取消订阅数值更新的特征.
	- parameter success:        a closure called when unsubscription succeed. 取消订阅成功时执行的闭包.
	- parameter failure:        a closure called when unsubscription failed. 取消订阅失败时执行的闭包.
	*/
	public func unsubscribe(_ characteristic: Characteristic, success: ((Response?) -> Void)?, failure: ((CuspError?) -> Void)?) {
		// 0. check if ble is available
		if let error = Cusp.central.assertAvailability() {
			failure?(error)
			return
		}
		// 1. create req
		let req = UnsubscribeRequest(characteristic: characteristic, success: success, failure: failure)
		// 2. add req
		self.requestQ.async(execute: { () -> Void in
			self.unsubscribeRequests.insert(req)
		})
		// 3. unsubscribe characteristic
		self.operationQ.async(execute: { () -> Void in
			self.core.setNotifyValue(false, for: characteristic)
		})
		// 4. set time out closure
		self.operationQ.asyncAfter(deadline: DispatchTime.now() + Double(req.timeoutPeriod)) { () -> Void in
			if req.timedOut {
				DispatchQueue.main.async(execute: { () -> Void in
					failure?(CuspError.timedOut)
				})
				// since req timed out, don't need it any more
				self.requestQ.async(execute: { () -> Void in
					self.unsubscribeRequests.remove(req)
				})
			}
		}
	}

	public func unsubscribe(characteristic c: String, ofService s: String, success: ((Response?) -> Void)?, failure: ((CuspError?) -> Void)?) {
		if let service = self[s] {
			if let char = service[c] {
				unsubscribe(char, success: success, failure: failure)
			} else {
				discoverCharacteristics(UUIDStrings: [c], ofService: service, success: { (resp) in
					if let char = service[c] {
						self.unsubscribe(char, success: success, failure: failure)
					} else {
						failure?(CuspError.characteristicNotFound)
					}
				}, failure: { (err) in
					failure?(err)
				})
			}
		} else {
			discoverServices(UUIDStrings: [s], success: { (resp) in
				if let service = self[s] {
					self.discoverCharacteristics(UUIDStrings: [c], ofService: service, success: { (resp) in
						if let char = service[c] {
							self.unsubscribe(char, success: success, failure: failure)
						} else {
							failure?(CuspError.characteristicNotFound)
						}
					}, failure: { (err) in
						failure?(err)
					})
				} else {
					failure?(CuspError.serviceNotFound)
				}
			}, failure: { (err) in
				failure?(err)
			})
		}
	}
}






{'fraction_non_alphanumeric': 0.10583153347732181, 'fraction_numerical': 0.004031677465802736, 'mean_word_length': 3.3196517412935322, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '3687557', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2093, 'n_words': 605}