janus
Version:
the two-faced application library-framework
126 lines (111 loc) • 3.68 kB
JavaScript
// Generated by CoffeeScript 1.12.2
(function() {
var Base, List, Manifest, Varying, types,
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;
types = require('../core/types');
Base = require('../core/base').Base;
Varying = require('../core/varying').Varying;
List = require('../collection/list').List;
Manifest = (function(superClass) {
extend(Manifest, superClass);
function Manifest(app, model1, criteria, options) {
var self;
this.model = model1;
Manifest.__super__.constructor.call(this);
self = this;
this.app = app.shadow();
this._valid = true;
this.result = new Varying(types.result.init());
this.requests = new List();
this.requests.destroyWith(this);
this._pending = 0;
this.listenTo(this.app, 'resolvedRequest', (function(_this) {
return function(request, result) {
_this._pending += 1;
_this.requests.add({
request: request,
result: result
});
return _this.reactTo(result, function(inner) {
if (!types.result.complete.match(inner)) {
return;
}
self._pending -= 1;
self._hook();
return this.stop();
});
};
})(this));
if (this.model.valid != null) {
this.reactTo(this.model.valid(), (function(_this) {
return function(isValid) {
return _this._valid = isValid;
};
})(this));
}
this.view = this.app.view(this.model, criteria, options);
if (this.view == null) {
return this._fault('internal: could not find view for model');
}
this.view.artifact();
this._hook();
this.result.set(types.result.pending());
}
Manifest.prototype._hook = function() {
if (this._pending > 0) {
return;
}
if (this._hooked === true) {
return;
}
this._hooked = true;
setTimeout(((function(_this) {
return function() {
_this._hooked = false;
if (_this._fault === true) {
return;
}
if (_this._pending > 0) {
return;
}
if (_this._valid === true) {
_this.result.set(types.result.success(_this.view));
} else {
_this.result.set(types.result.failure(_this.model.errors()));
}
_this.stopAll();
};
})(this)), 0);
};
Manifest.prototype._fault = function(x) {
this._fault = true;
this.result.set(types.result.failure(x));
this.stopAll();
};
Manifest.prototype.then = function(resolve, reject) {
return new Promise((function(_this) {
return function(resolve, reject) {
return _this.reactTo(_this.result, function(result) {
if (!types.result.complete.match(result)) {
return;
}
this.stop();
if (types.result.success.match(result)) {
resolve(result.get());
} else {
reject(result.get());
}
});
};
})(this)).then(resolve, reject);
};
Manifest.run = function(app, model, criteria, options) {
return new this(app, model, criteria, options);
};
return Manifest;
})(Base);
module.exports = {
Manifest: Manifest
};
}).call(this);