Skip to content

Instantly share code, notes, and snippets.

View clinuxrulz's full-sized avatar

Clinton Selke clinuxrulz

View GitHub Profile
@clinuxrulz
clinuxrulz / Plugin.ts
Last active March 4, 2024 09:15
Browser Plugin Utility (bidirectional asynchronous function calls)
export class Plugin {
private src: String;
private callbacks: any;
private iframe?: HTMLIFrameElement;
private messageHandler: (e: MessageEvent) => void;
private recvMsgHandler: (msg: string) => void;
private nextCallId: number = 0;
private resolveMap: any;
onLoad: () => void = () => {};
@clinuxrulz
clinuxrulz / string_factory.rs
Created August 12, 2021 12:05
Recycling strings in Rust
use std::cell::RefCell;
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::rc::Rc;
thread_local! {
static STRING_FACTORY: RefCell<StringFactory> = RefCell::new(StringFactory::new());
}
struct MyRc<A>(Rc<A>);
@clinuxrulz
clinuxrulz / Window.tsx
Last active May 13, 2024 19:46
SolidJS reusable draggable/resizable window example (requires bootstrap style sheet and an icon for grip drag corner "svgs/icons/resize%20grip.svg")
import { batch, Component, createEffect, createMemo, onCleanup, onMount } from 'solid-js';
import { createStore } from 'solid-js/store';
const Window: Component<{
title?: string,
width?: number,
height?: number,
initX?: number,
initY?: number,
}> = (props) => {
@clinuxrulz
clinuxrulz / cell_arrayed_keyed.ts
Created June 18, 2019 11:15
Cell array keyed
export function cellArrayKeyed2<K,A,B>(keyEq: (a:K,b:K)=>boolean, keyExtractor: (a:A)=>K, transform: (a:A) => B, cas: sodium.Cell<A[]>): sodium.Cell<B[]> {
return sodium.Operational
.updates(cas)
.accumLazy(
cas.sampleLazy().map(as => as.map(a => T2.of(keyExtractor(a), transform(a)))),
(nextAs: A[], lastResult: T2<K,B>[]) => {
let nextResult: T2<K,B>[] = [];
for (let i = 0; i < nextAs.length; ++i) {
let a = nextAs[i];
let key = keyExtractor(a);
@clinuxrulz
clinuxrulz / cell_switchC_no_coalesce.ts
Last active June 16, 2019 10:52
Coalesce free Cell::switchC
export class Cell<A> {
.
.
.
static switchC<A>(cca : Cell<Cell<A>>) : Cell<A> {
return Transaction.run(() => {
const za = cca.sampleLazy().map((ba : Cell<A>) => ba.sample()),
out = new StreamWithSend<A>();
let outValue: A = null;
class MergeState<A> {
constructor() {}
left : A = null;
left_present : boolean = false;
right : A = null;
right_present : boolean = false;
}
export class Stream<A> {
.
@clinuxrulz
clinuxrulz / cell_apply_no_coalesce.ts
Created June 14, 2019 07:24
Coalesce free Cell::apply
export class Cell<A> {
.
.
.
/**
* Apply a value inside a cell to a function inside a cell. This is the
* primitive for all function lifting.
*/
static apply<A,B>(cf : Cell<(a : A) => B>, ca : Cell<A>, sources? : Source[]) : Cell<B> {
return Transaction.run(() => {
@clinuxrulz
clinuxrulz / overworked_lift.ts
Created June 13, 2019 11:25
overworked sodium cell lift
test("cell lift work", done => {
let lines = [
"Work it harder",
"Make it better",
"Do it faster",
"Makes us stronger"
];
let idx = 0;
let c1 = new CellSink(0);
let c2 = new CellSink(0);
@clinuxrulz
clinuxrulz / EcsTHREESceneUpdater.ts
Created April 9, 2019 21:07
ECS + Sodium + THREE.js
import * as sodium from 'sodiumjs';
import * as THREE from 'three';
import { EcsComponentType } from './EcsComponentType';
import { EcsReadOnlySceneContext } from './EcsReadOnlySceneContext';
import { EcsSceneChanges } from './EcsSceneChanges';
import { IsEcsComponentValue } from './IsEcsComponentValue';
import { ArcComponent } from './components/ArcComponent';
import { Axes2DComponent } from './components/Axes2DComponent';
import { CircleComponent } from './components/CircleComponent';
import { Line3DComponent } from './components/Line3DComponent';
@clinuxrulz
clinuxrulz / cell_trace.ts
Last active February 20, 2019 02:51
Tracking high order dependencies
export function cellTrace<A>(ca: sodium.Cell<A>, extractor: (a: A) => (sodium.Stream<any>|sodium.Cell<any>)[]): sodium.Cell<A> {
let cKeepAlive = sodium.Cell.switchC(ca.map(
a =>
cellLiftArray(
extractor(a).map(
x => {
if (x instanceof sodium.Stream) {
return x.hold({} as any);
} else {