Giter VIP home page Giter VIP logo

immutable-js-store's Introduction

ImmutableStore

Tiny observable wrapper around Immutable JS with rewind/replay support. Use normal Immutable JS methods to update the store. Subscribe to be notified of changes.

Installation

npm install immutable-js-store

Api

constructor(initialState: ?Object)

Initialize the store (with an optional default state).

clearHistory(): Immutable.Collection

Removes all history before the current cursor.

getState(): Immutable.Collection

Returns the reference to the Immutable state at the current cursor. By default the cursor will point to the most recent state. Use the "step" methods to modify the cursor and undo or replay events.

hasNext(): boolean

Is there a newer state (after) the current cursor?

hasPrevious(): boolean

Is there an older state (before) the current cursor?

jumpToEnd(): ?Immutable.Collection

Move the cursor to the most recent (last) state. This will notify all current subscribers. This method returns the value of the state at the updated cursor if the cursor has changed.

jumpToStart(): ?Immutable.Collection

Move the cursor to the very first (initial) state. This will notify all current subscribers. This method returns the value of the state at the updated cursor if the cursor has changed.

stepBack(): ?Immutable.Collection

Decrease the cursor by 1, to the state that came before the current one. This will notify all current subscribers. This method returns the value of the state at the updated cursor if the cursor has changed.

stepForward(): ?Immutable.Collection

Increase the cursor by 1, to the state that came after the current one. This will notify all current subscribers. This method returns the value of the state at the updated cursor if the cursor has changed.

subscribe(subscriber): Function

Subscribe to store changes. Subscribers will be passed a reference to the current store-state when updates are made. Stepping backwards or forward will notify subscribers of the updated "current" state. This method returns an unsubscribe function; invoke it to stop being notified of changes to the store.

subscribeIn(path, subscriber): Function

Memoized subscription to a specific path in the Immutable store. Subscribers will be passed the value contained at the specified path within the current store-state. This method returns an unsubscribe function; invoke it to stop being notified of changes to the store.

Example

import ImmutableStore from 'immutable-js-store'

// Simple example store with a default state
const store = new ImmutableStore({
  user: {
    id: 1,
    name: 'Brian'
  },
  counter: 0
})

// Subscribe to any top-level store changes
const unsubscribeFromStore = store.subscribe(
  (store) => console.log(JSON.stringify(store.toJS()))
)

// Subscribe only to changes below store.user.name
const unsubscribeFromName = store.subscribeIn(['user', 'name'],
  (name) => console.log('name:', name)
)

store.set('counter', store.get('counter') + 1)
// { user: { id: 1, name: 'Brian' }, counter: 1}

store.set('counter', store.get('counter') + 1)
// { user: { id: 1, name: 'Brian' }, counter: 2}

store.setIn(['user', 'name'], 'Brian Vaughn')
// { user: { id: 1, name: 'Brian Vaughn' }, counter: 2}
// name: Brian Vaughn

store.stepBack()
// { user: { id: 1, name: 'Brian' }, counter: 2}
// name: Brian

store.stepBack()
// { user: { id: 1, name: 'Brian' }, counter: 1}

store.stepForward()
// { user: { id: 1, name: 'Brian' }, counter: 2}

store.stepForward()
// { user: { id: 1, name: 'Brian Vaughn' }, counter: 2}
// name: Brian Vaughn

immutable-js-store's People

Contributors

bvaughn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

digideskio

immutable-js-store's Issues

each node in tree as immutable store?

Do you think there's value in making nested documents an instance of immutable store?

So, if you init a store like:

const store = new ImmutableStore({
  user: {
    id: 1,
    name: 'Brian'
  },
  counter: 0
})

Here, store is an instance of ImmutableStore but, store.get('user') is just an Immutable Map. So, you can't do something like

store.get('user').subscribe()

Why?

Currently, To subscribe to any node in the tree, you have to know the full path from the root. But with all nodes as immutable store, you just need a reference to that node.

Thoughts? Performance implications?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.