update-map
v1.0.1
Published
Library for updating state minimally, based on changes
Downloads
2
Maintainers
Readme
Library for updating state minimally, based on changes
It is useful for using a simple state object to adjust a complex data structure or program state
const Update = require("update-map");
const dependency = [
["a",(scope,{a})=>{
scope.a_val=a;
}],
[["b","c"],(scope,{b,c})=>{
scope.bc_val=b+c;
}],
[["c"],async (scope,{c})=>{
return new Promise(function(resolve, reject) {
setTimeout(()=>{
scope.c_val=true;
resolve();
},0);
});
}],
];
const scope = {
a_val:0,
bc_val:0,
c_val:0,
};
const state = {
a:1,
b:2,
c:3,
};
const changes = ["a","b","c"];
const updateFunc = Update(dependency);
await updateFunc(scope,changes,state);
The above code takes a set of changes and calls functions which respond to those changes
Update is the factory function for the update map function,
dependency is an array of [key,value]
pairs, where the key
is one or more property names, and the value is a function
which receives the scope object and an object whose properties are
the values of state which were specified in key
for example, in the first function, this function will only be called if a was in the list of changes
scope is an object that is passed by reference and can be modified by the function
state is an object which represent the abstract representation of something
changes is an array of strings naming the changed properties of the old state vs the new state