Skip to content

Hi πŸ‘‹ I'm Joseph Duffy

Joseph Duffy

I enjoy building software. This website contains information about the apps I have created, open-source projects, and blog posts. Welcome to my corner of the internet!

β˜… My Favourite Apps

Overamped

Overamped Icon

Overamped is a Safari Extension that redirects AMP and Yandex Turbo pages to their canonical versions, no matter how the page was opened.

Four Squares

Four Squares Icon

Four Squares is a game of memory, skill, and concentration available for iOS, iPadOS, and macOS. Watch what happens each turn and replay what you see.

Recent Entries

Smuggling Values Across Actors


I recently started updating an app to use the Swift 6 language mode and ran in to an issue using AVCaptureMetadataOutputObjectsDelegate. The issue is that the compiler cannot reason about which actor the delegate function is called on and it must be treated as nonisolated, however we know that it's being called on a specific actor (in this case the MainActor), so how can we tell the compiler?

In Swift 5 language mode we can use MainActor.assumeIsolated and call it a day. But in Swift 6 this will produce an error:

import AVFoundation
import UIKit

@MainActor
final class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
    private var output: AVCaptureMetadataOutput?

    override func viewDidLoad() {
        super.viewDidLoad()

        let output = AVCaptureMetadataOutput()
        output.setMetadataObjectsDelegate(self, queue: .main)
        self.output = output
    }

    private func doSomethingWithMetadataObjects(_ metadataObjects: [AVMetadataObject]) {}

    // Must be marked nonisolated because the AVCaptureMetadataOutputObjectsDelegate protocol cannot declare the actor on which the function will be called.
    nonisolated func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        // We know it's on the main actor so we should be able to assume
        // isolated and pass metadata objects to another function.
        MainActor.assumeIsolated {
            // This does not compile though, presumably because AVMetadataObject
            // is not Sendable.
            doSomethingWithMetadataObjects(metadataObjects) // ❌ Error: Sending 'metadataObjects' risks causing data races
        }
    }
}

This reminded me of a macro I created recently, or more specifically the type used to workaround a similar issue with stored properties in Swift 5: UnsafeSendable. This type works by using @unchecked Sendable to send store values we know are sendable but have no native way of telling the compiler.

In this case, however, it can also be used to smuggle values across an actor boundary that we know is safe.

public struct Smuggler<Smuggled>: @unchecked Sendable {
    public var smuggled: Smuggled

    public init(unsafeSmuggled smuggled: Smuggled) {
        self.smuggled = smuggled
    }

    @available(*, deprecated, message: "Smuggler is not needed when `Smuggled` is Sendable")
    public init(unsafeSmuggled smuggled: Smuggled) where Smuggled: Sendable {
        self.smuggled = smuggled
    }
}

With this we can smuggle our value through:

    nonisolated func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        // We know it's on the main actor so we should be able to assume
        // isolated and pass metadata objects to another function.
        let smuggler = Smuggler(unsafeSmuggled: metadataObjects)
        MainActor.assumeIsolated {
            let metadataObjects = smuggler.smuggled
            doSomethingWithMetadataObjects(metadataObjects) // βœ… Works
        }
    }

We can add an extension to MainActor that makes this a little more convenient.

extension MainActor {
    public static func assumeIsolated<Smuggled>(
        smuggling smuggled: Smuggled,
        operation: @MainActor (_ smuggled: Smuggled) -> Void,
        file: StaticString = #fileID,
        line: UInt = #line
    ) {
        let smuggler = Smuggler(unsafeSmuggled: smuggled)
        withoutActuallyEscaping(operation) { escapingOperation in
            let smuggledOperation = Smuggler(unsafeSmuggled: escapingOperation)
            assumeIsolated({
                smuggledOperation.smuggled(smuggler.smuggled)
            }, file: file, line: line)
        }
    }
}

I have been playing around with trying to add a function to GlobalActor that would make the API a bit nicer but I think it's going to require a macro, lest we resort to reimplementing MainActor.assumeIsolated 😊

