@evolu/common 4.1.1 → 5.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 (78) hide show
  1. package/README.md +1 -1
  2. package/dist/src/Config.d.ts +27 -30
  3. package/dist/src/Config.d.ts.map +1 -1
  4. package/dist/src/Config.js +32 -5
  5. package/dist/src/Crdt.d.ts +5 -4
  6. package/dist/src/Crdt.d.ts.map +1 -1
  7. package/dist/src/Crdt.js +27 -24
  8. package/dist/src/Crypto.d.ts +16 -10
  9. package/dist/src/Crypto.d.ts.map +1 -1
  10. package/dist/src/Crypto.js +24 -11
  11. package/dist/src/Db.d.ts +58 -78
  12. package/dist/src/Db.d.ts.map +1 -1
  13. package/dist/src/Db.js +368 -170
  14. package/dist/src/Diff.d.ts +9 -3
  15. package/dist/src/Diff.d.ts.map +1 -1
  16. package/dist/src/Diff.js +12 -11
  17. package/dist/src/Error.d.ts +15 -0
  18. package/dist/src/Error.d.ts.map +1 -0
  19. package/dist/src/Error.js +14 -0
  20. package/dist/src/Evolu.d.ts +139 -91
  21. package/dist/src/Evolu.d.ts.map +1 -1
  22. package/dist/src/Evolu.js +267 -239
  23. package/dist/src/Model.d.ts +52 -0
  24. package/dist/src/Model.d.ts.map +1 -1
  25. package/dist/src/Model.js +47 -5
  26. package/dist/src/Owner.d.ts.map +1 -1
  27. package/dist/src/Owner.js +14 -8
  28. package/dist/src/Platform.d.ts +24 -34
  29. package/dist/src/Platform.d.ts.map +1 -1
  30. package/dist/src/Platform.js +9 -31
  31. package/dist/src/Public.d.ts +3 -5
  32. package/dist/src/Public.d.ts.map +1 -1
  33. package/dist/src/Public.js +2 -3
  34. package/dist/src/Sqlite.d.ts +25 -16
  35. package/dist/src/Sqlite.d.ts.map +1 -1
  36. package/dist/src/Sqlite.js +28 -20
  37. package/dist/src/Store.d.ts.map +1 -1
  38. package/dist/src/Sync.d.ts +70 -0
  39. package/dist/src/Sync.d.ts.map +1 -0
  40. package/dist/src/Sync.js +126 -0
  41. package/dist/src/index.d.ts +2 -5
  42. package/dist/src/index.d.ts.map +1 -1
  43. package/dist/src/index.js +2 -5
  44. package/package.json +12 -10
  45. package/src/Config.ts +77 -36
  46. package/src/Crdt.ts +61 -76
  47. package/src/Crypto.ts +83 -60
  48. package/src/Db.ts +782 -352
  49. package/src/Diff.ts +23 -21
  50. package/src/Error.ts +29 -0
  51. package/src/Evolu.ts +599 -499
  52. package/src/Model.ts +102 -6
  53. package/src/Owner.ts +24 -24
  54. package/src/Platform.ts +33 -81
  55. package/src/Public.ts +3 -5
  56. package/src/Sqlite.ts +81 -44
  57. package/src/Sync.ts +306 -0
  58. package/src/index.ts +2 -5
  59. package/dist/src/DbWorker.d.ts +0 -82
  60. package/dist/src/DbWorker.d.ts.map +0 -1
  61. package/dist/src/DbWorker.js +0 -335
  62. package/dist/src/ErrorStore.d.ts +0 -29
  63. package/dist/src/ErrorStore.d.ts.map +0 -1
  64. package/dist/src/ErrorStore.js +0 -13
  65. package/dist/src/OnCompletes.d.ts +0 -14
  66. package/dist/src/OnCompletes.d.ts.map +0 -1
  67. package/dist/src/OnCompletes.js +0 -25
  68. package/dist/src/SyncWorker.d.ts +0 -80
  69. package/dist/src/SyncWorker.d.ts.map +0 -1
  70. package/dist/src/SyncWorker.js +0 -150
  71. package/dist/src/Types.d.ts +0 -13
  72. package/dist/src/Types.d.ts.map +0 -1
  73. package/dist/src/Types.js +0 -1
  74. package/src/DbWorker.ts +0 -721
  75. package/src/ErrorStore.ts +0 -46
  76. package/src/OnCompletes.ts +0 -51
  77. package/src/SyncWorker.ts +0 -361
  78. package/src/Types.ts +0 -11
