bruce-models 1.9.8 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/dist/bruce-models.es5.js +317 -74
  2. package/dist/bruce-models.es5.js.map +1 -1
  3. package/dist/bruce-models.umd.js +317 -74
  4. package/dist/bruce-models.umd.js.map +1 -1
  5. package/dist/lib/account/account-settings.js +2 -2
  6. package/dist/lib/account/account.js +201 -185
  7. package/dist/lib/account/account.js.map +1 -1
  8. package/dist/lib/ann-document/ann-document.js +94 -86
  9. package/dist/lib/ann-document/ann-document.js.map +1 -1
  10. package/dist/lib/api/abstract-api.js +260 -246
  11. package/dist/lib/api/abstract-api.js.map +1 -1
  12. package/dist/lib/api/api-getters.js +134 -134
  13. package/dist/lib/api/api.js +93 -93
  14. package/dist/lib/api/bruce-api.js +216 -216
  15. package/dist/lib/api/cam-api.js +85 -82
  16. package/dist/lib/api/cam-api.js.map +1 -1
  17. package/dist/lib/api/global-api.js +81 -81
  18. package/dist/lib/api/idm-api.js +85 -82
  19. package/dist/lib/api/idm-api.js.map +1 -1
  20. package/dist/lib/bruce-models.js +86 -86
  21. package/dist/lib/calculator/calculator.js +341 -341
  22. package/dist/lib/client-file/client-file.js +249 -245
  23. package/dist/lib/client-file/client-file.js.map +1 -1
  24. package/dist/lib/common/bounds.js +61 -61
  25. package/dist/lib/common/bruce-event.js +46 -46
  26. package/dist/lib/common/bruce-variable.js +60 -60
  27. package/dist/lib/common/cache.js +165 -165
  28. package/dist/lib/common/camera.js +11 -11
  29. package/dist/lib/common/cartes.js +123 -123
  30. package/dist/lib/common/carto.js +60 -60
  31. package/dist/lib/common/color.js +86 -86
  32. package/dist/lib/common/delay-queue.js +56 -56
  33. package/dist/lib/common/dictionary.js +2 -2
  34. package/dist/lib/common/geometry.js +120 -120
  35. package/dist/lib/common/transform.js +2 -2
  36. package/dist/lib/common/utc.js +39 -39
  37. package/dist/lib/custom-form/custom-form-content.js +26 -26
  38. package/dist/lib/custom-form/custom-form.js +114 -106
  39. package/dist/lib/custom-form/custom-form.js.map +1 -1
  40. package/dist/lib/data-lab/data-lab.js +49 -49
  41. package/dist/lib/entity/entity-attachment-type.js +87 -83
  42. package/dist/lib/entity/entity-attachment-type.js.map +1 -1
  43. package/dist/lib/entity/entity-attachment.js +126 -122
  44. package/dist/lib/entity/entity-attachment.js.map +1 -1
  45. package/dist/lib/entity/entity-attribute.js +29 -29
  46. package/dist/lib/entity/entity-comment.js +86 -82
  47. package/dist/lib/entity/entity-comment.js.map +1 -1
  48. package/dist/lib/entity/entity-coords.js +153 -149
  49. package/dist/lib/entity/entity-coords.js.map +1 -1
  50. package/dist/lib/entity/entity-link.js +86 -82
  51. package/dist/lib/entity/entity-link.js.map +1 -1
  52. package/dist/lib/entity/entity-lod-category.js +108 -100
  53. package/dist/lib/entity/entity-lod-category.js.map +1 -1
  54. package/dist/lib/entity/entity-lod.js +197 -185
  55. package/dist/lib/entity/entity-lod.js.map +1 -1
  56. package/dist/lib/entity/entity-relation-type.js +117 -109
  57. package/dist/lib/entity/entity-relation-type.js.map +1 -1
  58. package/dist/lib/entity/entity-relation.js +140 -132
  59. package/dist/lib/entity/entity-relation.js.map +1 -1
  60. package/dist/lib/entity/entity-source.js +136 -136
  61. package/dist/lib/entity/entity-tag.js +157 -149
  62. package/dist/lib/entity/entity-tag.js.map +1 -1
  63. package/dist/lib/entity/entity-type-visual-settings.js +13 -13
  64. package/dist/lib/entity/entity-type.js +121 -113
  65. package/dist/lib/entity/entity-type.js.map +1 -1
  66. package/dist/lib/entity/entity.js +331 -319
  67. package/dist/lib/entity/entity.js.map +1 -1
  68. package/dist/lib/entity/getters/batched-data-getter.js +84 -84
  69. package/dist/lib/entity/getters/entity-filter-getter.js +306 -303
  70. package/dist/lib/entity/getters/entity-filter-getter.js.map +1 -1
  71. package/dist/lib/entity/getters/entity-globe.js +211 -211
  72. package/dist/lib/entity/getters/view-monitor.js +2 -2
  73. package/dist/lib/import/import-cad.js +53 -53
  74. package/dist/lib/import/import-csv.js +23 -23
  75. package/dist/lib/import/import-json.js +23 -23
  76. package/dist/lib/import/import-kml.js +23 -23
  77. package/dist/lib/import/imported-file.js +99 -91
  78. package/dist/lib/import/imported-file.js.map +1 -1
  79. package/dist/lib/internal/uploader.js +76 -76
  80. package/dist/lib/markup/markup.js +185 -185
  81. package/dist/lib/plugin/plugin.js +91 -83
  82. package/dist/lib/plugin/plugin.js.map +1 -1
  83. package/dist/lib/program-key/program-key.js +119 -111
  84. package/dist/lib/program-key/program-key.js.map +1 -1
  85. package/dist/lib/project/menu-item.js +121 -121
  86. package/dist/lib/project/project-view-bookmark.js +159 -147
  87. package/dist/lib/project/project-view-bookmark.js.map +1 -1
  88. package/dist/lib/project/project-view-legacy-tile.js +77 -77
  89. package/dist/lib/project/project-view-tile.js +125 -125
  90. package/dist/lib/project/project-view.js +141 -133
  91. package/dist/lib/project/project-view.js.map +1 -1
  92. package/dist/lib/project/zoom-control.js +17 -17
  93. package/dist/lib/server/message-broker.js +142 -142
  94. package/dist/lib/server/pending-action.js +71 -71
  95. package/dist/lib/server/task.js +63 -63
  96. package/dist/lib/style/style.js +125 -117
  97. package/dist/lib/style/style.js.map +1 -1
  98. package/dist/lib/tileset/tileset.js +431 -415
  99. package/dist/lib/tileset/tileset.js.map +1 -1
  100. package/dist/lib/user/permission.js +20 -20
  101. package/dist/lib/user/session.js +141 -137
  102. package/dist/lib/user/session.js.map +1 -1
  103. package/dist/lib/user/user-group.js +94 -94
  104. package/dist/lib/user/user.js +474 -446
  105. package/dist/lib/user/user.js.map +1 -1
  106. package/dist/lib/util/encrypt-utils.js +19 -19
  107. package/dist/lib/util/math-utils.js +40 -40
  108. package/dist/lib/util/object-utils.js +17 -17
  109. package/dist/lib/util/path-utils.js +61 -61
  110. package/dist/lib/util/url-utils.js +94 -94
  111. package/dist/types/api/abstract-api.d.ts +6 -0
  112. package/package.json +3 -2
