bajascript
Version:
BajaScript Rewritten in TypeScript.
1,177 lines (1,163 loc) • 326 kB
JavaScript
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.baja = factory();
}
}(this, function () {
var BaseBajaObj = function () {
}, baja = new BaseBajaObj;
(function (a, U) {
U.prototype.equals = function (R) {
return this === R
};
U.prototype.valueOf = function () {
return this
};
Function.prototype.$extend = function (R) {
if (typeof R === "function") {
this.prototype = new R;
this.$super = R
} else {
this.prototype = R;
this.$super = R.constructor
}
this.prototype.constructor = this;
return this
};
if (typeof Array.prototype.contains !== "function") Array.prototype.contains = typeof Array.prototype.indexOf === "function" ? function (R) {
return this.indexOf(R) > -1
} : function (R) {
var T;
for (T = 0; T < this.length; T++)if (this[T] ===
R)return true;
return false
}
})(baja, BaseBajaObj);
(function (a, U) {
a.version = "1.0.0.0";
var R = {}, T = null, V = {}, O = {};
(function () {
function G(u, o) {
var q = u.name + ": ";
if (u.stack) {
u.message && typeof u.stack === "string" && u.stack.indexOf(q + u.message) !== 0 && o(q + u.message);
o(u.stack)
} else {
u.message ? o(q + u.message) : o(q + u);
try {
var b, e;
q = arguments;
var f = 0, g = a.stackTraceLimit || 20;
if (q.callee && q.callee.caller) {
for (b = q.callee.caller; typeof b === "function" && f < g;) {
e = b.name;
if (!e) {
e = b.toString().replace(/[\r\n]/g, " ");
if (e.length > 200) e = e.substring(0, 200) + "..."
}
o(" at " + e);
b =
b.caller;
f++
}
f >= g && o("Stack trace limit exceeded (" + g + ")")
}
} catch (p) {
}
}
}
a.outln = function (u) {
if (a.isStopping())return this;
typeof bajaJsPrint === "function" && bajaJsPrint(u);
return this
};
a.clearOut = function () {
return this
};
a.error = function (u) {
if (a.isStopping())return this;
u = u instanceof Error ? u : Error(u);
G(u, a.outln);
if (a.isStarted() && a.isLogClientErrorsInServer() && u && !(u instanceof a.comm.ServerError)) {
var o = "";
G(u, function (q) {
o += q + "\n"
});
a.comm.error(o)
}
return this
};
a.stackTraceLimit = 20
})();
a.fail = function (G) {
G =
G || Error();
a.error(G instanceof Error ? G : Error(G))
};
a.ok = function () {
};
a.noop = a.ok;
var Q = function (G, u) {
a.strictAllArgs([G, u], [String, Object]);
this.$moduleName = G;
this.$data = u
}.$extend(U);
Q.prototype.get = function (G) {
G = a.objectify(G, "key");
G.def = a.def(G.def, null);
var u = this.$data.hasOwnProperty(G.key) ? this.$data[G.key] : G.def;
if (G.args && typeof u === "string") {
var o = G.args;
if (o.constructor !== Array) o = [o];
u = u.replace(/\{[0-9]+\}/g, function (q) {
var b = parseInt(q.substring(1, q.length - 1), 10);
return o[b] !== undefined ?
o[b] : q
})
}
return u
};
Q.prototype.getModuleName = function () {
return this.$moduleName
};
(function () {
function G(o) {
if (o.$index < o.$keys.length) {
++o.$index;
return o.$index !== o.$keys.length
} else return false
}
a.strictArg = function (o, q, b) {
if (o === undefined)if (b)throw Error(b); else {
o = "Invalid argument (undefined)";
if (q) o += ". Expected " + q.name;
throw Error(o);
} else if (o === null)return o;
if (q === undefined || q === null)return o;
b = typeof q;
if (b === "function" && (o.constructor === q || o instanceof q))return o;
if (b === "string" || b ===
"object") {
q = a.lt(q);
if (!a.hasType(o))throw Error("Invalid argument. Expected Type: " + q);
if (!o.getType().is(q))throw Error("Invalid argument. Type " + o.getType() + " is not expected Type: " + q);
return o
}
b = q.name;
if (!b && q.$name) b = q.$name;
q = o.constructor.name;
if (!q && o.constructor.$name) q = o.constructor.$name;
throw Error("Invalid argument type. Expected " + b + ", received " + q + " instead");
};
a.strictAllArgs = function (o, q) {
if (q.length !== o.length)throw Error("Invalid number of arguments. Expected " + q.length + ", received " +
o.length + " instead.");
var b;
for (b = 0; b < o.length; b++)a.strictArg(o[b], q[b])
};
a.def = function (o, q) {
return o === undefined ? q : o
};
var u = a.strictArg;
a.throttle = function (o, q) {
function b() {
var r = a.clock.ticks(), F = v - r;
if (F <= 0) v = r + e;
return F
}
q = a.objectify(q, "interval");
var e = q.interval || 100, f = q.ok || a.ok, g = q.fail || a.fail, p = q.drop, v = 0,
B = a.clock.expiredTicket;
return function () {
var r = this, F = Array.prototype.slice.call(arguments), k, m = b();
if (!(m > 0 && p)) {
k = function () {
try {
f(o.apply(r, F))
} catch (A) {
g(A)
} finally {
b()
}
};
B.cancel();
if (m <= 0) a.runAsync(k); else p || (B = a.clock.schedule(k, m))
}
}
};
a.hasType = function (o, q) {
if (o === null || o === undefined)return false;
if (o && typeof o === "object" && o instanceof a.Slot)return false;
if (typeof o.getType === "function") {
if (q)return o.getType().is(q);
return true
} else return false
};
a.objectify = function (o, q) {
if (!(o === undefined || o === null))if (o.constructor === Object)return o; else if (typeof q === "string") {
var b = {};
b[q] = o;
return b
}
return {}
};
a.lex = function (o) {
o = a.objectify(o, "module");
var q = o.module, b = new a.comm.Callback(o.ok,
o.fail, o.batch), e;
if (R.hasOwnProperty(q)) {
e = R[q];
b.ok(e);
return e
}
o = typeof o.ok === "function";
try {
b.addOk(function (g, p, v) {
if (T) T.lexicons[q] = v;
e = new Q(q, v);
R[q] = e;
g(e)
});
a.comm.lex(q, b, o)
} catch (f) {
b.fail(f)
}
return e
};
a.runAsync = function (o) {
a.clock.schedule(o, 0)
};
(function () {
function o(b, e, f, g) {
for (var p; e < f; e++) {
p = g(b[e], e);
if (p !== undefined)return p
}
}
function q(b, e, f) {
for (var g; b < e; b++) {
g = f(b);
if (g !== undefined)return g
}
}
a.iterate = function () {
var b = arguments, e = b[0], f = b[1], g = b[2];
b = b[3];
var p = Object.prototype.toString.call(e);
if (e === undefined || e === null)throw Error("undefined passed to baja.iterate()");
if (p === "[object Array]")switch (arguments.length) {
case 2:
return o(e, 0, e.length, f);
case 3:
return o(e, f, e.length, g);
case 4:
return o(e, f, g, b)
} else if (p === "[object Object]") {
if (e instanceof a.Cursor) f = e.each(f); else if (typeof g === "function")a:{
for (; e;) {
var v = f(e);
if (v !== undefined) {
f = v;
break a
} else e = g(e)
}
f = void 0
} else a:{
for (v in e)if (e.hasOwnProperty(v)) {
g = f(e[v], v);
if (g !== undefined) {
f = g;
break a
}
}
f = void 0
}
return f
} else if (p ===
"[object Number]")return typeof f === "number" ? q(e, f, g) : q(0, e, f); else if (p === "[object Arguments]")throw Error("Arguments object not iterable (pass through Array.prototype.slice first)");
throw Error(e + " is not iterable");
}
})();
a.Cursor = function () {
};
a.Cursor.prototype.get = function () {
throw Error("Not implemented");
};
a.Cursor.prototype.each = function () {
throw Error("Not implemented");
};
a.AsyncCursor = function () {
}.$extend(a.Cursor);
a.SyncCursor = function () {
}.$extend(a.Cursor);
a.SyncCursor.prototype.next = function () {
throw Error("Not implemented");
};
a.FilterCursor = function (o, q) {
a.FilterCursor.$super.apply(this, arguments);
this.$context = o;
this.$keys = (this.$orderedMap = q) && q.getKeys();
this.$filter = null;
this.$index = -1
}.$extend(a.SyncCursor);
a.FilterCursor.prototype.next = function () {
if (this.$filter) {
do if (!G(this))return false; while (!this.$filter.call(this.$context, this.get()));
return true
} else return G(this)
};
a.FilterCursor.prototype.get = function () {
var o = this.$index, q = this.$keys;
return o === -1 || o >= q.length ? null : this.$orderedMap.get(q[o])
};
a.FilterCursor.prototype.getKey =
function () {
var o = this.$index, q = this.$keys;
return o === -1 || o >= q.length ? null : q[o]
};
a.FilterCursor.prototype.getIndex = function () {
return this.$index
};
a.FilterCursor.prototype.each = function (o) {
u(o, Function);
for (var q; this.next();)if (q = o.call(this.$context, this.get(), this.getIndex()))return q
};
a.FilterCursor.prototype.isEmpty = function () {
var o = this.$index;
this.$index = -1;
var q = this.next();
this.$index = o;
return !q
};
a.FilterCursor.prototype.toArray = function () {
var o = this.$index, q = [];
for (this.$index = -1; this.next();)q.push(this.get());
this.$index = o;
return q
};
a.FilterCursor.prototype.toMap = function () {
var o = this.toArray(), q = {}, b, e;
for (e = 0; e < o.length; ++e) {
b = o[e];
q[b.getName()] = b
}
return q
};
a.FilterCursor.prototype.getSize = function () {
var o = this.$index, q = 0;
for (this.$index = -1; this.next();)++q;
this.$index = o;
return q
};
a.FilterCursor.prototype.filter = function (o) {
if (this.$filter) {
var q = this.$filter;
this.$filter = function (b) {
return q.call(this, b) && o.call(this, b)
}
} else this.$filter = o;
return this
};
a.FilterCursor.prototype.first = function () {
var o =
this.$index, q = null;
this.$index = -1;
if (this.next()) q = this.get();
this.$index = o;
return q
};
a.FilterCursor.prototype.last = function () {
for (var o = this.$index, q = null, b = this.$keys.length - 2; q === null && b >= -1;) {
this.$index = b;
if (this.next()) q = this.get();
b -= 2
}
this.$index = o;
return q
};
a.OrderedMap = function () {
a.OrderedMap.$super.apply(this, arguments);
this.$map = {};
this.$array = []
}.$extend(U);
a.OrderedMap.prototype.put = function (o, q) {
if (this.$map.hasOwnProperty(o)) this.$map[o] = q; else {
this.$map[o] = q;
this.$array.push(o)
}
};
a.OrderedMap.prototype.remove = function (o) {
u(o, String);
var q, b;
if (this.$map.hasOwnProperty(o)) {
q = this.$map[o];
delete this.$map[o];
for (b = 0; b < this.$array.length; ++b)if (this.$array[b] === o) {
this.$array.splice(b, 1);
break
}
return q
} else return null
};
a.OrderedMap.prototype.contains = function (o) {
u(o, String);
return this.$map.hasOwnProperty(o)
};
a.OrderedMap.prototype.get = function (o) {
return this.$map.hasOwnProperty(o) ? this.$map[o] : null
};
a.OrderedMap.prototype.rename = function (o, q) {
u(o, String);
u(q, String);
if (!this.contains(o))return false;
if (this.contains(q))return false;
var b = this.$map[o];
delete this.$map[o];
this.$map[q] = b;
for (b = 0; b < this.$array.length; ++b)if (this.$array[b] === o) {
this.$array[b] = q;
break
}
return true
};
a.OrderedMap.prototype.getIndex = function (o) {
u(o, String);
if (this.$map.hasOwnProperty(o)) {
var q;
for (q = 0; q < this.$array.length; ++q)if (this.$array[q] === o)return q
}
return -1
};
a.OrderedMap.prototype.getFromIndex = function (o) {
u(o, Number);
o = this.$array[o];
if (typeof o === "string")return this.$map[o];
return null
};
a.OrderedMap.prototype.getKeys =
function () {
return this.$array.slice(0)
};
a.OrderedMap.prototype.getSize = function () {
return this.$array.length
};
a.OrderedMap.prototype.sort = function (o) {
u(o, Function);
this.$array.sort(o)
};
a.OrderedMap.prototype.getCursor = function (o, q) {
q = q || a.FilterCursor;
return new q(o, this)
}
})();
var y = a.strictArg, K = a.strictAllArgs, C = a.def, I = a.objectify;
(function () {
a.clock = new U;
var G = function () {
this.$id = -1
}.$extend(U);
G.prototype.cancel = function () {
clearTimeout(this.$id);
delete V[this.$id.toString()];
this.$id = -1
};
G.prototype.isExpired =
function () {
return this.$id === -1
};
var u = function () {
u.$super.apply(this, arguments)
}.$extend(G);
u.prototype.cancel = function () {
clearInterval(this.$id);
delete O[this.$id.toString()];
this.$id = -1
};
a.clock.ticks = function () {
return (new Date).valueOf()
};
a.clock.expiredTicket = new G;
a.clock.schedule = function (o, q) {
K([o, q], [Function, Number]);
if (a.isStopped())return this.expiredTicket;
var b = new G;
b.$id = setTimeout(function () {
delete V[b.$id.toString()];
b.$id = -1;
o.call(b)
}, q);
return V[b.$id.toString()] = b
};
a.clock.schedulePeriodically =
function (o, q) {
K([o, q], [Function, Number]);
if (a.isStopped())return this.expiredTicket;
var b = new u;
b.$id = setInterval(function () {
o.call(b)
}, q);
return O[b.$id.toString()] = b
}
})();
(function () {
var G = false, u = false, o = false, q = null, b = null, e = "", f = "", g = "station:|slot:/", p = "",
v = false, B = null;
a.isStarted = function () {
return G
};
a.isStopped = function () {
return u
};
a.isStopping = function () {
return o
};
a.started = function (r) {
y(r, Function);
if (G)try {
r.call(a)
} catch (F) {
a.error(F)
} else {
q || (q = []);
q.push(r)
}
};
a.preStop = function (r) {
y(r,
Function);
if (u)try {
r.call(a)
} catch (F) {
a.error(F)
} else {
b || (b = []);
b.push(r)
}
};
a.start = function (r) {
r = I(r, "started");
var F = r.started;
r.started = function () {
G = true;
if (q) {
var k;
for (k = 0; k < q.length; ++k)try {
q[k].call(a)
} catch (m) {
a.error(m)
}
q = null
}
if (typeof F === "function")try {
F()
} catch (A) {
a.error(A)
}
};
a.comm.start(r);
a.registry.register(JSON.parse(a.$ctypes));
delete a.$ctypes;
B = a.registry.loadFromStorage();
typeof r.preStart === "function" && r.preStart()
};
a.save = function () {
T && a.registry.saveToStorage(T)
};
a.stop =
function (r) {
try {
if (!u) {
o = true;
a.save();
r = I(r, "stopped");
typeof r.preStop === "function" && a.preStop(r.preStop);
if (b) {
var F;
for (F = 0; F < b.length; ++F)try {
b[F].call(a)
} catch (k) {
a.error(k)
}
b = null
}
for (var m in V)V.hasOwnProperty(m) && V[m].cancel();
for (m in O)O.hasOwnProperty(m) && O[m].cancel();
V = {};
O = {};
a.comm.stop(r)
}
} finally {
u = true
}
};
a.getUserName = function () {
return e
};
a.getLanguage = function () {
return f
};
a.getUserHome = function () {
return a.Ord.make(g)
};
a.getTimeFormatPattern = function () {
return p
};
a.isLogClientErrorsInServer =
function () {
return v
};
a.initFromSysProps = function (r) {
e = r.userName;
f = r.lang;
g = r.userHome;
p = r.timeFormat;
v = r.logClientErrors || false;
if (r.enableWebStorage) {
if (B)if (B.lang !== r.lang || B.version !== a.version || B.regLastBuildTime !== r.regLastBuildTime) {
B = null;
a.registry.clearStorage()
}
B || (B = {
lang: r.lang,
version: a.version,
regLastBuildTime: r.regLastBuildTime,
types: {},
lexicons: {}
});
T = B;
B = null;
a.registry.register(T.types);
for (var F in T.lexicons)if (T.lexicons.hasOwnProperty(F)) R[F] = new Q(F, T.lexicons[F])
} else {
B =
null;
a.registry.clearStorage()
}
}
})();
(function () {
function G(u, o, q, b, e) {
K([o, q, b, e], [Array, Boolean, a.comm.Callback, Boolean]);
var f = [], g, p;
for (p = 0; p < o.length; ++p)if (u.$types.hasOwnProperty(o[p])) {
g = u.$types[o[p]];
if (q && (g.isComplex() || g.isFrozenEnum()) && !g.hasContract()) f.push(o[p])
} else f.push(o[p]);
var v = null;
b.addOk(function (B, r, F) {
v = [];
F !== undefined && u.register(F, true);
for (r = 0; r < o.length; ++r)v.push(u.$types[o[r]]);
B(v)
});
f.length > 0 ? a.comm.loadTypes(f, q, b, e) : b.ok();
return v
}
a.$ = function (u) {
var o =
a.lt(u, true);
if (!o)throw Error("Type '" + u + "' not found");
if (arguments.length === 1)return o.getInstance(); else {
var q = Array.prototype.slice.call(arguments);
q.shift();
return o.getInstance.apply(o, q)
}
};
a.importTypes = function (u) {
u = I(u, "typeSpecs");
var o = new a.comm.Callback(u.ok, u.fail, u.batch);
try {
return G(a.registry, u.typeSpecs, true, o, u.ok !== undefined)
} catch (q) {
o.fail(q)
}
return null
};
a.lt = function (u, o) {
if (a.registry.$types.hasOwnProperty(u))return a.registry.$types[u]; else {
o = C(o, false);
var q = new a.comm.Callback;
try {
var b = G(a.registry, [u], o, q, false);
if (b !== null)return b[0]
} catch (e) {
q.fail(e)
}
return null
}
};
(function () {
function u(f) {
if (!f.isFrozenEnum())throw Error("Type must be a FrozenEnum");
if (!f.hasContract()) {
f.loadContract();
if (!f.hasContract())throw Error("Unable to load the FrozenEnum Contract");
}
}
var o = {}, q = {}, b = {}, e = function (f, g, p, v, B, r, F, k, m, A, n, w, z, D, c, i, s, x, E) {
this.$typeSpec = f;
this.$superType = g;
this.$isAbstract = p;
this.$isInterface = v;
this.$interfaces = B;
this.$contract = r;
this.$isTransient = F;
this.$iconStr =
k;
this.$isValue = m;
this.$isSimple = A;
this.$isSingleton = n;
this.$isNumber = w;
this.$isComplex = z;
this.$isComponent = D;
this.$isLink = c;
this.$isAction = i;
this.$isTopic = s;
this.$isFrozenEnum = x;
this.$isOrdScheme = E
}.$extend(U);
e.prototype.equals = function (f) {
if (f === undefined || f === null)return false;
if (f.constructor !== e)return false;
return f.$typeSpec === this.$typeSpec
};
e.prototype.getModuleName = function () {
return this.$typeSpec.split(":")[0]
};
e.prototype.getTypeName = function () {
return this.$typeSpec.split(":")[1]
};
e.prototype.getTypeSpec =
function () {
return this.$typeSpec
};
e.prototype.getInstance = function (f) {
var g = this.getTypeSpec();
if (this.$isInterface)throw Error("Cannot call 'getInstance' on an Interface Type: " + g);
if (this.$isAbstract)throw Error("Cannot call 'getInstance' on an Abstract Type: " + g);
if ((this.isSimple() || this.isSingleton()) && b.hasOwnProperty(g))if (arguments.length === 0)return b[g].DEFAULT; else {
g = b[g].DEFAULT;
return g.make.apply(g, arguments)
}
if (q.hasOwnProperty(g))if (arguments.length === 0)return q[g]; else {
g = q[g];
return g.make.apply(g,
arguments)
}
for (var p = this, v; p !== null;)if (b.hasOwnProperty(p.getTypeSpec())) {
v = b[p.getTypeSpec()];
break
} else p = p.$superType;
if (p === null)throw Error("Could not find JS Constructor for Type: " + g);
if (typeof v !== "function")throw Error("Fatal error, could not create instance of Type: " + g);
p = new v(f);
if (g !== p.getType().getTypeSpec()) {
var B = this;
p.getType = function () {
return B
}
}
if (this.isComplex() || this.isFrozenEnum()) {
if (this.isComplex()) a.bson.decodeComplexContract(this, p); else if (this.isFrozenEnum()) {
this.hasContract() ||
this.loadContract();
v = this.getOrdinals();
if (v.length > 0) p.$ordinal = v[0]
}
p.contractCommitted(f)
}
if (this.isSimple()) {
q[g] = p;
if (arguments.length > 0) p = p.make.apply(p, arguments)
}
return p
};
e.prototype.is = function (f) {
if (f.constructor === String) f = a.lt(f);
if (this.$typeSpec === f.$typeSpec)return true;
var g;
if (f.$isInterface)for (g = 0; g < this.$interfaces.length; ++g) {
if (this.$interfaces[g].is(f))return true
} else if (f.$isSimple && !this.$isSimple)return false; else if (f.$isComponent && !this.$isComponent)return false; else if (f.$isComplex &&
!this.$isComplex)return false;
if (this.$superType)return this.$superType.is(f);
return false
};
e.prototype.isValue = function () {
return this.$isValue
};
e.prototype.isSimple = function () {
return this.$isSimple
};
e.prototype.isSingleton = function () {
return this.$isSingleton
};
e.prototype.isNumber = function () {
return this.$isNumber
};
e.prototype.isComplex = function () {
return this.$isComplex
};
e.prototype.isComponent = function () {
return this.$isComponent
};
e.prototype.isStruct = function () {
return this.isComplex() && !this.isComponent()
};
e.prototype.isLink = function () {
return this.$isLink
};
e.prototype.isAction = function () {
return this.$isAction
};
e.prototype.isTopic = function () {
return this.$isTopic
};
e.prototype.isFrozenEnum = function () {
return this.$isFrozenEnum
};
e.prototype.isOrdScheme = function () {
return this.$isOrdScheme
};
e.prototype.getSuperType = function () {
return this.$superType
};
e.prototype.getInterfaces = function () {
return this.$interfaces.slice(0)
};
e.prototype.isAbstract = function () {
return this.$isAbstract
};
e.prototype.isInterface = function () {
return this.$isInterface
};
e.prototype.isTransient = function () {
return this.$isTransient
};
e.prototype.toString = function () {
return this.getTypeSpec()
};
e.prototype.getContract = function () {
return this.$contract
};
e.prototype.hasContract = function () {
return this.$contract !== null
};
e.prototype.loadContract = function (f) {
f = I(f);
var g = new a.comm.Callback(f.ok, f.fail, f.batch);
try {
if (!(this.isComplex() || this.isFrozenEnum()) || this.isInterface() || this.hasContract()) g.ok(); else {
g.addOk(function (v, B, r) {
var F;
B = [];
for (F in r)if (r.hasOwnProperty(F)) a.registry.$types.hasOwnProperty(F) ||
B.push(F);
B.length > 0 && G(a.registry, B, false, new a.comm.Callback, false);
for (F in r)if (r.hasOwnProperty(F))if (a.registry.$types.hasOwnProperty(F)) {
a.registry.$types[F].$contract = r[F];
if (T && T.types.hasOwnProperty(F)) T.types[F].c = r[F]
}
v()
});
a.comm.loadContract(this.getTypeSpec(), g, f.ok !== undefined)
}
} catch (p) {
g.fail(p)
}
};
e.prototype.getIcon = function () {
if (this.$icon)return this.$icon;
return this.$icon = this.$iconStr ? a.Icon.DEFAULT.decodeFromString(this.$iconStr) : a.Icon.getStdObjectIcon()
};
e.prototype.getOrdinals =
function () {
u(this);
var f = [], g;
for (g = 0; g < this.$contract.length; ++g)f.push(this.$contract[g].o);
return f
};
e.prototype.isOrdinal = function (f) {
u(this);
var g = this.$contract, p;
if (this.$byOrdinal === undefined) {
this.$byOrdinal = {};
for (p = 0; p < g.length; ++p)this.$byOrdinal[g[p].o] = {o: g[p].o, t: g[p].t, dt: g[p].dt}
}
return this.$byOrdinal.hasOwnProperty(f)
};
e.prototype.getTag = function (f) {
u(this);
if (this.isOrdinal(f))return this.$byOrdinal[f].t; else throw Error("No tag for ordinal: " + f + " in: " + this.getTypeSpec());
};
e.prototype.getDisplayTag =
function (f) {
u(this);
if (this.isOrdinal(f))return this.$byOrdinal[f].dt; else throw Error("No display tag for ordinal: " + f + " in: " + this.getTypeSpec());
};
e.prototype.isTag = function (f) {
u(this);
var g = this.$contract, p;
if (this.$byTag === undefined) {
this.$byTag = {};
for (p = 0; p < g.length; ++p)this.$byTag[g[p].t] = {o: g[p].o, t: g[p].t, dt: g[p].dt}
}
return this.$byTag.hasOwnProperty(f)
};
e.prototype.tagToOrdinal = function (f) {
u(this);
if (this.isTag(f))return this.$byTag[f].o; else throw Error("No ordinal tag for tag: " + f + " in: " +
this.getTypeSpec());
};
e.prototype.getRange = function () {
u(this);
if (this.$range === undefined) this.$range = a.EnumRange.make(this);
return this.$range
};
e.prototype.getFrozenEnum = function (f) {
u(this);
var g = 0;
if (typeof f === "string") g = this.tagToOrdinal(f); else if (typeof f === "number" && this.isOrdinal(f)) g = f; else throw Error("Invalid argument for FrozenEnum creation");
if (this.$enums === undefined) this.$enums = {};
if (this.$enums.hasOwnProperty(g)) f = this.$enums[g]; else {
f = this.getInstance();
if (f.getOrdinal() !== g) {
var p =
new f.constructor;
p.getType = f.getType;
f = p;
f.$ordinal = g
}
this.$enums[g] = f
}
return f
};
e.prototype.hasConstructor = function () {
return b.hasOwnProperty(this.$typeSpec)
};
Function.prototype.registerType = function (f) {
y(f, String);
if (typeof this !== "function")throw Error("Can only loadType on a Function Constructor: " + f);
b[f] = this;
var g;
this.prototype.getType = function () {
g || (g = a.lt(f));
return g
};
return this
};
a.registry = new U;
a.registry.$types = {
"baja:Object": new e("baja:Object", null, true, false, [], null, false, null,
false, false, false, false, false, false, false, false, false, false, false),
"baja:Interface": new e("baja:Interface", null, true, true, [], null, false, null, false, false, false, false, false, false, false, false, false, false, false)
};
a.registry.register = function (f, g) {
if (f !== undefined) {
var p = a.lt("baja:Interface"), v = a.lt("baja:Object"), B, r, F = {};
for (B in f)if (f.hasOwnProperty(B)) {
r = f[B];
if (this.$types.hasOwnProperty(B)) {
if (r.c) {
this.$types[B].$contract = r.c;
if (T && T.types.hasOwnProperty(B)) T.types[B].c = r.c
}
} else {
this.$types[B] =
new e(B, null, r.a || false, r.i || false, [], C(r.c, null), r.t || false, C(r.ic, null), r.isv || false, r.iss || false, r.isg || false, r.isn || false, r.isx || false, r.isc || false, r.isl || false, r.isa || false, r.ist || false, r.ise || false, r.os || false);
F[B] = r;
if (g && T) r.t || (T.types[B] = r)
}
}
var k, m;
for (B in F)if (F.hasOwnProperty(B)) {
r = F[B];
m = this.$types[B];
if (!(m === p || m === v)) {
if (r.it && r.it.length > 0)for (k = 0; k < r.it.length; ++k)m.$interfaces.push(a.lt(r.it[k])); else r.i && m.$interfaces.push(p);
if (!r.i) m.$superType = r.p ? a.lt(r.p) : v
}
}
for (B in F)if (F.hasOwnProperty(B)) {
r =
F[B];
m = a.lt(B);
if (!m.isAbstract()) {
if (b.hasOwnProperty(B)) {
p = b[B];
if (m.isSimple()) {
if (p.DEFAULT === undefined)throw Error("Concrete Simple implementations must define a DEFAULT instance argument on the Constructor: " + B);
if (typeof p.DEFAULT.make !== "function")throw Error("Concrete Simple implementations must define a make method: " + B);
if (typeof p.DEFAULT.decodeFromString !== "function")throw Error("Concrete Simple implementations must define a decodeFromString method: " + B);
if (typeof p.DEFAULT.encodeToString !==
"function")throw Error("Concrete Simple implementations must define a encodeToString method: " + B);
}
if (m.isSingleton())if (p.DEFAULT === undefined)throw Error("Concrete Singletons must define a DEFAULT instance argument on the Constructor: " + B);
}
if (r.os && m.isOrdScheme()) o[r.os] = m
}
}
}
};
a.registry.getOrdScheme = function (f) {
return C(o[f], null)
};
a.registry.hasType = function (f) {
return this.$types.hasOwnProperty(f)
};
a.registry.loadType = a.lt;
a.registry.loadTypesWithContract = a.importTypes;
a.registry.getConcreteTypes =
function (f) {
f = I(f);
var g;
if (f.type) g = f.type.toString();
a.strictAllArgs([g, f.ok], [String, Function]);
f = new a.comm.Callback(f.ok, f.fail, f.batch);
f.addOk(function (p, v, B) {
v = B.s;
a.registry.register(B.t, true);
var r = [];
a.iterate(v, function (F) {
r.push(a.lt(F))
});
p(r)
});
a.comm.getConcreteTypes(g, f)
};
a.registry.clearStorage = function () {
};
a.registry.saveToStorage = function () {
};
a.registry.loadFromStorage = function () {
return null
}
})()
})()
})(baja, BaseBajaObj);
(function (a, U) {
function R(b) {
function e() {
var g = new Q(function () {
a.comm.reconnect()
}, function (p) {
p.noReconnect || (p.delayReconnect ? a.clock.schedule(e, 1E3) : a.comm.reconnect())
});
g.addReq("sys", "hello", {});
g.commit()
}
if (!a.isStopping()) {
C = null;
try {
u(b)
} catch (f) {
}
I.cancel();
b.noReconnect || e()
}
}
function T(b, e, f) {
if (!(e >= b.$queue.length)) {
var g = null, p = b.$queue[e].m, v = b.$queue[e].cb, B, r;
try {
v.$batchNext = function () {
v.$batchNext = a.noop;
T(b, ++e, f)
};
if (!p)try {
v.ok();
return
} catch (F) {
v.fail(F)
}
if (p.t !== "rt") v.ok();
else {
for (B = 0; B < f.m.length; ++B)if (f.m[B].r === p.r) {
g = f.m[B];
break
}
if (g !== null)if (g.t === "rp")try {
v.ok(g.b)
} catch (k) {
v.fail(k)
} else if (g.t === "e") {
r = new a.comm.BoxError(g.et, g.b);
if (g.cf) {
r.noReconnect = true;
r.sessionLimit = g.et === "BoxSessionLimitError";
r.fatalFault = g.et === "BoxFatalFaultError";
r.nonOperational = g.et === "BoxNonOperationalError";
R(r)
}
v.fail(r)
} else v.fail(Error("Fatal error reading BOX Frame: " + JSON.stringify(f))); else v.fail(Error("BOX Error: response not found for request: " + JSON.stringify(p)))
}
} catch (m) {
a.error(m)
}
}
}
function V(b, e, f) {
if (!(e >= b.$queue.length)) {
var g = b.$queue[e].cb;
try {
g.$batchNext = function () {
g.$batchNext = a.noop;
V(b, ++e, f)
};
g.fail(f)
} catch (p) {
a.error(p)
}
}
}
var O = a.strictArg, Q;
a.comm = new U;
var y = 0, K = 2500, C = null, I = a.clock.expiredTicket, G = {}, u = function (b) {
a.outln("Comms failed: " + b.name);
b.noReconnect || a.outln("Attempting reconnect...")
};
a.comm.setCommFailCallback = function (b) {
O(b, Function);
u = b
};
a.comm.reconnect = function () {
throw Error("baja.comm.reconnect not implemented");
};
a.comm.ServerError = function (b) {
this.name =
"ServerError";
this.message = b || "Default Message"
}.$extend(Error);
a.comm.BoxError = function (b, e) {
this.name = b;
this.message = e || "Default Message"
}.$extend(a.comm.ServerError);
a.comm.BoxFrame = function (b) {
var e = [], f;
for (f = 0; f < b.length; ++f)b[f].m && e.push(b[f].m);
this.$body = {p: "box", v: 1, m: e}
};
a.comm.BoxFrame.prototype.toString = function () {
return JSON.stringify(this.$body)
};
a.comm.BoxFrame.prototype.isEmpty = function () {
return this.$body.m.length === 0
};
a.comm.BoxFrame.prototype.send = function () {
throw Error("baja.comm.BoxFrame#send not implemented");
};
a.comm.Batch = function () {
this.$queue = [];
this.$committed = false;
this.$async = true
}.$extend(U);
a.comm.Batch.prototype.addReq = function (b, e, f, g) {
if (this.$committed)throw Error("Cannot add request to a commited Batch!");
this.$queue.push({m: {r: y++, t: "rt", c: b, k: e, b: f}, cb: g})
};
a.comm.Batch.prototype.addCallback = function (b, e) {
if (this.$committed)throw Error("Cannot add callback to a commited Batch!");