Skip to content

Commit

Permalink
fix stores package imports from atoms package
Browse files Browse the repository at this point in the history
  • Loading branch information
bowheart committed Jan 6, 2025
1 parent 0b82521 commit ccf4be4
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 141 deletions.
10 changes: 0 additions & 10 deletions packages/atoms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js",
"default": "./dist/cjs/index.js"
},
"./factories": {
"import": "./dist/esm/factories",
"require": "./dist/cjs/factories",
"default": "./dist/cjs/factories"
},
"./utils": {
"import": "./dist/esm/utils",
"require": "./dist/cjs/utils",
"default": "./dist/cjs/utils"
}
},
"files": [
Expand Down
4 changes: 3 additions & 1 deletion packages/atoms/src/classes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export * from './templates/AtomTemplate'
export * from './templates/AtomTemplateBase'
export * from './AtomApi'
export * from './Ecosystem'
export * from './GraphNode'
export { ExternalNode, GraphNode } from './GraphNode'
export * from './MappedSignal'
export * from './SelectorInstance'
export * from './SignalInstance'
export * from './ZeduxPlugin'
25 changes: 25 additions & 0 deletions packages/atoms/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import {
destroyNodeFinish,
destroyNodeStart,
scheduleDependents,
} from './classes/GraphNode'
import { createInjector } from './factories/createInjector'
import {
destroyBuffer,
flushBuffer,
getEvaluationContext,
startBuffer,
} from './utils/evaluationContext'

export * from '@zedux/core'
export * from './classes/index'
export * from './factories/index'
export * from './injectors/index'
export { getEcosystem, getInternals, setInternals, wipe } from './store/index'
export * from './types/index'

// These are very obfuscated on purpose. Don't use! They're for Zedux packages.
export const zi = {
b: destroyNodeStart,
c: createInjector,
d: destroyBuffer,
e: destroyNodeFinish,
f: flushBuffer,
g: getEvaluationContext,
s: startBuffer,
u: scheduleDependents,
}
2 changes: 2 additions & 0 deletions packages/atoms/src/utils/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const OutOfRange = 8 // not a flag; use a value bigger than any flag
/**
* The InternalEvaluationTypes. These get translated to user-friendly
* EvaluationTypes by `ecosytem.why`.
*
* IMPORTANT! Keep these in sync with `@zedux/stores/atoms-port.ts`
*/
export const Invalidate = 1
export const Destroy = 2
Expand Down
2 changes: 1 addition & 1 deletion packages/atoms/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"lib": ["DOM", "ESNext"]
},
"include": ["../stores", "./src", "./test", "../../global.d.ts"]
"include": ["./src", "./test", "../../global.d.ts"]
}
4 changes: 2 additions & 2 deletions packages/stores/src/AtomApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AtomInstanceTtl, Prettify } from '@zedux/atoms'
import { is, Store } from '@zedux/core'
import { AtomInstanceTtl, Prettify } from '@zedux/atoms/types/index'
import { prefix } from '@zedux/atoms/utils/general'
import { prefix } from './atoms-port'
import { AtomApiGenerics } from './types'

export class AtomApi<G extends AtomApiGenerics> {
Expand Down
118 changes: 20 additions & 98 deletions packages/stores/src/AtomInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,28 @@ import {
AnyAtomGenerics,
} from './types'
import {
AtomInstance as NewAtomInstance,
Cleanup,
Ecosystem,
ExportsInfusedSetter,
PromiseState,
PromiseStatus,
DehydrationFilter,
NodeFilter,
DehydrationOptions,
InternalEvaluationReason,
ExplicitEvents,
Transaction,
} from '@zedux/atoms/types/index'
import { Invalidate, prefix, PromiseChange } from '@zedux/atoms/utils/general'
import { pluginActions } from '@zedux/atoms/utils/plugin-actions'
ZeduxPlugin,
zi,
} from '@zedux/atoms'
import { AtomApi } from './AtomApi'
import {
Invalidate,
prefix,
PromiseChange,
getErrorPromiseState,
getInitialPromiseState,
getSuccessPromiseState,
} from '@zedux/atoms/utils/promiseUtils'
import { InjectorDescriptor } from '@zedux/atoms/utils/types'
import { Ecosystem } from '@zedux/atoms/classes/Ecosystem'
import { AtomApi } from './AtomApi'
import { AtomInstance as NewAtomInstance } from '@zedux/atoms/classes/instances/AtomInstance'
import { AtomTemplateBase } from '@zedux/atoms/classes/templates/AtomTemplateBase'
import {
destroyNodeFinish,
destroyNodeStart,
normalizeNodeFilter,
scheduleDependents,
} from '@zedux/atoms/classes/GraphNode'
import {
destroyBuffer,
flushBuffer,
getEvaluationContext,
startBuffer,
} from '@zedux/atoms/utils/evaluationContext'
InjectorDescriptor,
} from './atoms-port'
import { AtomTemplate } from './AtomTemplate'

