 //
 //  URLRequester.swift
-//  Marksman 2
+//  TinyRedux
 //
 //  Created by Daniel Tartaglia on 3/23/17.
 //  Copyright © 2017 Daniel Tartaglia. All rights reserved.

public typealias URLRequesterState = Set<URLRequest>


// Implementation
public final class URLRequester<State> {

	public init(store: Store<State>, lens: @escaping (State) -> URLRequesterState) {
		self.store = store
		unsubscribe = store.subscribe { [weak self] state in
			let requests = lens(state)
			self?.configure(using: requests)
		}
	}

	private let store: Store<State>
	private var unsubscribe: Unsubscriber?
	private var current: [URLRequest: URLSessionTask] = [:]

	private func configure(using target: URLRequesterState) {

import Foundation


/// Logic
public
enum URLRequesterEvent: Event {
	case success(request: URLRequest, data: Data, response: URLResponse)
	case failure(request: URLRequest, error: Error)
	
	public
	var request: URLRequest {
		switch self {
		case .success(let (request, _, _)):
			return request
		case .failure(let (request, _)):
			return request
		}
	}
}
