This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create | |
export const { useAuth } = createContexts({ | |
Auth() { | |
const [a, setA] = React.useState("a"); | |
const [b, setB] = React.useState("b"); | |
return { | |
a, | |
b, | |
setA, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type EventOfTarget< | |
Target extends EventTarget, | |
Type extends string, | |
> = Target extends Record<`on${Type}`, infer Cb> | |
? // @ts-expect-error `Cb extends Fn` broke the inference for some reason | |
Parameters<Cb>[0] // correct type | |
: Target extends Record<'onEvent', (type: Type, cb: infer Cb) => any> | |
? // @ts-expect-error `Cb extends Fn` broke the inference for some reason | |
Parameters<Cb>[0] // general type | |
: never |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { $corsProxyEnabled } from '@/src/entities/cors-proxy/model/store' | |
import { checkInstanceUrl } from '@/src/features/instance/api/check' | |
import { InstanceCheckResult } from '@/src/features/instance/model/model' | |
import { BuildConfig } from '@/src/shared/model/build-config' | |
import { | |
combine, | |
createEffect, | |
createEvent, | |
createStore, | |
restore, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const random = (min = 0, max = Number.MAX_SAFE_INTEGER - 1) => | |
Math.floor(Math.random() * (max - min + 1)) + min | |
const randomObj = (obj: Record<string, any>, deep: number) => { | |
if (deep <= 0) return obj | |
const keys = random(5, 10) | |
for (let i = 0; i < keys; i++) { | |
const type = random(0, 2) | |
if (type === 0) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { test } from 'uvu' | |
import * as assert from 'uvu/assert' | |
import { createTestCtx } from '@reatom/testing' | |
import { atom } from '@reatom/core' | |
import { sleep } from '@reatom/utils' | |
import { reatomAsync, withAbort } from '@reatom/async' | |
test('safe pooling', async () => { | |
const createTask = reatomAsync(async () => Math.random()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); |