Skip to content

Instantly share code, notes, and snippets.

View groue's full-sized avatar

Gwendal Roué groue

View GitHub Profile
@groue
groue / Database+withDeferredUniqueIndexes.swift
Last active August 23, 2022 14:43
Deferred unique indexes with GRDB
import GRDB
extension Database {
/// Executes the provided closure with deferred unique indexes
func withDeferredUniqueIndexes(_ execute: () throws -> Void) throws {
try inSavepoint {
// Fetch all unique indexes, but primary keys.
let uniqueConstraints: [(table: String, index: IndexInfo)] = try String
.fetchAll(self, sql: "SELECT name FROM sqlite_master WHERE type = 'table'")
.filter { !Database.isGRDBInternalTable($0) }
@groue
groue / ContainerView.swift
Last active April 18, 2022 17:29
Exploring @‍Query initializers
import SwiftUI
/// A Container view which uses various techniques
/// for controlling RequestView.
struct ContainerView: View {
@State var parameter = 0
var body: some View {
List {
Section {
@groue
groue / ScopedFunctions.md
Last active March 13, 2021 15:16
Pitch: Scoped Functions

Pitch: Scoped Functions

Introduction

Scoped Functions are functions that enhance the leading dot syntax inside their body, so that it gives short-hand access to the members of a specific value. Such functions exist so that API designers and their consumers can collaborate towards a way to write focused code where an agreed implicit and obvious context does not have to be spelled out.

Motivation

This pitch considers that developers often want to use short-hand identifier resolution in particular contexts, and api designers often want to make it possible.

@groue
groue / SinglePublisher.swift
Last active October 8, 2021 13:34
SinglePublisher
import Combine
import Foundation
// MARK: - SinglePublisher
/// The protocol for "single publishers", which publish exactly one element, or
/// an error.
///
/// `Just`, `Future` and `URLSession.DataTaskPublisher` are examples of
/// publishers that conform to `SinglePublisher`.
extension Collection {
/// Returns an array of subsequences of maximum size `chunkSize`.
///
/// For example:
///
/// // ["ABCD", "EFGH", "IJ"]
/// "ABCDEFGHIJ".split(chunkSize: 4)
///
/// - parameter chunkSize: the maximum size of the returned subsequences.
/// - precondition: chunkSize > 0
@groue
groue / Combine+Weak.swift
Created June 15, 2020 08:09
Combine assignWeakly
import Combine
extension Publisher where Failure == Never {
/// Same as assign(to:on:), but root object is not retained.
func assignWeakly<Root: AnyObject>(
to keyPath: ReferenceWritableKeyPath<Root, Self.Output>,
on object: Root)
-> AnyCancellable
{
var cancellable: AnyCancellable?
@groue
groue / GRDB+DeleteAllOther.swift
Last active June 15, 2020 15:14
GRDB request.deleteAllOther(db)
import GRDB
extension QueryInterfaceRequest where RowDecoder: MutablePersistableRecord {
/// Deletes all records in the database, but the ones selected by the request.
///
/// For example:
///
/// // Delete all players whose score is below 1000
/// let bestPlayers = Player.filter(Column("score") > 1000)
/// try dbQueue.write(bestPlayers.deleteAllOther)
@groue
groue / AsynchronousOperation.swift
Last active October 30, 2020 13:11
AsynchronousOperation
import Foundation
/// To create an operation:
///
/// 1. Subclass AsynchronousOperation, override main, and eventually cancel the
/// operation, or set result to a non-nil value.
///
/// 2. Use makeOperation { op in ... }, and eventually cancel the
/// operation, or set result to a non-nil value.
open class AsynchronousOperation<Output, Failure: Error>: Operation {
@groue
groue / CancelBag.swift
Last active April 5, 2024 19:12
CancelBag
import Combine
import Foundation
/// A thread-safe store for cancellables which addresses usability pain points
/// with stock Combine apis.
///
/// ## Thread-safe storage of cancellables
///
/// let cancelBag = CancelBag()
/// cancellable.store(in: cancelBag)
import Combine
/// A publisher that delivers values to its downstream subscriber on a
/// specific scheduler.
///
/// Unlike Combine's Publishers.ReceiveOn, ReceiveValuesOn only re-schedule
/// values and completion. It does not re-schedule subscription.
struct ReceiveValuesOn<Upstream: Publisher, Context: Scheduler>: Publisher {
typealias Output = Upstream.Output
typealias Failure = Upstream.Failure