Skip to content

Commit

Permalink
fix type and export observe utility
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Jan 25, 2025
1 parent 218a2b2 commit e59ef51
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jotai-effect",
"description": "👻🔁",
"version": "1.1.0",
"version": "1.1.1",
"type": "module",
"author": "David Maskasky",
"contributors": [
Expand Down
18 changes: 12 additions & 6 deletions src/atomEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import type { Atom, Getter, Setter } from 'jotai/vanilla'
import { atom } from 'jotai/vanilla'

type Cleanup = () => void

type GetterWithPeek = Getter & { peek: Getter }

type SetterWithRecurse = Setter & { recurse: Setter }
export type Effect = Parameters<typeof atomEffect>[0]
export type AtomWithEffect<T extends Atom<unknown> = Atom<void>> = T & {
effect: Effect
}

export type Effect = (
get: GetterWithPeek,
set: SetterWithRecurse
) => void | Cleanup

type Ref = {
/** inProgress */
i: number
Expand All @@ -31,7 +35,7 @@ type Ref = {

export function atomEffect(
effect: (get: GetterWithPeek, set: SetterWithRecurse) => void | Cleanup
): AtomWithEffect {
): Atom<void> & { effect: Effect } {
const refreshAtom = atom(0)
const refAtom = atom(
() => ({ i: 0 }) as Ref,
Expand Down Expand Up @@ -119,7 +123,9 @@ export function atomEffect(
setLabel(refAtom, 'ref')
setLabel(baseAtom, 'base')
}
const effectAtom = atom((get) => void get(baseAtom)) as AtomWithEffect
const effectAtom = atom((get) => void get(baseAtom)) as Atom<void> & {
effect: Effect
}
effectAtom.effect = effect
return effectAtom
function refresh(ref: Ref) {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { atomEffect } from './atomEffect'
export { withAtomEffect } from './withAtomEffect'
export { observe } from './observe'
6 changes: 3 additions & 3 deletions src/withAtomEffect.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Atom } from 'jotai/vanilla'
import type { AtomWithEffect, Effect } from './atomEffect'
import type { Effect } from './atomEffect'
import { atomEffect } from './atomEffect'

export function withAtomEffect<T extends Atom<unknown>>(
targetAtom: T,
effect: Effect
): AtomWithEffect<T> {
): T & { effect: Effect } {
const effectAtom = atomEffect((get, set) => {
const getter = ((a) =>
a === targetWithEffect ? get(targetAtom) : get(a)) as typeof get
Expand All @@ -19,7 +19,7 @@ export function withAtomEffect<T extends Atom<unknown>>(
effectAtom.debugPrivate = true
}
const descriptors = Object.getOwnPropertyDescriptors(
targetAtom as AtomWithEffect<T>
targetAtom as T & { effect: Effect }
)
descriptors.read.value = (get) => {
try {
Expand Down

0 comments on commit e59ef51

Please sign in to comment.