@hamak/ui-store-impl 0.3.0 → 0.4.1

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 (42) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/es2015/fs/commands/fs-commands.js +311 -0
  3. package/dist/es2015/fs/commands/structure-commands.js +21 -0
  4. package/dist/es2015/fs/core/fs-adapter.js +123 -0
  5. package/dist/es2015/fs/core/fs-facade.js +78 -0
  6. package/dist/es2015/fs/index.js +9 -0
  7. package/dist/es2015/fs/utils/data-updater.js +244 -0
  8. package/dist/es2015/fs/utils/deep-equal.js +28 -0
  9. package/dist/es2015/index.js +1 -0
  10. package/dist/fs/commands/fs-commands.d.ts +96 -0
  11. package/dist/fs/commands/fs-commands.d.ts.map +1 -0
  12. package/dist/fs/commands/fs-commands.js +311 -0
  13. package/dist/fs/commands/structure-commands.d.ts +76 -0
  14. package/dist/fs/commands/structure-commands.d.ts.map +1 -0
  15. package/dist/fs/commands/structure-commands.js +21 -0
  16. package/dist/fs/core/fs-adapter.d.ts +57 -0
  17. package/dist/fs/core/fs-adapter.d.ts.map +1 -0
  18. package/dist/fs/core/fs-adapter.js +123 -0
  19. package/dist/fs/core/fs-facade.d.ts +37 -0
  20. package/dist/fs/core/fs-facade.d.ts.map +1 -0
  21. package/dist/fs/core/fs-facade.js +78 -0
  22. package/dist/fs/index.d.ts +7 -0
  23. package/dist/fs/index.d.ts.map +1 -0
  24. package/dist/fs/index.js +9 -0
  25. package/dist/fs/utils/data-updater.d.ts +36 -0
  26. package/dist/fs/utils/data-updater.d.ts.map +1 -0
  27. package/dist/fs/utils/data-updater.js +248 -0
  28. package/dist/fs/utils/deep-equal.d.ts +5 -0
  29. package/dist/fs/utils/deep-equal.d.ts.map +1 -0
  30. package/dist/fs/utils/deep-equal.js +28 -0
  31. package/dist/index.d.ts +1 -0
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +1 -0
  34. package/package.json +8 -5
  35. package/src/fs/commands/fs-commands.ts +405 -0
  36. package/src/fs/commands/structure-commands.ts +105 -0
  37. package/src/fs/core/fs-adapter.ts +180 -0
  38. package/src/fs/core/fs-facade.ts +100 -0
  39. package/src/fs/index.ts +11 -0
  40. package/src/fs/utils/data-updater.ts +273 -0
  41. package/src/fs/utils/deep-equal.ts +35 -0
  42. package/src/index.ts +1 -0
