Skip to content

Instantly share code, notes, and snippets.

View sslotsky's full-sized avatar
🌶️
Working from home

Sam Slotsky sslotsky

🌶️
Working from home
View GitHub Profile
@sslotsky
sslotsky / drag-tracker.tsx
Created February 20, 2023 00:59
A qwik component to track the change in movement for both mouse and single-touch dragging. Accepts all div attributes as well as children.
import {
component$,
HTMLAttributes,
QRL,
Slot,
useSignal,
useTask$,
} from "@builder.io/qwik";
interface Movement {
@sslotsky
sslotsky / use-image-url.ts
Last active January 8, 2023 15:39
A qwik hook for managing blurry image fallbacks. Computes the blurry image URL until the high-res image is ready to display.
import {
useResource$,
useSignal,
useTask$,
ResourceReturn,
} from "@builder.io/qwik";
import { MasonryPhoto } from "~/trcp/router";
export type State = "blank" | "blurry" | "done";
@sslotsky
sslotsky / api.ts
Last active January 2, 2023 14:09
Qwik + Vendure GraphQL hooks
import { $, useEnvData, useStore } from "@builder.io/qwik";
import { isBrowser, isServer } from "@builder.io/qwik/build";
import { GraphQLClient, rawRequest } from "graphql-request";
import { parse, serialize } from "cookie";
export const AUTH_TOKEN_KEY = "vendure-auth-token";
export function getHeaders() {
const headers: HeadersInit = {};
function debounce(fn, interval) {
let timeout;
return function(...args) {
clearTimeout(timeout);
return new Promise(resolve => {
timeout = setTimeout(() => resolve(fn(...args)), interval);
});
}
}
let (>>=) = (m: chainableList('a), f: list('a) => chainableList('b)) => {
switch(m) {
| Value(l) => doSomeStuffAndReturn(l |> f)
| Failure => logThisFailure(m); Failure
}
};
/* Fluent list operations in ReasonML */
let l = return([1, 2, 3])
>>= wrap(List.map(n => n * 2))
>>= wrap(List.filter(n => n mod 2 != 0))
>>= tail;
let return = x => Value(x);
let wrap = (f, l) => return(l |> f);
let (>>=) = (m: chainableList('a), f: list('a) => chainableList('b)) => switch(m) {
| Value(l) => l|> f
| Failure => Failure
};
let tail = x => switch(x |> List.tl) {
| l => Value(l)
| exception _ => Failure
};
type chainableList('a) =
| Value(list('a))
| Failure;