<commit_before>//
//  ExtensionPluggableDelegate.swift
//  ZamzamKit watchOS
//
//  Created by <NAME> on 2019-07-18.
//  Copyright © 2019 Zamzam Inc. All rights reserved.
//

#if os(watchOS)
import WatchKit

/// Subclassed by the `ExtensionDelegate` to pass lifecycle events to loaded plugins.
///
/// The application plugins will be processed in sequence.
///
///     class ExtensionDelegate: ExtensionPluggableDelegate {
///
///         override func plugins() -> [ExtensionPlugin] {
///             return [
///                 LoggerPlugin(),
///                 LocationPlugin()
///             ]
///         }
///     }
///
/// Each application module has access to the `ExtensionDelegate` lifecycle events:
///
///     final class LoggerPlugin: ExtensionPlugin {
///         private let log = Logger()
///
///         func applicationDidFinishLaunching(_ application: WKExtension) {
///             log.config(for: application)
///         }
///
///         func applicationDidBecomeActive(_ application: WKExtension) {
///             log.info("App did become active.")
///         }
///
///         func applicationWillResignActive(_ application: WKExtension) {
///             log.warn("App will resign active.")
///         }
///
///         func applicationWillEnterForeground(_ application: WKExtension) {
///             log.warn("App will enter foreground.")
///         }
///
///         func applicationDidEnterBackground(_ application: WKExtension) {
///             log.warn("App did enter background.")
///         }
///     }
open class ExtensionPluggableDelegate: NSObject, WKExtensionDelegate {
    
    /// Lazy implementation of application modules list
    public private(set) lazy var lazyPlugins: [ExtensionPlugin] = plugins()
    
    /// List of application plugins for binding to `ExtensionDelegate` events
    open func plugins() -> [ExtensionPlugin] {
        [ /* Populated from sub-class */ ]
    }
}

public extension ExtensionPluggableDelegate {
    
    func applicationDidFinishLaunching() {
        lazyPlugins.forEach { $0.applicationDidFinishLaunching(.shared()) }
    }
}

public extension ExtensionPluggableDelegate {
    
    func applicationDidBecomeActive() {
        lazyPlugins.forEach { $0.applicationDidBecomeActive(.shared()) }
    }
    
    func applicationWillResignActive() {
        lazyPlugins.forEach { $0.applicationWillResignActive(.shared()) }
    }
    
    func applicationWillEnterForeground() {
        lazyPlugins.forEach { $0.applicationWillEnterForeground(.shared()) }
    }
    
    func applicationDidEnterBackground() {
        lazyPlugins.forEach { $0.applicationDidEnterBackground(.shared()) }
    }
}

/// Conforming to an app module and added to `ExtensionDelegate.plugins()` will trigger events.
public protocol ExtensionPlugin {
    func applicationDidFinishLaunching(_ application: WKExtension)
    
    func applicationDidBecomeActive(_ application: WKExtension)
    func applicationWillResignActive(_ application: WKExtension)
    func applicationWillEnterForeground(_ application: WKExtension)
    func applicationDidEnterBackground(_ application: WKExtension)
}<commit_msg>Fix pluggable extension delegate for watchOS<commit_after>//
//  ExtensionPluggableDelegate.swift
//  ZamzamKit watchOS
//
//  Created by <NAME> on 2019-07-18.
//  Copyright © 2019 Zamzam Inc. All rights reserved.
//

#if os(watchOS)
import WatchKit

/// Subclassed by the `ExtensionDelegate` to pass lifecycle events to loaded plugins.
///
/// The application plugins will be processed in sequence after calling `application() -> [ExtensionPlugin]`.
///
///     class ExtensionDelegate: ExtensionPluggableDelegate {
///
///         override func application() -> [ExtensionPlugin] {[
///             LoggerPlugin(),
///             LocationPlugin()
///         ]}
///     }
///
/// Each application module has access to the `ExtensionDelegate` lifecycle events:
///
///     final class LoggerPlugin: ExtensionPlugin {
///         private let log = Logger()
///
///         func applicationDidFinishLaunching(_ application: WKExtension) {
///             log.config(for: application)
///         }
///
///         func applicationDidBecomeActive(_ application: WKExtension) {
///             log.info("App did become active.")
///         }
///
///         func applicationWillResignActive(_ application: WKExtension) {
///             log.warn("App will resign active.")
///         }
///
///         func applicationWillEnterForeground(_ application: WKExtension) {
///             log.warn("App will enter foreground.")
///         }
///
///         func applicationDidEnterBackground(_ application: WKExtension) {
///             log.warn("App did enter background.")
///         }
///     }
open class ExtensionPluggableDelegate: NSObject, WKExtensionDelegate {
    
    /// List of application plugins for binding to `ExtensionDelegate` events
    public private(set) lazy var plugins: [ExtensionPlugin] = { application() }()
    
    /// List of application plugins for binding to `ExtensionDelegate` events
    open func application() -> [ExtensionPlugin] {[]} // Override
}

public extension ExtensionPluggableDelegate {
    
    func applicationDidFinishLaunching() {
        plugins.forEach { $0.applicationDidFinishLaunching(.shared()) }
    }
}

public extension ExtensionPluggableDelegate {
    
    func applicationDidBecomeActive() {
        plugins.forEach { $0.applicationDidBecomeActive(.shared()) }
    }
    
    func applicationWillResignActive() {
        plugins.forEach { $0.applicationWillResignActive(.shared()) }
    }
    
    func applicationWillEnterForeground() {
        plugins.forEach { $0.applicationWillEnterForeground(.shared()) }
    }
    
    func applicationDidEnterBackground() {
        plugins.forEach { $0.applicationDidEnterBackground(.shared()) }
    }
}

/// Conforming to an app module and added to `ExtensionDelegate.application()` will trigger events.
public protocol ExtensionPlugin {
    func applicationDidFinishLaunching(_ application: WKExtension)
    
    func applicationDidBecomeActive(_ application: WKExtension)
    func applicationWillResignActive(_ application: WKExtension)
    func applicationWillEnterForeground(_ application: WKExtension)
    func applicationDidEnterBackground(_ application: WKExtension)
}
{'fraction_non_alphanumeric': 0.11609540356973622, 'fraction_numerical': 0.005370399620912968, 'mean_word_length': 2.6244991413852317, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1791311', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1600, 'n_words': 411}