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

feat: Async store and compute() function #14

Merged
merged 2 commits into from
Feb 16, 2023
Merged

feat: Async store and compute() function #14

merged 2 commits into from
Feb 16, 2023

Conversation

mnasyrov
Copy link
Owner

@mnasyrov mnasyrov commented Jan 24, 2023

Async store

This is a new implementation of the store. The previous one was based on BehaviourSubject which emitted synchronous notifications for state updates.

The new store is based on its own subscription collection and schedules a notification of subscribers on the next micro task. Also subscribers receive only on notification with a current state. All subsequent events are dropped because they don't matter.

Behaviour of a subscription is the same: a new subscriber receives a current state on subscribing synchronously.

Subscribers can receive a current state synchronously at any time by invoking store.notify() manually.

compute()

This function creates a computable query which calculates its values by provided "computation" function and dependencies.

Rules of "Computation" function:

  • it must have no side effects
  • it recalculates only when specified dependencies are updated
  • its "formula" may have other sources of values, however they don't trigger updates

"Computation" function provides a resolver for using a dependency withing a calculation expression.

Dependency can be declared explicitly as an array by the second argument. It has the following advantages:

  • Faster dependency subscription
  • Ability to specify extra queries or observables as dependencies

A custom value comparator can be specified by "options" object as the second argument.
It helps to decide if a new value differs from a previous one in complex cases.

Example

const greeting = createStore('Hello');
const username = createStore('World');

// Dependency are implicitly resolved
const message = compute((get) => get(greeting) + ' ' + get(username) + '!');

// Dependency declared explicitly
const messageUppercase = compute(() => message.get().toUpperCase(), [message]);

expect(message.get()).toBe('Hello World!');
expect(messageUppercase.get()).toBe('HELLO WORLD!');

Perfomance

Screenshot 2023-02-15 at 07 45 34

@mnasyrov mnasyrov self-assigned this Jan 24, 2023
@coveralls
Copy link

coveralls commented Jan 24, 2023

Pull Request Test Coverage Report for Build 4180689321

  • 174 of 174 (100.0%) changed or added relevant lines in 7 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 4068088631: 0.0%
Covered Lines: 512
Relevant Lines: 512

💛 - Coveralls

@mnasyrov mnasyrov changed the base branch from main to 2.x February 16, 2023 21:38
@mnasyrov mnasyrov changed the title feat: compute() function feat: Async store and compute() function Feb 16, 2023
@mnasyrov mnasyrov merged commit 5041b5c into 2.x Feb 16, 2023
@mnasyrov mnasyrov deleted the feat/compute branch February 16, 2023 21:48
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.

2 participants