Skip to content

Commit

Permalink
refactor(primitives): codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
artalar committed Apr 11, 2024
1 parent a727c9b commit 6b8997a
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 67 deletions.
6 changes: 3 additions & 3 deletions packages/async/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,11 @@ export const fetchList = reatomAsync(
withRetry({
onReject: (ctx, error, retries) => 100 * Math.min(200, retries ** 3),
}),
withAssign((list, name) => ({
withAssign((target, name) => ({
loadingAtom: atom(
(ctx) =>
ctx.spy(fetchList.pendingAtom) > 0 ||
ctx.spy(fetchList.retriesAtom) > 0,
ctx.spy(target.pendingAtom) > 0 ||
ctx.spy(target.retriesAtom) > 0,
`${name}.loadingAtom`,
),
})),
Expand Down
10 changes: 5 additions & 5 deletions packages/primitives/src/reatomArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ export const reatomArray = <T>(
name?: string,
): ArrayAtom<T> =>
atom(initState, name).pipe(
withAssign((theAtom, name) => ({
withAssign((target, name) => ({
toReversed: action(
(ctx) => theAtom(ctx, (prev) => prev.slice().reverse()),
(ctx) => target(ctx, (prev) => prev.slice().reverse()),
`${name}.toReversed`,
),
toSorted: action(
(ctx, compareFn?: (a: T, b: T) => number) =>
theAtom(ctx, (prev) => prev.slice().sort(compareFn)),
target(ctx, (prev) => prev.slice().sort(compareFn)),
`${name}.toSorted`,
),
toSpliced: action(
(ctx, start: number, deleteCount: number, ...items: T[]) =>
theAtom(ctx, (state) => {
target(ctx, (state) => {
state = state.slice()
state.splice(start, deleteCount, ...items)
return state
Expand All @@ -34,7 +34,7 @@ export const reatomArray = <T>(
),
with: action(
(ctx, i: number, value: T) =>
theAtom(ctx, (state) => {
target(ctx, (state) => {
if (Object.is(state.at(i), value)) return state
state = state.slice()
state[i] = value
Expand Down
10 changes: 5 additions & 5 deletions packages/primitives/src/reatomBoolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export interface BooleanAtom extends AtomMut<boolean> {

export const reatomBoolean = (init = false, name?: string): BooleanAtom =>
atom(init, name).pipe(
withAssign((theAtom, name) => ({
toggle: action((ctx) => theAtom(ctx, (prev) => !prev), `${name}.toggle`),
setTrue: action((ctx) => theAtom(ctx, true) as true, `${name}.setTrue`),
withAssign((target, name) => ({
toggle: action((ctx) => target(ctx, (prev) => !prev), `${name}.toggle`),
setTrue: action((ctx) => target(ctx, true) as true, `${name}.setTrue`),
setFalse: action(
(ctx) => theAtom(ctx, false) as false,
(ctx) => target(ctx, false) as false,
`${name}.setFalse`,
),
reset: action((ctx) => theAtom(ctx, init), `${name}.reset`),
reset: action((ctx) => target(ctx, init), `${name}.reset`),
})),
)
35 changes: 20 additions & 15 deletions packages/primitives/src/reatomEnum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { action, Action, atom, AtomMut } from '@reatom/core'
import { action, Action, atom, AtomMut, Ctx, throwReatomError } from '@reatom/core'

export type EnumFormat = 'camelCase' | 'snake_case'

Expand All @@ -7,10 +7,10 @@ export type EnumAtom<
Format extends EnumFormat = 'camelCase',
> = AtomMut<T> & {
[Variant in T as Format extends 'camelCase'
? `set${Capitalize<Variant>}`
: Format extends 'snake_case'
? `set_${Variant}`
: never]: Action<[], Variant>
? `set${Capitalize<Variant>}`
: Format extends 'snake_case'
? `set_${Variant}`
: never]: Action<[], Variant>
} & {
reset: Action<[], T>
enum: { [K in T]: K }
Expand All @@ -37,13 +37,18 @@ export const reatomEnum = <
format = 'camelCase' as Format,
initState = variants[0],
}: EnumAtomOptions<T, Format> = typeof options === 'string'
? { name: options }
: options
? { name: options }
: options

const theAtom = atom(initState, name) as EnumAtom<T, Format>
const cases = (theAtom.enum = {} as { [K in T]: K })
const stateAtom = atom(initState, name) as EnumAtom<T, Format>
const enumAtom: typeof stateAtom = Object.assign((ctx: Ctx, update: any) => {
const state = stateAtom(ctx, update)
throwReatomError(!variants.includes(state), `invalid enum value "${state}" for "${name}" enum`)
return state
}, stateAtom)
const cases = (enumAtom.enum = {} as { [K in T]: K })

theAtom.reset = action((ctx) => theAtom(ctx, initState!), `${name}.reset`)
enumAtom.reset = action((ctx) => enumAtom(ctx, initState!), `${name}.reset`)

for (const variant of variants) {
cases[variant] = variant
Expand All @@ -56,11 +61,11 @@ export const reatomEnum = <
: `_${firstLetter}`),
)

;(theAtom as any)[setterName] = action(
(ctx) => theAtom(ctx, variant)!,
`${name}.${setterName}`,
)
; (enumAtom as any)[setterName] = action(
(ctx) => enumAtom(ctx, variant)!,
`${name}.${setterName}`,
)
}

return theAtom as EnumAtom<T, Format>
return enumAtom as EnumAtom<T, Format>
}
14 changes: 7 additions & 7 deletions packages/primitives/src/reatomMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export const reatomMap = <Key, Value>(
name?: string,
): MapAtom<Key, Value> =>
atom(initState, name).pipe(
withAssign((theAtom, name) => ({
get: (ctx: Ctx, key: Key) => ctx.get(theAtom).get(key),
has: (ctx: Ctx, key: Key) => ctx.get(theAtom).has(key),
withAssign((target, name) => ({
get: (ctx: Ctx, key: Key) => ctx.get(target).get(key),
has: (ctx: Ctx, key: Key) => ctx.get(target).has(key),
set: action(
(ctx, key: Key, value: Value) =>
theAtom(ctx, (prev) => {
target(ctx, (prev) => {
const valuePrev = prev.get(key)
return Object.is(valuePrev, value) &&
(value !== undefined || prev.has(key))
Expand All @@ -31,15 +31,15 @@ export const reatomMap = <Key, Value>(
),
delete: action(
(ctx, key: Key) =>
theAtom(ctx, (prev) => {
target(ctx, (prev) => {
if (!prev.has(key)) return prev
const next = new Map(prev)
next.delete(key)
return next
}),
`${name}.delete`,
),
clear: action((ctx) => theAtom(ctx, new Map()), `${name}.clear`),
reset: action((ctx) => theAtom(ctx, initState), `${name}.reset`),
clear: action((ctx) => target(ctx, new Map()), `${name}.clear`),
reset: action((ctx) => target(ctx, initState), `${name}.reset`),
})),
)
10 changes: 5 additions & 5 deletions packages/primitives/src/reatomNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ export interface NumberAtom extends AtomMut<number> {

export const reatomNumber = (initState = 0, name?: string): NumberAtom =>
atom(initState, name).pipe(
withAssign((theAtom, name) => ({
withAssign((target, name) => ({
increment: action(
(ctx, by = 1) => theAtom(ctx, (prev) => prev + by),
(ctx, by = 1) => target(ctx, (prev) => prev + by),
`${name}.increment`,
),
decrement: action(
(ctx, by = 1) => theAtom(ctx, (prev) => prev - by),
(ctx, by = 1) => target(ctx, (prev) => prev - by),
`${name}.decrement`,
),
random: action((ctx) => theAtom(ctx, Math.random()), `${name}.decrement`),
reset: action((ctx) => theAtom(ctx, initState), `${name}.reset`),
random: action((ctx) => target(ctx, Math.random()), `${name}.decrement`),
reset: action((ctx) => target(ctx, initState), `${name}.reset`),
})),
)
14 changes: 7 additions & 7 deletions packages/primitives/src/reatomRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { withAssign } from './withAssign'

export interface RecordAtom<T extends Rec> extends AtomMut<T> {
merge: Action<[slice: Partial<T>], T>
omit: Action<(keyof T)[], T>
reset: Action<(keyof T)[], T>
omit: Action<Array<(keyof T)>, T>
reset: Action<Array<(keyof T)>, T>
}

export const reatomRecord = <T extends Rec>(
initState: T,
name?: string,
): RecordAtom<T> =>
atom(initState, name).pipe(
withAssign((theAtom) => ({
withAssign((target) => ({
merge: action(
(ctx, slice: Partial<T>) =>
theAtom(ctx, (prev) => {
target(ctx, (prev) => {
for (const key in prev) {
if (!Object.is(prev[key], slice[key])) {
return { ...prev, ...slice }
Expand All @@ -28,8 +28,8 @@ export const reatomRecord = <T extends Rec>(
),

omit: action(
(ctx, ...keys: (keyof T)[]) =>
theAtom(ctx, (prev) => {
(ctx, ...keys: Array<(keyof T)>) =>
target(ctx, (prev) => {
if (keys.some((key) => key in prev)) return omit(prev, keys) as any
return prev
}),
Expand All @@ -38,7 +38,7 @@ export const reatomRecord = <T extends Rec>(

reset: action(
(ctx, ...keys: (keyof T)[]) =>
theAtom(ctx, (prev) => {
target(ctx, (prev) => {
if (keys.length === 0) return initState
const next = {} as T
let changed = false
Expand Down
21 changes: 8 additions & 13 deletions packages/primitives/src/reatomSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,25 @@ export const reatomSet = <T>(
name?: string,
): SetAtom<T> =>
atom(initState, name).pipe(
withAssign((theAtom, name) => ({
set: action((ctx, el) => {
return theAtom(ctx, (prev) => {
if (prev.has(el)) return prev
const next = new Set(prev)
next.add(el)
return next
})
}, `${name}.set`),
withAssign((target, name) => ({
set: action((ctx, el) =>
target(ctx, (prev) => prev.has(el) ? prev : new Set(prev).add(el))
, `${name}.set`),
delete: action((ctx, el) => {
return theAtom(ctx, (prev) => {
return target(ctx, (prev) => {
if (!prev.has(el)) return prev
const next = new Set(prev)
next.delete(el)
return next
})
}, `${name}.delete`),
clear: action((ctx) => {
return theAtom(ctx, (prev) => {
return target(ctx, (prev) => {
if (prev.size === 0) return prev
return new Set<T>()
})
}, `${name}.clear`),
reset: action((ctx) => theAtom(ctx, initState), `${name}.reset`),
has: (ctx: Ctx, el: T) => ctx.get(theAtom).has(el),
reset: action((ctx) => target(ctx, initState), `${name}.reset`),
has: (ctx: Ctx, el: T) => ctx.get(target).has(el),
})),
)
2 changes: 1 addition & 1 deletion packages/primitives/src/reatomString.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as assert from 'uvu/assert'

import { reatomString } from './reatomString'

test('reatomString..reset', () => {
test('reatomString.reset', () => {
const ctx = createCtx()
const a = reatomString(`string`)

Expand Down
4 changes: 2 additions & 2 deletions packages/primitives/src/reatomString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const reatomString: {
<T extends string>(init: T, name?: string): StringAtom<T>
} = (init = '', name?: string) =>
atom(init, name).pipe(
withAssign((theAtom) => ({
reset: action((ctx) => theAtom(ctx, init)),
withAssign((target, name) => ({
reset: action((ctx) => target(ctx, init), `${name}.reset`),
})),
)
5 changes: 1 addition & 4 deletions packages/primitives/src/withAssign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@ export const withAssign =
<Target extends Atom, Props extends object>(
getProps: (target: Target, name: string) => Props,
) =>
(target: Target) => {
const name = target.__reatom.name ?? __count('withAssign')
return assign(target, getProps(target, name)) as Target & Props
}
(target: Target) => assign(target, getProps(target, target.__reatom.name!)) as Target & Props

0 comments on commit 6b8997a

Please sign in to comment.