1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | 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; }; }();
|
8 |
|
9 | var _hashIt = require('hash-it');
|
10 |
|
11 | var _hashIt2 = _interopRequireDefault(_hashIt);
|
12 |
|
13 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
14 |
|
15 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
16 |
|
17 | var Fact = function () {
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function Fact(id, valueOrMethod, options) {
|
27 | _classCallCheck(this, Fact);
|
28 |
|
29 | this.id = id;
|
30 | var defaultOptions = { cache: true };
|
31 | if (typeof options === 'undefined') {
|
32 | options = defaultOptions;
|
33 | }
|
34 | if (typeof valueOrMethod !== 'function') {
|
35 | this.value = valueOrMethod;
|
36 | this.type = this.constructor.CONSTANT;
|
37 | } else {
|
38 | this.calculationMethod = valueOrMethod;
|
39 | this.type = this.constructor.DYNAMIC;
|
40 | }
|
41 |
|
42 | if (!this.id) throw new Error('factId required');
|
43 | if (typeof this.value === 'undefined' && typeof this.calculationMethod === 'undefined') {
|
44 | throw new Error('facts must have a value or method');
|
45 | }
|
46 |
|
47 | this.priority = parseInt(options.priority || 1, 10);
|
48 | this.options = Object.assign({}, defaultOptions, options);
|
49 | this.cacheKeyMethod = this.defaultCacheKeys;
|
50 | return this;
|
51 | }
|
52 |
|
53 | _createClass(Fact, [{
|
54 | key: 'isConstant',
|
55 | value: function isConstant() {
|
56 | return this.type === this.constructor.CONSTANT;
|
57 | }
|
58 | }, {
|
59 | key: 'isDynamic',
|
60 | value: function isDynamic() {
|
61 | return this.type === this.constructor.DYNAMIC;
|
62 | }
|
63 |
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | }, {
|
72 | key: 'calculate',
|
73 | value: function calculate(params, almanac) {
|
74 |
|
75 | if (Object.prototype.hasOwnProperty.call(this, 'value')) {
|
76 | return this.value;
|
77 | }
|
78 | return this.calculationMethod(params, almanac);
|
79 | }
|
80 |
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | }, {
|
88 | key: 'defaultCacheKeys',
|
89 |
|
90 |
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | value: function defaultCacheKeys(id, params) {
|
100 | return { params: params, id: id };
|
101 | }
|
102 |
|
103 | |
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | }, {
|
111 | key: 'getCacheKey',
|
112 | value: function getCacheKey(params) {
|
113 | if (this.options.cache === true) {
|
114 | var cacheProperties = this.cacheKeyMethod(this.id, params);
|
115 | var _hash = Fact.hashFromObject(cacheProperties);
|
116 | return _hash;
|
117 | }
|
118 | }
|
119 | }], [{
|
120 | key: 'hashFromObject',
|
121 | value: function hashFromObject(obj) {
|
122 | return (0, _hashIt2.default)(obj);
|
123 | }
|
124 | }]);
|
125 |
|
126 | return Fact;
|
127 | }();
|
128 |
|
129 | Fact.CONSTANT = 'CONSTANT';
|
130 | Fact.DYNAMIC = 'DYNAMIC';
|
131 |
|
132 | exports.default = Fact; |
\ | No newline at end of file |