@woosh/meep-engine 2.55.0 → 2.56.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 (155) hide show
  1. package/editor/view/node-graph/PortView.js +2 -2
  2. package/package.json +1 -1
  3. package/src/core/binary/BinaryBuffer.js +2 -1
  4. package/src/core/cache/LoadingCache.js +10 -5
  5. package/src/core/collection/array/array_remove_element.js +6 -9
  6. package/src/core/collection/array/array_remove_first.js +13 -6
  7. package/src/core/collection/array/array_remove_first.spec.js +39 -0
  8. package/src/core/collection/array/binarySearchHighIndex.spec.js +10 -9
  9. package/src/core/collection/heap/Uin32Heap.spec.js +36 -0
  10. package/src/core/collection/heap/Uint32Heap.js +10 -5
  11. package/src/core/function/FunctionCompiler.js +4 -4
  12. package/src/core/function/Functions.js +0 -19
  13. package/src/core/geom/3d/SurfacePoint3.js +30 -20
  14. package/src/core/geom/3d/SurfacePoint3.spec.js +116 -0
  15. package/src/core/geom/3d/aabb/AABB3.js +10 -9
  16. package/src/core/geom/3d/aabb/{aabb3_array_contains_point.js → aabb3_array_intersects_point.js} +4 -1
  17. package/src/core/geom/3d/aabb/aabb3_from_min_max.js +25 -1
  18. package/src/core/geom/3d/aabb/aabb3_from_threejs_geometry.js +2 -25
  19. package/src/core/geom/3d/aabb/aabb3_signed_distance_sqr_to_point.js +5 -1
  20. package/src/core/geom/3d/apply_mat4_transform_to_direction_v3_array.js +5 -1
  21. package/src/core/geom/3d/apply_mat4_transform_to_v3_array.js +5 -1
  22. package/src/core/geom/3d/tetrahedra/TetrahedralMesh.js +2 -2
  23. package/src/core/geom/3d/triangle/computeTriangleRayIntersectionBarycentric.spec.js +26 -0
  24. package/src/core/geom/3d/v3_compute_triangle_normal.spec.js +18 -0
  25. package/src/core/geom/Vector2.js +1 -1
  26. package/src/core/geom/Vector3.js +1 -1
  27. package/src/core/geom/packing/miniball/Miniball.spec.js +24 -0
  28. package/src/core/math/statistics/computeStatisticalPartialMedian.js +2 -2
  29. package/src/core/model/object/read_property.js +2 -2
  30. package/src/core/model/object/write_property.js +3 -3
  31. package/src/core/primitives/numbers/compareNumbers.js +4 -4
  32. package/src/core/primitives/numbers/number_compare_ascending.js +9 -0
  33. package/src/core/primitives/numbers/number_compare_ascending.spec.js +9 -0
  34. package/src/core/primitives/numbers/number_compare_descending.js +9 -0
  35. package/src/core/primitives/numbers/number_compare_descending.spec.js +9 -0
  36. package/src/core/primitives/numbers/number_format_by_thousands.spec.js +12 -0
  37. package/src/core/primitives/numbers/number_pretty_print.js +1 -1
  38. package/src/core/primitives/strings/compareStrings.spec.js +12 -0
  39. package/src/core/primitives/strings/string_capitalize.js +15 -0
  40. package/src/core/primitives/strings/string_capitalize.spec.js +13 -0
  41. package/src/core/primitives/strings/string_compute_byte_size.js +21 -0
  42. package/src/core/primitives/strings/string_compute_common_prefix.js +44 -0
  43. package/src/core/primitives/strings/string_compute_common_prefix.spec.js +23 -0
  44. package/src/core/primitives/strings/string_format_camel_to_kebab.js +9 -0
  45. package/src/core/primitives/strings/string_format_camel_to_kebab.spec.js +8 -0
  46. package/src/core/primitives/strings/string_format_kebab_to_underscore.js +8 -0
  47. package/src/core/time/current_time_in_seconds.js +11 -0
  48. package/src/engine/Clock.js +3 -13
  49. package/src/engine/animation/curve/AnimationCurve.spec.js +27 -0
  50. package/src/engine/asset/AssetManager.js +2 -2
  51. package/src/engine/ecs/EntityManager.js +8 -1
  52. package/src/engine/ecs/EntityManager.spec.js +56 -6
  53. package/src/engine/ecs/animation/Animation.spec.js +22 -0
  54. package/src/engine/ecs/attachment/Attachment.js +24 -25
  55. package/src/engine/ecs/attachment/AttachmentBinding.js +27 -30
  56. package/src/engine/ecs/attachment/AttachmentSystem.js +21 -24
  57. package/src/engine/ecs/attachment/BoneAttachmentBinding.js +6 -9
  58. package/src/engine/ecs/attachment/TransformAttachmentBinding.js +0 -3
  59. package/src/engine/ecs/components/CharacterController.js +24 -18
  60. package/src/engine/ecs/dynamic_actions/DynamicActorSystem.js +3 -2
  61. package/src/engine/ecs/fow/FogOfWarRevealer.js +2 -3
  62. package/src/engine/ecs/ik/IKMath.js +6 -1
  63. package/src/engine/ecs/ik/IKProblem.js +17 -17
  64. package/src/engine/ecs/ik/InverseKinematics.js +6 -7
  65. package/src/engine/ecs/ik/InverseKinematicsSystem.js +24 -26
  66. package/src/engine/ecs/storage/BinaryBufferSerializer.js +3 -3
  67. package/src/engine/ecs/systems/TagSystem.js +1 -6
  68. package/src/engine/ecs/terrain/ecs/layers/TerrainLayer.js +2 -2
  69. package/src/engine/graphics/ecs/mesh/skeleton/BoneMapping.js +2 -2
  70. package/src/engine/graphics/ecs/mesh-v2/ShadedGeometry.spec.js +14 -0
  71. package/src/engine/graphics/ecs/mesh-v2/render/adapters/SGCacheKey.js +21 -9
  72. package/src/engine/graphics/ecs/mesh-v2/render/adapters/SGCacheKey.spec.js +79 -0
  73. package/src/engine/graphics/render/visibility/IncrementalDeltaSet.spec.js +7 -6
  74. package/src/engine/simulation/Ticker.js +17 -20
  75. package/src/generation/GridTaskGroup.js +5 -9
  76. package/src/generation/filtering/numeric/CellFilterCache.js +12 -16
  77. package/src/generation/filtering/numeric/complex/CellFilterFXAA.js +31 -32
  78. package/src/generation/filtering/numeric/complex/CellFilterLookupTable.js +6 -9
  79. package/src/generation/filtering/numeric/complex/CellFilterSimplexNoise.js +7 -10
  80. package/src/generation/filtering/numeric/complex/CellFilterSobel.js +6 -9
  81. package/src/generation/filtering/numeric/math/CellFilterMembershipGeneralizedBell.js +18 -21
  82. package/src/generation/filtering/numeric/sampling/AbstractCellFilterSampleGridLayer.js +24 -25
  83. package/src/generation/filtering/numeric/sampling/CellFilterSampleLayerLinear.js +1 -1
  84. package/src/generation/grid/GridData.js +23 -22
  85. package/src/generation/grid/GridData.spec.js +41 -0
  86. package/src/generation/grid/GridTaskGenerator.js +7 -8
  87. package/src/generation/grid/layers/GridDataLayer.js +23 -25
  88. package/src/generation/markers/GridActionRuleSet.js +20 -22
  89. package/src/generation/markers/GridCellActionPlaceMarker.js +40 -43
  90. package/src/generation/markers/GridCellActionPlaceMarkerGroup.js +7 -9
  91. package/src/generation/markers/MarkerNode.js +44 -44
  92. package/src/generation/markers/actions/MarkerNodeActionEntityPlacement.js +15 -18
  93. package/src/generation/markers/actions/MarkerNodeActionSequence.js +6 -9
  94. package/src/generation/markers/actions/MarkerNodeProcessingRuleSet.js +5 -7
  95. package/src/generation/markers/actions/MarkerProcessingRule.js +25 -26
  96. package/src/generation/markers/actions/placement/MarkerNodeEntityProcessorClingToTerrain.js +12 -15
  97. package/src/generation/markers/actions/placement/MarkerNodeEntityProcessorRandomRotation.js +2 -5
  98. package/src/generation/markers/actions/placement/MarkerNodeEntityProcessorSequence.js +6 -9
  99. package/src/generation/markers/actions/probability/MarkerNodeActionSelectWeighted.js +8 -11
  100. package/src/generation/markers/actions/probability/MarkerNodeActionWeightedElement.js +13 -13
  101. package/src/generation/markers/actions/util/GridCellActionDebugBreak.js +5 -8
  102. package/src/generation/markers/actions/util/GridCellActionLogToConsole.js +1 -4
  103. package/src/generation/markers/emitter/MarkerNodeConsumerBuffer.js +10 -13
  104. package/src/generation/markers/emitter/MarkerNodeEmitterFromAction.js +5 -9
  105. package/src/generation/markers/emitter/MarkerNodeEmitterGridCellAction.js +12 -15
  106. package/src/generation/markers/emitter/MarkerNodeEmitterGroup.js +5 -8
  107. package/src/generation/markers/emitter/MarkerNodeEmitterPredicated.js +18 -21
  108. package/src/generation/markers/matcher/MarkerNodeMatcher.js +2 -1
  109. package/src/generation/markers/matcher/MarkerNodeMatcherBinary.js +12 -13
  110. package/src/generation/markers/matcher/MarkerNodeMatcherContainsTag.js +7 -9
  111. package/src/generation/markers/matcher/MarkerNodeMatcherNot.js +7 -9
  112. package/src/generation/markers/predicate/GridDataNodePredicateBinary.js +10 -14
  113. package/src/generation/markers/predicate/GridDataNodePredicateNot.js +9 -11
  114. package/src/generation/markers/predicate/GridDataNodePredicateOverlaps.js +6 -9
  115. package/src/generation/markers/transform/MarkerNodeTransformRotateRandom.js +2 -6
  116. package/src/generation/markers/transform/MarkerNodeTransformerAddPositionYFromFilter.js +6 -9
  117. package/src/generation/markers/transform/MarkerNodeTransformerOffsetPosition.js +1 -4
  118. package/src/generation/markers/transform/MarkerNodeTransformerRecordProperty.js +12 -15
  119. package/src/generation/markers/transform/MarkerNodeTransformerRecordPropertyClosure.js +14 -17
  120. package/src/generation/markers/transform/MarkerNodeTransformerRecordUniqueRandomEnum.js +21 -23
  121. package/src/generation/markers/transform/MarkerNodeTransformerRemoveTag.js +1 -4
  122. package/src/generation/markers/transform/MarkerNodeTransformerSequence.js +6 -9
  123. package/src/generation/markers/transform/MarkerNodeTransformerYRotateByFilter.js +13 -17
  124. package/src/generation/markers/transform/MarkerNodeTransformerYRotateByFilterGradient.js +12 -16
  125. package/src/generation/placement/GridCellActionTransformNearbyMarkers.js +26 -29
  126. package/src/generation/placement/GridCellPlacementRule.js +30 -32
  127. package/src/generation/placement/action/GridCellActionPlaceTags.js +26 -28
  128. package/src/generation/placement/action/GridCellActionWriteFilterToLayer.js +16 -20
  129. package/src/generation/placement/action/random/weighted/CellActionSelectWeightedRandom.js +13 -16
  130. package/src/generation/placement/action/random/weighted/WeightedGridCellAction.js +11 -14
  131. package/src/generation/placement/action/util/CellMatcherWithinAABB.js +2 -6
  132. package/src/generation/placement/action/util/GridCellActionSequence.js +7 -9
  133. package/src/generation/placement/action/util/GridCellDisplacedAction.js +9 -12
  134. package/src/generation/rules/CellMatcherFromFilter.js +6 -9
  135. package/src/generation/rules/CellMatcherLayerBitMaskTest.js +6 -9
  136. package/src/generation/rules/GridLayerCellMatcher.js +11 -14
  137. package/src/generation/rules/cell/CellMatcherContainsMarkerWithinRadius.js +11 -14
  138. package/src/generation/rules/cell/CellMatcherGridPattern.js +6 -8
  139. package/src/generation/rules/cell/GridPatternMatcherCell.js +11 -12
  140. package/src/generation/rules/logic/CellMatcherBinary.js +10 -14
  141. package/src/generation/rules/logic/CellMatcherDecorator.js +5 -8
  142. package/src/generation/theme/AreaMask.js +15 -17
  143. package/src/generation/theme/AreaTheme.js +7 -8
  144. package/src/generation/theme/TerrainLayerDescription.js +10 -12
  145. package/src/generation/theme/TerrainLayerRule.js +11 -13
  146. package/src/generation/theme/TerrainTheme.js +6 -7
  147. package/src/generation/theme/Theme.js +15 -17
  148. package/src/generation/theme/ThemeEngine.js +16 -18
  149. package/src/view/string_tag_to_css_class_name.js +2 -2
  150. package/src/view/tooltip/gml/TooltipParser.js +2 -2
  151. package/src/core/cache/PersistentCacheAdapter.js +0 -378
  152. package/src/core/primitives/strings/StringUtils.js +0 -105
  153. package/src/core/primitives/strings/StringUtils.spec.js +0 -42
  154. package/src/engine/ecs/components/MonsterAI.js +0 -15
  155. package/src/generation/markers/MarkerRelation.js +0 -13
