1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
8 |
|
9 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
10 |
|
11 | var _fact = require('./fact');
|
12 |
|
13 | var _fact2 = _interopRequireDefault(_fact);
|
14 |
|
15 | var _errors = require('./errors');
|
16 |
|
17 | var _debug = require('./debug');
|
18 |
|
19 | var _debug2 = _interopRequireDefault(_debug);
|
20 |
|
21 | var _selectn = require('selectn');
|
22 |
|
23 | var _selectn2 = _interopRequireDefault(_selectn);
|
24 |
|
25 | var _lodash = require('lodash.isobjectlike');
|
26 |
|
27 | var _lodash2 = _interopRequireDefault(_lodash);
|
28 |
|
29 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
30 |
|
31 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | var Almanac = function () {
|
39 | function Almanac(factMap) {
|
40 | var runtimeFacts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
41 |
|
42 | _classCallCheck(this, Almanac);
|
43 |
|
44 | this.factMap = new Map(factMap);
|
45 | this.factResultsCache = new Map();
|
46 |
|
47 | for (var factId in runtimeFacts) {
|
48 | var fact = void 0;
|
49 | if (runtimeFacts[factId] instanceof _fact2.default) {
|
50 | fact = runtimeFacts[factId];
|
51 | } else {
|
52 | fact = new _fact2.default(factId, runtimeFacts[factId]);
|
53 | }
|
54 |
|
55 | this._addConstantFact(fact);
|
56 | (0, _debug2.default)('almanac::constructor initialized runtime fact:' + fact.id + ' with ' + fact.value + '<' + _typeof(fact.value) + '>');
|
57 | }
|
58 | }
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | _createClass(Almanac, [{
|
68 | key: '_getFact',
|
69 | value: function _getFact(factId) {
|
70 | return this.factMap.get(factId);
|
71 | }
|
72 |
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 | }, {
|
79 | key: '_addConstantFact',
|
80 | value: function _addConstantFact(fact) {
|
81 | this.factMap.set(fact.id, fact);
|
82 | this._setFactValue(fact, {}, fact.value);
|
83 | }
|
84 |
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | }, {
|
93 | key: '_setFactValue',
|
94 | value: function _setFactValue(fact, params, value) {
|
95 | var cacheKey = fact.getCacheKey(params);
|
96 | var factValue = Promise.resolve(value);
|
97 | if (cacheKey) {
|
98 | this.factResultsCache.set(cacheKey, factValue);
|
99 | }
|
100 | return factValue;
|
101 | }
|
102 |
|
103 | |
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | }, {
|
110 | key: 'addRuntimeFact',
|
111 | value: function addRuntimeFact(factId, value) {
|
112 | var fact = new _fact2.default(factId, value);
|
113 | return this._addConstantFact(fact);
|
114 | }
|
115 |
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | }, {
|
126 | key: 'factValue',
|
127 | value: function factValue(factId) {
|
128 | var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
129 | var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
130 |
|
131 | var factValuePromise = void 0;
|
132 | var fact = this._getFact(factId);
|
133 | if (fact === undefined) {
|
134 | return Promise.reject(new _errors.UndefinedFactError('Undefined fact: ' + factId));
|
135 | }
|
136 | if (fact.isConstant()) {
|
137 | factValuePromise = Promise.resolve(fact.calculate(params, this));
|
138 | } else {
|
139 | var cacheKey = fact.getCacheKey(params);
|
140 | var cacheVal = cacheKey && this.factResultsCache.get(cacheKey);
|
141 | if (cacheVal) {
|
142 | factValuePromise = Promise.resolve(cacheVal);
|
143 | (0, _debug2.default)('almanac::factValue cache hit for fact:' + factId);
|
144 | } else {
|
145 | (0, _debug2.default)('almanac::factValue cache miss for fact:' + factId + '; calculating');
|
146 | factValuePromise = this._setFactValue(fact, params, fact.calculate(params, this));
|
147 | }
|
148 | }
|
149 | if (path) {
|
150 | return factValuePromise.then(function (factValue) {
|
151 | if ((0, _lodash2.default)(factValue)) {
|
152 | var pathValue = (0, _selectn2.default)(path)(factValue);
|
153 | (0, _debug2.default)('condition::evaluate extracting object property ' + path + ', received: ' + pathValue);
|
154 | return pathValue;
|
155 | } else {
|
156 | (0, _debug2.default)('condition::evaluate could not compute object path(' + path + ') of non-object: ' + factValue + ' <' + (typeof factValue === 'undefined' ? 'undefined' : _typeof(factValue)) + '>; continuing with ' + factValue);
|
157 | return factValue;
|
158 | }
|
159 | });
|
160 | }
|
161 | return factValuePromise;
|
162 | }
|
163 | }]);
|
164 |
|
165 | return Almanac;
|
166 | }();
|
167 |
|
168 | exports.default = Almanac; |
\ | No newline at end of file |