UnitJS/src/UnitJS/Async/Box.js

43 lines
734 B
JavaScript

import * as Async from UnitJS.Async;
function Box(initValue) {
this.value = initValue;
}
Box.prototype.get = function(f) {
f(this.value);
};
Box.prototype.set = function(newValue) {
return function(f) {
this.value = newValue;
f();
}.bind(this);
};
Box.prototype.use = function(f) {
return function(g) {
f(this.value)(g);
}.bind(this);
};
Box.prototype.update = function(modifier) {
return function(f) {
modifier(this.value)(function (newValue) {this.value = newValue; f()}.bind(this));
}.bind(this);
};
return {
make: make
};
function make(initValue) {
var box = new Box(initValue);
return {
get: box.get.bind(box),
set: box.set.bind(box),
use: box.use.bind(box),
update: box.update.bind(box),
};
}