Skip to content

Instantly share code, notes, and snippets.

View markmals's full-sized avatar

Mark Malstrom markmals

View GitHub Profile
@markmals
markmals / signal-helpers.ts
Last active November 8, 2024 22:10
Helper functions missing from Angular's Signal implementation
import { signal, computed } from '@angular/core'
function writable<T>(fn: () => T) {
const c = computed(() => signal(fn()))
const w = () => c()()
Object.assign(w, { set: value => c().set(value) })
return w
}
function derived<T>(value: T, fn: (previous: T) => T) {
@markmals
markmals / vanilla-rsc.tsx
Last active October 12, 2024 04:41
Playing around with ideas for a 'close to the metal' React framework
import { Suspense, useState, use, useActionState, cache } from 'react';
import { db, Messages } from './db.server';
import { defineRoute, withState } from 'reverb';
import { eq } from 'drizzle-orm';
function Counter() {
'use client';
const [count, setCount] = useState(0);
return <button onClick={() => setCount(c => c + 1)}>Increment count: {count}</button>;
}
@markmals
markmals / *Pointer.swift
Last active September 26, 2024 06:14
An approximation of Rust & C++ smart pointers in Swift with non-copyable types
public protocol Pointer<Pointee>: ~Copyable {
associatedtype Pointee
var pointee: Pointee { get nonmutating set }
}
public struct UniquePointer<Pointee>: ~Copyable, Pointer {
private let memory: UnsafeMutablePointer<Pointee>
public var pointee: Pointee {
get { memory.pointee }
@markmals
markmals / apps.md
Last active September 8, 2024 20:54
Lowest-friction way to create an app with great user experience and developer experience

A Vision for Creating Modern Apps

Introduction

Delivering an excellent user experience and a seamless developer experience is essential for creating high-quality modern applications. Leveraging a modern, flexible tech stack that minimizes friction for developers while maximizing performance and user satisfaction is key. The vision presented here focuses on practicality, simplicity, cohesion, and efficiency across various platforms, using technologies tailored to specific platforms and use cases across for the web, Apple platforms, Android, Windows, and games.

Static Websites

Stack

@markmals
markmals / ReferenceSet.swift
Last active August 16, 2024 10:33
Vanilla Reactive System
// A reference type Set
private final class ReferenceSet<Element: Hashable>: Hashable, Collection {
typealias Element = Element
typealias Iterator = Set<Element>.Iterator
typealias Index = Set<Element>.Index
typealias Indices = Set<Element>.Indices
typealias SubSequence = Set<Element>.SubSequence
private var inner = Set<Element>()
@markmals
markmals / effect-stream.ts
Created December 4, 2023 16:48
Create an async iterable stream of values from a Solid.js signal effect
import { createMemo, createEffect } from 'solid-js';
export async function* createEffectStream<T>(fn: () => T) {
let promises: Promise<T>[] = [];
let resolve: (value: T) => void;
promises.push(
new Promise(r => {
resolve = r;
}),
);
@markmals
markmals / observable.ts
Last active December 3, 2023 21:35
Simple implementation of the observer design pattern in TypeScript with Lit integration
interface Observer {
execute(): void;
dependencies: Set<Set<Observer>>;
}
let context: Observer[] = [];
interface Constructor<T> {
new (...args: any[]): T;
}
@markmals
markmals / signals.swift
Last active March 24, 2024 06:02
An implementation of Solid.js's signals API using Swift's @observable macro
import Foundation
import Observation
@Observable
final class Signal<T> {
var value: T
init(value: T) {
self.value = value
}
@markmals
markmals / fine-grained-observable.swift
Created September 25, 2023 20:02
Fine-grained @observable interactions vs coarse-grained decoding
// With Observable
@Observable
class Book {
var title: String
var author: String
var inStock: Bool
init(title: String, author: String, inStock: Bool) {
self.title = title
import { subject } from '@webstd/combine';
import type { Publisher } from '@webstd/combine';
import type { Identifiable } from '@webstd/types';
import { element, ReactiveElement, html, environment } from '@webstd/custom-elements';
import { Author } from './author.js'
class Book implements Identifiable {
id = crypto.randomUUID(); // A unique identifier that never changes.
title$ = subject('Sample Book Title');