Skip to content

Instantly share code, notes, and snippets.

View palpatim's full-sized avatar

Tim Schmelter palpatim

View GitHub Profile
@palpatim
palpatim / CombineRetainCycleTester.swift
Last active October 9, 2020 16:38
Combine retain cycle tester
import Foundation
import Combine
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
//PlaygroundPage.current.finishExecution()
let subject = PassthroughSubject<Int, Never>()
/// Proves that even strong references inside a cancellable's `receiveCompletion` or `receiveValue`
@palpatim
palpatim / AmplifyService.swift
Created August 18, 2020 19:57
Amplify API sample using Combine
//
// AmplifyService.swift
// AmplifyAuthSample
//
// Created by Schmelter, Tim on 8/13/20.
// Copyright © 2020 Amazon Web Services. All rights reserved.
//
import Amplify
import AmplifyPlugins
@palpatim
palpatim / ReplayOnce.swift
Created July 27, 2020 19:23
Buffering, replaying Combine publisher
import Combine
import Foundation
/// A Subscription that buffers values until the receiver demands them. Useful for
/// subscribing to Subjects that produce values irrespective of whether there is a
/// downstream subscriber (e.g., a PassthroughSubject used by an imperative process
/// to deliver a stream of events. A BufferingSubscription can be used to request
/// unlimited demand from the upstream publisher, and buffer the values until the
/// downstream subscriber demands them.
@available(iOS 13.0, *)
@palpatim
palpatim / CombineMergeSample.swift
Last active February 12, 2020 16:59
Combine Framework merge behavior
// Copy this into an Xcode Playground
import Foundation
import Combine
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let odds = PassthroughSubject<Int, Never>()
let evens = PassthroughSubject<Int, Never>()
import Foundation
protocol TestClassFactory {
static func make() -> TestClass
}
protocol TestClass {
var testValue: String { get set }
}
class Person {
name: string;
friends: Array<Person>;
constructor(name: string) {
this.name = name;
this.friends = [];
}
addFriends(friends: Array<Person>) {
// @flow
function doAsyncWork(shouldSucceed: boolean): Promise<number> {
let p = new Promise(function asyncWorker(resolve, reject) {
setTimeout(function() {
if (shouldSucceed) {
resolve(42);
} else {
let error = new Error("rejected");
reject(error);
/* @flow */
type EnumName = string;
type EnumProperties = {[key: any]: any};
/// An abstract class that defines semantics for typesafe `enum` style implementations in JavaScript.
/// Relies on ES6 extensions to support static properties, and is therefore not suitable for all
/// projects.
///
/// Usage:
import Foundation
class Node<T: Comparable> {
let value: T
var left: Node?
var right: Node?
init(value: T, left: Node? = nil, right: Node? = nil) {
self.value = value
self.left = left
#!/usr/bin/env ruby
class AppShell
require "logger"
require "optparse"
@@BANNER = "A shell with app boilerplate & option parsing, suitable for subclassing"
@@VERSION = [0, 0, 1]
LOG_LEVELS = [ Logger::WARN, Logger::INFO, Logger::DEBUG ]