janus
Version:
the two-faced application library-framework
659 lines (579 loc) • 17.5 kB
JavaScript
// Generated by CoffeeScript 1.12.2
(function() {
var DerivedVarying, FlatMappedVarying, FlattenedVarying, ManagedVarying, MappedVarying, Observation, ReducingVarying, UnreducedVarying, Varying, apply, fix, identity, isFunction, noop, nothing, ref, stopAll, stopOne, uniqueId,
slice = [].slice,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
ref = require('../util/util'), isFunction = ref.isFunction, fix = ref.fix, uniqueId = ref.uniqueId, identity = ref.identity;
noop = (function() {});
nothing = {
isNothing: true
};
stopOne = function() {
var ref1;
this._value = nothing;
if ((ref1 = this._inner) != null) {
ref1.stop();
}
this._inner = null;
this._applicantObs[0].stop();
};
stopAll = function() {
var ao, i, len, ref1, ref2;
this._value = nothing;
if ((ref1 = this._inner) != null) {
ref1.stop();
}
this._inner = null;
ref2 = this._applicantObs;
for (i = 0, len = ref2.length; i < len; i++) {
ao = ref2[i];
ao.stop();
}
};
apply = [
function() {
return noop;
}, function() {
return noop;
}, function(f, x) {
var a, b;
a = x[0];
b = x[1];
return function() {
return f(a._value, b._value);
};
}, function(f, x) {
var a, b, c;
a = x[0];
b = x[1];
c = x[2];
return function() {
return f(a._value, b._value, c._value);
};
}, function(f, x) {
var a, b, c, d;
a = x[0];
b = x[1];
c = x[2];
d = x[3];
return function() {
return f(a._value, b._value, c._value, d._value);
};
}, function(f, x) {
var a, b, c, d, e;
a = x[0];
b = x[1];
c = x[2];
d = x[3];
e = x[4];
return function() {
return f(a._value, b._value, c._value, d._value, e._value);
};
}, function(f, x) {
var a, b, c, d, e, m;
a = x[0];
b = x[1];
c = x[2];
d = x[3];
e = x[4];
m = x[5];
return function() {
return f(a._value, b._value, c._value, d._value, e._value, m._value);
};
}, function(f, x) {
var a, b, c, d, e, m, n;
a = x[0];
b = x[1];
c = x[2];
d = x[3];
e = x[4];
m = x[5];
n = x[6];
return function() {
return f(a._value, b._value, c._value, d._value, e._value, m._value, n._value);
};
}, function(f, x) {
var a, b, c, d, e, m, n, o;
a = x[0];
b = x[1];
c = x[2];
d = x[3];
e = x[4];
m = x[5];
n = x[6];
o = x[7];
return function() {
return f(a._value, b._value, c._value, d._value, e._value, m._value, n._value, o._value);
};
}, function(f, x) {
var a, b, c, d, e, m, n, o, p;
a = x[0];
b = x[1];
c = x[2];
d = x[3];
e = x[4];
m = x[5];
n = x[6];
o = x[7];
p = x[8];
return function() {
return f(a._value, b._value, c._value, d._value, e._value, m._value, n._value, o._value, p._value);
};
}
];
Observation = (function() {
function Observation(parent1, id1, f_1, _stop) {
this.parent = parent1;
this.id = id1;
this.f_ = f_1;
this._stop = _stop;
}
Observation.prototype.stop = function() {
if (this.stopped === true) {
return;
}
this.stopped = true;
this._stop();
};
return Observation;
})();
Varying = (function() {
var _mapAll;
Varying.prototype.isVarying = true;
Varying.prototype._refCount = 0;
Varying.prototype._generation = 0;
function Varying(value) {
this.set(value);
this._observers = {};
}
Varying.prototype.map = function(f) {
return new MappedVarying(this, f);
};
Varying.prototype.flatten = function() {
return new FlattenedVarying(this);
};
Varying.prototype.flatMap = function(f) {
return new FlatMappedVarying(this, f);
};
Varying.prototype._react = function(f_) {
var id, ref1;
if (f_ == null) {
f_ = noop;
}
id = uniqueId();
this._refCount += 1;
if ((ref1 = this.refCount$) != null) {
ref1.set(this._refCount);
}
return this._observers[id] = new Observation(this, id, f_, (function(_this) {
return function() {
var ref2;
delete _this._observers[id];
_this._refCount -= 1;
return (ref2 = _this.refCount$) != null ? ref2.set(_this._refCount) : void 0;
};
})(this));
};
Varying.prototype._reactImmediate = function(f_) {
var observation;
observation = this._react(f_);
if (f_ != null) {
f_.call(observation, this._value);
}
return observation;
};
Varying.prototype.react = function(x, y) {
if (x === false) {
return this._react(y);
} else if (x === true) {
return this._reactImmediate(y);
} else {
return this._reactImmediate(x);
}
};
Varying.prototype.set = function(value) {
if (this._value === value) {
return;
}
this._value = value;
this._propagate();
};
Varying.prototype._propagate = function() {
var _, generation, observer, ref1;
generation = this._generation += 1;
ref1 = this._observers;
for (_ in ref1) {
observer = ref1[_];
observer.f_(this._value);
if (generation !== this._generation) {
return;
}
}
};
Varying.prototype.get = function() {
return this._value;
};
Varying.prototype.pipe = function(f) {
return f(this);
};
Varying.prototype.refCount = function() {
return this.refCount$ != null ? this.refCount$ : this.refCount$ = new Varying(this._refCount);
};
_mapAll = function(flat) {
return function() {
var args, f;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
if (isFunction(args[0])) {
f = args[0];
return (fix(function(curry) {
return function(args) {
if (args.length < f.length) {
return function() {
var more;
more = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return curry(args.concat(more));
};
} else {
return new ReducingVarying(args, f, flat);
}
};
}))(args.slice(1));
} else {
if (isFunction(args[args.length - 1])) {
f = args.pop();
return new ReducingVarying(args, f, flat);
} else {
return function() {
var more;
more = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return _mapAll(flat).apply(null, args.concat(more));
};
}
}
};
};
Varying.mapAll = _mapAll(false);
Varying.flatMapAll = _mapAll(true);
Varying.all = function(vs) {
return new UnreducedVarying(vs);
};
Varying.lift = function(f) {
return function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return new ReducingVarying(args, f, false);
};
};
Varying.managed = function() {
var computation, i, resources;
resources = 2 <= arguments.length ? slice.call(arguments, 0, i = arguments.length - 1) : (i = 0, []), computation = arguments[i++];
return new ManagedVarying(resources, computation);
};
Varying.of = function(x) {
if ((x != null ? x.isVarying : void 0) === true) {
return x;
} else {
return new Varying(x);
}
};
Varying.box = function(x) {
return new Varying(x);
};
return Varying;
})();
DerivedVarying = (function(superClass) {
extend(DerivedVarying, superClass);
function DerivedVarying(applicants, _f, _flatten) {
var first, length;
if (_f == null) {
_f = identity;
}
this._flatten = _flatten != null ? _flatten : false;
this.a = applicants;
this._f = _f;
this._recompute$ = this._recompute.bind(this, false);
this._observers = {};
this._value = nothing;
length = applicants.length;
this._apply = length === 1 ? (first = this.a[0], function() {
return _f(first._value);
}) : length < 10 ? apply[this.a.length](_f, this.a) : function() {
var a;
return _f.apply(null, (function() {
var i, len, results;
results = [];
for (i = 0, len = applicants.length; i < len; i++) {
a = applicants[i];
results.push(a._value);
}
return results;
})());
};
this._stop = length === 1 ? stopOne : stopAll;
}
DerivedVarying.prototype.get = function() {
var a, result;
if (this._refCount > 0) {
return this._value;
} else {
result = this._f.apply(null, (function() {
var i, len, ref1, results;
ref1 = this.a;
results = [];
for (i = 0, len = ref1.length; i < len; i++) {
a = ref1[i];
results.push(a.get());
}
return results;
}).call(this));
if ((this._flatten === true) && ((result != null ? result.isVarying : void 0) === true)) {
return result.get();
} else {
return result;
}
}
};
DerivedVarying.prototype.set = void 0;
DerivedVarying.prototype._react = function(f_) {
var a, id, ref1;
if (f_ == null) {
f_ = noop;
}
this._refCount += 1;
if ((ref1 = this.refCount$) != null) {
ref1.set(this._refCount);
}
if (this._refCount === 1) {
this._applicantObs = (function() {
var i, len, ref2, results;
ref2 = this.a;
results = [];
for (i = 0, len = ref2.length; i < len; i++) {
a = ref2[i];
results.push(a.react(false, this._recompute$));
}
return results;
}).call(this);
this._recompute(true);
}
id = uniqueId();
return this._observers[id] = new Observation(this, id, f_, (function(_this) {
return function() {
var ref2;
delete _this._observers[id];
_this._refCount -= 1;
if (_this._refCount === 0) {
_this._stop();
}
return (ref2 = _this.refCount$) != null ? ref2.set(_this._refCount) : void 0;
};
})(this));
};
DerivedVarying.prototype._recompute = function(silent) {
var first, ref1, value;
if (silent == null) {
silent = false;
}
value = this._apply();
if (value === this._value) {
if (this._inner != null) {
this._inner.stop();
this._inner = null;
}
return;
}
if (this._flatten === true) {
if ((ref1 = this._inner) != null) {
ref1.stop();
}
if ((value != null ? value.isVarying : void 0) === true) {
first = true;
this._inner = value.react((function(_this) {
return function(ival) {
if (_this._value === ival) {
return;
}
_this._value = ival;
if (first !== true) {
return _this._propagate();
}
};
})(this));
first = false;
} else {
this._inner = null;
this._value = value;
}
} else {
this._value = value;
}
if (silent !== true) {
this._propagate();
}
};
return DerivedVarying;
})(Varying);
MappedVarying = (function(superClass) {
extend(MappedVarying, superClass);
function MappedVarying(parent, f) {
MappedVarying.__super__.constructor.call(this, [parent], f, false);
}
return MappedVarying;
})(DerivedVarying);
FlatMappedVarying = (function(superClass) {
extend(FlatMappedVarying, superClass);
function FlatMappedVarying(parent, f) {
FlatMappedVarying.__super__.constructor.call(this, [parent], f, true);
}
return FlatMappedVarying;
})(DerivedVarying);
FlattenedVarying = (function(superClass) {
extend(FlattenedVarying, superClass);
function FlattenedVarying(parent, f) {
FlattenedVarying.__super__.constructor.call(this, [parent], null, true);
}
return FlattenedVarying;
})(DerivedVarying);
ReducingVarying = (function(superClass) {
extend(ReducingVarying, superClass);
function ReducingVarying(parents, f, flat) {
ReducingVarying.__super__.constructor.call(this, parents, f, flat);
}
return ReducingVarying;
})(DerivedVarying);
UnreducedVarying = (function(superClass) {
extend(UnreducedVarying, superClass);
function UnreducedVarying(a1) {
this.a = a1;
this._observers = {};
this._value = nothing;
this._recompute$ = this._recompute.bind(this, false);
}
UnreducedVarying.prototype.map = function(f) {
return new ReducingVarying(this.a, f);
};
UnreducedVarying.prototype.flatMap = function(f) {
return new ReducingVarying(this.a, f, true);
};
UnreducedVarying.prototype.flatten = void 0;
UnreducedVarying.prototype.get = function() {
var a, i, len, ref1, results;
if (this._refCount > 0) {
return this._value;
} else {
ref1 = this.a;
results = [];
for (i = 0, len = ref1.length; i < len; i++) {
a = ref1[i];
results.push(a.get());
}
return results;
}
};
UnreducedVarying.prototype._apply = function() {
var a, i, len, ref1, results;
ref1 = this.a;
results = [];
for (i = 0, len = ref1.length; i < len; i++) {
a = ref1[i];
results.push(a._value);
}
return results;
};
UnreducedVarying.prototype._stop = stopAll;
UnreducedVarying.prototype._propagate = function() {
var _, generation, observer, ref1;
generation = this._generation += 1;
ref1 = this._observers;
for (_ in ref1) {
observer = ref1[_];
observer.f_.apply(observer, this._value);
if (generation !== this._generation) {
return;
}
}
};
UnreducedVarying.prototype._reactImmediate = function(f_) {
var observation;
observation = this._react(f_);
if (f_ != null) {
f_.apply(observation, this._value);
}
return observation;
};
return UnreducedVarying;
})(DerivedVarying);
ManagedVarying = (function(superClass) {
extend(ManagedVarying, superClass);
function ManagedVarying(_resources, _computation) {
var inner, resources;
this._resources = _resources;
this._computation = _computation;
inner = new Varying();
ManagedVarying.__super__.constructor.call(this, inner);
this._awake = false;
resources = null;
this.refCount().react(false, (function(_this) {
return function(count) {
var f, i, len, resource, results;
if (count > 0 && _this._awake === false) {
_this._awake = true;
resources = (function() {
var i, len, ref1, results;
ref1 = this._resources;
results = [];
for (i = 0, len = ref1.length; i < len; i++) {
f = ref1[i];
results.push(f());
}
return results;
}).call(_this);
return inner.set(_this._computation.apply(null, resources));
} else if (count === 0 && _this._awake === true) {
_this._awake = false;
results = [];
for (i = 0, len = resources.length; i < len; i++) {
resource = resources[i];
results.push(resource.destroy());
}
return results;
}
};
})(this));
}
ManagedVarying.prototype.get = function() {
var f, i, len, resource, resources, result;
if (this._awake === true) {
return ManagedVarying.__super__.get.call(this);
} else {
result = null;
resources = (function() {
var i, len, ref1, results;
ref1 = this._resources;
results = [];
for (i = 0, len = ref1.length; i < len; i++) {
f = ref1[i];
results.push(f());
}
return results;
}).call(this);
result = this._computation.apply(null, resources).get();
for (i = 0, len = resources.length; i < len; i++) {
resource = resources[i];
resource.destroy();
}
return result;
}
};
return ManagedVarying;
})(FlattenedVarying);
module.exports = {
Varying: Varying,
Observation: Observation,
FlatMappedVarying: FlatMappedVarying,
FlattenedVarying: FlattenedVarying,
MappedVarying: MappedVarying,
ReducingVarying: ReducingVarying
};
}).call(this);