Skip to content

Instantly share code, notes, and snippets.

View xaphod's full-sized avatar

Tim Carr xaphod

View GitHub Profile
@xaphod
xaphod / backup.js
Created January 25, 2022 01:55
Scheduled firestore backup
const functions = require('firebase-functions');
const firestore = require('@google-cloud/firestore');
const { storageBucket, projectId } = require('...');
const client = new firestore.v1.FirestoreAdminClient();
const firestoreBackup = functions
.runWith({ memory: '512MB', timeoutSeconds: 540 })
.pubsub
.schedule('every 24 hours')
@xaphod
xaphod / VideoWriter.swift
Created March 10, 2021 15:51
Creating a video from UIImage (or SampleBuffers from camera)
import UIKit
import AVFoundation
class VideoWriter {
fileprivate var writer: AVAssetWriter
fileprivate var writerInput: AVAssetWriterInput
fileprivate var pixelBufferAdaptor: AVAssetWriterInputPixelBufferAdaptor
fileprivate let queue: DispatchQueue
static var ciContext = CIContext.init() // we reuse a single context for performance reasons
@xaphod
xaphod / BeaconLoader.js
Created October 23, 2020 14:58
Conditionally load HelpScout Beacon v2 in a React site
import { useLayoutEffect, useState } from 'react';
import loadBeacon from './loadBeacon';
const BeaconLoader = () => {
const [didLoad, setDidLoad] = useState(false);
useLayoutEffect(() => {
if (!didLoad && myConditionForWantingTheBeaconToShow) {
setDidLoad(true);
loadBeacon({ displayName, email, helpscoutSignature }); // parameters are optional, see loadBeacon.js
@xaphod
xaphod / colors.js
Last active August 11, 2020 19:00
Darken a color in HSL space
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-multi-assign */
/*
* License: contains code from various sources, including https://github.com/gka/chroma.js/, which has Apache2 license
* Author's contributions: public domain / no attribution required.
* You're on your own to figure out if you can use this code in your project, author disclaims all liability.
*
*/
@xaphod
xaphod / ObserverProtocol.swift
Created May 14, 2020 00:49
Using associatedtype in protocols with type erasure to have two protocols restricted to the same type
import Foundation
// SINGLE VARIANT - Observer only has one thing it is watching
protocol IsObserver: class {
associatedtype DataType
func dataDidUpdate(_ data: [DataType])
}
protocol HasObserver {
const express = require('express');
const bodyParser = require('body-parser');
const { stripe, webhookSecret } = require('./index');
const router = express();
router.post('/stripe/webhook', bodyParser.raw({ type: 'application/json' }), async (req, res, next) => {
const sig = req.headers['stripe-signature'];
let event;
try {
@xaphod
xaphod / main.swift
Created November 28, 2019 15:06
Swift type checking
import UIKit
protocol Animal {}
class Cat : Animal {}
class Dog : Animal {}
class GreatDane : Dog {}
let c = Cat()
let d = Dog()
let gd = GreatDane()
@xaphod
xaphod / 1.swift
Created November 30, 2017 01:39
Approach 1: Swift 4's Decodable when you don't know the type you're decoding
import Foundation
protocol KeyedStruct {
var type_: String { get set }
}
struct Inputs : Codable {
struct KeyStruct : KeyedStruct, Codable {
var type_: String
}
@xaphod
xaphod / gist:7aab1302004f6e933593a11ad8f5a72d
Last active March 1, 2018 07:16
Scroll keyboard up just enough to show control. Swift 3.
// note that currentFirstResponder is obtained by this:
//
// static var currentFirstResponder: UIResponder?
// func findFirstResponder(sender: Any) {
// currentFirstResponder = self
// }
//
// class func currentFirstResponder() -> UIResponder? {
// currentFirstResponder = nil
// UIApplication.shared.sendAction(#selector(findFirstResponder), to: nil, from: nil, for: nil)
@xaphod
xaphod / RXTimer.h
Last active January 5, 2023 08:57 — forked from couchdeveloper/RXTimer.h
A timer based on dispatch_source_create()Objective-C
//
// RXTimer.h
//
// Copyright 2013 Andreas Grosam
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0