datavis-tech / topologica

Minimal library for reactive dataflow programming. Based on topological sort.
MIT License
50 stars 1 forks source link

Deleting properties #52

Open caracal7 opened 5 years ago

caracal7 commented 5 years ago

I thinking about implementing properties deleting.

For example I have reactive graph like this:

b = a => a + 1;
c = b => b + 1;
d = a => a + 1;
e = (b, c) => b + c;
f = e => e * 2;
log = f => console.log(f);
a = 5;

If I delete d property it is not a problem: I need only delete it from states, functions and edges. But if I want to delete c property I can't decide what I need to do with:

  1. Should I reset state of dependent e property to undefined?
  2. Should I reset state of f property to undefined which is dependent to e?
  3. What I need to do with log function? I think this function should not executed with undefined argument

P.S. Sorry for my English :)

curran commented 5 years ago

Setting the property values to undefined will have pretty much the same behavior as leaving them alone, because the reactive functions are only invoked if all inputs are defined.

Maybe try a first implementation that just deletes the property from state, functions and edges? Then we can see how it behaves and think more deeply about the other cases.

caracal7 commented 5 years ago

I've tested several ways and in my cases better is unset all dependent function states.

    const deleteProperty = property => {
        const unsetDependents = property => {
            if(edges[property])
                edges[property].forEach(property2 => {
                    state[property2] = undefined;
                    unsetDependents(property2);
                });
        }
        unsetDependents(property)

        Object.keys(edges).forEach(edge => {
            let index = edges[edge].indexOf(property);
            if (!!~index) edges[edge].splice(index, 1);
        })

        delete state[property];
        delete functions[property];
    }