<commit_before>//
//  EventMonitor.swift
//  UberGo
//
//  Created by <NAME> on 6/4/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Cocoa
import Foundation

typealias EventMonitorHandler = (NSEvent?) -> Void

class EventMonitor {

    // MARK: - Variable
    private var monitor: Any?
    private let mask: NSEventMask
    private let handler: EventMonitorHandler

    public init(mask: NSEventMask, handler: @escaping EventMonitorHandler) {
        self.mask = mask
        self.handler = handler
    }

    deinit {
        stop()
    }

    public func start() {
        self.monitor = NSEvent.addGlobalMonitorForEvents(matching: self.mask, handler: self.handler)
    }

    public func stop() {
        if self.monitor != nil {
            NSEvent.removeMonitor(self.monitor!)
            self.monitor = nil
        }
    }
}
<commit_msg>Improved thread safe and monitor state for Event Monitor -> Prevent duplicated monitor<commit_after>//
//  EventMonitor.swift
//  UberGo
//
//  Created by <NAME> on 6/4/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Cocoa
import Foundation

typealias EventMonitorHandler = (NSEvent?) -> Void

class EventMonitor {

    // MARK: - Variable
    private var monitor: Any?
    private let mask: NSEventMask
    private let handler: EventMonitorHandler
    private var isMonitoring = false
    fileprivate let lock = NSLock()

    public init(mask: NSEventMask, handler: @escaping EventMonitorHandler) {
        self.mask = mask
        self.handler = handler
    }

    deinit {
        stop()
    }

    public func start() {

        // Lock
        lock.lock()
        defer {
            self.lock.unlock()
        }

        guard isMonitoring == false else { return }
        monitor = NSEvent.addGlobalMonitorForEvents(matching: self.mask, handler: self.handler)
        isMonitoring = true
    }

    public func stop() {

        // Lock
        lock.lock()
        defer {
            self.lock.unlock()
        }

        guard isMonitoring else { return }

        if self.monitor != nil {
            NSEvent.removeMonitor(self.monitor!)
            self.monitor = nil
        }

        isMonitoring = false
    }
}

{'fraction_non_alphanumeric': 0.09759418974126191, 'fraction_numerical': 0.007262823422605538, 'mean_word_length': 2.048409405255878, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '6623583', 'n_tokens_mistral': 667, 'n_tokens_neox': 618, 'n_words': 176}