Coder Social home page Coder Social logo

syncstate / core Goto Github PK

View Code? Open in Web Editor NEW
89.0 4.0 8.0 839 KB

SyncState is a state management library for React & JS apps

Home Page: http://syncstate.github.io/

License: MIT License

TypeScript 97.66% HTML 1.15% JavaScript 1.19%
syncstate react react-native state-management immer

core's Introduction

SyncState Logo SyncState

SyncState is a document-based state management library for JS apps that can power realtime multi-user, offline-first, undoable states across systems.

  • Elegant on the surface, yet scales to large apps.
  • Based on JSON patches.
  • Plugins for History, Multi-user and more.
  • Powered by Redux and Immer

Documentation

Find Complete Documentation here

Functional at the core. Mutate on the surface.

JSON patches as actions which can be transferred between the threads and sessions. Supports local mutation with Immer.js.

Like Redux, like MobX

The state is functional but the internal reactivity is based on the MobX approach.

Undo / Redo plugin

A plugin that stores the changes as JSON patches (instead of a series of app snapshots).

Easily build multiuser apps

The remote plugin helps achieve multiuser functionality. It comes with a bit of server piece that works for conflict resolution.

Examples

Contributing

Have something to add? We are lucky to have you, head over to Contribution Guidelines and learn how you can be a part of a wonderful growing community of SyncState.

Installation

You can install @syncstate/core for the core functionality as a package from NPM.

# NPM
npm install @syncstate/core --save

# Yarn
yarn add @syncstate/core

The recommended way to use SyncState with React is to use @syncstate/react. It has a hook for reactive updates and a Provider component to make the store available to the whole app.

# NPM
npm install @syncstate/react --save

# Yarn
yarn add @syncstate/react

Both packages are available as CJS as well as ESM packages. The source code is written in TypeScript and the published code is compiled to ES5 for compatibility across older browsers.

Counter example

import { createDocStore } from '@syncstate/core';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Provider, useDoc } from '@syncstate/react';

// Create a store with an initial state
const store = createDocStore({ counter: 0 });

// Wrap your App with Provider and pass the store prop
ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);

function App() {
  /**
   * useDoc hook with no arguments returns the root document and the function to modify the document.
   * This also adds a listener at the document root and updates the component
   * when the document changes.
   */
  const [doc, setDoc] = useDoc();

  const increment = () => {
    setDoc((doc) => {
      // This looks like a mutation but here we are updating the draft state
      // using Immer.js which generates JSON patches for our internal reducers.
      doc.counter++;
    });
  };

  const decrement = () => {
    setDoc((doc) => {
      doc.counter--;
    });
  };

  return (
    <div>
      <button onClick={decrement}>-</button>
      {doc.count}
      <button onClick={increment}>+</button>
    </div>
  );
}

Watch the introductory talk about SyncState at React Native EU 2020

introductory talk about SyncState

Built with ❤️   at GeekyAnts.

Authors :

License

Licensed under the MIT License, Copyright © 2020 SyncState. See LICENSE for more information.

core's People

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  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  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

core's Issues

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.