main.js
function Firkin(c) {
let t = this;
if (c)
{
if (c.mut==false) t.mut = false;
if (c.trc==true) t.trc = true;
}
t.err = function(msg)
{
if (t.trc) console.trace(msg);
else console.log(msg);
}
t.v = {};
t.vds = {};
t.tgs = {};
t.fs = [];
this.set = function(n,v)
{
if (t.mut==false) { t.err('cannot mutate'); }
else
{
let ok = true;
if (t.vds[n]) t.vds[n].forEach( f => {
if (!f(v)) {
ok = false;
t.err('validation failed: '+f.toString());
}
});
if (ok) {
t.v[n] = v;
if (t.tgs[n]) t.tgs[n].forEach( (f) => f(v) );
t.fs.forEach( ff => {
ff.i.forEach( i => {
})
});
}
}
}
t.get = function(n)
{
return t.v[n];
}
t.vld = function(n, f)
{
if (!t.vds[n]) t.vds[n] = [];
t.vds[n].push(f);
}
t.trg = function(n, f)
{
if (!t.tgs[n]) t.tgs[n] = [];
t.tgs[n].push(f);
}
t.fnc = function(i, o, f)
{
t.fs.push({i:i, o:o, f:f});
}
}
let a = new Firkin({mut: false});
let b = new Firkin;
b.vld('x', (x) => x < 10 );
b.vld('y', (y) => typeof y == 'number' );
b.trg('z', (z) => console.log('got z =', z) );
a.fnc( ['x','y'], ['z'], (x,y) => ({'z': x + y}) );
a.set('x', 10);
b.set('x', 20);
b.set('y', 'hello');
console.log('ax =',a.get('x'));
console.log('bx =',b.get('x'));