@@ -1,62 +1,62 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Bounds = void 0;
4
- const geometry_1 = require("./geometry");
5
- var Bounds;
6
- (function (Bounds) {
7
- /**
8
- * Calculates boundaries from entity.
9
- * This will not use the set entity boundaries and instead calculate from geometry and location.
10
- * @param entity
11
- */
12
- function FromEntity(entity) {
13
- const bounds = {
14
- maxAltitude: null,
15
- maxLatitude: null,
16
- maxLongitude: null,
17
- minAltitude: null,
18
- minLatitude: null,
19
- minLongitude: null
20
- };
21
- const points = [];
22
- if (entity.location) {
23
- points.push(entity.location);
24
- }
25
- if (entity.geometry) {
26
- if (entity.geometry.Point) {
27
- points.push(...geometry_1.Geometry.ParsePoints(entity.geometry.Point));
28
- }
29
- if (entity.geometry.LineString) {
30
- points.push(...geometry_1.Geometry.ParsePoints(entity.geometry.LineString));
31
- }
32
- if (entity.geometry.Polygon) {
33
- for (const ring of entity.geometry.Polygon) {
34
- points.push(...geometry_1.Geometry.ParsePoints(ring.LinearRing));
35
- }
36
- }
37
- }
38
- for (const point of points) {
39
- if (bounds.maxLatitude == null || point.latitude > bounds.maxLatitude) {
40
- bounds.maxLatitude = point.latitude;
41
- }
42
- if (bounds.maxLongitude == null || point.longitude > bounds.maxLongitude) {
43
- bounds.maxLongitude = point.longitude;
44
- }
45
- if (bounds.maxAltitude == null || point.altitude > bounds.maxAltitude) {
46
- bounds.maxAltitude = point.altitude;
47
- }
48
- if (bounds.minLatitude == null || point.latitude < bounds.minLatitude) {
49
- bounds.minLatitude = point.latitude;
50
- }
51
- if (bounds.minLongitude == null || point.longitude < bounds.minLongitude) {
52
- bounds.minLongitude = point.longitude;
53
- }
54
- if (bounds.minAltitude == null || point.altitude < bounds.minAltitude) {
55
- bounds.minAltitude = point.altitude;
56
- }
57
- }
58
- return bounds;
59
- }
60
- Bounds.FromEntity = FromEntity;
61
- })(Bounds = exports.Bounds || (exports.Bounds = {}));
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Bounds = void 0;
4
+ const geometry_1 = require("./geometry");
5
+ var Bounds;
6
+ (function (Bounds) {
7
+ /**
8
+ * Calculates boundaries from entity.
9
+ * This will not use the set entity boundaries and instead calculate from geometry and location.
10
+ * @param entity
11
+ */
12
+ function FromEntity(entity) {
13
+ const bounds = {
14
+ maxAltitude: null,
15
+ maxLatitude: null,
16
+ maxLongitude: null,
17
+ minAltitude: null,
18
+ minLatitude: null,
19
+ minLongitude: null
20
+ };
21
+ const points = [];
22
+ if (entity.location) {
23
+ points.push(entity.location);
24
+ }
25
+ if (entity.geometry) {
26
+ if (entity.geometry.Point) {
27
+ points.push(...geometry_1.Geometry.ParsePoints(entity.geometry.Point));
28
+ }
29
+ if (entity.geometry.LineString) {
30
+ points.push(...geometry_1.Geometry.ParsePoints(entity.geometry.LineString));
31
+ }
32
+ if (entity.geometry.Polygon) {
33
+ for (const ring of entity.geometry.Polygon) {
34
+ points.push(...geometry_1.Geometry.ParsePoints(ring.LinearRing));
35
+ }
36
+ }
37
+ }
38
+ for (const point of points) {
39
+ if (bounds.maxLatitude == null || point.latitude > bounds.maxLatitude) {
40
+ bounds.maxLatitude = point.latitude;
41
+ }
42
+ if (bounds.maxLongitude == null || point.longitude > bounds.maxLongitude) {
43
+ bounds.maxLongitude = point.longitude;
44
+ }
45
+ if (bounds.maxAltitude == null || point.altitude > bounds.maxAltitude) {
46
+ bounds.maxAltitude = point.altitude;
47
+ }
48
+ if (bounds.minLatitude == null || point.latitude < bounds.minLatitude) {
49
+ bounds.minLatitude = point.latitude;
50
+ }
51
+ if (bounds.minLongitude == null || point.longitude < bounds.minLongitude) {
52
+ bounds.minLongitude = point.longitude;
53
+ }
54
+ if (bounds.minAltitude == null || point.altitude < bounds.minAltitude) {
55
+ bounds.minAltitude = point.altitude;
56
+ }
57
+ }
58
+ return bounds;
59
+ }
60
+ Bounds.FromEntity = FromEntity;
61
+ })(Bounds = exports.Bounds || (exports.Bounds = {}));
62
62
  //# sourceMappingURL=bounds.js.map
