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

opp-store

v1.0.0-alpha.46

Published

State management for every frameworks.

Downloads

342

Readme

opp-store

State management for every frameworks.

Installation

Using npm:

$ npm install --save opp-store

Usage

In Front-End development, there are two kinds of state to manage: immutable (like: react), mutable (like: vue).They are incompatible to each other.

How do we achieve to share state in incompatible framewroks.Beacause we have two kinds of state at same time.Everytime you can state by setState.All kinds of state will be changed, just like they share state to each other.

import { createStore } from 'opp-store';

const initialState = {};
const store = createStore(initialState);

const listener = (state) => console.log(state?.name);
const unsubscribe = store.subscribe(listener);

store.setState({ name: 'xiaoshuang' }); // will trigger listener

const mutableState = store.getMutableState();
mutableState === initialState;
mutableState.name === 'xiaoshuang';

const immutableState = store.getImmutableState();
immutableState !== initialState;
immutableState.name === 'xiaoshuang';

Documentation

createStore

Create Store with initial state.

type Listener = (state: object) => void;

type Store = {
  setState: (state: object) => void;
  subscribe: (listener: Listener) => () => void;
  unsubscribe: (listener: Listener) => void;
  getMutableState: () => object;
  getImmutableState: () => object;
};

type createStore = (initialState: object) => Store;

Store.setState

Just like React.Component.setState, pass the partial state you wanna change.

type setState = (state: object) => void;
import { createStore } from 'opp-store';

const initialState = { age: 30, sex: 'M' };
const store = createStore(initialState);

store.setState({ age: 31 }); // { age: 31, sex: 'M' }
store.setState({ sex: 'F' }); // { age: 31, sex: 'F' }

Store.subscribe

Subcribe listener to store, and trigger it when state changed.Return callback to subscribe the listener you give.

type Listener = (state: object) => void;
type subscribe = (listener: Listener) => () => void;
import { createStore } from 'opp-store';

const initialState = {};
const store = createStore(initialState);

const unsubscribe = store.subscribe(console.log);

store.setState({ age: 30 }); // will log { age: 30 }
unsubscribe();
store.setState({ age: 31 }); // will log nothing

Store.unsubscribe

Unsubcribe listener from store.

type Listener = (state: object) => void;
type subscribe = (listener: Listener) => () => void;
import { createStore } from 'opp-store';

const initialState = {};
const store = createStore(initialState);

store.subscribe(console.log);
store.setState({ age: 30 }); // will log { age: 30 }

store.unsubscribe(console.log);
store.setState({ age: 31 }); // will log nothing

Store.getMutableState

type getMutableState = () => object;
import { createStore } from 'opp-store';

const initialState = {};
const store = createStore(initialState);

store.setState({ age: 30 });

const mutableState = store.getMutableState();

mutableState === initialState;

Store.getImmutableState

type getImmutableState = () => object;
import { createStore } from 'opp-store';

const initialState = {};
const store = createStore(initialState);

store.setState({ age: 30 });

const immutableState = store.getImmutableState();

immutableState !== initialState;
immutableState.age === initialState.age;

createPlugin

Create plugin with initial state, and register to opp-core by registerPlugin.

import { SyncHook, SyncBailHook } from 'opp-tapable';

type StorePluginHooks = {
  setState: new SyncHook(['source']),
  useStore: new SyncBailHook(['getter']),
  subscribe: new SyncBailHook(['listener']),
  getMutableState: new SyncBailHook([]),
  getImmutableState: new SyncBailHook([]),
};

type StorePlugin = Plugin & { hooks: StorePluginHooks };

type createPlugin = (initialState: object) => (plugin: Plugin) => StorePlugin;
import { createPlugin } from 'opp-store';
import { getPlugin, registerPlugin, start } from 'opp-core';

const store = createPlugin({})({ name: 'store' });

(async () => {
  registerPlugin(store);
  await start();

  getPlugin('store') === store.hooks;
})();

Plugin.hook.useStore

Front-End hook under Component lifecycle, for example: React-Hooks.

type Getter = (state: object) => any;

type useStore = (getter: Getter) => ReturnType<Getter>;
import { usePlugin } from 'opp-react';

const Input = (props) => {
  const store = usePlugin('store'); 

  const getter = (state = {}) => state.name;
  const value = store.useStore(getter);

  const onChange = (name) => store.setState({ name });

  return (
    <Input type="text" value={value} onChange={onChange} />
  );
};