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

@slimlib/store

v1.6.2

Published

Simple Proxy-based store for SPA

Downloads

38

Readme

Store

Proxy-based store for SPAs.

  1. Simple
  2. Relatively fast
  3. Small size (less than 1Kb minified not gzipped)
  4. Typescript support

Changelog

Installation

Using npm:

npm install --save-dev @slimlib/store

Usage

React

import { createStore, useStore } from '@slimlib/store/react';

// create store
const [state, store] = createStore();

// action
function doSomething() {
    state.field = value;
}

//component
function Component() {
    const state = useStore(store);
    
    // use state
}

Preact

import { createStore, useStore } from '@slimlib/store/preact';

// create store
const [state, store] = createStore();

// action
function doSomething() {
    state.field = value;
}

//component
function Component() {
    const state = useStore(store);
    
    // use state
}

Svelte

In store

import { createStore } from '@slimlib/store/svelte';

// create store
const [state, subscribe] = createStore();

// action
export function doSomething() {
    state.field = value;
}

export default { subscribe };

In component

<script>
import { storeName } from './stores/storeName';
</script>

// use it in reactive way for reading data
$storeName

Angular

In store

import { SlimlibStore } from '@slimlib/store/angular';

// create store
@Injectable()
export class StoreName extends SlimlibStore<State> {
    constructor() {
        super(/*Initial state*/{ field: 123 }});
    }

    // selectors
    field = this.select(state => state.field);

    // actions
    doSomething() {
        this.state.field = value;
    }
}

rxjs

import { createStore, toObservable } from '@slimlib/store/rxjs';

// create store
const [state, store] = createStore();

// action
export function doSomething() {
    state.field = value;
}

// observable
export const state$ = toObservable(store);

API

main and core exports

createStoreFactory(notifyAfterCreation: boolean)

Returns createStore factory (see next) which notifies immediately after creating store if notifyAfterCreation is truthy.

createStore<T>(initialState: T): [T, Store<T>, () => void]

Store factory function that takes initial state and returns proxy object, store and function to notify subscribers. Proxy object meant to be left for actions implementations, store is for subscription for changes and notification only for some edge cases when an original object has been changed and listeners have to be notified.

unwrapValue(value: T): T

Unwraps a potential proxy object and returns a plain object if possible or value itself.

Store<T>

type StoreCallback<T> = (value: T) => void;
type UnsubscribeCallback = () => void;
interface Store<T> {
    (cb: StoreCallback<T>): UnsubscribeCallback;
    (): T;
}

Publish/subscribe/read pattern implementation. Meant to be used in components / services that want to subscribe for store changes.

react and preact exports

createStore<T>(initialState: T): [T, Store<T>, () => void]

Store factory created with notifyAfterCreation === false.

useStore<T>(store: Store<T>): Readonly<T>

Function to subscribe to store inside component. Returns current state.

svelte export

createStore<T>(initialState: T): [T, Store<T>, () => void]

Store factory created with notifyAfterCreation === true.

angular export

createStore<T>(initialState: T): [T, Store<T>, () => void]

Store factory created with notifyAfterCreation === false.

toSignal<T>(store: Store<T>): Signal<T> - converts store to signal

SlimlibStore

Base class for store services.

constructor(initialState: T) - creates store with initial state
state: T - store state (proxy object)
select<R>(...signals: Signal[], projector: (state: T, ...signalValue: SignalValue<signals[index]>) => R): Signal<R> - selector function that returns a signal

rxjs export

createStore<T>(initialState: T): [T, Store<T>, () => void]

Store factory created with notifyAfterCreation === false.

toObservable<T>(store: Store<T>): Observable<T> - converts store to observable

Limitations

Map, Set, WeakMap, WeakSet cannot be used as values in current implementation.

Mixing proxied values and values from an underlying object can fail for cases where code needs checking for equality.

For example searching for an array element from the underlying object in a proxied array will fail.

Similar projects

Valtio - more sophisticated but similar approach, less limitations

License

MIT