All Projects → ionic-team → Stencil Store

ionic-team / Stencil Store

Store is a lightweight shared state library by the StencilJS core team. Implements a simple key/value map that efficiently re-renders components when necessary.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Stencil Store

Libdict
C library of key-value data structures.
Stars: ✭ 234 (+118.69%)
Mutual labels:  dictionary, key-value, map
teaful
🍵 Tiny, easy and powerful React state management
Stars: ✭ 638 (+496.26%)
Mutual labels:  management, state
Dictionary
A dictionary data type with a fast b-tree based search
Stars: ✭ 39 (-63.55%)
Mutual labels:  key-value, dictionary
Jotai
👻 Primitive and flexible state management for React
Stars: ✭ 6,453 (+5930.84%)
Mutual labels:  management, state
Collectable
High-performance immutable data structures for modern JavaScript and TypeScript applications. Functional interfaces, deep/composite operations API, mixed mutability API, TypeScript definitions, ES2015 module exports.
Stars: ✭ 233 (+117.76%)
Mutual labels:  dictionary, map
Store
A beautifully-simple framework-agnostic modern state management library.
Stars: ✭ 204 (+90.65%)
Mutual labels:  management, state
k8s-kv
Kubernetes config maps backed KV store for stateful apps
Stars: ✭ 29 (-72.9%)
Mutual labels:  key-value, state
closeable-map
Application state management made simple: a Clojure map that implements java.io.Closeable.
Stars: ✭ 42 (-60.75%)
Mutual labels:  map, state
Statty
A tiny and unobtrusive state management library for React and Preact apps
Stars: ✭ 516 (+382.24%)
Mutual labels:  management, state
Unstated
State so simple, it goes without saying
Stars: ✭ 7,785 (+7175.7%)
Mutual labels:  management, state
Buckets Js
A complete, fully tested and documented data structure library written in pure JavaScript.
Stars: ✭ 1,128 (+954.21%)
Mutual labels:  dictionary, map
Rki Covid Api
🦠🇩🇪📈 An API for the spread of covid-19 in Germany. Data from Robert-Koch-Institut.
Stars: ✭ 98 (-8.41%)
Mutual labels:  state, map
Redux Data Structures
Reducer factory functions for common data structures: counters, maps, lists (queues, stacks), sets, etc.
Stars: ✭ 157 (+46.73%)
Mutual labels:  dictionary, map
Dictomaton
Finite state dictionaries in Java
Stars: ✭ 124 (+15.89%)
Mutual labels:  dictionary, state
Imtools
Fast and memory-efficient immutable collections and helper data structures
Stars: ✭ 85 (-20.56%)
Mutual labels:  dictionary, map
Hashmap
HashMap JavaScript class for Node.js and the browser. The keys can be anything and won't be stringified
Stars: ✭ 363 (+239.25%)
Mutual labels:  key-value, map
Dyno
Package dyno is a utility to work with dynamic objects at ease.
Stars: ✭ 81 (-24.3%)
Mutual labels:  dictionary, map
Outstated
Simple hooks-based state management for React
Stars: ✭ 102 (-4.67%)
Mutual labels:  management, state
Mdict Java
Query library for Mdict (mdx or mdd) , a popular dictionary file format.
Stars: ✭ 106 (-0.93%)
Mutual labels:  dictionary
Zookeeper
Apache ZooKeeper
Stars: ✭ 10,061 (+9302.8%)
Mutual labels:  key-value

@stencil/store

Store is a lightweight shared state library by the StencilJS core team. It implements a simple key/value map that efficiently re-renders components when necessary.

Highlights:

  • Lightweight
  • Zero dependencies
  • Simple API, like a reactive Map
  • Best performance

Installation

npm install @stencil/store --save-dev

Example

store.ts:

import { createStore } from "@stencil/store";

const { state, onChange } = createStore({
  clicks: 0,
  seconds: 0,
  squaredClicks: 0
});

onChange('clicks', value => {
  state.squaredClicks = value ** 2;
});

export default state;

component.tsx:

import { Component, h } from '@stencil/core';
import state from '../store';

@Component({
  tag: 'app-profile',
})
export class AppProfile {

  componentWillLoad() {
    setInterval(() => state.seconds++, 1000);
  }

  render() {
    return (
      <div>
        <p>
          <MyGlobalCounter />
          <p>
            Seconds: {state.seconds}
            <br />
            Squared Clicks: {state.squaredClicks}
          </p>
        </p>
      </div>
    );
  }
}

const MyGlobalCounter = () => {
  return (
    <button onClick={() => state.clicks++}>
      {state.clicks}
    </button>
  );
};

API

createStore<T>(initialState?: T, shouldUpdate?)

Create a new store with the given initial state. The type is inferred from initialState, or can be passed as the generic type T.

By default, store performs a exact comparison (===) between the new value, and the previous one in order to prevent unnecessary rerenders, however, this behaviour can be changed by providing a shouldUpdate function through the second argument. When this function returns false, the value won't be updated. By providing a custom shouldUpdate() function, applications can create their own fine-grained change detection logic, beyond the default ===. This may be useful for certain use-cases to avoid any expensive re-rendering.

const shouldUpdate = (newValue, oldValue, propChanged) => {
  return JSON.stringify(newValue) !== JSON.stringify(oldValue);
}

Returns a store object with the following properties.

store.state

The state object is proxied, i. e. you can directly get and set properties. If you access the state object in the render function of your component, Store will automatically re-render it when the state object is changed.

Note: Proxy objects are not supported by IE11 (not even with a polyfill), so you need to use the store.get and store.set methods of the API if you wish to support IE11.

store.on(event, listener)

Add a listener to the store for a certain action.

store.onChange(propName, listener)

Add a listener that is called when a specific property changes (either from a set or reset).

store.get(propName)

Get a property's value from the store.

store.set(propName, value)

Set a property's value in the store.

store.reset()

Reset the store to its initial state.

store.use(...subscriptions)

Use the given subscriptions in the store. A subscription is an object that defines one or more of the properties get, set or reset.

store.dispose()

Resets the store and all the internal state of the store that should not survive between tests.

Testing

Like any global state library, state should be disposed between each spec test. Use the dispose() API in the beforeEach hook.

import store from '../store';

beforeEach(() => {
  store.dispose();
});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].