Skip to content

Commit

Permalink
[Types] Add Read & Write types, reduce duplicate type declaration (pm…
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornstar authored Mar 20, 2021
1 parent a4768c7 commit 6f72fca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
24 changes: 11 additions & 13 deletions src/core/atom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
Getter,
Setter,
Read,
Write,
Atom,
WritableAtom,
WithInitialValue,
Expand All @@ -13,22 +13,20 @@ let keyCount = 0 // global key count for all atoms

// writable derived atom
export function atom<Value, Update>(
read: (get: Getter) => Value | Promise<Value>,
write: (get: Getter, set: Setter, update: Update) => void | Promise<void>
read: Read<Value>,
write: Write<Update>
): WritableAtom<Value, Update>

// write-only derived atom
export function atom<Value, Update>(
read: Value,
write: (get: Getter, set: Setter, update: Update) => void | Promise<void>
write: Write<Update>
): Value extends AnyFunction
? never
: WritableAtom<Value, Update> & WithInitialValue<Value>

// read-only derived atom
export function atom<Value>(
read: (get: Getter) => Value | Promise<Value>
): Atom<Value>
export function atom<Value>(read: Read<Value>): Atom<Value>

// invalid read-only derived atom
export function atom(read: AnyFunction): never
Expand All @@ -39,19 +37,19 @@ export function atom<Value>(
): PrimitiveAtom<Value> & WithInitialValue<Value>

export function atom<Value, Update>(
read: Value | ((get: Getter) => Value | Promise<Value>),
write?: (get: Getter, set: Setter, update: Update) => void | Promise<void>
read: Value | Read<Value>,
write?: Write<Update>
) {
const key = `atom${++keyCount}`
const config = {
toString: () => key,
} as WritableAtom<Value, Update> & { init?: Value }
if (typeof read === 'function') {
config.read = read as (get: Getter) => Value | Promise<Value>
config.read = read as Read<Value>
} else {
config.init = read
config.read = (get: Getter) => get(config)
config.write = (get: Getter, set: Setter, update: Update) => {
config.read = (get) => get(config)
config.write = (get, set, update) => {
set(config, typeof update === 'function' ? update(get(config)) : update)
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
export type SetStateAction<Value> = Value | ((prev: Value) => Value)

export type Getter = <Value>(atom: Atom<Value>) => Value
export type Read<Value> = (get: Getter) => Value | Promise<Value>

export type Setter = <Value, Update>(
atom: WritableAtom<Value, Update>,
update: Update
) => void
export type Write<Update> = (
get: Getter,
set: Setter,
update: Update
) => void | Promise<void>

export type Scope = symbol | string | number

Expand All @@ -22,11 +28,11 @@ export type Atom<Value> = {
toString: () => string
debugLabel?: string
scope?: Scope
read: (get: Getter) => Value | Promise<Value>
read: Read<Value>
}

export type WritableAtom<Value, Update> = Atom<Value> & {
write: (get: Getter, set: Setter, update: Update) => void | Promise<void>
write: Write<Update>
onMount?: OnMount<Update>
}

Expand Down

0 comments on commit 6f72fca

Please sign in to comment.