Skip to content

Instantly share code, notes, and snippets.

View markmals's full-sized avatar

Mark Malstrom markmals

View GitHub Profile
@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');
import { Observable, ObservationIgnored, withObservationTracking } from '@webstd/observable';
import type { Identifiable } from '@webstd/types';
import { CustomElement, ReactiveElement, html, Environment, State } from '@webstd/custom-elements';
import { stream } from '@webstd/request';
import { Author } from './author.js'
@Observable
class Book implements Identifiable {
@ObservationIgnored id = crypto.randomUUID(); // A unique identifier that never changes.
@markmals
markmals / solid-for-uikit-v2.swift
Last active June 15, 2023 03:02
My ideal Swift Macro version of a Solid.js-like renderer for UIKit via Swift
import SwiftSignal
import FineGrainedViews
// Macro usage:
final class CounterViewController: UIViewController {
private let count = Signal(0)
override func viewDidLoad() {
super.viewDidLoad()
@markmals
markmals / preact-server-components.tsx
Last active April 23, 2023 00:39
A sketch idea of Preact Server Components with coarse-grained progressive hydration
import { useSignal } from "@preact/signals"
import { action$, island$, json, loader$ } from "preact-server-components"
import { useEffect } from "preact/hooks"
import { db } from "./db.server"
const Counter = island$(() => {
let count = useSignal(0)
// TODO: Resumable hydration
// We don't want to do this work on the server on initial load and then again on
@markmals
markmals / test.component.ts
Last active October 20, 2022 20:03
My Ideal Angular Component Syntax
import { Component, State, Computed, html, css } from '@angular/core'
import FooDirective from './foo.directive.ts'
import FooComponent from './foo.component.ts'
// The `selector` is derived from the class's name and a globally configured selector prefix
// In this case, the selector for this class would be `app-test`, using the default prefix
export class TestComponent implements Component {
@State() foo = ""
@Computed() get bar(): number {
@markmals
markmals / wrangular.ts
Created July 28, 2022 21:18
A sketch for a component library for Angular
// =====================================================================
// FILE: app/routes/index.route.ts
import { Component } from "@angular/core";
import { Paths } from "@remix-run/angular";
import { SidebarListStyle } from "pineappleui";
@Component({
selector: "app-content-view",
template: `