Skip to content

Instantly share code, notes, and snippets.

/* eslint-disable */
// https://github.com/Chronstruct/display-primitives/issues/1#issuecomment-1518496207
// declare global {
// namespace JSX {
// interface IntrinsicElements<T extends string> {
// input: React.DetailedHTMLProps<
// React.InputHTMLAttributes<HTMLInputElement>,
// HTMLInputElement
import {
type AsyncAction,
action,
atom,
spawn,
take,
toAbortError,
isCausedBy,
} from '@reatom/framework';
import { reatomComponent } from '@reatom/npm-react';
@artalar
artalar / example.ts
Created March 26, 2024 22:28
reatom-router
// in the app root
const ROUTES = reatomRoutes({
auth: {
login: {},
registration: {},
},
goods: {
':id': {
edit: {},
},
@artalar
artalar / reatomList.ts
Last active March 19, 2024 05:13
reatomZod
import {
type Atom,
type Action,
type Ctx,
type Rec,
atom,
action,
type Fn,
type CtxSpy,
isCausedBy,
@artalar
artalar / index.ts
Created January 29, 2024 14:06
reatomFetch
export type UrlSearchParamsInit = ConstructorParameters<
typeof URLSearchParams
>[0];
export interface FetchRequestInit<
Result = unknown,
Params = unknown,
Body = unknown
> extends RequestInit {
url?: string | URL;
@artalar
artalar / client.ts
Last active March 25, 2024 20:11
reatomGql real example
import { fingerprint } from 'src/infrastructure/fingerprint';
import { Client, fetchExchange, makeOperation, mapExchange } from 'urql';
export const client = new Client({
url: '/api/graphql',
requestPolicy: 'network-only',
exchanges: [
mapExchange({
async onOperation(operation) {
return makeOperation(operation.kind, operation, {
import { AsyncAction } from '@reatom/async'
import { Atom, atom } from '@reatom/core'
export const withReadyAtom =
<T extends AsyncAction & { dataAtom?: Atom }>(initState = false) =>
(anAsync: T): T & { readyAtom: Atom<boolean> } => {
// use `spy` to prevent any race conditions
const readyAtom = atom((ctx, state?: boolean) => {
// trigger connection to start the fetch if `onConnect` used
if (anAsync.dataAtom) ctx.spy(anAsync.dataAtom)
// BASE
// debounce
const onChange = debounce((ctx, event) => {
inputAtom(ctx, event.currentTarget.value);
}, 500);
// concurrent
const onChange = concurrent(async (ctx, event) => {
await ctx.schedule(() => sleep(500));
// create
export const { useAuth } = createContexts({
Auth() {
const [a, setA] = React.useState("a");
const [b, setB] = React.useState("b");
return {
a,
b,
setA,
@artalar
artalar / ctx.ts
Last active August 15, 2023 02:34
How to add extra logic to Reatom context
const ctxBase = createCtx();
const listeners = new Set();
const { subscribe } = ctx;
export const ctx = Object.assign({}, ctxBase, {
subscribe(...a) {
const un = subscribe.apply(this, a);
// logs
if (a.length === 1) return un;