@@ -0,0 +1,248 @@
1
+ import { isDraft, original } from 'immer';
2
+ import { navigate, ensurePath, areSameItineraryStep } from '@hamak/shared-utils';
3
+ /**
4
+ * Type guard for checking if a value is an object
5
+ */
6
+ function isObject(o) {
7
+ if (o === undefined || o === null) {
8
+ return false;
9
+ }
10
+ return (isDraft(o) && typeof original(o) === 'object')
11
+ || typeof o === 'object';
12
+ }
13
+ /**
14
+ * Type guard for checking if a value is an array
15
+ */
16
+ function isArray(o) {
17
+ if (o === undefined || o === null) {
18
+ return false;
19
+ }
20
+ return (isDraft(o) && Array.isArray(original(o)))
21
+ || Array.isArray(o);
22
+ }
23
+ /**
24
+ * DataUpdater class handles operations on data structures using itineraries
25
+ */
26
+ export class DataUpdater {
27
+ /**
28
+ * Delete a value at the specified itinerary
29
+ */
30
+ executeDelete(data, itinerary) {
31
+ if (itinerary === undefined) {
32
+ return;
33
+ }
34
+ const { parent, value: step } = itinerary;
35
+ const parentNode = navigate(data, parent);
36
+ if (!(parentNode === undefined)) {
37
+ if (isArray(parentNode)) {
38
+ if (step.type === 'position') {
39
+ parentNode.splice(step.position, 1);
40
+ }
41
+ else {
42
+ // TODO log expected PositionStep
43
+ }
44
+ }
45
+ else if (isObject(parentNode)) {
46
+ if (step.type === 'property') {
47
+ if (parentNode !== null) {
48
+ delete parentNode[step.propertyName];
49
+ }
50
+ }
51
+ else {
52
+ // TODO log expected PropertyStep
53
+ }
54
+ }
55
+ else {
56
+ // TODO log expected object
57
+ }
58
+ }
59
+ else {
60
+ // TODO log Parent Node not found
61
+ }
62
+ }
63
+ /**
64
+ * Set a value at the specified itinerary
65
+ */
66
+ executeSet(data, itinerary, value, prototypeToParent) {
67
+ if (itinerary === undefined) {
68
+ return;
69
+ }
70
+ const { parent, value: step } = itinerary;
71
+ const parentNode = ensurePath(data, parent, prototypeToParent);
72
+ if (!(parentNode === undefined)) {
73
+ if (isArray(parentNode)) {
74
+ if (step.type === 'position') {
75
+ parentNode[step.position] = value;
76
+ }
77
+ else {
78
+ // TODO log expected PositionStep
79
+ }
80
+ }
81
+ else if (isObject(parentNode)) {
82
+ if (step.type === 'property') {
83
+ if (parentNode !== null) {
84
+ parentNode[step.propertyName] = value;
85
+ }
86
+ }
87
+ else {
88
+ // TODO log expected PropertyStep
89
+ }
90
+ }
91
+ else {
92
+ // TODO log expected object
93
+ }
94
+ }
95
+ else {
96
+ // TODO log Parent Node not found
97
+ }
98
+ }
99
+ /**
100
+ * Add a value at the specified itinerary
101
+ */
102
+ executeAdd(data, itinerary, value, prototypeToParent) {
103
+ const targetNode = ensurePath(data, itinerary, prototypeToParent);
104
+ if (!(targetNode === undefined)) {
105
+ if (isArray(targetNode)) {
106
+ targetNode.push(value);
107
+ }
108
+ else {
109
+ // TODO log expected object
110
+ }
111
+ }
112
+ else {
113
+ // TODO log Parent Node not found
114
+ }
115
+ }
116
+ /**
117
+ * Insert a value at the specified position
118
+ */
119
+ executeInsert(data, itinerary, value, prototypeToParent) {
120
+ const { value: step, parent } = itinerary;
121
+ if (step.type === 'position') {
122
+ const parentNode = ensurePath(data, parent, prototypeToParent);
123
+ if (parentNode !== undefined) {
124
+ if (isArray(parentNode)) {
125
+ parentNode.splice(step.position, 0, value);
126
+ }
127
+ else {
128
+ // TODO log expected object
129
+ }
130
+ }
131
+ else {
132
+ // TODO log Parent Node not found
133
+ }
134
+ }
135
+ else {
136
+ // TODO log expecting position step
137
+ }
138
+ }
139
+ /**
140
+ * Rebase a pending command's itinerary on top of an altering command.
141
+ * Returns null if the pending command should be dropped (e.g., targets a deleted element).
142
+ * Enhanced: Handles rebasing for nested itineraries, not just at the first divergence.
143
+ */
144
+ rebasePendingCommandOn(alteringCmd, pendingCmd) {
145
+ // Convert itinerary stack to array (root to leaf)
146
+ function itineraryToArray(it) {
147
+ const arr = [];
148
+ let current = it;
149
+ while (current) {
150
+ arr.push(current.value);
151
+ current = current.parent;
152
+ }
153
+ return arr.reverse();
154
+ }
155
+ // Convert array back to itinerary stack (leaf to root)
156
+ function arrayToItinerary(arr) {
157
+ // Assumes arr.length > 0
158
+ let it = { parent: undefined, value: arr[0] };
159
+ for (let i = 1; i < arr.length; i++) {
160
+ it = { parent: it, value: arr[i] };
161
+ }
162
+ return it;
163
+ }
164
+ // Rebase a single position step if needed
165
+ function rebasePositionStep(alteringStep, alteringCmd, pendingStep) {
166
+ if (pendingStep.type !== 'position' || alteringStep.type !== 'position')
167
+ return pendingStep;
168
+ let { position: pendingIdx } = pendingStep;
169
+ const { position: alteringIdx } = alteringStep;
170
+ if (alteringCmd.name === 'insert-at' && pendingIdx >= alteringIdx) {
171
+ pendingIdx += 1;
172
+ }
173
+ else if (alteringCmd.name === 'delete-at') {
174
+ if (pendingIdx > alteringIdx) {
175
+ pendingIdx -= 1;
176
+ }
177
+ // If pendingIdx === alteringIdx, leave unchanged (may be dropped by batch context)
178
+ }
179
+ return pendingIdx !== pendingStep.position
180
+ ? { ...pendingStep, position: pendingIdx }
181
+ : pendingStep;
182
+ }
183
+ const alteringArr = itineraryToArray(alteringCmd.itinerary);
184
+ const pendingArr = itineraryToArray(pendingCmd.itinerary);
185
+ // Find first index where steps differ
186
+ let commonLength = 0;
187
+ while (commonLength < alteringArr.length &&
188
+ commonLength < pendingArr.length &&
189
+ areSameItineraryStep(alteringArr[commonLength], pendingArr[commonLength])) {
190
+ commonLength++;
191
+ }
192
+ // Rebase all position steps after the divergence point
193
+ const rebasedArr = pendingArr.slice();
194
+ for (let i = 0; i < alteringArr.length; i++) {
195
+ if (alteringArr[i].type === 'position' &&
196
+ pendingArr.length > i &&
197
+ pendingArr[i].type === 'position') {
198
+ // Only rebase if the pending itinerary matches the altering up to this step
199
+ const isDescendant = alteringArr
200
+ .slice(0, i)
201
+ .every((step, j) => areSameItineraryStep(step, pendingArr[j]));
202
+ if (isDescendant) {
203
+ rebasedArr[i] = rebasePositionStep(alteringArr[i], alteringCmd, pendingArr[i]);
204
+ }
205
+ }
206
+ }
207
+ // If no itinerary, return as is
208
+ if (rebasedArr.length === 0)
209
+ return pendingCmd;
210
+ return {
211
+ ...pendingCmd,
212
+ itinerary: arrayToItinerary(rebasedArr),
213
+ };
214
+ }
215
+ /**
216
+ * Executes a batch of commands, rebasing each on previous mutations.
217
+ * @param data The data object to mutate
218
+ * @param command The batch update command
219
+ */
220
+ executeBatch(data, command) {
221
+ const { commands } = command;
222
+ const applied = [];
223
+ for (let i = 0; i < commands.length; i++) {
224
+ let rebased = commands[i];
225
+ for (let j = 0; j < applied.length && rebased != null; j++) {
226
+ rebased = this.rebasePendingCommandOn(applied[j], rebased);
227
+ }
228
+ if (rebased != null) {
229
+ switch (rebased.name) {
230
+ case 'add-at':
231
+ this.executeAdd(data, rebased.itinerary, rebased.value, rebased.prototypes);
232
+ break;
233
+ case 'insert-at':
234
+ this.executeInsert(data, rebased.itinerary, rebased.value, rebased.prototypes);
235
+ break;
236
+ case 'set-at':
237
+ this.executeSet(data, rebased.itinerary, rebased.value, rebased.prototypes);
238
+ break;
239
+ case 'delete-at':
240
+ this.executeDelete(data, rebased.itinerary);
241
+ break;
242
+ }
243
+ applied.push(rebased);
244
+ }
245
+ // If rebased is null, skip this command (it targets a deleted element)
246
+ }
247
+ }
248
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Deep equality check for objects and arrays
3
+ */
4
+ export declare function deepEqual(obj1: any, obj2: any): boolean;
5
+ //# sourceMappingURL=deep-equal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deep-equal.d.ts","sourceRoot":"","sources":["../../../src/fs/utils/deep-equal.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,GAAG,OAAO,CA+BvD"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Deep equality check for objects and arrays
3
+ */
4
+ export function deepEqual(obj1, obj2) {
5
+ if (obj1 === obj2) {
6
+ return true;
7
+ }
8
+ if (obj1 === null || obj2 === null || typeof obj1 !== 'object' || typeof obj2 !== 'object') {
9
+ return false;
10
+ }
11
+ if (Array.isArray(obj1) !== Array.isArray(obj2)) {
12
+ return false;
13
+ }
14
+ const keys1 = Object.keys(obj1);
15
+ const keys2 = Object.keys(obj2);
16
+ if (keys1.length !== keys2.length) {
17
+ return false;
18
+ }
19
+ for (const key of keys1) {
20
+ if (!keys2.includes(key)) {
21
+ return false;
22
+ }
23
+ if (!deepEqual(obj1[key], obj2[key])) {
24
+ return false;
25
+ }
26
+ }
27
+ return true;
28
+ }
package/dist/index.d.ts CHANGED
@@ -5,4 +5,5 @@
5
5
  export * from './core';
6
6
  export * from './middleware';
7
7
  export * from './plugin';
8
+ export * from './fs';
8
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,QAAQ,CAAC;AACvB,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,QAAQ,CAAC;AACvB,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,MAAM,CAAC"}
package/dist/index.js CHANGED
@@ -5,3 +5,4 @@
5
5
  export * from './core';
6
6
  export * from './middleware';
7
7
  export * from './plugin';
8
+ export * from './fs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hamak/ui-store-impl",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "UI Store Implementation - Redux store implementation with middleware",
@@ -34,10 +34,13 @@
34
34
  }
35
35
  },
36
36
  "dependencies": {
37
- "@hamak/ui-store-api": "0.3.0",
38
- "@hamak/ui-store-spi": "0.3.0",
39
- "@hamak/microkernel-api": "0.3.0",
40
- "@hamak/microkernel-spi": "0.3.0",
37
+ "@hamak/shared-utils": "0.4.1",
38
+ "@hamak/ui-store-api": "0.4.1",
39
+ "@hamak/ui-store-spi": "0.4.1",
40
+ "@hamak/microkernel-api": "0.4.1",
41
+ "@hamak/microkernel-spi": "0.4.1",
42
+ "@reduxjs/toolkit": "^2.0.0",
43
+ "immer": "^10.0.0",
41
44
  "redux": "^5.0.1",
42
45
  "redux-thunk": "^3.1.0"
43
46
  },