@@ -1,47 +1,47 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BruceEvent = void 0;
4
- /**
5
- * Simple event utility.
6
- * Instantiate the model, then subscribe and trigger events.
7
- */
8
- class BruceEvent {
9
- constructor() {
10
- // Counter to help differentiate subscriptions.
11
- this._counter = 0;
12
- // Record of all callback subscriptions for this event.
13
- this.callbacks = [];
14
- }
15
- Subscribe(callback) {
16
- let id = this._counter++;
17
- let newBruceEventCallback = {
18
- _id: id,
19
- removeCallback: () => {
20
- this.Unsubscribe(id);
21
- },
22
- callback: callback
23
- };
24
- this.callbacks.push(newBruceEventCallback);
25
- return newBruceEventCallback.removeCallback;
26
- }
27
- Unsubscribe(id) {
28
- let index = this.callbacks.findIndex(x => x._id == id);
29
- if (index > -1) {
30
- this.callbacks.splice(index, 1);
31
- }
32
- }
33
- Trigger(data) {
34
- let callbacks = this.callbacks;
35
- for (let i = 0; i < callbacks.length; i++) {
36
- let callback = callbacks[i];
37
- if (callback.callback) {
38
- callback.callback(data);
39
- }
40
- }
41
- }
42
- Clear() {
43
- this.callbacks = [];
44
- }
45
- }
46
- exports.BruceEvent = BruceEvent;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BruceEvent = void 0;
4
+ /**
5
+ * Simple event utility.
6
+ * Instantiate the model, then subscribe and trigger events.
7
+ */
8
+ class BruceEvent {
9
+ constructor() {
10
+ // Counter to help differentiate subscriptions.
11
+ this._counter = 0;
12
+ // Record of all callback subscriptions for this event.
13
+ this.callbacks = [];
14
+ }
15
+ Subscribe(callback) {
16
+ let id = this._counter++;
17
+ let newBruceEventCallback = {
18
+ _id: id,
19
+ removeCallback: () => {
20
+ this.Unsubscribe(id);
21
+ },
22
+ callback: callback
23
+ };
24
+ this.callbacks.push(newBruceEventCallback);
25
+ return newBruceEventCallback.removeCallback;
26
+ }
27
+ Unsubscribe(id) {
28
+ let index = this.callbacks.findIndex(x => x._id == id);
29
+ if (index > -1) {
30
+ this.callbacks.splice(index, 1);
31
+ }
32
+ }
33
+ Trigger(data) {
34
+ let callbacks = this.callbacks;
35
+ for (let i = 0; i < callbacks.length; i++) {
36
+ let callback = callbacks[i];
37
+ if (callback.callback) {
38
+ callback.callback(data);
39
+ }
40
+ }
41
+ }
42
+ Clear() {
43
+ this.callbacks = [];
44
+ }
45
+ }
46
+ exports.BruceEvent = BruceEvent;
47
47
  //# sourceMappingURL=bruce-event.js.map
