This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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)); |
This file contains hidden or 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
// 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; |
This file contains hidden or 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
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 | |
} |
This file contains hidden or 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 analyticsAtom = atom((ctx) => { | |
const { url } = ctx.spy(routeAtom); | |
const { name } = ctx.get(userAtom); | |
ctx.schedule(() => logAnalytics("visit_page", url, name)); | |
}); |
This file contains hidden or 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
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(() => |
This file contains hidden or 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
<> | |
<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 |
This file contains hidden or 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
// 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; |