npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lwce/store

v0.0.1

Published

LWC global store

Downloads

4

Readme

@lwce/store

This library provides a global store use to manage shared component state. It allows components to store their data globally and react then the data is updated, either programmatically or from another component.

Alternatives

Othes stores, like Redux or MobX can be obviously used with LWC, but they require extra work from the developers. For example, as Redux deals with unmutable data, the reactive wire adapters cannot be used seamlessly.

On the other hands, the essentials store is tightly integrated with LWC, fully leveraging the reactive membrane. It is compatible with any existing component.

Global Store

The library features a single global store, holding entries referenced by a key. At anytime, one can retrieve an entry by key and manipulate its value.

Entries

An entry in the store is more than just a piece of data, but is a set of values:

interface StoreEntry {
  loading: boolean     // Indicates if the object is being loaded
  data: any            // The actual value, cen be undefined
  error: string		   // The error message, if initialization failed
  initialized: boolean // If the object has already been initialized
}

Accessing Entries

An entry in the store can be retrieved using:
getEntry(key: string, initializer?: Initializer, mode?: Mode): StoreEntry

If the entry already exists in the store, then it is returned as is. If the entry does not yet exist, then it is created and, unless the mode is set to Mode.LAZY, it gets initialized. Mode.FORCE forces the entry to reinitialize.

Note that once an entry is created, it will stay in the store as the same object. Only its content can be modified.

One can check if an entry exists in the store using the following function:
hasEntry(key: string): boolean

An entry can be reinitialized at any time, using:
reinitialize(key: string, initializer?: Initializer): void

Initializing entries

Store entries are initialized using initializers, which return a synchronous value, or a Promise resolving to a value. During the initialization phase and until the Promise resolves, the entry has its state set to "loading". Afterward, the data and error data members are updated with the result of the initialization.

Once the value is initialized, the initialized property is also set to true and stays as this forever. A component can rely on this property to, for example, display a loading icon only the very first time the entry is loaded (not yet initialized), and keep display the existing data on further initialization.

When initializing an object, the store calls the available initializers in order: the explicit one passed to getEntry() or reinitialize (), and then the registered ones. It stops with the first one not returning undefined.

Initializers should be registered globally, before the store entries are accessed, using:
registerInitializer(initializer: Initializer): void

Serializing the store

To support Server Side Rendering in the future, all the store entries are maintained within a global object that can eventually be serialized as JSON. Similarly, the store can be initialized with a JSON payload.

useStore

useStore is a wire adapter that references an object by key in the store. Multiple use of useStore with the same key will reference the same physical object. Because the wire adapters create a reactive membrane on these objects, all the components using the same object is the store will be notified of the changes.

Use useStore

Bellow is the syntax for useStore:
@wire(useStore,{key: 'mykey', initializer?: initializer, mode?: mode}) myvar;

If mode is Mode.FORCE, then the entry will be reinitialized when the component is connected: this forces the data to refresh. If it is Mode.LAZY, then the entry will have to be initialized explicitly. This is useful when the data is not needed right away for rendering the component (ex: data needed for validation, lookup, ...).