Although this isn't really "smuggling" because the value is not actually crossing an actor boundary I like the term and I'm sticking to it! Especially because I assume this workaround will be temporary.

P.S. I created a demo project of the issue, which I also submitted to Apple as feedback FB13950073 ✌️

The Bug That Bit Me Twice


I've been working on a fix for a bug in Overamped, which causes the popover UI shown when tapping on an image in Google Images to be blank, if the link goes to an AMP page. This was a silly bug that never should've happened; knowing that Google can change their page structure at any time I should've been more cautious with my checks.

As a temporary quick fix I removed all custom handling of Google results, tested my changes in the simulator, and uploaded a new build to TestFlight.

After installing the TestFlight update on my phone I checked a search result that I knew recreated the problem, but it was still happening! I have other extensions installed so I disabled some, refreshed, and the bug was fixed!

I thought it would be very strange for the same – very specific – bug to appear in multiple extensions, so I did a little digging.

Keep Reading β†’

Overamped version 1.2.1


Release Notes

  • Fixed an issue that can occur on newer versions of Safari on Google Images
  • Improved layout of popover on larger screens

Partial in Swift


Partial is now available in its own Swift package on GitHub. This post is still valid, but somewhat out of date.

Structs are incredibly useful in Swift, especially when representing static read-only data. However, the values of a struct often come from multiple sources, such as view controllers, network requests, and files on disk, which can make the creation of these structs cumbersome.

There are numerous methods to work around this, but each have their downsides. One of these methods is to change the struct to a class and update the properties to vars, but this removes the advantages of read-only structs. Another is to make a "builder" object, but the API of this object must be kept in-sync with the object is wraps.

Partial eliminates these problems by providing a type-safe API for building structs by utilising generics and KeyPaths. Although I learned of the concept of Partial through TypeScript – which [provides Partial as a built-in type][1] – the Swift implementation supports many more use cases.

Keep Reading β†’

Overamped version 1.2.0


Release Notes

Support for the Overamped Install Checker has been added, enabling a method of automatically checking if the Safari Extension is enabled and setup correctly.

The new install checker can be found in the About tab.

Don't Use Scope Modifiers with Extensions


Extending types in Swift support setting the scope for the extension, i.e. public, internal, or private, with internal being implicit if nothing is specified.

This may seem useful, but given the following snippet it's impossible to know what the scope of a function is:

func doSomething() {
    // Do the thing
}
Keep Reading β†’

Overamped version 1.1.0


Release Notes

A new option in the Settings allows for a notification to be posted whenever Overamped redirects an AMP or Yandex Turbo page in Safari.

A new screen has been added under Advanced Statistics that displays each event that has occurred and allows for the deletion of individual events.

The permissions model has been simplified. The "Other Websites" will now be the only option shown when first installing the extension.

Overamped 1.1.0 and the Year of Small


So far my Year of Small is going really well; as I write this I'm making some final changes and preparing to push out v1.1.0-RC.1, which I hope to submit the App Store in the next couple of days.

Overamped 1.1.0 is an update I first started working on almost 3 months ago, and it should've been released earlier.

My initial plan for 1.1.0 was to:

  • Add a screen showing recently logged events
  • Add an option to send a notification when the Web Extension redirects a link
  • Simplify the permissions model
  • Add widgets

Easy, right? Well, adding widgets is why this update has been so delayed.

Keep Reading β†’

pastelghouls Available Again


pastelghouls is once again available for download on the App Store. It's a free sticker pack containing 6 ghoulish sticker. Originally released 19th October 2016, just in time for halloween, it was removed from the App Store December 7th 2019 due to not having an update for a substantial period of time.

As part of my year of small I wanted to make this available again, and at the same time setup fastlane to automate the screenshots and store the app metadata to make future updates easier.

pastelghouls was created by my friend Joshua Robins and published by my company Yetii Ltd.