Skip to content

Commit

Permalink
ES2016
Browse files Browse the repository at this point in the history
  • Loading branch information
grassick committed May 27, 2021
1 parent d28af9e commit 1a9e28a
Show file tree
Hide file tree
Showing 124 changed files with 6,279 additions and 10,213 deletions.
4 changes: 2 additions & 2 deletions lib/Database.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Database_1 = require("./database/Database");
test("finds filters by primary key only", function () {
const Database_1 = require("./database/Database");
test("finds filters by primary key only", () => {
expect(Database_1.getWherePrimaryKey(null)).toBeNull();
expect(Database_1.getWherePrimaryKey({ type: "field", table: "t1", column: "x" })).toBeNull();
expect(Database_1.getWherePrimaryKey({ type: "id", table: "t1" })).toBeNull();
Expand Down
332 changes: 113 additions & 219 deletions lib/PageStackDisplay.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/__fixtures__/mockDatabase.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = (function () {
exports.default = () => {
return {
query: jest.fn(),
addChangeListener: jest.fn(),
removeChangeListener: jest.fn(),
transaction: jest.fn()
};
});
};
10 changes: 5 additions & 5 deletions lib/__fixtures__/mockInstanceCtx.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = (function () {
exports.default = () => {
return {
createBlock: jest.fn(),
contextVars: [],
Expand All @@ -9,15 +9,15 @@ exports.default = (function () {
actionLibrary: {},
pageStack: {},
contextVarValues: {},
getFilters: function () { return []; },
getFilters: () => [],
setFilter: jest.fn(),
locale: "en",
onSelectContextVar: jest.fn(),
schema: {},
dataSource: {},
renderChildBlock: jest.fn(),
widgetLibrary: { widgets: {} },
registerForValidation: function () { return function () { }; },
T: function (str) { return str; }
registerForValidation: () => () => { },
T: (str) => str
};
});
};
4 changes: 2 additions & 2 deletions lib/__fixtures__/schema.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var mwater_expressions_1 = require("mwater-expressions");
const mwater_expressions_1 = require("mwater-expressions");
function simpleSchema() {
var schema = new mwater_expressions_1.Schema();
let schema = new mwater_expressions_1.Schema();
schema = schema.addTable({ id: "t1", name: { _base: "en", en: "T1" }, primaryKey: "primary", contents: [
{ id: "text", name: { _base: "en", en: "Text" }, type: "text" },
{ id: "number", name: { _base: "en", en: "Number" }, type: "number" },
Expand Down
44 changes: 13 additions & 31 deletions lib/contextVarValues.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
Expand All @@ -33,47 +20,42 @@ var __importStar = (this && this.__importStar) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateContextVarValue = exports.ContextVarValueEditor = void 0;
var React = __importStar(require("react"));
var blocks_1 = require("./widgets/blocks");
var mwater_expressions_1 = require("mwater-expressions");
var mwater_expressions_ui_1 = require("mwater-expressions-ui");
const React = __importStar(require("react"));
const blocks_1 = require("./widgets/blocks");
const mwater_expressions_1 = require("mwater-expressions");
const mwater_expressions_ui_1 = require("mwater-expressions-ui");
/** Allows editing of the value for one context variable */
var ContextVarValueEditor = /** @class */ (function (_super) {
__extends(ContextVarValueEditor, _super);
function ContextVarValueEditor() {
return _super !== null && _super.apply(this, arguments) || this;
}
ContextVarValueEditor.prototype.render = function () {
var value = this.props.contextVarValue;
class ContextVarValueEditor extends React.Component {
render() {
const value = this.props.contextVarValue;
if (this.props.contextVar.type === "row" && this.props.schema.getTable(this.props.contextVar.table)) {
return React.createElement(mwater_expressions_ui_1.IdLiteralComponent, { schema: this.props.schema, dataSource: this.props.dataSource, idTable: this.props.contextVar.table, value: value, onChange: this.props.onContextVarValueChange });
}
if (this.props.contextVar.type === "rowset") {
return React.createElement(mwater_expressions_ui_1.ExprComponent, { schema: this.props.schema, dataSource: this.props.dataSource, table: this.props.contextVar.table, types: ["boolean"], value: value, onChange: this.props.onContextVarValueChange, variables: blocks_1.createExprVariables(this.props.availContextVars) });
}
return React.createElement(mwater_expressions_ui_1.ExprComponent, { schema: this.props.schema, dataSource: this.props.dataSource, table: this.props.contextVar.table || null, types: [this.props.contextVar.type], idTable: this.props.contextVar.idTable, enumValues: this.props.contextVar.enumValues, value: value, onChange: this.props.onContextVarValueChange, variables: blocks_1.createExprVariables(this.props.availContextVars), preferLiteral: true });
};
return ContextVarValueEditor;
}(React.Component));
}
}
exports.ContextVarValueEditor = ContextVarValueEditor;
/** Validate a context var value */
function validateContextVarValue(schema, contextVar, allContextVars, value) {
var exprValidator = new mwater_expressions_1.ExprValidator(schema, blocks_1.createExprVariables(allContextVars));
const exprValidator = new mwater_expressions_1.ExprValidator(schema, blocks_1.createExprVariables(allContextVars));
// Check type
if (contextVar.type == "row") {
if (value != null && typeof (value) != "string" && typeof (value) != "number") {
return "Invalid value for row variable " + contextVar.name;
return `Invalid value for row variable ${contextVar.name}`;
}
}
else if (contextVar.type == "rowset") {
// rowset must be a boolean expression
var error = exprValidator.validateExpr(value, { table: contextVar.table, types: ["boolean"] });
const error = exprValidator.validateExpr(value, { table: contextVar.table, types: ["boolean"] });
if (error) {
return error;
}
}
else {
var error = exprValidator.validateExpr(value, { types: [contextVar.type] });
const error = exprValidator.validateExpr(value, { types: [contextVar.type] });
if (error) {
return error;
}
Expand Down
11 changes: 5 additions & 6 deletions lib/contexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFilteredContextVarValues = void 0;
var lodash_1 = __importDefault(require("lodash"));
const lodash_1 = __importDefault(require("lodash"));
/** Gets context variables with filters baked into rowsets. Use for all queries, as that may depend on rowset filters */
function getFilteredContextVarValues(instanceCtx) {
var results = {};
for (var _i = 0, _a = instanceCtx.contextVars; _i < _a.length; _i++) {
var cv = _a[_i];
const results = {};
for (const cv of instanceCtx.contextVars) {
if (cv.type == "rowset") {
// Create and expression
var expr = {
const expr = {
type: "op",
op: "and",
table: cv.table,
exprs: lodash_1.default.compact([instanceCtx.contextVarValues[cv.id]].concat(lodash_1.default.map(instanceCtx.getFilters(cv.id), function (f) { return f.expr; })))
exprs: lodash_1.default.compact([instanceCtx.contextVarValues[cv.id]].concat(lodash_1.default.map(instanceCtx.getFilters(cv.id), f => f.expr)))
};
if (expr.exprs.length == 1) {
results[cv.id] = expr.exprs[0];
Expand Down
42 changes: 19 additions & 23 deletions lib/database/BatchingCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,55 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BatchingCache = void 0;
var canonical_json_1 = __importDefault(require("canonical-json"));
const canonical_json_1 = __importDefault(require("canonical-json"));
/** Cache that batches multiple identical requests to avoid repeating the same
* query over and over while the first request is in progress
*/
var BatchingCache = /** @class */ (function () {
function BatchingCache(process) {
class BatchingCache {
constructor(process) {
this.cache = {};
this.process = process;
}
BatchingCache.prototype.get = function (request) {
var _this = this;
get(request) {
// Check if known
var key = canonical_json_1.default(request);
var existing = this.cache[key];
const key = canonical_json_1.default(request);
const existing = this.cache[key];
if (existing) {
if (existing.status == "complete") {
return Promise.resolve(existing.value);
}
// Is pending, add to promises
return new Promise(function (resolve, reject) {
existing.promises.push({ resolve: resolve, reject: reject });
return new Promise((resolve, reject) => {
existing.promises.push({ resolve, reject });
});
}
// Create promise
return new Promise(function (resolve, reject) {
var entry = {
promises: [{ resolve: resolve, reject: reject }],
return new Promise((resolve, reject) => {
const entry = {
promises: [{ resolve, reject }],
status: "pending",
request: request
};
// Add to cache
_this.cache[key] = entry;
this.cache[key] = entry;
// Perform actual process
_this.process(request).then(function (value) {
this.process(request).then((value) => {
// Mark completed
entry.status = "complete";
entry.value = value;
// Resolve all promises
for (var _i = 0, _a = entry.promises; _i < _a.length; _i++) {
var promise = _a[_i];
for (const promise of entry.promises) {
promise.resolve(value);
}
}).catch(function (err) {
}).catch((err) => {
// Remove from cache
delete _this.cache[key];
delete this.cache[key];
// Reject all promises
for (var _i = 0, _a = entry.promises; _i < _a.length; _i++) {
var promise = _a[_i];
for (const promise of entry.promises) {
promise.reject(err);
}
});
});
};
return BatchingCache;
}());
}
}
exports.BatchingCache = BatchingCache;
45 changes: 21 additions & 24 deletions lib/database/DataSourceDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataSourceDatabase = void 0;
var lodash_1 = __importDefault(require("lodash"));
var QueryCompiler_1 = require("./QueryCompiler");
var blocks_1 = require("../widgets/blocks");
const lodash_1 = __importDefault(require("lodash"));
const QueryCompiler_1 = require("./QueryCompiler");
const blocks_1 = require("../widgets/blocks");
/** Database which is driven from a data source. Changes must be handled externally and updates triggered manually */
var DataSourceDatabase = /** @class */ (function () {
function DataSourceDatabase(schema, dataSource, transactionHandler) {
class DataSourceDatabase {
constructor(schema, dataSource, transactionHandler) {
this.schema = schema;
this.dataSource = dataSource;
this.transactionHandler = transactionHandler;
this.changeListeners = [];
}
DataSourceDatabase.prototype.query = function (options, contextVars, filteredContextVarValues) {
var _this = this;
var queryCompiler = new QueryCompiler_1.QueryCompiler(this.schema, blocks_1.createExprVariables(contextVars), blocks_1.createExprVariableValues(contextVars, filteredContextVarValues));
var _a = queryCompiler.compileQuery(options), jsonql = _a.jsonql, rowMapper = _a.rowMapper;
return new Promise(function (resolve, reject) {
_this.dataSource.performQuery(jsonql, function (error, rows) {
query(options, contextVars, filteredContextVarValues) {
const queryCompiler = new QueryCompiler_1.QueryCompiler(this.schema, blocks_1.createExprVariables(contextVars), blocks_1.createExprVariableValues(contextVars, filteredContextVarValues));
const { jsonql, rowMapper } = queryCompiler.compileQuery(options);
return new Promise((resolve, reject) => {
this.dataSource.performQuery(jsonql, (error, rows) => {
if (error) {
reject(error);
}
Expand All @@ -30,28 +29,26 @@ var DataSourceDatabase = /** @class */ (function () {
}
});
});
};
}
/** Adds a listener which is called with each change to the database */
DataSourceDatabase.prototype.addChangeListener = function (changeListener) {
addChangeListener(changeListener) {
this.changeListeners = lodash_1.default.union(this.changeListeners, [changeListener]);
};
DataSourceDatabase.prototype.removeChangeListener = function (changeListener) {
}
removeChangeListener(changeListener) {
this.changeListeners = lodash_1.default.difference(this.changeListeners, [changeListener]);
};
}
/** Force change event to fire after clearing cache */
DataSourceDatabase.prototype.triggerChange = function () {
triggerChange() {
this.dataSource.clearCache();
for (var _i = 0, _a = this.changeListeners; _i < _a.length; _i++) {
var changeListener = _a[_i];
for (const changeListener of this.changeListeners) {
changeListener();
}
};
DataSourceDatabase.prototype.transaction = function () {
}
transaction() {
if (!this.transactionHandler) {
throw new Error("Not implemented");
}
return this.transactionHandler();
};
return DataSourceDatabase;
}());
}
}
exports.DataSourceDatabase = DataSourceDatabase;
Loading

0 comments on commit 1a9e28a

Please sign in to comment.