Skip to content

Instantly share code, notes, and snippets.

@artalar
artalar / reatomAsync.tsx
Created March 14, 2023 13:41
Dependent effects cancelation with Reatom
// new API: reatomAsync automatically cancels the previous request
// and all dependent requests when the new request is triggered
const getList = reatomAsync.from(getListApi).pipe(withDataAtom([]));
const getId = reatomAsync.from(getIdApi).pipe(withAbort());
onUpdate(getId.onFulfilled, (ctx, { data: id }) => getList(ctx, id));
const Component = () => {
const [list] = useAtom(getList.dataAtom);
const handleChange = useAction((ctx, e) => getId(ctx, e.currentTarget.value));
// https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback
// https://github.com/reactjs/rfcs/pull/220#issuecomment-1259938816
import React from 'react';
// Allow to access a fresh closures in the function but returns stable reference during rerenders
export function useCallbackRef<T extends (...args: unknown[]) => unknown>(callback: T): T {
const ref: React.MutableRefObject<{
stableFn: T;
callback: T;
type LLNode<T = any> = {
prev: null | LLNode<T>
value: T
next: null | LLNode<T>
}
const LLAdd = <T>(prev: LLNode<T>, value: T, next = prev.next) => {
prev.next = { prev, value, next }
if (next !== null) next.prev = prev.next
}
const analyticsAtom = atom((ctx) => {
const { url } = ctx.spy(routeAtom);
const { name } = ctx.get(userAtom);
ctx.schedule(() => logAnalytics("visit_page", url, name));
});
type MyPromise<T> = Promise<Awaited<T>>
async function call<T extends () => any>(fn: T): MyPromise<ReturnType<T>> {
return fn()
}
const res = call(() =>
call(() =>
call(() =>
call(() =>
@artalar
artalar / json-template.jsx
Last active August 6, 2022 00:06
json-template
<>
<h1 id="login">Login</h1>
<form aria-describedby="login" onSubmit={handleSubmit}>
<input type="email" value={email} onChange={handleChangeEmail} />
<input type="password" value={password} onChange={handleChangePassword} />
<button type="submit">Submit</button>
</form>
</>;
// VS
// inspired by https://doc.rust-lang.org/reference/expressions/block-expr.html
function some() {
{
const a = 1;
const b = 1;
var x = 1;
}
{
const a = 2;

Structure

Concepts

workflow - is a common term for user story / business workflow

utils - basic reusable constants, functions and services without knowledge of a workflow

unit testing. tip: use uvu (without CLI)

blocks - basic reusable view components (ui-kit) without knowledge of a workflow

export type Merge<Intersection> = Intersection extends Fn
? Intersection
: Intersection extends new (...a: any[]) => any
? Intersection
: Intersection extends object
? {
[Key in keyof Intersection]: Intersection[Key]
}
: Intersection
import * as t from 'runtypes'
export const oneMillisecond = 1
export const oneSecond = oneMillisecond * 1000
export const oneMinute = oneSecond * 60
export const oneHour = oneMinute * 60
export const oneDay = oneHour * 24
/** 30 messages per second for all users
* and one message per second for one user