@@ -1,61 +1,61 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BruceVariable = void 0;
4
- const entity_1 = require("../entity/entity");
5
- const path_utils_1 = require("../util/path-utils");
6
- function getVariable(str) {
7
- const start = str.indexOf("${");
8
- if (start > -1) {
9
- const end = str.indexOf("}");
10
- if (end > -1) {
11
- if (start < end) {
12
- const path = str.substring(start + 2, end);
13
- const txt = str.substring(start, end + 1);
14
- return { path: path, text: txt };
15
- }
16
- }
17
- }
18
- return null;
19
- }
20
- /**
21
- * Utilities for parsing "Bruce variables", typically within strings.
22
- *
23
- * A Bruce variable is a string that starts with "${" and ends with "}".
24
- * Within those brackets is a path to an attribute in an entity.
25
- */
26
- var BruceVariable;
27
- (function (BruceVariable) {
28
- /**
29
- * Replaces all Bruce variables in a string with the value of the attribute.
30
- * @param str
31
- * @param data
32
- * @param legacyParse
33
- * @returns
34
- */
35
- function SwapValues(params) {
36
- let { str, entity: data, legacyParse } = params;
37
- if (!legacyParse) {
38
- legacyParse = false;
39
- }
40
- while (true) {
41
- const variable = getVariable(str);
42
- if (variable) {
43
- const path = legacyParse ? path_utils_1.PathUtils.ParseLegacy(variable.path) : path_utils_1.PathUtils.Parse(variable.path);
44
- let value = "";
45
- if (path.length > 0) {
46
- value = entity_1.Entity.GetValue({
47
- entity: data,
48
- path: path
49
- });
50
- }
51
- str = str.replace(variable.text, value);
52
- }
53
- else {
54
- break;
55
- }
56
- }
57
- return str;
58
- }
59
- BruceVariable.SwapValues = SwapValues;
60
- })(BruceVariable = exports.BruceVariable || (exports.BruceVariable = {}));
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BruceVariable = void 0;
4
+ const entity_1 = require("../entity/entity");
5
+ const path_utils_1 = require("../util/path-utils");
6
+ function getVariable(str) {
7
+ const start = str.indexOf("${");
8
+ if (start > -1) {
9
+ const end = str.indexOf("}");
10
+ if (end > -1) {
11
+ if (start < end) {
12
+ const path = str.substring(start + 2, end);
13
+ const txt = str.substring(start, end + 1);
14
+ return { path: path, text: txt };
15
+ }
16
+ }
17
+ }
18
+ return null;
19
+ }
20
+ /**
21
+ * Utilities for parsing "Bruce variables", typically within strings.
22
+ *
23
+ * A Bruce variable is a string that starts with "${" and ends with "}".
24
+ * Within those brackets is a path to an attribute in an entity.
25
+ */
26
+ var BruceVariable;
27
+ (function (BruceVariable) {
28
+ /**
29
+ * Replaces all Bruce variables in a string with the value of the attribute.
30
+ * @param str
31
+ * @param data
32
+ * @param legacyParse
33
+ * @returns
34
+ */
35
+ function SwapValues(params) {
36
+ let { str, entity: data, legacyParse } = params;
37
+ if (!legacyParse) {
38
+ legacyParse = false;
39
+ }
40
+ while (true) {
41
+ const variable = getVariable(str);
42
+ if (variable) {
43
+ const path = legacyParse ? path_utils_1.PathUtils.ParseLegacy(variable.path) : path_utils_1.PathUtils.Parse(variable.path);
44
+ let value = "";
45
+ if (path.length > 0) {
46
+ value = entity_1.Entity.GetValue({
47
+ entity: data,
48
+ path: path
49
+ });
50
+ }
51
+ str = str.replace(variable.text, value);
52
+ }
53
+ else {
54
+ break;
55
+ }
56
+ }
57
+ return str;
58
+ }
59
+ BruceVariable.SwapValues = SwapValues;
60
+ })(BruceVariable = exports.BruceVariable || (exports.BruceVariable = {}));
61
61
  //# sourceMappingURL=bruce-variable.js.map
