Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derived Stores #40

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open

Derived Stores #40

wants to merge 25 commits into from

Conversation

crutchcorn
Copy link
Member

@crutchcorn crutchcorn commented Feb 7, 2024

This PR implements an initial Derived class implementation:

const count = new Store(10);

const doubleCount = new Derived([count], () => {
  return count.state * 2;
})

doubleCount.subscribe(() => console.log(doubleCount.state));
count.setState(() => 20);

This Derived implementation even solves the diamond problem:

"a" is the root node with "b" and "c" relying on it. Then "d" relies on both "b" and "c"

Where writes to "a" will call "d" twice (with a flicker of incorrect data known as a "Glitch") because of the dual-derived nature. Our implementation does not have this problem and therefore can be considered "glitch-free"

This is a push-based/hot signals implementation

Benchmarks

Due to the relatively naive nature of this code (I wrote it in one night at ~midnight) there are performance implication in using this. Namely, in our benchmarks we are a far cry from Angular or Solid's implementation, but come close to Vue's implementation:

@crutchcorn
Copy link
Member Author

image

I've increased our performance and reduced our bundle size. We're no-longer the slowest on our own benchmark and are even 1.5x faster than Vue:

image

@tannerlinsley
Copy link
Contributor

Woo! 🏎️

@eneajaho
Copy link

Angular is the fastest, nice 😄!

Copy link

nx-cloud bot commented Feb 17, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit fa65a7e. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

@lucasmotta
Copy link

Out of curiosity, what's the state of this PR and what's stopping it from being reviewed/merged?

@AndrejNemec
Copy link

Out of curiosity, what's the state of this PR and what's stopping it from being reviewed/merged?

+1

crutchcorn and others added 2 commits August 12, 2024 14:33
# Conflicts:
#	.eslintrc.cjs
#	knip.json
#	package.json
#	packages/solid-store/package.json
#	packages/store/package.json
#	packages/store/src/index.ts
#	packages/store/src/tests/index.test.tsx
#	packages/store/tests/derived.bench.ts
#	packages/store/tests/derived.test.ts
#	packages/store/tests/effect.test.ts
#	packages/store/tests/index.test.tsx
#	packages/store/tests/store.test.ts
#	packages/vue-store/package.json
#	pnpm-lock.yaml
Copy link

pkg-pr-new bot commented Aug 12, 2024

commit: fa65a7e

@tanstack/angular-store

pnpm add https://pkg.pr.new/@tanstack/angular-store@40

@tanstack/react-store

pnpm add https://pkg.pr.new/@tanstack/react-store@40

@tanstack/solid-store

pnpm add https://pkg.pr.new/@tanstack/solid-store@40

@tanstack/store

pnpm add https://pkg.pr.new/@tanstack/store@40

@tanstack/svelte-store

pnpm add https://pkg.pr.new/@tanstack/svelte-store@40

@tanstack/vue-store

pnpm add https://pkg.pr.new/@tanstack/vue-store@40

Open in Stackblitz

// What store called the current update, if any
_whatStoreIsCurrentlyInUse: Store<unknown> | null = null

constructor(deps: Deps, fn: () => TState, options?: DerivedOptions<TState>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be better if this had a flat options style like useQuery now does?

new Derived({
  storeKey: [],
  storeFn: () => alert("expensive")
  ...options
})

@tannerlinsley
Copy link
Contributor

tannerlinsley commented Aug 17, 2024 via email

ZainW and others added 2 commits September 28, 2024 22:13
* feat: add lazyness to derived

* ci: apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
@crutchcorn
Copy link
Member Author

crutchcorn commented Nov 25, 2024

I was originally holding off as performance didn't seem up-to-snuff, but comparing it against the TS39 Polyfill, it seems that we're roughly double the speed of that, so I want to merge this in so we can start using ASAP

Source of the benchmarks:

https://github.com/crutchcorn/js-reactivity-benchmark/tree/tanstack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants