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

true-store

v0.5.2

Published

Dead simple state management for javascript applications.

Downloads

42

Readme

TrueStore

Dead simple state management on top of Immutable.js.

npm version Build Status Coverage Status

true story

Install

TrueStore is available on npm:

npm install true-store

Overview

TrueStore provides a simple store with observer capabilities. It also gives you copies of your state when you ask for values, avoiding undesired mutation bugs.

Why you might wanna use it:

  • Simplicity: simple get and set interface. No weird magic going on.
  • Control: Thanks to the explicit observers, you always know when your code will update.
  • Performance: Since updates are in your control, if you do it right it will be fast. If you do it wrong, you can fix it. Also, thanks to Immutable.js the observers don't need to run if data updates but is equal to the previous value.
  • Predictability: Thanks to Immutable.js you won't run into weird mutation bugs. Your data only updates when you call methods like set and merge. When reading you'll get copies of your data to do as you please.

constructor

import Store from 'true-store';

let store = new Store(); // default state = {}
let store = new Store({
    user: { name: 'John Doe' },
});

get

store.get(); // { user: {name: 'John Doe'} }
store.get('user'); // {name: 'John Doe'}
store.get('user.name'); // 'John Doe'

set

store.set('user', { name: 'Jane' });
store.set('user.name', 'Jane');
store.set('messages', []);

merge

store.merge({ user: { age: 42 } }); // { user: { name: 'John Doe', age: 42 }}
store.merge({ messages: ['hello world'] });

observer

function somethingChanged() {}
function userChanged() {}
function userOrMessagesChanged() {}

store.observer(somethingChanged);
store.observer(userChanged, ['user']);
store.observer(userOrMessagesChanged, ['user', 'messages']);

let observer = store.observer(somethingChanged);
observer.release(); // observer won't run after release

transaction

// observers will be called only once, after the transaction ends
store.transaction(() => {
    store.set('a', 1);
    store.set('b', 2);
    store.set('c', 3);
});

// transactions can be nested, only the root will trigger observers
store.transaction(() => {
    store.set('a', 1);
    store.set('b', 2);
    store.transaction(() => {
        store.set('c', 3);
        store.set('d', 4);
    });
});

Integration with React

TrueStore works anywhere. If you wanna use it with React, you just need to:

  • Get values from the store and use at will, usually in your render method.
  • Use an observer to tell the component to update when something changes.
  • Release the observer when the component unmounts.

Example

store.js

import Store from 'true-store';

export default new Store({
    count: 0,
});

actions.js

import store from './store';

export function increment() {
    let count = store.get('count');
    store.set('count', count + 1);
}

counter.js

import React from 'react';

import store from './store';
import { increment } from './actions';


class Counter extends React.Component {

    componentDidMount() {
        this.observer = store.observer(this.forceUpdate.bind(this));
    }

    componentWillUnmount() {
        this.observer.release();
    }

    render() {
        return <button onClick={increment}>
            {store.get('count')}
        </button>;
    }
}