@@ -1,13 +1,12 @@
1
1
  import { AreaMask } from "./AreaMask.js";
2
2
 
3
3
  export class AreaTheme {
4
- constructor() {
5
- this.mask = new AreaMask();
4
+ mask = new AreaMask();
5
+
6
+ /**
7
+ *
8
+ * @type {Theme}
9
+ */
10
+ theme = null;
6
11
 
7
- /**
8
- *
9
- * @type {Theme}
10
- */
11
- this.theme = null;
12
- }
13
12
  }
@@ -2,19 +2,17 @@ import { computeStringHash } from "../../core/primitives/strings/computeStringHa
2
2
  import Vector2 from "../../core/geom/Vector2.js";
3
3
 
4
4
  export class TerrainLayerDescription {
5
- constructor() {
6
- /**
7
- * URL to diffuse texture
8
- * @type {string}
9
- */
10
- this.diffuse = '';
5
+ /**
6
+ * URL to diffuse texture
7
+ * @type {string}
8
+ */
9
+ diffuse = '';
11
10
 
12
- /**
13
- *
14
- * @type {Vector2}
15
- */
16
- this.size = new Vector2(1, 1);
17
- }
11
+ /**
12
+ *
13
+ * @type {Vector2}
14
+ */
15
+ size = new Vector2(1, 1);
18
16
 
19
17
  hash() {
20
18
  return computeStringHash(this.diffuse) ^ this.size.hash();
@@ -2,20 +2,18 @@ import { assert } from "../../core/assert.js";
2
2
  import { TerrainLayerDescription } from "./TerrainLayerDescription.js";
3
3
 
4
4
  export class TerrainLayerRule {
5
- constructor() {
6
- /**
7
- *
8
- * @type {CellFilter}
9
- */
10
- this.filter = null;
11
-
12
- /**
13
- *
14
- * @type {TerrainLayerDescription}
15
- */
16
- this.layer = new TerrainLayerDescription();
5
+ /**
6
+ *
7
+ * @type {CellFilter}
8
+ */
9
+ filter = null;
10
+
11
+ /**
12
+ *
13
+ * @type {TerrainLayerDescription}
14
+ */
15
+ layer = new TerrainLayerDescription();
17
16
 
18
- }
19
17
 
20
18
  /**
21
19
  *
@@ -2,13 +2,12 @@ import { assert } from "../../core/assert.js";
2
2
  import { seededRandom } from "../../core/math/random/seededRandom.js";
3
3
 
4
4
  export class TerrainTheme {
5
- constructor() {
6
- /**
7
- *
8
- * @type {TerrainLayerRule[]}
9
- */
10
- this.rules = [];
11
- }
5
+ /**
6
+ *
7
+ * @type {TerrainLayerRule[]}
8
+ */
9
+ rules = [];
10
+
12
11
 
13
12
  /**
14
13
  *
@@ -4,26 +4,24 @@ import { CellProcessingRuleSet } from "./cell/CellProcessingRuleSet.js";
4
4
  import { assert } from "../../core/assert.js";
5
5
 
6
6
  export class Theme {
7
- constructor() {
8
7
 
9
- /**
10
- *
11
- * @type {TerrainTheme}
12
- */
13
- this.terrain = new TerrainTheme();
8
+ /**
9
+ *
10
+ * @type {TerrainTheme}
11
+ */
12
+ terrain = new TerrainTheme();
14
13
 
15
- /**
16
- *
17
- * @type {MarkerNodeProcessingRuleSet}
18
- */
19
- this.nodes = new MarkerNodeProcessingRuleSet();
14
+ /**
15
+ *
16
+ * @type {MarkerNodeProcessingRuleSet}
17
+ */
18
+ nodes = new MarkerNodeProcessingRuleSet();
20
19
 
21
- /**
22
- *
23
- * @type {CellProcessingRuleSet}
24
- */
25
- this.cells = new CellProcessingRuleSet();
26
- }
20
+ /**
21
+ *
22
+ * @type {CellProcessingRuleSet}
23
+ */
24
+ cells = new CellProcessingRuleSet();
27
25
 
28
26
  /**
29
27
  *
@@ -55,27 +55,25 @@ function prepare_terrain_for_themes(layer_mapping, themes) {
55
55
  }
56
56
 
57
57
  export class ThemeEngine {
58
- constructor() {
59
58
 
60
- /**
61
- *
62
- * @type {QuadTreeNode<AreaTheme>}
63
- */
64
- this.areas = new QuadTreeNode();
59
+ /**
60
+ *
61
+ * @type {QuadTreeNode<AreaTheme>}
62
+ */
63
+ areas = new QuadTreeNode();
65
64
 
66
- /**
67
- *
68
- * @type {function():number}
69
- */
70
- this.random = seededRandom(1);
65
+ /**
66
+ *
67
+ * @type {function():number}
68
+ */
69
+ random = seededRandom(1);
71
70
 
72
- /**
73
- *
74
- * @type {AssetManager}
75
- * @private
76
- */
77
- this.__assetManager = null;
78
- }
71
+ /**
72
+ *
73
+ * @type {AssetManager}
74
+ * @private
75
+ */
76
+ __assetManager = null;
79
77
 
