Skip to content

Commit

Permalink
Fix devtools package
Browse files Browse the repository at this point in the history
  • Loading branch information
onurkerimov committed Apr 28, 2024
1 parent f65c085 commit 3b95cfc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
5 changes: 2 additions & 3 deletions packages/devtools/src/devtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ const devtools = (instanceName = 'xoid') => {
})
})

internal.send = (atom: Atom) => {
const internal = atom[INTERNAL]
internal.send = (internal: any) => {
const debugValue = internal.debugValue
if (debugValue) {
const id = atomMap[debugValue].map.get(atom)
const id = atomMap[debugValue].map.get(internal)
const regKey = id ? `${debugValue}-${id}` : debugValue
$registry.focus((s) => s[regKey]).set(internal.get())
}
Expand Down
24 changes: 11 additions & 13 deletions packages/xoid/src/atom.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Atom, Stream, Init, GetState, Actions } from './internal/types'
import type { Atom, Stream, Init, Actions } from './internal/types'
import { createAtom, createInternal, tools } from './internal/utils'
import { createSelector } from './internal/createSelector'

Expand All @@ -7,22 +7,20 @@ import { createSelector } from './internal/createSelector'
* Second argument can be used to attach actions to the atom.
* @see [xoid.dev/docs/quick-tutorial](https://xoid.dev/docs/quick-tutorial)
*/
export function atom<T>(): Stream<T>
export function atom<T>(init: Init<T>): Atom<T>
export function atom<T, U>(init: Init<T>, getActions?: (atom: Atom<T>) => U): Atom<T> & Actions<U>
export function atom<T, U = undefined>(init?: Init<T>, getActions?: (atom: Atom<T>) => U) {
const isFunction = typeof init === 'function'
const internal = createInternal(
(isFunction ? undefined : init) as T,
(() => tools.send(a)) as any
)
export function atom<T, U>(init: Init<T>, getActions?: (a: Atom<T>) => U): Atom<T> & Actions<U>
export function atom<T>(): Stream<T>
export function atom<T, U = undefined>(init?: Init<T>, getActions?: (a: Atom<T>) => U) {
// @ts-ignore
const internal = (typeof init === 'function' ? createSelector : createInternal)(init)
// @ts-ignore
internal.isStream = !arguments.length
if (isFunction) createSelector(internal, init as (get: GetState) => T)
const a = createAtom(internal, getActions)
return a

// @ts-ignore
return createAtom(internal, getActions)
}

atom.plugins = [] as ((atom: Atom<any>) => void)[]
atom.plugins = [] as ((a: Atom<any>) => void)[]

// intentionally untyped
;(atom as any).internal = tools
6 changes: 4 additions & 2 deletions packages/xoid/src/internal/createSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Internal, tools } from './utils'
import { createInternal, tools } from './utils'
import { GetState } from './types'
import { createEvent } from './createEvent'
import { INTERNAL } from './createFocus'
Expand All @@ -16,7 +16,8 @@ export const createGetState =
return read[INTERNAL].get()
}

export const createSelector = <T,>(internal: Internal<T>, init: (get: GetState) => T) => {
export const createSelector = <T,>(init: (get: GetState) => T) => {
const internal = createInternal()
const { get, set, listeners } = internal
const e = createEvent()

Expand All @@ -41,4 +42,5 @@ export const createSelector = <T,>(internal: Internal<T>, init: (get: GetState)
if (isPending) evaluate()
return get()
}
return internal
}
8 changes: 5 additions & 3 deletions packages/xoid/src/internal/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,24 @@ export const subscribeInternal =
}
}

export const createInternal = <T,>(value: T, send?: () => void): Internal<T> => {
export const createInternal = <T,>(value?: T): Internal<T> => {
const listeners = new Set<() => void>()
return {
const self = {
listeners,
get: () => value as T,
set: (nextValue: T) => {
if (value === nextValue) return
value = nextValue
send && send()
// Used by devtools
tools.send(self)
listeners.forEach((listener) => listener())
},
subscribe: (listener: () => void) => {
listeners.add(listener)
return () => void listeners.delete(listener)
},
}
return self
}

export function createAtom<T>(internal: Internal<T>, getActions?: any) {
Expand Down

0 comments on commit 3b95cfc

Please sign in to comment.