Giter VIP home page Giter VIP logo

merge-with-observable's Introduction

mergeWithObservable

npm version license

Function which recursively merges provided object into MobX observable taking into account custom rules when specified.

Imagine the situation when your nested observable objects in the state have to be updated by different update messages. It may be the case when every distinct update message brings only partial data. You need a clever way how to apply this data.

This repo contains

mergeWithObservable(observableObject, objectToMerge, rulesDefinition)

function which tries to solve this situation.

Table of Contents

Installation

Execute this command in your environment.

npm install merge-with-observable --save

or

yarn add merge-with-observable

Table of Contents

Usage

Let's demonstrate how to apply mergeWithObservable function. Let's imagine a project where we can find this type of observables:

{
    sport: {
        id: 2,
        name: "soccer",
        bets: [
            3, 4, 5, 6
        ]
    }
}

We have sport and bets for this sport. Let's imagine system has initial knowledge only about sport ids:

const observableObject = observable({
    sport: {
        id: 2
    }
});

Then we say we have 3 different update messages with partial data:

const objectToMerge1 = {
    sport: {
        id: 2,
        name: "soccer"
    }
};

and

const objectToMerge2 = {
    sport: {
        id: 2,
        bets: [
            1, 2, 3, 4
        ]
    }
};

and

const objectToMerge3 = {
    sport: {
        id: 2,
        bets: [
            3, 4, 5, 6
        ]
    }
};

We don't want same bets to be present in the object after the merge. Also we want only bets starting at 2. How we can do that?

First we define our constrain in the rules object (same shape as observable):

const rulesDefinition = {
    sport: {
        bets: (observableObject, objectToMerge, key) => {
            if (observableObject[key] === undefined) {
                observableObject[key] = observable([]);
            }
            objectToMerge[key].forEach((newValue) => {
                if (observableObject[key].find((value) => value === newValue) === undefined && newValue >= 2) {
                    observableObject[key].push(newValue);
                }
            });
        }
    }
};

Then we merge 3 update messages with this calls:

mergeWithObservable(observableObject, objectToMerge1, rulesDefinition);
mergeWithObservable(observableObject, objectToMerge2, rulesDefinition);
mergeWithObservable(observableObject, objectToMerge3, rulesDefinition);

Profit. Our observable is now in this state:

{
    sport: {
        id: 2,
        name: "soccer",
        bets: [
            2, 3, 4, 5, 6
        ]
    }
}

Table of Contents

merge-with-observable's People

Contributors

kavolorn avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

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.