@@ -1,166 +1,166 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CacheControl = void 0;
4
- class CacheItem {
5
- /**
6
- * @param id
7
- * @param data
8
- * @param duration in seconds until considered expired.
9
- */
10
- constructor(id, data, duration) {
11
- this.Id = id;
12
- this.Data = data;
13
- this.Duration = duration;
14
- this._created = duration > -1 ? new Date() : null;
15
- }
16
- IsExpired() {
17
- if (this._created == null) {
18
- return false;
19
- }
20
- return (new Date().getTime() - this._created.getTime()) / 1000 > this.Duration;
21
- }
22
- }
23
- // Milliseconds between each data cleaning.
24
- const CLEANER_INTERVAL = 5000;
25
- // Max number of items to clean in each cleaning.
26
- const MAX_CLEAN_BATCH = 100;
27
- // (Default) Max idle time to keep the cleaner alive.
28
- // Set to default API cache duration by default.
29
- const DEFAULT_MAX_CLEAN_IDLE = 60 * 5;
30
- /**
31
- * Simple local cache controller.
32
- */
33
- class CacheControl {
34
- constructor() {
35
- this.data = {};
36
- this.Disabled = false;
37
- this.cleanerInterval = null;
38
- // Max idle time to keep the cleaner alive.
39
- // This value is increased when a new item is added with a larger cache duration.
40
- this.maxCleanIdle = DEFAULT_MAX_CLEAN_IDLE;
41
- }
42
- /**
43
- * @param id
44
- * @param data
45
- * @param duration seconds to keep the data in cache. -1 for infinite.
46
- */
47
- Set(id, data, duration = -1) {
48
- this.data[id + ""] = new CacheItem(id + "", data, duration);
49
- if (duration > -1) {
50
- this.startCleaning();
51
- if (this.maxCleanIdle < duration) {
52
- // Add 5 seconds to be sure cleaner stays alive to get it.
53
- this.maxCleanIdle = duration + 5;
54
- }
55
- }
56
- }
57
- Get(id) {
58
- if (this.Disabled) {
59
- return null;
60
- }
61
- const item = this.data[id + ""];
62
- if (item == null) {
63
- return null;
64
- }
65
- if (item.IsExpired()) {
66
- delete this.data[id + ""];
67
- return null;
68
- }
69
- return item.Data;
70
- }
71
- Clear() {
72
- this.data = {};
73
- this.stopCleaning();
74
- }
75
- Remove(id) {
76
- delete this.data[id + ""];
77
- if (Object.keys(this.data).length <= 0) {
78
- this.stopCleaning();
79
- }
80
- }
81
- RemoveByStartsWith(text) {
82
- for (const key in this.data) {
83
- if (key.startsWith(text)) {
84
- delete this.data[key];
85
- }
86
- }
87
- if (Object.keys(this.data).length <= 0) {
88
- this.stopCleaning();
89
- }
90
- }
91
- RemoveByContains(text) {
92
- for (const key in this.data) {
93
- if (key.includes(text)) {
94
- delete this.data[key];
95
- }
96
- }
97
- if (Object.keys(this.data).length <= 0) {
98
- this.stopCleaning();
99
- }
100
- }
101
- GetKeys() {
102
- if (this.Disabled) {
103
- return [];
104
- }
105
- return Object.keys(this.data);
106
- }
107
- GetValues() {
108
- if (this.Disabled) {
109
- return [];
110
- }
111
- const values = [];
112
- for (const key in this.data) {
113
- const data = this.Get(key);
114
- if (data != null) {
115
- values.push(data);
116
- }
117
- }
118
- return values;
119
- }
120
- startCleaning() {
121
- if (this.cleanerInterval == null) {
122
- let cleanIdleStart = null;
123
- this.cleanerInterval = setInterval(() => {
124
- let cleaned = 0;
125
- for (const key in this.data) {
126
- const item = this.data[key];
127
- if (item.IsExpired()) {
128
- delete this.data[key];
129
- cleaned += 1;
130
- }
131
- if (cleaned >= MAX_CLEAN_BATCH) {
132
- break;
133
- }
134
- }
135
- if (cleaned <= 0) {
136
- // Stop cleaner if nothing in cache.
137
- if (Object.keys(this.data).length <= 0) {
138
- this.stopCleaning();
139
- }
140
- // Stop cleaner if idle time is reached.
141
- else {
142
- if (cleanIdleStart == null) {
143
- cleanIdleStart = new Date().getTime();
144
- }
145
- const now = new Date().getTime();
146
- if (now - cleanIdleStart > this.maxCleanIdle * 1000) {
147
- this.stopCleaning();
148
- }
149
- }
150
- }
151
- else {
152
- cleanIdleStart = null;
153
- }
154
- }, CLEANER_INTERVAL);
155
- }
156
- }
157
- stopCleaning() {
158
- if (this.cleanerInterval != null) {
159
- clearInterval(this.cleanerInterval);
160
- this.cleanerInterval = null;
161
- this.maxCleanIdle = DEFAULT_MAX_CLEAN_IDLE;
162
- }
163
- }
164
- }
165
- exports.CacheControl = CacheControl;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CacheControl = void 0;
4
+ class CacheItem {
5
+ /**
6
+ * @param id
7
+ * @param data
8
+ * @param duration in seconds until considered expired.
9
+ */
10
+ constructor(id, data, duration) {
11
+ this.Id = id;
12
+ this.Data = data;
13
+ this.Duration = duration;
14
+ this._created = duration > -1 ? new Date() : null;
15
+ }
16
+ IsExpired() {
17
+ if (this._created == null) {
18
+ return false;
19
+ }
20
+ return (new Date().getTime() - this._created.getTime()) / 1000 > this.Duration;
21
+ }
22
+ }
23
+ // Milliseconds between each data cleaning.
24
+ const CLEANER_INTERVAL = 5000;
25
+ // Max number of items to clean in each cleaning.
26
+ const MAX_CLEAN_BATCH = 100;
27
+ // (Default) Max idle time to keep the cleaner alive.
28
+ // Set to default API cache duration by default.
29
+ const DEFAULT_MAX_CLEAN_IDLE = 60 * 5;
30
+ /**
31
+ * Simple local cache controller.
32
+ */
33
+ class CacheControl {
34
+ constructor() {
35
+ this.data = {};
36
+ this.Disabled = false;
37
+ this.cleanerInterval = null;
38
+ // Max idle time to keep the cleaner alive.
39
+ // This value is increased when a new item is added with a larger cache duration.
40
+ this.maxCleanIdle = DEFAULT_MAX_CLEAN_IDLE;
41
+ }
42
+ /**
43
+ * @param id
44
+ * @param data
45
+ * @param duration seconds to keep the data in cache. -1 for infinite.
46
+ */
47
+ Set(id, data, duration = -1) {
48
+ this.data[id + ""] = new CacheItem(id + "", data, duration);
49
+ if (duration > -1) {
50
+ this.startCleaning();
51
+ if (this.maxCleanIdle < duration) {
52
+ // Add 5 seconds to be sure cleaner stays alive to get it.
53
+ this.maxCleanIdle = duration + 5;
54
+ }
55
+ }
56
+ }
57
+ Get(id) {
58
+ if (this.Disabled) {
59
+ return null;
60
+ }
61
+ const item = this.data[id + ""];
62
+ if (item == null) {
63
+ return null;
64
+ }
65
+ if (item.IsExpired()) {
66
+ delete this.data[id + ""];
67
+ return null;
68
+ }
69
+ return item.Data;
70
+ }
71
+ Clear() {
72
+ this.data = {};
73
+ this.stopCleaning();
74
+ }
75
+ Remove(id) {
76
+ delete this.data[id + ""];
77
+ if (Object.keys(this.data).length <= 0) {
78
+ this.stopCleaning();
79
+ }
80
+ }
81
+ RemoveByStartsWith(text) {
82
+ for (const key in this.data) {
83
+ if (key.startsWith(text)) {
84
+ delete this.data[key];
85
+ }
86
+ }
87
+ if (Object.keys(this.data).length <= 0) {
88
+ this.stopCleaning();
89
+ }
90
+ }
91
+ RemoveByContains(text) {
92
+ for (const key in this.data) {
93
+ if (key.includes(text)) {
94
+ delete this.data[key];
95
+ }
96
+ }
97
+ if (Object.keys(this.data).length <= 0) {
98
+ this.stopCleaning();
99
+ }
100
+ }
101
+ GetKeys() {
102
+ if (this.Disabled) {
103
+ return [];
104
+ }
105
+ return Object.keys(this.data);
106
+ }
107
+ GetValues() {
108
+ if (this.Disabled) {
109
+ return [];
110
+ }
111
+ const values = [];
112
+ for (const key in this.data) {
113
+ const data = this.Get(key);
114
+ if (data != null) {
115
+ values.push(data);
116
+ }
117
+ }
118
+ return values;
119
+ }
120
+ startCleaning() {
121
+ if (this.cleanerInterval == null) {
122
+ let cleanIdleStart = null;
123
+ this.cleanerInterval = setInterval(() => {
124
+ let cleaned = 0;
125
+ for (const key in this.data) {
126
+ const item = this.data[key];
127
+ if (item.IsExpired()) {
128
+ delete this.data[key];
129
+ cleaned += 1;
130
+ }
131
+ if (cleaned >= MAX_CLEAN_BATCH) {
132
+ break;
133
+ }
134
+ }
135
+ if (cleaned <= 0) {
136
+ // Stop cleaner if nothing in cache.
137
+ if (Object.keys(this.data).length <= 0) {
138
+ this.stopCleaning();
139
+ }
140
+ // Stop cleaner if idle time is reached.
141
+ else {
142
+ if (cleanIdleStart == null) {
143
+ cleanIdleStart = new Date().getTime();
144
+ }
145
+ const now = new Date().getTime();
146
+ if (now - cleanIdleStart > this.maxCleanIdle * 1000) {
147
+ this.stopCleaning();
148
+ }
149
+ }
150
+ }
151
+ else {
152
+ cleanIdleStart = null;
153
+ }
154
+ }, CLEANER_INTERVAL);
155
+ }
156
+ }
157
+ stopCleaning() {
158
+ if (this.cleanerInterval != null) {
159
+ clearInterval(this.cleanerInterval);
160
+ this.cleanerInterval = null;
161
+ this.maxCleanIdle = DEFAULT_MAX_CLEAN_IDLE;
162
+ }
163
+ }
164
+ }
165
+ exports.CacheControl = CacheControl;
166
166
  //# sourceMappingURL=cache.js.map