Remove Box in favor of Atom

This commit is contained in:
Aria Minaei 2022-12-01 15:37:19 +01:00
parent 2bd1dc85a9
commit c354a602a4
13 changed files with 63 additions and 49 deletions

View file

@ -115,7 +115,7 @@ class Scope {
/**
* Wraps an object whose (sub)properties can be individually tracked.
*/
export default class Atom<State extends {}> implements IdentityPrismProvider {
export default class Atom<State> implements IdentityPrismProvider {
private _currentState: State
/**
* @internal
@ -130,6 +130,8 @@ export default class Atom<State extends {}> implements IdentityPrismProvider {
*/
readonly pointer: Pointer<State>
readonly prism: Prism<State> = this.getIdentityPrism([]) as $IntentionalAny
constructor(initialState: State) {
this._currentState = initialState
this._rootScope = new Scope(undefined, [])
@ -141,7 +143,7 @@ export default class Atom<State extends {}> implements IdentityPrismProvider {
*
* @param newState - The new state of the atom.
*/
setState(newState: State) {
set(newState: State) {
const oldState = this._currentState
this._currentState = newState
@ -150,11 +152,23 @@ export default class Atom<State extends {}> implements IdentityPrismProvider {
/**
* Gets the current state of the atom.
* @deprecated
*/
getState() {
return this._currentState
}
get() {
return this.getState()
}
/**
* @deprecated
*/
setState(newState: State) {
this.set(newState)
}
/**
* Gets the state of the atom at `path`.
*/

View file

@ -6,8 +6,6 @@
export type {IdentityPrismProvider} from './Atom'
export {default as Atom, val, pointerToPrism} from './Atom'
export {default as Box} from './Box'
export type {IBox} from './Box'
export {isPrism} from './prism/Interface'
export type {Prism} from './prism/Interface'
export {default as iterateAndCountTicks} from './prism/iterateAndCountTicks'

View file

@ -4,7 +4,7 @@ import App from './App'
import type {ToolsetConfig} from '@theatre/studio'
import studio from '@theatre/studio'
import extension from '@theatre/r3f/dist/extension'
import {Box, prism, Ticker, val} from '@theatre/dataverse'
import {Atom, prism, Ticker, val} from '@theatre/dataverse'
/**
* Let's take a look at how we can use `prism`, `Ticker`, and `val` from Theatre.js's Dataverse library
@ -26,7 +26,7 @@ studio.extend({
id: '@theatre/hello-world-extension',
toolbars: {
global(set, studio) {
const exampleBox = new Box('mobile')
const exampleBox = new Atom('mobile')
const untapFn = prism<ToolsetConfig>(() => [
{

View file

@ -4,8 +4,8 @@
* @packageDocumentation
*/
import type {Prism} from '@theatre/dataverse'
import {Box} from '@theatre/dataverse'
import type { Prism} from '@theatre/dataverse';
import {Atom} from '@theatre/dataverse'
import {prism, val} from '@theatre/dataverse'
import {findIndex} from 'lodash-es'
import queueMicrotask from 'queue-microtask'
@ -58,17 +58,17 @@ export function usePrism<T>(
debugLabel?: string,
): T {
const fnAsCallback = useCallback(fn, deps)
const boxRef = useRef<Box<typeof fn>>(null as $IntentionalAny)
if (!boxRef.current) {
boxRef.current = new Box(fnAsCallback)
const atomRef = useRef<Atom<typeof fn>>(null as $IntentionalAny)
if (!atomRef.current) {
atomRef.current = new Atom(fnAsCallback)
} else {
boxRef.current.set(fnAsCallback)
atomRef.current.setState(fnAsCallback)
}
const prsm = useMemo(
() =>
prism(() => {
const fn = boxRef.current.prism.getValue()
const fn = atomRef.current.prism.getValue()
return fn()
}),
[],