Skip to content

Commit

Permalink
feat!: implement proxies, signals, and mapped signals (#147)
Browse files Browse the repository at this point in the history
@affects atoms, core, machines, react, stores
  • Loading branch information
bowheart authored Jan 6, 2025
1 parent febd154 commit 6785d58
Show file tree
Hide file tree
Showing 102 changed files with 5,842 additions and 1,438 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"type": "node",
"request": "launch",
"runtimeExecutable": "yarn",
"args": ["jest", "--runInBand"],
"args": ["jest", "--runInBand", "--coverage=false"],
"console": "integratedTerminal",
"cwd": "${workspaceRoot}",
"internalConsoleOptions": "neverOpen"
},
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
2 changes: 1 addition & 1 deletion bench/src/frameworks/zedux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const zeduxFramework: ReactiveFramework = {
)

return {
write: v => instance.setState(v),
write: v => instance.set(v),
read: () => ecosystem.live.get(instance),
}
},
Expand Down
4 changes: 4 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const jestCompilerOptions: Omit<RawCompilerOptions, 'paths'> & {
'@zedux/machines/*': ['./packages/machines/src/*'],
'@zedux/react': ['./packages/react/src'],
'@zedux/react/*': ['./packages/react/src/*'],
'@zedux/stores': ['./packages/stores/src'],
'@zedux/stores/*': ['./packages/stores/src/*'],
},
}

Expand All @@ -40,6 +42,8 @@ const config: Config.InitialOptions = {
'<rootDir>/packages/machines/test',
'<rootDir>/packages/react/src',
'<rootDir>/packages/react/test',
'<rootDir>/packages/core/src',
'<rootDir>/packages/core/test',
],
setupFilesAfterEnv: ['./jest.setup.ts'],
testEnvironment: 'jsdom',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@
},
"workspaces": [
"packages/*"
]
],
"packageManager": "yarn@1.22.4+sha512.a1833b862fe52169bd6c2a033045a07df5bc6a23595c259e675fed1b2d035ab37abe6ce309720abb6636d68f03615054b6292dc0a70da31c8697fda228b50d18"
}
16 changes: 9 additions & 7 deletions packages/atoms/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `@zedux/atoms`

The core atomic model of Zedux. This is a standalone package, meaning it's the only package you need to install to use Zedux's atomic model. It includes the Zedux core store package as well as all APIs related to atoms and ecosystems.
The core atomic model of Zedux. This is a standalone package, meaning it's the only package you need to install to use Zedux's atomic model. It includes the Zedux core store package as well as all APIs related to signals, atoms, and ecosystems.

This package is framework-independent, though many of its APIs are heavily inspired by React.

Expand Down Expand Up @@ -32,10 +32,10 @@ import { atom, createEcosystem } from '@zedux/atoms'
const greetingAtom = atom('greeting', 'Hello, World!')
const ecosystem = createEcosystem({ id: 'root' })

const instance = ecosystem.getInstance(greetingAtom)
const instance = ecosystem.getNode(greetingAtom)

instance.store.subscribe(newState => console.log('state updated:', newState))
instance.setState('Goodbye, World!')
instance.on('change', ({ newState }) => console.log('state updated:', newState))
instance.set('Goodbye, World!')
instance.destroy()
```

Expand All @@ -51,12 +51,13 @@ On top of this, `@zedux/atoms` exports the following APIs and many helper types

- [`AtomApi`](https://omnistac.github.io/zedux/docs/api/classes/AtomApi)
- [`AtomInstance`](https://omnistac.github.io/zedux/docs/api/classes/AtomInstance)
- [`AtomInstanceBase`](https://omnistac.github.io/zedux/docs/api/classes/AtomInstanceBase)
- [`AtomTemplate`](https://omnistac.github.io/zedux/docs/api/classes/AtomTemplate)
- [`AtomTemplateBase`](https://omnistac.github.io/zedux/docs/api/classes/AtomTemplateBase)
- [`Ecosystem`](https://omnistac.github.io/zedux/docs/api/classes/Ecosystem)
- [`IonTemplate`](https://omnistac.github.io/zedux/docs/api/classes/IonTemplate)
- [`SelectorCache`](https://omnistac.github.io/zedux/docs/api/classes/SelectorCache)
- [`MappedSignal`](https://omnistac.github.io/zedux/docs/api/classes/MappedSignal)
- [`SelectorInstance`](https://omnistac.github.io/zedux/docs/api/classes/SelectorInstance)
- [`Signal`](https://omnistac.github.io/zedux/docs/api/classes/Signal)
- [`ZeduxPlugin`](https://omnistac.github.io/zedux/docs/api/classes/ZeduxPlugin)

### Factories
Expand All @@ -76,11 +77,12 @@ On top of this, `@zedux/atoms` exports the following APIs and many helper types
- [`injectCallback()`](https://omnistac.github.io/zedux/docs/api/injectors/injectCallback)
- [`injectEffect()`](https://omnistac.github.io/zedux/docs/api/injectors/injectEffect)
- [`injectInvalidate()`](https://omnistac.github.io/zedux/docs/api/injectors/injectInvalidate)
- [`injectMappedSignal()`](https://omnistac.github.io/zedux/docs/api/injectors/injectMappedSignal)
- [`injectMemo()`](https://omnistac.github.io/zedux/docs/api/injectors/injectMemo)
- [`injectPromise()`](https://omnistac.github.io/zedux/docs/api/injectors/injectPromise)
- [`injectRef()`](https://omnistac.github.io/zedux/docs/api/injectors/injectRef)
- [`injectSelf()`](https://omnistac.github.io/zedux/docs/api/injectors/injectSelf)
- [`injectStore()`](https://omnistac.github.io/zedux/docs/api/injectors/injectStore)
- [`injectSignal()`](https://omnistac.github.io/zedux/docs/api/injectors/injectSignal)
- [`injectWhy()`](https://omnistac.github.io/zedux/docs/api/injectors/injectWhy)

### Utils
Expand Down
12 changes: 6 additions & 6 deletions packages/atoms/src/classes/AtomApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { is, Store } from '@zedux/core'
import { is } from '@zedux/core'
import {
AtomInstanceTtl,
AtomApiGenerics,
Expand All @@ -11,14 +11,14 @@ export class AtomApi<G extends AtomApiGenerics> {

public exports?: G['Exports']
public promise: G['Promise']
public store: G['Store']
public signal: G['Signal']
public ttl?: AtomInstanceTtl | (() => AtomInstanceTtl)
public value: G['State'] | G['Store']
public value: G['State'] | G['Signal']

constructor(value: AtomApi<G> | G['Store'] | G['State']) {
constructor(value: AtomApi<G> | G['Signal'] | G['State']) {
this.promise = undefined as G['Promise']
this.value = value as G['Store'] | G['State']
this.store = (is(value, Store) ? value : undefined) as G['Store']
this.value = value as G['Signal'] | G['State']
this.signal = (value?.izn ? value : undefined) as G['Signal']

if (is(value, AtomApi)) {
Object.assign(this, value as AtomApi<G>)
Expand Down
Loading

0 comments on commit 6785d58

Please # to comment.