Coder Social home page Coder Social logo

apply-changes's Introduction

apply-changes

Take a changes array in the format produced by Object.observe and Array.observe, and apply those changes to another object.

There's many 'changeset' formats floating around, might as well conform to a format built into JavaScript itself.

Example

var apply = require('apply-changes')

var localUser = {
  name: 'Original'
}

var remoteUser = {
  name: 'Original'
}

Object.observe(localUser, function(changes) {
  apply(remoteUser, changes)
  console.log(remoteUser.name) // 'Updated' 
  console.log(localUser.name === remoteUser.name) // true
})

localUser.name = 'Updated'

Why

Say you have an environment where Object.observe is implemented (e.g. node 0.11.x), and you want to easily replicate changes to another environment perhaps one without Object.observe (e.g. vintage 2013 web browser). In this scenario, you could send a raw stream of changes directly from Object.observe callback to observe-apply to have changes applied in the other environment.

Caveats

The recipient of the changes must be treated as read-only. apply-changes does not verify the state of recieving object matches the state of source object. Updates are likely to corrupt data if you change the recipient object.

Licence

MIT

apply-changes's People

Contributors

timoxley avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

runningskull

apply-changes'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.