80
78
  set assetManager(v) {
81
79
  this.__assetManager = v;
@@ -1,4 +1,4 @@
1
- import { camelToKebab } from "../core/primitives/strings/StringUtils.js";
1
+ import { string_format_camel_to_kebab } from "../core/primitives/strings/string_format_camel_to_kebab.js";
2
2
 
3
3
  /**
4
4
  *
@@ -6,7 +6,7 @@ import { camelToKebab } from "../core/primitives/strings/StringUtils.js";
6
6
  * @returns {string}
7
7
  */
8
8
  export function string_tag_to_css_class_name(a) {
9
- const name = camelToKebab(a);
9
+ const name = string_format_camel_to_kebab(a);
10
10
 
11
11
  return `tag-${name}`;
12
12
  }
@@ -1,8 +1,8 @@
1
1
  import { Cache } from "../../../core/cache/Cache.js";
2
- import { computeUTF8StringByteSize } from "../../../core/primitives/strings/StringUtils.js";
3
2
  import { strictEquals } from "../../../core/function/Functions.js";
4
3
  import { parseTooltipString } from "./parser/parseTooltipString.js";
5
4
  import { computeStringHash } from "../../../core/primitives/strings/computeStringHash.js";
5
+ import { string_compute_byte_size } from "../../../core/primitives/strings/string_compute_byte_size.js";
6
6
 
7
7
  export class TooltipParser {
8
8
 
@@ -15,7 +15,7 @@ export class TooltipParser {
15
15
  */
16
16
  this.__cache = new Cache({
17
17
  maxWeight: 1048576,
18
- keyWeigher: computeUTF8StringByteSize,
18
+ keyWeigher: string_compute_byte_size,
19
19
  valueWeigher(tokens) {
20
20
  return tokens.length * 256;
21
21
  },
@@ -1,378 +0,0 @@
1
- import { IndexedDBStorage } from "../../engine/save/storage/IndexedDBStorage.js";
2
- import { assert } from "../assert.js";
3
- import { Base64 } from "../binary/Base64.js";
4
- import { BinaryBuffer } from "../binary/BinaryBuffer.js";
5
-
6
- const scratch_buffer = new BinaryBuffer();
7
-
8
- /**
9
- *
10
- * @type {number}
11
- */
12
- const BUFFER_MAX_RETAINED_SIZE = 131072;
13
-
14
- function cleanup_buffer() {
15
- if (scratch_buffer.length > BUFFER_MAX_RETAINED_SIZE) {
16
- scratch_buffer.position = 0;
17
- scratch_buffer.setCapacity(BUFFER_MAX_RETAINED_SIZE);
18
- }
19
- }
20
-
21
- /**
22
- * @template T
23
- * @param {T} object
24
- * @param {BinaryClassSerializationAdapter} adapter
25
- * @returns {string}
26
- */
27
- function object2string(object, adapter) {
28
-
29
- scratch_buffer.position = 0;
30
-
31
- // serialize key
32
- adapter.serialize(scratch_buffer, object);
33
-
34
- // convert key to string
35
- const end = scratch_buffer.position;
36
- const array_buffer = scratch_buffer.data.slice(0, end);
37
-
38
- const result = Base64.encode(array_buffer);
39
-
40
- cleanup_buffer();
41
-
42
- return result;
43
- }
44
-
45
- /**
46
- * @template T
47
- * @param {string} string
48
- * @param {BinaryClassSerializationAdapter} adapter
49
- * @param {Class<T>} klass
50
- * @returns T
51
- */
52
- function string2object(string, adapter, klass) {
53
- /**
54
- *
55
- * @type {T}
56
- */
57
- const result = new klass();
58
-
59
- const arrayBuffer = Base64.decode(string);
60
-
61
- scratch_buffer.fromArrayBuffer(arrayBuffer);
62
-
63
- adapter.deserialize(scratch_buffer, result);
64
-
65
- cleanup_buffer();
66
-
67
- return result;
68
- }
69
-
70
- /**
71
- * Attaches to a cache instance and persists all the cache data asynchronously
72
- * Behaves similarly to WeakMap class with similar interface.
73
- * @template K,V
74
- */
75
- export class PersistentCacheAdapter {
76
- /**
77
- * @template K,V
78
- * @param {string} storageKey
79
- * @param {BinaryClassSerializationAdapter} keyAdapter
80
- * @param {Class<K>|function} keyClass
81
- * @param {BinaryClassSerializationAdapter} valueAdapter
82
- * @param {Class<V>|function} valueClass
83
- */
84
- constructor({
85
- storageKey,
86
- keyAdapter,
87
- keyClass,
88
- valueAdapter,
89
- valueClass
90
- }) {
91
- assert.typeOf(storageKey, 'string', 'storageKey');
92
-
93
- assert.defined(keyAdapter, 'keyAdapter');
94
- assert.notNull(keyAdapter, 'keyAdapter');
95
- assert.defined(keyClass, 'keyClass');
96
- assert.notNull(keyClass, 'keyClass');
97
-
98
- assert.defined(valueAdapter, 'valueAdapter');
99
- assert.notNull(valueAdapter, 'valueAdapter');
100
- assert.defined(valueClass, 'valueClass');
101
- assert.notNull(valueClass, 'valueClass');
102
-
103
- /**
104
- *
105
- * @type {string}
106
- * @private
107
- */
108
- this.__storage_key = storageKey;
109
- /**
110
- *
111
- * @type {BinaryClassSerializationAdapter}
112
- * @private
113
- */
114
- this.__key_adapter = keyAdapter;
115
- /**
116
- *
117
- * @type {Class<K>|Function}
118
- * @private
119
- */
120
- this.__key_class = keyClass;
121
-
122
- /**
123
- *
124
- * @type {BinaryClassSerializationAdapter}
125
- * @private
126
- */
127
- this.__value_adapter = valueAdapter;
128
- /**
129
- *
130
- * @type {Class<V>|Function}
131
- * @private
132
- */
133
- this.__value_class = valueClass;
134
-
135
- /**
136
- *
137
- * @type {Storage}
138
- * @private
139
- */
140
- this.__storage = new IndexedDBStorage(`meep.core.persistent_cache/${storageKey}`);
141
-
142
- /**
143
- *
144
- * @type {Cache|null}
145
- * @private
146
- */
147
- this.__cache = null;
148
- }
149
-
150
- /**
151
- *
152
- * @param {Cache<K,V>} cache
153
- * @param {boolean} ingest should data stored in the persisted cache be uploaded to the cache or not
154
- */
155
- async attach(cache, ingest = true) {
156
- if (this.__cache !== null) {
157
- throw new Error(`Adapted already connected to a cache`);
158
- }
159
-
160
- this.__cache = cache;
161
-
162
- // connect signals
163
- cache.onSet.add(this.__handle_set, this);
164
- cache.onRemoved.add(this.__handle_remove, this);
165
-
166
- if (ingest) {
167
- await this.__ingest();
168
- }
169
- }
170
-
171
- /**
172
- *
173
- * @return {Promise<void>}
174
- * @private
175
- */
176
- async __ingest() {
177
- /**
178
- *
179
- * @type {string[]}
180
- */
181
- const keys = await this.__storage.promiseList();
182
-
183
- const key_count = keys.length;
184
-
185
- const pairs = [];
186
-
187
- /**
188
- *
189
- * @type {Promise[]}
190
- */
191
- const read_promises = [];
192
-
193
- const start = performance.now();
194
-
195
- for (let i = 0; i < key_count; i++) {
196
- const key_string = keys[i];
197
-
198
- const promise = new Promise(async (resolve, reject) => {
199
- const key = string2object(key_string, this.__key_adapter, this.__key_class);
200
-
201
- // read value
202
- let value;
203
-
204
- try {
205
- value = await this.__get_by_string_unsafe(key_string);
206
- } catch (e) {
207
-
208
- // failed to read value, skip
209
- resolve();
210
-
211
- return;
212
- }
213
-
214
- // first read into temp array to prevent thrashing when adding one thing to cache would cause another thing to be removed
215
- pairs.push([key, value]);
216
-
217
- resolve();
218
- });
219
-
220
- read_promises.push(promise);
221
- }
222
-
223
- await Promise.all(read_promises);
224
-
225
- console.log(`${this.__storage_key} : Ingesting ${key_count} values finished after ${performance.now() - start}ms`);
226
-
227
- const cache = this.__cache;
228
-
229
- // disconnect signals
230
- cache.onSet.remove(this.__handle_set, this);
231
- cache.onRemoved.remove(this.__handle_remove, this);
232
-
233
- // add to cache
234
- for (let i = 0; i < pairs.length; i++) {
235
- const [key, value] = pairs[i];
236
-
237
- cache.put(key, value);
238
- }
239
-
240
- // reconnect signals
241
- cache.onSet.add(this.__handle_set, this);
242
- cache.onRemoved.add(this.__handle_remove, this);
243
-
244
- }
245
-
246
- /**
247
- *
248
- * @param {K} key
249
- * @param {V} value
250
- * @private
251
- */
252
- __handle_set(key, value) {
253
- this.set(key, value);
254
- }
255
-
256
- /**
257
- *
258
- * @param {K} key
259
- * @private
260
- */
261
- __handle_remove(key) {
262
- this.delete(key);
263
- }
264
-
265
-
266
- /**
267
- *
268
- * @param {K} key
269
- * @param {V} value
270
- * @private
271
- */
272
- async set(key, value) {
273
- assert.defined(key, 'key');
274
- assert.notNull(key, 'key');
275
-
276
- assert.defined(value, 'value');
277
- assert.notNull(value, 'value');
278
-
279
- const key_string = object2string(key, this.__key_adapter);
280
-
281
- // serialize value
282
- scratch_buffer.position = 0;
283
- this.__value_adapter.serialize(scratch_buffer, value);
284
-
285
- cleanup_buffer();
286
-
287
- const value_data = scratch_buffer.data.slice(0, scratch_buffer.position);
288
-
289
- await this.__storage.promiseStoreBinary(key_string, value_data);
290
- }
291
-
292
- /**
293
- *
294
- * @param {K} key
295
- * @returns {V|undefined}
296
- * @private
297
- */
298
- async get(key) {
299
- assert.defined(key, 'key');
300
- assert.notNull(key, 'key');
301
-
302
- const key_string = object2string(key, this.__key_adapter);
303
-
304
- // check if value is present
305
- const exists = await this.__storage.promiseContains(key_string);
306
-
307
- if (!exists) {
308
- // value doesn't exist
309
- return undefined;
310
- }
311
-
312
- return this.__get_by_string_unsafe(key_string);
313
- }
314
-
315
- /**
316
- * @param {string} key_string
317
- * @return {Promise<V>}
318
- * @private
319
- */
320
- async __get_by_string_unsafe(key_string) {
321
-
322
- /**
323
- *
324
- * @type {ArrayBuffer}
325
- */
326
- const value_data = await this.__storage.promiseLoadBinary(key_string);
327
-
328
- const value = new this.__value_class();
329
-
330
- scratch_buffer.fromArrayBuffer(value_data);
331
-
332
- try {
333
- this.__value_adapter.deserialize(scratch_buffer, value);
334
- } catch (e) {
335
- // failed to deserialize
336
- // console.error('Failed to deserialize the value: ', e);
337
-
338
- // delete the value
339
- this.__storage.promiseRemove(key_string);
340
-
341
- throw new Error('Failed to deserialize');
342
- } finally {
343
- cleanup_buffer();
344
- }
345
-
346
- return value;
347
- }
348
-
349
- /**
350
- *
351
- * @param {K} key
352
- * @returns {boolean}
353
- * @private
354
- */
355
- async has(key) {
356
- assert.defined(key, 'key');
357
- assert.notNull(key, 'key');
358
-
359
- const key_string = object2string(key, this.__key_adapter);
360
-
361
- return await this.__storage.promiseContains(key_string);
362
- }
363
-
364
- /**
365
- *
366
- * @param {K} key
367
- * @private
368
- */
369
- async delete(key) {
370
- assert.defined(key, 'key');
371
- assert.notNull(key, 'key');
372
-
373
- const key_string = object2string(key, this.__key_adapter);
374
-
375
- return await this.__storage.promiseRemove(key_string);
376
- }
377
- }
378
-
@@ -1,105 +0,0 @@
1
- /**
2
- * @author Alex Goldring 2018
3
- * @copyright Alex Goldring 2018
4
- */
5
-
6
- /**
7
- * Compute byte size of a UTF8 string
8
- * @param {string} value
9
- * @returns {number}
10
- */
11
- export function computeUTF8StringByteSize(value) {
12
- const length = value.length;
13
-
14
- let p = 0;
15
-
16
- for (let i = 0; i < length; i++) {
17
- let c = value.charCodeAt(i);
18
- while (c > 0xff) {
19
- p++;
20
- c >>= 8;
21
- }
22
- p++;
23
- }
24
-
25
- return p;
26
- }
27
-
28
- /**
29
- *
30
- * @param {string} string
31
- * @returns {string}
32
- */
33
- export function capitalize(string) {
34
- const length = string.length;
35
-
36
- if (length === 0) {
37
- return string;
38
- } else {
39
- return string.charAt(0).toLocaleUpperCase() + string.substring(1);
40
- }
41
- }
42
-
43
- /**
44
- * @example: ['abra', 'abc', 'abode'] => 'ab'
45
- * @param {String[]} strings
46
- * @returns string
47
- */
48
- export function computeCommonPrefix(strings) {
49
- let i, j;
50
-
51
- const numInputs = strings.length;
52
-
53
- let result = "";
54
-
55
- if (numInputs === 0) {
56
- return result;
57
- }
58
-
59
- const firstString = strings[0];
60
-
61
- let lengthLimit = firstString.length;
62
-
63
- for (i = 1; i < numInputs; i++) {
64
- lengthLimit = Math.min(strings[i].length, lengthLimit);
65
- }
66
-
67
-
68
- main_loop:for (i = 0; i < lengthLimit; i++) {
69
- const letter0 = firstString.charAt(i);
70
-
71
- for (j = 1; j < numInputs; j++) {
72
-
73
- const string = strings[j];
74
-
75
- const letter1 = string.charAt(i);
76
-
77
- if (letter0 !== letter1) {
78
- break main_loop;
79
- }
80
- }
81
-
82
- result += letter0;
83
- }
84
-
85
- return result;
86
- }
87
-
88
- /**
89
- *
90
- * @param {string} string
91
- * @returns {string}
92
- */
93
- export function camelToKebab(string) {
94
- return string.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
95
- }
96
-
97
-
98
- /**
99
- *
100
- * @param {string} string
101
- * @returns {string}
102
- */
103
- export function kebabToUnderscore(string) {
104
- return string.replace('-', '_');
105
- }