Instantly share code, notes, and snippets.

View ConnExampleUsage.swift
GetAllUsers().execute(
onSuccess: { (users: [User]) in
// Do something with the users. They are already typed!
},
onError: { (error: Error) in
// Do something with the error.
}
)
View RequestType+execute.swift
public extension RequestType {
public func execute (
dispatcher: NetworkDispatcher = URLSessionNetworkDispatcher.instance,
onSuccess: @escaping (ResponseType) -> Void,
onError: @escaping (Error) -> Void
) {
dispatcher.dispatch(
request: self.data,
onSuccess: { (responseData: Data) in
do {
View URLSessionNetworkDispatcher.swift
public enum ConnError: Swift.Error {
case invalidURL
case noData
}
public struct URLSessionNetworkDispatcher: NetworkDispatcher {
public static let instance = URLSessionNetworkDispatcher()
private init() {}
public func dispatch(request: RequestData, onSuccess: @escaping (Data) -> Void, onError: @escaping (Error) -> Void) {
View NetworkDispatcher.swift
public protocol NetworkDispatcher {
func dispatch(request: RequestData, onSuccess: @escaping (Data) -> Void, onError: @escaping (Error) -> Void)
}
View RequestType.swift
public protocol RequestType {
associatedtype ResponseType: Codable
var data: RequestData { get }
}
View CodableUser.swift
struct User: Codable {
let id: Int
let username: String
}
View RequestData.swift
public struct RequestData {
public let path: String
public let method: HTTPMethod
public let params: [String: Any?]?
public let headers: [String: String]?
public init (
path: String,
method: HTTPMethod = .get,
params: [String: Any?]? = nil,
View Konex.swift
//
// Konex.swift
// Konex
//
// Created by Fernando Ortiz on 24/5/18.
// Copyright © 2018 Fernando Ortiz. All rights reserved.
//
import Foundation
View withLifecycleDebug.js
import React, { Component } from 'react';
const { log } = console;
export default (prefix) => (WrappedComponent) => {
const message = text => `[${prefix}] ${text}`;
return class EnhancedComponent extends WrappedComponent {
render() {
View BaseOperation.swift
// From here https://agostini.tech/2017/07/30/understanding-operation-and-operationqueue-in-swift/
class BaseOperation: Operation {
private var _executing = false {
willSet {
willChangeValue(forKey: "isExecuting")
}
didSet {
didChangeValue(forKey: "isExecuting")
}