const StoreState = 1
Expand Down Expand Up @@ -144,7 +131,7 @@ export class AtomInstance<
* @see GraphNode.destroy
*/
public destroy(force?: boolean) {
if (!destroyNodeStart(this, force)) return
if (!zi.b(this, force)) return

// Clean up effect injectors first, then everything else
const nonEffectInjectors: InjectorDescriptor[] = []
Expand All @@ -160,7 +147,7 @@ export class AtomInstance<
})
this._subscription?.unsubscribe()

destroyNodeFinish(this)
zi.e(this)
}

/**
Expand Down Expand Up @@ -228,76 +215,11 @@ export class AtomInstance<
meta?: any
): G['State'] => this.store.setStateDeep(settable, meta)

/**
* @see GraphNode.d
*/
public d(options?: DehydrationFilter) {
if (!this.f(options)) return

const { t } = this
const state = this.get()
const transform =
(typeof options === 'object' &&
!is(options, AtomTemplateBase) &&
(options as DehydrationOptions).transform) ??
true

return transform && t.dehydrate ? t.dehydrate(state) : state
}

/**
* @see GraphNode.f
*/
public f(options?: NodeFilter) {
const { id, t } = this
const lowerCaseId = id.toLowerCase()
const {
exclude = [],
excludeFlags = [],
include = [],
includeFlags = [],
} = normalizeNodeFilter(options)

if (
exclude.some(templateOrKey =>
typeof templateOrKey === 'string'
? lowerCaseId.includes(templateOrKey.toLowerCase())
: is(templateOrKey, AtomTemplateBase) &&
t.key === (templateOrKey as AtomTemplateBase).key
) ||
excludeFlags.some(flag => t.flags?.includes(flag))
) {
return false
}

if (
(!include.length && !includeFlags.length) ||
include.some(templateOrKey =>
typeof templateOrKey === 'string'
? lowerCaseId.includes(templateOrKey.toLowerCase())
: is(templateOrKey, AtomTemplateBase) &&
t.key === (templateOrKey as AtomTemplateBase).key
) ||
includeFlags.some(flag => t.flags?.includes(flag))
) {
return true
}

return false
}

/**
* @see GraphNode.h
*/
public h(val: any) {
this.setState(this.t.hydrate ? this.t.hydrate(val) : val)
}

/**
* @see GraphNode.j
*/
public j() {
const { n, s } = getEvaluationContext()
const { n, s } = zi.g()
this._nextInjectors = []
this._isEvaluating = true

Expand All @@ -308,7 +230,7 @@ export class AtomInstance<
// real ideal is to move off stores completely in favor of signals.
Store._scheduler = this.e._scheduler

startBuffer(this)
zi.s(this)

try {
const newFactoryResult = this._eval()
Expand Down Expand Up @@ -361,7 +283,7 @@ export class AtomInstance<
injector.cleanup?.()
})

destroyBuffer(n, s)
zi.d(n, s)

throw err
} finally {
Expand Down Expand Up @@ -389,7 +311,7 @@ export class AtomInstance<

if (this.l !== 'Initializing') {
// let this.i flush updates after status is set to Active
flushBuffer(n, s)
zi.f(n, s)
}
}

