<commit_before>//
//  ReactiveLayer.swift
//  SwiftRebound
//
//  Created by <NAME> on 03/11/2016.
//
//

#if os(OSX)
    import Cocoa
#else
    import UIKit
#endif

///
/// Layer that provides a way to draw and layout reactively
///
open class ReactiveLayer : CALayer {
    /// Trigger for the drawReactive() call
    fileprivate var _drawTrigger: Optional<() -> ()> = nil;
    
    /// Lifetime for the trigger
    fileprivate var _drawLifetime: Lifetime? = nil;
    
    /// Trigger for the layoutReactive() call
    fileprivate var _layoutSublayersTrigger: Optional<() -> ()> = nil;
    
    /// Lifetime for the trigger
    fileprivate var _layoutSublayersLifetime: Lifetime? = nil;
    
    /// Tracks whether or not we're going to redraw
    fileprivate var _isDrawing = false;
    
    /// Queue used for synchronising drawing requests
    fileprivate let _queue = DispatchQueue(label: "io.logicalshift.ReactiveLayer");
    
    /// Context used for drawing
    fileprivate var _drawCtx: CGContext?;
    
    ///
    /// Retrieves or creates the draw trigger
    ///
    fileprivate func getDrawTrigger() -> () -> () {
        if let trigger = _drawTrigger {
            return trigger;
        } else {
            let (trigger, lifetime) = Binding.trigger({ [unowned self] in
                // The caller should run on the queue and set the draw context
                self.drawReactive(in: self._drawCtx!);
                }, causeUpdate: { [unowned self] in
                    // Perform the display request async on the queue (we won't queue while we're already drawing)
                    self._queue.async {
                        RunLoop.main.perform(#selector(self.setNeedsDisplay), target: self, argument: nil, order: 0, modes: [RunLoopMode.defaultRunLoopMode, RunLoopMode.UITrackingRunLoopMode]);
                    }
            });
            
            _drawTrigger    = trigger;
            _drawLifetime   = lifetime;
            
            return trigger;
        }
    }
    
    ///
    /// Retrieves or creates the layout trigger
    ///
    fileprivate func getLayoutTrigger() -> () -> () {
        if let trigger = _layoutSublayersTrigger {
            return trigger;
        } else {
            let (trigger, lifetime) = Binding.trigger({ [unowned self] in
                // The caller should run on the queue
                self.layoutSublayersReactive();
                }, causeUpdate: { [unowned self] in
                    // Perform the layout request async on the queue (we won't queue while we're already laying out)
                    self._queue.async {
                        RunLoop.main.perform(#selector(self.setNeedsLayout), target: self, argument: nil, order: 0, modes: [RunLoopMode.defaultRunLoopMode, RunLoopMode.UITrackingRunLoopMode]);
                    }
            });
            
            _layoutSublayersTrigger     = trigger;
            _layoutSublayersLifetime    = lifetime;
            
            return trigger;
        }
    }
    
    override open func draw(in ctx: CGContext) {
        let trigger = getDrawTrigger();
        <commit_msg>RunLoop modes are different on OS X and iOS<commit_after>//
//  ReactiveLayer.swift
//  SwiftRebound
//
//  Created by <NAME> on 03/11/2016.
//
//

#if os(OSX)
    import Cocoa
    
    fileprivate let runLoopModes = [ RunLoopMode.defaultRunLoopMode, RunLoopMode.eventTrackingRunLoopMode ];
#else
    import UIKit
    
    fileprivate let runLoopModes = [ RunLoopMode.defaultRunLoopMode, RunLoopMode.UITrackingRunLoopMode ];
#endif

///
/// Layer that provides a way to draw and layout reactively
///
open class ReactiveLayer : CALayer {
    /// Trigger for the drawReactive() call
    fileprivate var _drawTrigger: Optional<() -> ()> = nil;
    
    /// Lifetime for the trigger
    fileprivate var _drawLifetime: Lifetime? = nil;
    
    /// Trigger for the layoutReactive() call
    fileprivate var _layoutSublayersTrigger: Optional<() -> ()> = nil;
    
    /// Lifetime for the trigger
    fileprivate var _layoutSublayersLifetime: Lifetime? = nil;
    
    /// Tracks whether or not we're going to redraw
    fileprivate var _isDrawing = false;
    
    /// Queue used for synchronising drawing requests
    fileprivate let _queue = DispatchQueue(label: "io.logicalshift.ReactiveLayer");
    
    /// Context used for drawing
    fileprivate var _drawCtx: CGContext?;
    
    ///
    /// Retrieves or creates the draw trigger
    ///
    fileprivate func getDrawTrigger() -> () -> () {
        if let trigger = _drawTrigger {
            return trigger;
        } else {
            let (trigger, lifetime) = Binding.trigger({ [unowned self] in
                // The caller should run on the queue and set the draw context
                self.drawReactive(in: self._drawCtx!);
                }, causeUpdate: { [unowned self] in
                    // Perform the display request async on the queue (we won't queue while we're already drawing)
                    self._queue.async {
                        RunLoop.main.perform(#selector(self.setNeedsDisplay), target: self, argument: nil, order: 0, modes: runLoopModes);
                    }
            });
            
            _drawTrigger    = trigger;
            _drawLifetime   = lifetime;
            
            return trigger;
        }
    }
    
    ///
    /// Retrieves or creates the layout trigger
    ///
    fileprivate func getLayoutTrigger() -> () -> () {
        if let trigger = _layoutSublayersTrigger {
            return trigger;
        } else {
            let (trigger, lifetime) = Binding.trigger({ [unowned self] in
                // The caller should run on the queue
                self.layoutSublayersReactive();
                }, causeUpdate: { [unowned self] in
                    // Perform the layout request async on the queue (we won't queue while we're already laying out)
                    self._queue.async {
                        RunLoop.main.perform(#selector(self.setNeedsLayout), target: self, argument: nil, order: 0, modes: runLoopModes);
                    }
            });
            
            _layoutSublayersTrigger     = trigger;
            _layoutSublayersLifetime    = lifetime;
            
            return trigger;
        }
    }
    
    override open func draw(in ctx: CGContext) {
        let trigger = getDrawTrigger();
        
{'fraction_non_alphanumeric': 0.09305230288836847, 'fraction_numerical': 0.00312256049960968, 'mean_word_length': 1.8035010940919036, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '1990287', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1669, 'n_words': 514}