package/src/Diff.ts CHANGED
@@ -1,6 +1,6 @@
1
+ import * as Arr from "effect/Array";
1
2
  import * as Predicate from "effect/Predicate";
2
- import * as ReadonlyArray from "effect/ReadonlyArray";
3
- import { Query, Row } from "./Db.js";
3
+ import type { Query, Row } from "./Db.js";
4
4
  import { Value } from "./Sqlite.js";
5
5
 
6
6
  export interface QueryPatches {
@@ -21,25 +21,27 @@ export interface ReplaceAtPatch {
21
21
  readonly value: Row;
22
22
  }
23
23
 
24
- export const applyPatches =
25
- (patches: ReadonlyArray<Patch>) =>
26
- (current: ReadonlyArray<Row>): ReadonlyArray<Row> =>
27
- patches.reduce((next, patch) => {
28
- switch (patch.op) {
29
- case "replaceAll":
30
- return patch.value;
31
- case "replaceAt": {
32
- return ReadonlyArray.replace(next, patch.index, patch.value);
33
- }
24
+ export const applyPatches = (
25
+ patches: ReadonlyArray<Patch>,
26
+ current: ReadonlyArray<Row>,
27
+ ): ReadonlyArray<Row> =>
28
+ patches.reduce((next, patch) => {
29
+ switch (patch.op) {
30
+ case "replaceAll":
31
+ return patch.value;
32
+ case "replaceAt": {
33
+ return Arr.replace(next, patch.index, patch.value);
34
34
  }
35
- }, current);
35
+ }
36
+ }, current);
36
37
 
37
- // We detect only a change in the whole result and in-place edits.
38
- // In the future, we will add more heuristics. We will probably not implement
39
- // the Myers diff algorithm because it's faster to rerender all than
40
- // to compute many detailed patches. We will only implement a logic
41
- // a developer would implement manually, if necessary.
42
- // Another idea is to make makePatches configurable via custom functions.
38
+ /**
39
+ * We detect only changes in the whole result and in-place edits. In the future,
40
+ * we will add more heuristics. We will probably not implement the Myers diff
41
+ * algorithm because it's faster to rerender all than to compute many detailed
42
+ * patches. We will only implement logic a developer would implement manually,
43
+ * if necessary.
44
+ */
43
45
  export const makePatches = (
44
46
  previousRows: ReadonlyArray<Row> | undefined,
45
47
  nextRows: ReadonlyArray<Row>,
@@ -90,8 +92,8 @@ export const areEqual = (
90
92
  return true;
91
93
  }
92
94
 
93
- const aIsArray = Array.isArray(a);
94
- const bIsArray = Array.isArray(b);
95
+ const aIsArray = Arr.isArray(a);
96
+ const bIsArray = Arr.isArray(b);
95
97
  if (aIsArray && bIsArray) {
96
98
  if (a.length !== b.length) return false;
97
99
  for (let i = 0; i < a.length; i++)
package/src/Error.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { TimestampError } from "./Crdt.js";
2
+
3
+ /** The EvoluError type is used to represent errors that can occur in Evolu. */
4
+ export type EvoluError = TimestampError | UnexpectedError;
5
+
6
+ /**
7
+ * UnexpectedError represents errors that can occur unexpectedly anywhere, even
8
+ * in third-party libraries, because Evolu uses Effect to track all errors.
9
+ */
10
+ export interface UnexpectedError {
11
+ readonly _tag: "UnexpectedError";
12
+ readonly error: unknown;
13
+ }
14
+
15
+ export const makeUnexpectedError = (error: unknown): UnexpectedError => ({
16
+ _tag: "UnexpectedError",
17
+ error,
18
+ });
19
+
20
+ /** Error isn't a structured cloneable object. */
21
+ export const ensureTransferableError = (error: unknown): unknown => {
22
+ if (error instanceof Error)
23
+ return {
24
+ message: error.message,
25
+ name: error.name,
26
+ stack: error.stack,
27
+ };
28
+ return error;
29
+ };