Expand Down Expand Up @@ -472,11 +394,11 @@ export class AtomInstance<
oldState: G['State'] | undefined,
action: ActionChain
) {
scheduleDependents({ p: oldState, r: this.w, s: this }, false)
zi.u({ p: oldState, r: this.w, s: this }, false)

if (this.e._mods.stateChanged) {
this.e.modBus.dispatch(
pluginActions.stateChanged({
ZeduxPlugin.actions.stateChanged({
action,
node: this,
newState,
Expand Down Expand Up @@ -527,7 +449,7 @@ export class AtomInstance<
const state: PromiseState<any> = getInitialPromiseState(currentState?.data)
this._promiseStatus = state.status

scheduleDependents({ s: this, t: PromiseChange }, true, true)
zi.u({ s: this, t: PromiseChange }, true, true)

return state as unknown as G['State']
}
Expand Down
3 changes: 1 addition & 2 deletions packages/stores/src/IonTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { injectAtomGetters } from '@zedux/atoms/injectors/injectAtomGetters'
import { AtomConfig } from '@zedux/atoms/types/index'
import { AtomConfig, injectAtomGetters } from '@zedux/atoms'
import { AtomInstance } from './AtomInstance'
import { AtomTemplate } from './AtomTemplate'
import { ion } from './ion'
Expand Down
2 changes: 1 addition & 1 deletion packages/stores/src/atom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Store, StoreStateType } from '@zedux/core'
import { AtomConfig, AtomApiPromise, PromiseState } from '@zedux/atoms'
import { Store, StoreStateType } from '@zedux/core'
import { AtomApi } from './AtomApi'
import { AtomTemplate, AtomTemplateRecursive } from './AtomTemplate'
import { AtomValueOrFactory } from './types'
Expand Down
52 changes: 52 additions & 0 deletions packages/stores/src/atoms-port.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// This file contains lots of code and types duplicated from the `@zedux/atoms`
// package. This is the best way to get them in the `tsc` build without breaking
// type compatibility with the external `@zedux/atoms` package. That breakage
// happens when we give TS a `@zedux/atoms` `paths` alias and let it pull in
// duplicated classes e.g. in `dist/esm/atoms/classes/...`
import { PromiseState } from '@zedux/atoms'

export type InjectorDescriptor<T = any> = T extends undefined
? {
cleanup?: () => void
result?: T
type: string
}
: {
cleanup?: () => void
result: T
type: string
}

export const prefix = '@@zedux'

/**
* IMPORTANT! Keep these in sync with `@zedux/atoms/utils/general.ts`
*/
export const Invalidate = 1
export const Destroy = 2
export const PromiseChange = 3
export const EventSent = 4

export const getErrorPromiseState = <T>(error: Error): PromiseState<T> => ({
error,
isError: true,
isLoading: false,
isSuccess: false,
status: 'error',
})

export const getInitialPromiseState = <T>(data?: T): PromiseState<T> => ({
data,
isError: false,
isLoading: true,
isSuccess: false,
status: 'loading' as const,
})

export const getSuccessPromiseState = <T>(data: T): PromiseState<T> => ({
data,
isError: false,
isLoading: false,
isSuccess: true,
status: 'success',
})
23 changes: 12 additions & 11 deletions packages/stores/src/injectPromise.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { injectEffect, injectMemo, injectRef } from '@zedux/atoms'
import { detailedTypeof, RecursivePartial, Store } from '@zedux/core'
import {
getErrorPromiseState,
getInitialPromiseState,
getSuccessPromiseState,
} from '@zedux/atoms/utils/promiseUtils'
import {
injectEffect,
injectMemo,
InjectorDeps,
InjectPromiseConfig,
injectRef,
InjectStoreConfig,
injectWhy,
PromiseState,
} from '@zedux/atoms/types/index'
import { Invalidate } from '@zedux/atoms/utils/general'
import { readInstance } from '@zedux/atoms/utils/evaluationContext'
} from '@zedux/atoms'
import { detailedTypeof, RecursivePartial, Store } from '@zedux/core'
import {
getErrorPromiseState,
getInitialPromiseState,
getSuccessPromiseState,
} from './atoms-port'
import { AtomApi } from './AtomApi'
import { api } from './api'
import { injectStore } from './injectStore'
Expand Down Expand Up @@ -109,7 +110,7 @@ export const injectPromise: {
if (
runOnInvalidate &&
// injectWhy is an unrestricted injector - using it conditionally is fine:
readInstance().w.some(reason => reason.t === Invalidate)
injectWhy().some(reason => reason.type === 'cache invalidated')
) {
refs.current.counter++
}
Expand Down
Loading

0 comments on commit ccf4be4

Please sign in to comment.