@livestore/livestore 0.0.46-dev.4 → 0.0.46

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 (115) hide show
  1. package/README.md +10 -0
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/__tests__/react/fixture.d.ts +18 -2
  4. package/dist/__tests__/react/fixture.d.ts.map +1 -1
  5. package/dist/__tests__/react/fixture.js +27 -3
  6. package/dist/__tests__/react/fixture.js.map +1 -1
  7. package/dist/__tests__/react/utils/otel.d.ts +10 -0
  8. package/dist/__tests__/react/utils/otel.d.ts.map +1 -0
  9. package/dist/__tests__/react/utils/otel.js +42 -0
  10. package/dist/__tests__/react/utils/otel.js.map +1 -0
  11. package/dist/index.d.ts +1 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/react/LiveStoreProvider.js +39 -6
  15. package/dist/react/LiveStoreProvider.js.map +1 -1
  16. package/dist/react/LiveStoreProvider.test.d.ts +2 -0
  17. package/dist/react/LiveStoreProvider.test.d.ts.map +1 -0
  18. package/dist/react/LiveStoreProvider.test.js +51 -0
  19. package/dist/react/LiveStoreProvider.test.js.map +1 -0
  20. package/dist/react/components/DiffableList copy.d.ts +19 -0
  21. package/dist/react/components/DiffableList copy.d.ts.map +1 -0
  22. package/dist/react/components/DiffableList copy.js +62 -0
  23. package/dist/react/components/DiffableList copy.js.map +1 -0
  24. package/dist/react/components/DiffableList.d.ts +2 -9
  25. package/dist/react/components/DiffableList.d.ts.map +1 -1
  26. package/dist/react/components/DiffableList.js +10 -102
  27. package/dist/react/components/DiffableList.js.map +1 -1
  28. package/dist/react/components/DiffableList2.d.ts +20 -0
  29. package/dist/react/components/DiffableList2.d.ts.map +1 -0
  30. package/dist/react/components/DiffableList2.js +119 -0
  31. package/dist/react/components/DiffableList2.js.map +1 -0
  32. package/dist/react/components/DiffableList3.d.ts +19 -0
  33. package/dist/react/components/DiffableList3.d.ts.map +1 -0
  34. package/dist/react/components/DiffableList3.js +62 -0
  35. package/dist/react/components/DiffableList3.js.map +1 -0
  36. package/dist/react/components/LiveList.d.ts +21 -0
  37. package/dist/react/components/LiveList.d.ts.map +1 -0
  38. package/dist/react/components/LiveList.js +31 -0
  39. package/dist/react/components/LiveList.js.map +1 -0
  40. package/dist/react/index.d.ts +1 -1
  41. package/dist/react/index.d.ts.map +1 -1
  42. package/dist/react/index.js +1 -1
  43. package/dist/react/index.js.map +1 -1
  44. package/dist/react/useAtom.d.ts +1 -1
  45. package/dist/react/useAtom.d.ts.map +1 -1
  46. package/dist/react/useAtom.js.map +1 -1
  47. package/dist/react/useQuery.d.ts +4 -1
  48. package/dist/react/useQuery.d.ts.map +1 -1
  49. package/dist/react/useQuery.js +24 -19
  50. package/dist/react/useQuery.js.map +1 -1
  51. package/dist/react/useQuery.test.js +11 -11
  52. package/dist/react/useQuery.test.js.map +1 -1
  53. package/dist/react/useRow.d.ts.map +1 -1
  54. package/dist/react/useRow.js +14 -69
  55. package/dist/react/useRow.js.map +1 -1
  56. package/dist/react/useRow.test.js +440 -28
  57. package/dist/react/useRow.test.js.map +1 -1
  58. package/dist/react/useRowOld.d.ts +40 -0
  59. package/dist/react/useRowOld.d.ts.map +1 -0
  60. package/dist/react/useRowOld.js +134 -0
  61. package/dist/react/useRowOld.js.map +1 -0
  62. package/dist/react/useTemporaryQuery.d.ts +15 -3
  63. package/dist/react/useTemporaryQuery.d.ts.map +1 -1
  64. package/dist/react/useTemporaryQuery.js +60 -27
  65. package/dist/react/useTemporaryQuery.js.map +1 -1
  66. package/dist/react/useTemporaryQuery.test.js +10 -9
  67. package/dist/react/useTemporaryQuery.test.js.map +1 -1
  68. package/dist/reactive.d.ts +23 -5
  69. package/dist/reactive.d.ts.map +1 -1
  70. package/dist/reactive.js +44 -11
  71. package/dist/reactive.js.map +1 -1
  72. package/dist/reactive.test.js +1 -1
  73. package/dist/reactive.test.js.map +1 -1
  74. package/dist/reactiveQueries/base-class.d.ts +1 -1
  75. package/dist/reactiveQueries/base-class.d.ts.map +1 -1
  76. package/dist/reactiveQueries/base-class.js.map +1 -1
  77. package/dist/reactiveQueries/graphql.d.ts +2 -2
  78. package/dist/reactiveQueries/graphql.d.ts.map +1 -1
  79. package/dist/reactiveQueries/graphql.js +16 -10
  80. package/dist/reactiveQueries/graphql.js.map +1 -1
  81. package/dist/reactiveQueries/sql.d.ts +1 -1
  82. package/dist/reactiveQueries/sql.d.ts.map +1 -1
  83. package/dist/reactiveQueries/sql.js +15 -11
  84. package/dist/reactiveQueries/sql.js.map +1 -1
  85. package/dist/reactiveQueries/sql.test.js +1 -40
  86. package/dist/reactiveQueries/sql.test.js.map +1 -1
  87. package/dist/store.d.ts +2 -2
  88. package/dist/store.d.ts.map +1 -1
  89. package/dist/store.js +10 -7
  90. package/dist/store.js.map +1 -1
  91. package/package.json +6 -8
  92. package/src/__tests__/react/fixture.tsx +35 -2
  93. package/src/__tests__/react/utils/otel.ts +61 -0
  94. package/src/index.ts +12 -1
  95. package/src/react/LiveStoreProvider.test.tsx +82 -0
  96. package/src/react/LiveStoreProvider.tsx +42 -7
  97. package/src/react/components/LiveList.tsx +84 -0
  98. package/src/react/index.ts +1 -1
  99. package/src/react/useAtom.ts +1 -1
  100. package/src/react/useQuery.test.tsx +11 -11
  101. package/src/react/useQuery.ts +29 -22
  102. package/src/react/useRow.test.tsx +502 -30
  103. package/src/react/useRow.ts +19 -107
  104. package/src/react/useTemporaryQuery.test.tsx +17 -16
  105. package/src/react/useTemporaryQuery.ts +96 -28
  106. package/src/reactive.test.ts +1 -1
  107. package/src/reactive.ts +76 -15
  108. package/src/reactiveQueries/base-class.ts +2 -1
  109. package/src/reactiveQueries/graphql.ts +21 -15
  110. package/src/reactiveQueries/sql.test.ts +1 -54
  111. package/src/reactiveQueries/sql.ts +20 -14
  112. package/src/store.ts +12 -8
  113. package/tsconfig.json +0 -1
  114. package/src/react/components/DiffableList.tsx +0 -192
  115. package/src/react/utils/useCleanup.ts +0 -25
@@ -204,14 +204,16 @@ export declare const schema: import("@livestore/common/dist/schema/index.js").Li
204
204
  }, never>;
205
205
  export declare const parseTodos: (rawRows: readonly any[]) => readonly {
206
206
  text: string;
207
- completed: boolean;
208
207
  id: string;
208
+ completed: boolean;
209
209
  }[];
210
- export declare const makeTodoMvc: ({ otelTracer, otelContext, useGlobalDbGraph, }?: {
210
+ export declare const makeTodoMvc: ({ otelTracer, otelContext, useGlobalDbGraph, strictMode, }?: {
211
211
  otelTracer?: otel.Tracer | undefined;
212
212
  otelContext?: otel.Context | undefined;
213
213
  useGlobalDbGraph?: boolean | undefined;
214
+ strictMode?: boolean | undefined;
214
215
  }) => Promise<{
216
+ [Symbol.dispose]: () => Promise<void>;
215
217
  wrapper: ({ children }: any) => React.JSX.Element;
216
218
  AppComponentSchema: DbSchema.TableDef<DbSchema.SqliteDsl.TableDefinition<"UserInfo", {
217
219
  username: {
@@ -221,6 +223,13 @@ export declare const makeTodoMvc: ({ otelTracer, otelContext, useGlobalDbGraph,
221
223
  nullable: false;
222
224
  primaryKey: false;
223
225
  };
226
+ text: {
227
+ columnType: "text";
228
+ schema: __Schema.Schema<string, string, never>;
229
+ default: import("effect/Option").Some<"">;
230
+ nullable: false;
231
+ primaryKey: false;
232
+ };
224
233
  id: DbSchema.SqliteDsl.ColumnDefinition<string, string>;
225
234
  }>, false, {
226
235
  isSingleton: false;
@@ -228,9 +237,11 @@ export declare const makeTodoMvc: ({ otelTracer, otelContext, useGlobalDbGraph,
228
237
  disableAutomaticIdColumn: false;
229
238
  }, __Schema.Schema<{
230
239
  readonly username: string;
240
+ readonly text: string;
231
241
  readonly id: string;
232
242
  }, {
233
243
  readonly username: string;
244
+ readonly text: string;
234
245
  readonly id: string;
235
246
  }, never>>;
236
247
  store: import("@livestore/livestore/src/store.js").Store<import("@livestore/livestore/src/store.js").BaseGraphQLContext, import("@livestore/common/dist/schema/index.js").LiveStoreSchema<{
@@ -351,5 +362,10 @@ export declare const makeTodoMvc: ({ otelTracer, otelContext, useGlobalDbGraph,
351
362
  readonly newTodoText: string | null;
352
363
  readonly filter: string;
353
364
  }, never>>>;
365
+ makeRenderCount: () => {
366
+ readonly val: number;
367
+ inc: () => void;
368
+ };
369
+ strictMode: boolean;
354
370
  }>;
355
371
  //# sourceMappingURL=fixture.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fixture.d.ts","sourceRoot":"","sources":["../../../src/__tests__/react/fixture.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAE5D,OAAO,KAAK,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAC/C,OAAO,KAAK,MAAM,OAAO,CAAA;AAIzB,OAAO,EAAe,QAAQ,EAA8D,MAAM,gBAAgB,CAAA;AAGlH,MAAM,MAAM,IAAI,GAAG;IACjB,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,OAAO,CAAA;CACnB,CAAA;AAED,MAAM,MAAM,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,WAAW,CAAA;AAEnD,MAAM,MAAM,QAAQ,GAAG;IACrB,WAAW,EAAE,MAAM,CAAA;IACnB,MAAM,EAAE,MAAM,CAAA;CACf,CAAA;AAED,eAAO,MAAM,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAIhB,CAAA;AAEF,eAAO,MAAM,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAId,CAAA;AAEF,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAAiB,CAAA;AACpC,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SAAyB,CAAA;AAE5C,eAAO,MAAM,UAAU;;;;GAAyB,CAAA;AAEhD,eAAO,MAAM,WAAW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4BAiDS,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAkBnC,CAAA"}
@@ -16,11 +16,24 @@ export const app = DbSchema.table('app', {
16
16
  export const tables = { todos, app };
17
17
  export const schema = makeSchema({ tables });
18
18
  export const parseTodos = ParseUtils.many(todos);
19
- export const makeTodoMvc = async ({ otelTracer, otelContext, useGlobalDbGraph = true, } = {}) => {
19
+ export const makeTodoMvc = async ({ otelTracer, otelContext, useGlobalDbGraph = true, strictMode = process.env.REACT_STRICT_MODE !== undefined, } = {}) => {
20
20
  const AppComponentSchema = DbSchema.table('UserInfo', {
21
21
  username: DbSchema.text({ default: '' }),
22
+ text: DbSchema.text({ default: '' }),
22
23
  });
23
24
  const dbGraph = useGlobalDbGraph ? globalDbGraph : makeDbGraph();
25
+ const makeRenderCount = () => {
26
+ let val = 0;
27
+ const inc = () => {
28
+ val += strictMode ? 0.5 : 1;
29
+ };
30
+ return {
31
+ get val() {
32
+ return val;
33
+ },
34
+ inc,
35
+ };
36
+ };
24
37
  const store = await createStore({
25
38
  schema,
26
39
  boot: (db) => db.execute(sql `INSERT OR IGNORE INTO app (id, newTodoText, filter) VALUES ('static', '', 'all');`),
@@ -32,7 +45,18 @@ export const makeTodoMvc = async ({ otelTracer, otelContext, useGlobalDbGraph =
32
45
  const cud = makeCudMutations(tables);
33
46
  // TODO improve typing of `LiveStoreContext`
34
47
  const storeContext = { store };
35
- const wrapper = ({ children }) => (React.createElement(LiveStoreReact.LiveStoreContext.Provider, { value: storeContext }, children));
36
- return { wrapper, AppComponentSchema, store, dbGraph, cud };
48
+ const MaybeStrictMode = strictMode ? React.StrictMode : React.Fragment;
49
+ const wrapper = ({ children }) => (React.createElement(MaybeStrictMode, null,
50
+ React.createElement(LiveStoreReact.LiveStoreContext.Provider, { value: storeContext }, children)));
51
+ return {
52
+ [Symbol.dispose]: () => store.destroy(),
53
+ wrapper,
54
+ AppComponentSchema,
55
+ store,
56
+ dbGraph,
57
+ cud,
58
+ makeRenderCount,
59
+ strictMode,
60
+ };
37
61
  };
38
62
  //# sourceMappingURL=fixture.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"fixture.js","sourceRoot":"","sources":["../../../src/__tests__/react/fixture.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAEvC,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AAErD,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,gBAAgB,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAA;AAClH,OAAO,KAAK,cAAc,MAAM,sBAAsB,CAAA;AAetD,MAAM,CAAC,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,OAAO,EAAE;IAC3C,EAAE,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;IACvC,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;IACrD,SAAS,EAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;CACjE,CAAC,CAAA;AAEF,MAAM,CAAC,MAAM,GAAG,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,EAAE;IACvC,EAAE,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;IACvC,WAAW,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,QAAQ,EAAE,IAAI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AAE,CAAC,EAAE,CAAC,OAAO,CAAC,GAAG,CAAA,mFAAmF,CAAC;QAChH,MAAM,EAAE,MAAM,EAAE;QAChB,OAAO;QACP,UAAU;QACV,mBAAmB,EAAE,WAAW;KACjC,CAAC,CAAA;IAEF,MAAM,GAAG,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAA;IAEpC,4CAA4C;IAC5C,MAAM,YAAY,GAAqB,EAAE,KAAK,EAAU,CAAA;IAExD,MAAM,eAAe,GAAG,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAA;IAEtE,MAAM,OAAO,GAAG,CAAC,EAAE,QAAQ,EAAO,EAAE,EAAE,CAAC,CACrC,oBAAC,eAAe;QACd,oBAAC,cAAc,CAAC,gBAAgB,CAAC,QAAQ,IAAC,KAAK,EAAE,YAAY,IAC1D,QAAQ,CACgC,CAC3B,CACnB,CAAA;IAED,OAAO;QACL,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE;QACvC,OAAO;QACP,kBAAkB;QAClB,KAAK;QACL,OAAO;QACP,GAAG;QACH,eAAe;QACf,UAAU;KACX,CAAA;AACH,CAAC,CAAA"}
@@ -0,0 +1,10 @@
1
+ import type { Attributes } from '@opentelemetry/api';
2
+ import type { InMemorySpanExporter } from '@opentelemetry/sdk-trace-base';
3
+ type SimplifiedNestedSpan = {
4
+ _name: string;
5
+ attributes: any;
6
+ children: SimplifiedNestedSpan[];
7
+ };
8
+ export declare const getSimplifiedRootSpan: (exporter: InMemorySpanExporter, mapAttributes?: ((attributes: Attributes) => Attributes) | undefined) => SimplifiedNestedSpan;
9
+ export {};
10
+ //# sourceMappingURL=otel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"otel.d.ts","sourceRoot":"","sources":["../../../../src/__tests__/react/utils/otel.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,KAAK,EAAE,oBAAoB,EAAgB,MAAM,+BAA+B,CAAA;AAEvF,KAAK,oBAAoB,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,GAAG,CAAC;IAAC,QAAQ,EAAE,oBAAoB,EAAE,CAAA;CAAE,CAAA;AAEhG,eAAO,MAAM,qBAAqB,aACtB,oBAAoB,gCACD,UAAU,KAAK,UAAU,sCAiCvD,CAAA"}
@@ -0,0 +1,42 @@
1
+ import { identity } from '@livestore/utils/effect';
2
+ export const getSimplifiedRootSpan = (exporter, mapAttributes) => {
3
+ const spans = exporter.getFinishedSpans();
4
+ const spansMap = new Map(spans.map((span) => [span.spanContext().spanId, { span, children: [] }]));
5
+ const mapAttributesfn = mapAttributes ?? identity;
6
+ spansMap.forEach((nestedSpan) => {
7
+ const parentSpan = nestedSpan.span.parentSpanId ? spansMap.get(nestedSpan.span.parentSpanId) : undefined;
8
+ if (parentSpan) {
9
+ parentSpan.children.push(nestedSpan);
10
+ }
11
+ });
12
+ const rootSpan = spansMap.get(spans.find((_) => _.name === 'test').spanContext().spanId);
13
+ const simplifySpan = (span) => omitEmpty({
14
+ _name: span.span.name,
15
+ attributes: mapAttributesfn(span.span.attributes),
16
+ children: span.children
17
+ .filter((_) => _.span.name !== 'createStore')
18
+ // .sort((a, b) => compareHrTime(a.span.startTime, b.span.startTime))
19
+ .map(simplifySpan),
20
+ });
21
+ // console.dir(
22
+ // spans.map((_) => [_.spanContext().spanId, _.name, _.attributes, _.parentSpanId]),
23
+ // { depth: 10 },
24
+ // )
25
+ return simplifySpan(rootSpan);
26
+ };
27
+ // const compareHrTime = (a: [number, number], b: [number, number]) => {
28
+ // if (a[0] !== b[0]) return a[0] - b[0]
29
+ // return a[1] - b[1]
30
+ // }
31
+ const omitEmpty = (obj) => {
32
+ const result = {};
33
+ for (const key in obj) {
34
+ if (obj[key] !== undefined &&
35
+ !(Array.isArray(obj[key]) && obj[key].length === 0) &&
36
+ Object.keys(obj[key]).length > 0) {
37
+ result[key] = obj[key];
38
+ }
39
+ }
40
+ return result;
41
+ };
42
+ //# sourceMappingURL=otel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"otel.js","sourceRoot":"","sources":["../../../../src/__tests__/react/utils/otel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAMlD,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,QAA8B,EAC9B,aAAsD,EACtD,EAAE;IACF,MAAM,KAAK,GAAG,QAAQ,CAAC,gBAAgB,EAAE,CAAA;IACzC,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAqB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,CAAA;IAEtH,MAAM,eAAe,GAAG,aAAa,IAAI,QAAQ,CAAA;IAEjD,QAAQ,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;QAC9B,MAAM,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;QACxG,IAAI,UAAU,EAAE,CAAC;YACf,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;QACtC,CAAC;IACH,CAAC,CAAC,CAAA;IAGF,MAAM,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAE,CAAC,WAAW,EAAE,CAAC,MAAM,CAAE,CAAA;IAE1F,MAAM,YAAY,GAAG,CAAC,IAAgB,EAAwB,EAAE,CAC9D,SAAS,CAAC;QACR,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI;QACrB,UAAU,EAAE,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC;QACjD,QAAQ,EAAE,IAAI,CAAC,QAAQ;aACpB,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,aAAa,CAAC;YAC7C,qEAAqE;aACpE,GAAG,CAAC,YAAY,CAAC;KACrB,CAAC,CAAA;IAEJ,eAAe;IACf,sFAAsF;IACtF,mBAAmB;IACnB,IAAI;IAEJ,OAAO,YAAY,CAAC,QAAQ,CAAC,CAAA;AAC/B,CAAC,CAAA;AAED,wEAAwE;AACxE,0CAA0C;AAC1C,uBAAuB;AACvB,IAAI;AAEJ,MAAM,SAAS,GAAG,CAAC,GAAQ,EAAE,EAAE;IAC7B,MAAM,MAAM,GAAQ,EAAE,CAAA;IACtB,KAAK,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACtB,IACE,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS;YACtB,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;YACnD,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,EAChC,CAAC;YACD,MAAM,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAA;QACxB,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export { Store, createStore } from './store.js';
2
2
  export type { BaseGraphQLContext, QueryDebugInfo, RefreshReason, BootDb } from './store.js';
3
3
  export type { QueryDefinition, LiveStoreCreateStoreOptions, LiveStoreContext } from './effect/LiveStore.js';
4
4
  export { MainDatabaseWrapper, type DebugInfo, emptyDebugInfo } from './MainDatabaseWrapper.js';
5
- export type { GetAtom, AtomDebugInfo, RefreshDebugInfo, SerializedAtom, Atom, Node, Ref, Effect } from './reactive.js';
5
+ export type { GetAtom, AtomDebugInfo, RefreshDebugInfo, ReactiveGraphSnapshot, SerializedAtom, SerializedEffect, Atom, Node, Ref, Effect, } from './reactive.js';
6
6
  export { LiveStoreJSQuery, computed } from './reactiveQueries/js.js';
7
7
  export { LiveStoreSQLQuery, querySQL, type MapRows } from './reactiveQueries/sql.js';
8
8
  export { LiveStoreGraphQLQuery, queryGraphQL } from './reactiveQueries/graphql.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAC/C,YAAY,EAAE,kBAAkB,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAE3F,YAAY,EAAE,eAAe,EAAE,2BAA2B,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAE3G,OAAO,EAAE,mBAAmB,EAAE,KAAK,SAAS,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AAE9F,YAAY,EAAE,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,cAAc,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AACtH,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AACpE,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,KAAK,OAAO,EAAE,MAAM,0BAA0B,CAAA;AACpF,OAAO,EAAE,qBAAqB,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAA;AAClF,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,OAAO,EAAE,WAAW,EAAE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OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA"}
@@ -19,11 +19,40 @@ export const LiveStoreProvider = ({ fallback, graphQLOptions, otelTracer, otelRo
19
19
  return React.createElement(LiveStoreContext.Provider, { value: storeCtx }, children);
20
20
  };
21
21
  const useCreateStore = ({ schema, graphQLOptions, otelTracer, otelRootSpanContext, boot, makeDb, batchUpdates, }) => {
22
- const [ctxValue, setCtxValue] = React.useState();
22
+ const [_, rerender] = React.useState(0);
23
+ const ctxValueRef = React.useRef(undefined);
24
+ const inputPropsCacheRef = React.useRef({
25
+ schema,
26
+ graphQLOptions,
27
+ otelTracer,
28
+ otelRootSpanContext,
29
+ boot,
30
+ makeDb,
31
+ batchUpdates,
32
+ });
33
+ const oldStoreAlreadyDestroyedRef = React.useRef(false);
34
+ if (inputPropsCacheRef.current.schema !== schema ||
35
+ inputPropsCacheRef.current.graphQLOptions !== graphQLOptions ||
36
+ inputPropsCacheRef.current.otelTracer !== otelTracer ||
37
+ inputPropsCacheRef.current.otelRootSpanContext !== otelRootSpanContext ||
38
+ inputPropsCacheRef.current.boot !== boot ||
39
+ inputPropsCacheRef.current.makeDb !== makeDb ||
40
+ inputPropsCacheRef.current.batchUpdates !== batchUpdates) {
41
+ inputPropsCacheRef.current = {
42
+ schema,
43
+ graphQLOptions,
44
+ otelTracer,
45
+ otelRootSpanContext,
46
+ boot,
47
+ makeDb,
48
+ batchUpdates,
49
+ };
50
+ ctxValueRef.current?.store.destroy();
51
+ oldStoreAlreadyDestroyedRef.current = true;
52
+ ctxValueRef.current = undefined;
53
+ }
23
54
  React.useEffect(() => {
24
55
  let store;
25
- // resetting the store context while we're creating a new store
26
- setCtxValue(undefined);
27
56
  void (async () => {
28
57
  try {
29
58
  store = await createStore({
@@ -35,16 +64,20 @@ const useCreateStore = ({ schema, graphQLOptions, otelTracer, otelRootSpanContex
35
64
  makeDb,
36
65
  batchUpdates,
37
66
  });
38
- setCtxValue({ store });
67
+ ctxValueRef.current = { store };
68
+ oldStoreAlreadyDestroyedRef.current = false;
69
+ rerender((c) => c + 1);
39
70
  }
40
71
  catch (e) {
41
72
  shouldNeverHappen(`Error creating LiveStore store: ${e}`);
42
73
  }
43
74
  })();
44
75
  return () => {
45
- store?.destroy();
76
+ if (oldStoreAlreadyDestroyedRef.current === false) {
77
+ store?.destroy();
78
+ }
46
79
  };
47
80
  }, [schema, graphQLOptions, otelTracer, otelRootSpanContext, boot, makeDb, batchUpdates]);
48
- return ctxValue;
81
+ return ctxValueRef.current;
49
82
  };
50
83
  //# sourceMappingURL=LiveStoreProvider.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"LiveStoreProvider.js","sourceRoot":"","sources":["../../src/react/LiveStoreProvider.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAGpD,OAAO,KAAK,MAAM,OAAO,CAAA;AAKzB,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAaxD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAA4C,EAC3E,QAAQ,EACR,cAAc,EACd,UAAU,EACV,mBAAmB,EACnB,QAAQ,EACR,MAAM,EACN,IAAI,EACJ,MAAM,EACN,YAAY,GACsD,EAAe,EAAE;IACnF,MAAM,QAAQ,GAAG,cAAc,CAAC;QAC9B,MAAM;QACN,cAAc;QACd,UAAU;QACV,mBAAmB;QACnB,IAAI;QACJ,MAAM;QACN,YAAY;KACb,CAAC,CAAA;IAEF,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO,QAAQ,CAAA;IACjB,CAAC;IAED,MAAM,CAAC,gBAAgB,GAAG,QAAQ,CAAC,KAAK,CAAA;IAExC,OAAO,oBAAC,gBAAgB,CAAC,QAAQ,IAAC,KAAK,EAAE,QAAQ,IAAG,QAAQ,CAA6B,CAAA;AAC3F,CAAC,CAAA;AAED,MAAM,cAAc,GAAG,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,CAAC,MAAM,EAAE,cAAc,EAAE,UAAU,EAAE,mBAAmB,EAAE,IAAI,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC,CAAA;IAEzF,OAAO,WAAW,CAAC,OAAO,CAAA;AAC5B,CAAC,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=LiveStoreProvider.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LiveStoreProvider.test.d.ts","sourceRoot":"","sources":["../../src/react/LiveStoreProvider.test.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,51 @@
1
+ import { sql } from '@livestore/common';
2
+ import { makeDb } from '@livestore/web';
3
+ import { render, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react';
4
+ import React from 'react';
5
+ import { describe, expect, it } from 'vitest';
6
+ import { parseTodos, schema } from '../__tests__/react/fixture.js';
7
+ import { querySQL } from '../reactiveQueries/sql.js';
8
+ import * as LiveStoreReact from './index.js';
9
+ import { LiveStoreProvider } from './LiveStoreProvider.js';
10
+ class TestInMemoryStorage {
11
+ filename = '__test__in-memory__';
12
+ constructor() { }
13
+ execute = async () => { };
14
+ mutate = async () => { };
15
+ export = async () => undefined;
16
+ getMutationLogData = async () => new Uint8Array();
17
+ dangerouslyReset = async () => { };
18
+ shutdown = async () => {
19
+ await new Promise((resolve) => setTimeout(resolve, 200));
20
+ };
21
+ }
22
+ describe('LiveStoreProvider', () => {
23
+ it('simple', async () => {
24
+ let renderCount = 0;
25
+ const allTodos$ = querySQL(`select * from todos`, { map: parseTodos });
26
+ let latestStoreCtx = undefined;
27
+ const App = () => {
28
+ renderCount++;
29
+ latestStoreCtx = LiveStoreReact.useStore();
30
+ const todos = LiveStoreReact.useQuery(allTodos$);
31
+ return React.createElement("div", null, JSON.stringify(todos));
32
+ };
33
+ const Root = ({ forceUpdate }) => {
34
+ const bootCb = React.useCallback((db) => db.execute(sql `INSERT OR IGNORE INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0);`), []);
35
+ // eslint-disable-next-line react-hooks/exhaustive-deps
36
+ const makeDbMemo = React.useMemo(() => makeDb(() => () => new TestInMemoryStorage()), [forceUpdate]);
37
+ return (React.createElement(LiveStoreProvider, { schema: schema, fallback: React.createElement("div", null, "Loading LiveStore"), makeDb: makeDbMemo, boot: bootCb },
38
+ React.createElement(App, null)));
39
+ };
40
+ const { rerender } = render(React.createElement(Root, { forceUpdate: 1 }));
41
+ expect(renderCount).toBe(0);
42
+ await waitForElementToBeRemoved(() => screen.getByText('Loading LiveStore'));
43
+ expect(renderCount).toBe(1);
44
+ rerender(React.createElement(Root, { forceUpdate: 2 }));
45
+ await waitFor(() => screen.getByText('Loading LiveStore'));
46
+ await waitForElementToBeRemoved(() => screen.getByText('Loading LiveStore'));
47
+ expect(renderCount).toBe(2);
48
+ await latestStoreCtx.store.destroy();
49
+ });
50
+ });
51
+ //# sourceMappingURL=LiveStoreProvider.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LiveStoreProvider.test.js","sourceRoot":"","sources":["../../src/react/LiveStoreProvider.test.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAA;AAC3F,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAE7C,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,+BAA+B,CAAA;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAA;AAEpD,OAAO,KAAK,cAAc,MAAM,YAAY,CAAA;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAE1D,MAAM,mBAAmB;IACvB,QAAQ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/base-class.js';
3
+ export type Props<TItem> = {
4
+ items$: LiveQuery<ReadonlyArray<TItem>>;
5
+ /**
6
+ * @example
7
+ * ```tsx
8
+ * renderContainer={(children) => <ul>{children}</ul>}
9
+ * ```
10
+ */
11
+ renderContainer: (ref: React.LegacyRef<any>) => React.ReactNode;
12
+ renderItem: (item: TItem, opts: {
13
+ index: number;
14
+ isInitialListRender: boolean;
15
+ }) => React.ReactNode;
16
+ getKey: (item: TItem, index: number) => string | number;
17
+ };
18
+ export declare const DiffableList: <TItem>({ items$, renderContainer, renderItem, getKey, }: Props<TItem>) => React.ReactNode;
19
+ //# sourceMappingURL=DiffableList%20copy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DiffableList copy.d.ts","sourceRoot":"","sources":["../../../src/react/components/DiffableList copy.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,MAAM,OAAO,CAAA;AAGzB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qCAAqC,CAAA;AAUpE,MAAM,MAAM,KAAK,CAAC,KAAK,IAAI;IACzB,MAAM,EAAE,SAAS,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAA;IACvC;;;;;OAKG;IACH,eAAe,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,KAAK,CAAC,SAAS,CAAA;IAE/D,UAAU,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,mBAAmB,EAAE,OAAO,CAAA;KAAE,KAAK,KAAK,CAAC,SAAS,CAAA;IACnG,MAAM,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,CAAA;CACxD,CAAA;AAED,eAAO,MAAM,YAAY,4DAKtB,MAAM,KAAK,CAAC,KAAG,MAAM,SAwEvB,CAAA"}
@@ -0,0 +1,62 @@
1
+ import * as million from 'million';
2
+ import React from 'react';
3
+ import { computed } from '../../reactiveQueries/js.js';
4
+ import { useQuery } from '../useQuery.js';
5
+ export const DiffableList = ({ items$, renderContainer, renderItem, getKey, }) => {
6
+ const ref = React.useRef(null);
7
+ const container = renderContainer(ref);
8
+ const [hasMounted, setHasMounted] = React.useState(false);
9
+ React.useEffect(() => setHasMounted(true), []);
10
+ const keys$ = computed((get) => get(items$).map(getKey));
11
+ const elsRef = React.useRef([]);
12
+ // const ContextBridge = itsFine.useContextBridge()
13
+ // const { store } = useStore()
14
+ // const renderListEl = React.useCallback(
15
+ // (parentEl: HTMLElement, index: number, item$: LiveQuery<TItem>) => {
16
+ // const root = ReactDOM.createRoot(parentEl)
17
+ // root.render(
18
+ // // <ContextBridge>
19
+ // <LiveStoreContext.Provider value={{ store }}>
20
+ // <ItemWrapper item$={item$} renderItem={renderItem} opts={{ index, isInitialListRender: !hasMounted }} />
21
+ // </LiveStoreContext.Provider>,
22
+ // // </ContextBridge>,
23
+ // )
24
+ // return root
25
+ // },
26
+ // [hasMounted, renderItem, store],
27
+ // )
28
+ React.useLayoutEffect(() => {
29
+ if (ref.current === null) {
30
+ throw new Error('ref.current is null');
31
+ }
32
+ const keys = keys$.run();
33
+ const queries$ = keys.map((_key, index) => computed((get) => get(items$)[index]));
34
+ // const list = million.mapArray(
35
+ // queries$.map((item$, index) =>
36
+ // ItemWrapperBlock({
37
+ // item$,
38
+ // opts: { index, isInitialListRender: !hasMounted },
39
+ // renderItem,
40
+ // }),
41
+ // ),
42
+ // )
43
+ // million.mount(list, ref.current)
44
+ // const keys = keys$.run()
45
+ // for (let index = 0; index < keys.length; index++) {
46
+ // const parentEl = document.createElement('div')
47
+ // ref.current!.append(parentEl)
48
+ // const item$ = computed((get) => get(items$)[index]!) as LiveQuery<TItem>
49
+ // const root = renderListEl(parentEl, index, item$)
50
+ // elsRef.current.push({ el: parentEl, item$, root, id: keys[index]! })
51
+ // }
52
+ // eslint-disable-next-line react-hooks/exhaustive-deps
53
+ }, []);
54
+ React.useEffect(() => () => keys$.destroy(), [keys$]);
55
+ return React.createElement(React.Fragment, null, container);
56
+ };
57
+ const ItemWrapper = ({ item$, opts, renderItem, }) => {
58
+ const item = useQuery(item$);
59
+ return React.createElement(React.Fragment, null, renderItem(item, opts));
60
+ };
61
+ const ItemWrapperBlock = million.block(ItemWrapper);
62
+ //# sourceMappingURL=DiffableList%20copy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DiffableList copy.js","sourceRoot":"","sources":["../../../src/react/components/DiffableList copy.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AAClC,OAAO,KAAK,MAAM,OAAO,CAAA;AAIzB,OAAO,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAA;AAGtD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAoBzC,MAAM,CAAC,MAAM,YAAY,GAAG,CAAS,EACnC,MAAM,EACN,eAAe,EACf,UAAU,EACV,MAAM,GACO,EAAmB,EAAE;IAClC,MAAM,GAAG,GAAG,KAAK,CAAC,MAAM,CAAc,IAAI,CAAC,CAAA;IAC3C,MAAM,SAAS,GAAG,eAAe,CAAC,GAAG,CAAC,CAAA;IAEtC,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;IAEzD,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,CAAA;IAE9C,MAAM,KAAK,GAAG,QAAQ,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAA;IAOxD,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAU,EAAE,CAAC,CAAA;IAExC,mDAAmD;IACnD,+BAA+B;IAE/B,0CAA0C;IAC1C,yEAAyE;IACzE,iDAAiD;IACjD,mBAAmB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AAA;AACtC,CAAC,CAAA;AAED,MAAM,gBAAgB,GAAG,OAAO,CAAC,KAAK,CAAC,WAAkB,CAAC,CAAA"}
@@ -2,19 +2,12 @@ import React from 'react';
2
2
  import type { LiveQuery } from '../../reactiveQueries/base-class.js';
3
3
  export type Props<TItem> = {
4
4
  items$: LiveQuery<ReadonlyArray<TItem>>;
5
- /**
6
- * @example
7
- * ```tsx
8
- * renderContainer={(children) => <ul>{children}</ul>}
9
- * ```
10
- */
11
- renderContainer: (ref: React.LegacyRef<any>) => React.ReactNode;
12
5
  renderItem: (item: TItem, opts: {
13
6
  index: number;
14
7
  isInitialListRender: boolean;
15
8
  }) => React.ReactNode;
9
+ /** Needs to be unique across all list items */
16
10
  getKey: (item: TItem, index: number) => string | number;
17
11
  };
18
- export declare const DiffableList_: <TItem>({ items$, renderContainer, renderItem, getKey, }: Props<TItem>) => React.ReactNode;
19
- export declare const DiffableList: <TItem>({ items$, renderContainer, renderItem, getKey, }: Props<TItem>) => React.ReactNode;
12
+ export declare const DiffableList: <TItem>({ items$, renderItem, getKey }: Props<TItem>) => React.ReactNode;
20
13
  //# sourceMappingURL=DiffableList.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"DiffableList.d.ts","sourceRoot":"","sources":["../../../src/react/components/DiffableList.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,MAAM,OAAO,CAAA;AAGzB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qCAAqC,CAAA;AAIpE,MAAM,MAAM,KAAK,CAAC,KAAK,IAAI;IACzB,MAAM,EAAE,SAAS,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAA;IACvC;;;;;OAKG;IACH,eAAe,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,KAAK,CAAC,SAAS,CAAA;IAE/D,UAAU,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,mBAAmB,EAAE,OAAO,CAAA;KAAE,KAAK,KAAK,CAAC,SAAS,CAAA;IACnG,MAAM,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,CAAA;CACxD,CAAA;AAED,eAAO,MAAM,aAAa,4DAKvB,MAAM,KAAK,CAAC,KAAG,MAAM,SAyIvB,CAAA;AAED,eAAO,MAAM,YAAY,4DAKtB,MAAM,KAAK,CAAC,KAAG,MAAM,SAIvB,CAAA"}
1
+ {"version":3,"file":"DiffableList.d.ts","sourceRoot":"","sources":["../../../src/react/components/DiffableList.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qCAAqC,CAAA;AASpE,MAAM,MAAM,KAAK,CAAC,KAAK,IAAI;IACzB,MAAM,EAAE,SAAS,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAA;IAEvC,UAAU,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,mBAAmB,EAAE,OAAO,CAAA;KAAE,KAAK,KAAK,CAAC,SAAS,CAAA;IACnG,+CAA+C;IAC/C,MAAM,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,CAAA;CACxD,CAAA;AAED,eAAO,MAAM,YAAY,0CAA4C,MAAM,KAAK,CAAC,KAAG,MAAM,SA6BzF,CAAA"}
@@ -1,113 +1,21 @@
1
- import { FI } from '@livestore/fractional-index';
2
- import { casesHandled } from '@livestore/utils';
3
- import * as itsFine from 'its-fine';
4
1
  import React from 'react';
5
- import ReactDOM from 'react-dom/client';
6
2
  import { computed } from '../../reactiveQueries/js.js';
7
3
  import { useQuery } from '../useQuery.js';
8
- export const DiffableList_ = ({ items$, renderContainer, renderItem, getKey, }) => {
9
- const ref = React.useRef(null);
10
- const container = renderContainer(ref);
4
+ import { useTemporaryQuery } from '../useTemporaryQuery.js';
5
+ export const DiffableList = ({ items$, renderItem, getKey }) => {
11
6
  const [hasMounted, setHasMounted] = React.useState(false);
12
7
  React.useEffect(() => setHasMounted(true), []);
13
- const keys$ = computed((get) => get(items$).map(getKey));
14
- const elsRef = React.useRef([]);
15
- const ContextBridge = itsFine.useContextBridge();
16
- const renderListEl = React.useCallback((parentEl, index, item$) => {
17
- const root = ReactDOM.createRoot(parentEl);
18
- root.render(React.createElement(ContextBridge, null,
19
- React.createElement(ItemWrapper, { "item$": item$, renderItem: renderItem, opts: { index, isInitialListRender: !hasMounted } })));
20
- return root;
21
- }, [ContextBridge, hasMounted, renderItem]);
22
- React.useLayoutEffect(() => {
23
- if (ref.current === null) {
24
- throw new Error('ref.current is null');
25
- }
26
- const keys = keys$.run();
27
- for (let index = 0; index < keys.length; index++) {
28
- const parentEl = document.createElement('div');
29
- ref.current.append(parentEl);
30
- const item$ = computed((get) => get(items$)[index]);
31
- const root = renderListEl(parentEl, index, item$);
32
- elsRef.current.push({ el: parentEl, item$, root, id: keys[index] });
33
- }
34
- // eslint-disable-next-line react-hooks/exhaustive-deps
35
- }, []);
36
- React.useEffect(() => () => keys$.destroy(), [keys$]);
37
- React.useEffect(() => {
38
- // const keys = keys$.run()
39
- return keys$.subscribe((keys) => {
40
- const prevKeys = elsRef.current.map((el) => el.id);
41
- let arrayIsEqual = true;
42
- for (let i = 0; i < keys.length; i++) {
43
- if (keys[i] !== prevKeys[i]) {
44
- arrayIsEqual = false;
45
- break;
46
- }
47
- }
48
- if (arrayIsEqual)
49
- return;
50
- const previousAgg = FI.aggregateMake(prevKeys, FI.fractionalIndexImplNumber);
51
- const { newEvents } = FI.getNewEvents(previousAgg, keys, FI.fractionalIndexImplNumber);
52
- console.log('newEvents', newEvents);
53
- for (const event of newEvents) {
54
- switch (event.op) {
55
- case 'remove': {
56
- const { index } = event;
57
- const el = elsRef.current[index];
58
- el.root.unmount();
59
- el.el.remove();
60
- el.item$.destroy();
61
- elsRef.current.splice(index, 1);
62
- break;
63
- }
64
- case 'add': {
65
- const { index } = event;
66
- const parentEl = document.createElement('div');
67
- ref.current.append(parentEl);
68
- const item$ = computed((get) => get(items$)[index]);
69
- const root = renderListEl(parentEl, index, item$);
70
- elsRef.current.splice(index, 0, { el: parentEl, item$, root, id: keys[index] });
71
- break;
72
- }
73
- case 'move': {
74
- // const { newIndex, previousIndex } = event
75
- // const el = elsRef.current[previousIndex]!
76
- // const item$ = el.item$
77
- // const root = el.root
78
- // const elEl = el.el
79
- // elsRef.current.splice(previousIndex, 1)
80
- // elsRef.current.splice(newIndex, 0, { el: elEl, item$, root })
81
- // ref.current!.insertBefore(elEl, elsRef.current[newIndex + 1]?.el)
82
- // // move dom element
83
- break;
84
- }
85
- default: {
86
- casesHandled(event);
87
- }
88
- }
89
- }
90
- });
91
- // for (let index = 0; index < keys.length; index++) {
92
- // if (prevKeys[index] === keys[index]) continue
93
- // // check if `keys[index]` === `prevKeys[index + 1]`
94
- // // which probably means that
95
- // if (keys[index] === prevKeys[index + 1]) {
96
- // // sp
97
- // }
98
- // prevKeys[index] = keys[index] as any
99
- // }
100
- // TODO in the future use a more efficient diffing algorithm that re-uses elements more optimally
101
- // right now we're only looking one step ahead
102
- // reconcile until `keys` and `prevKeys` are equal
103
- // prevKeys = keys
104
- }, [items$, keys$, renderListEl]);
105
- return React.createElement(React.Fragment, null, container);
8
+ const keysCb = React.useCallback(() => computed((get) => get(items$).map(getKey)), [getKey, items$]);
9
+ const keys = useTemporaryQuery(keysCb);
10
+ const arr = React.useMemo(() => keys.map((key) => [key, computed((get) => get(items$).find((item) => getKey(item, 0) === key))]), [getKey, items$, keys]);
11
+ return (React.createElement(React.Fragment, null, arr.map(([key, item$], index) => (React.createElement(ItemWrapperMemo, { key: key, itemKey: key, "item$": item$, opts: { isInitialListRender: !hasMounted, index }, renderItem: renderItem })))));
106
12
  };
107
- export const DiffableList = ({ items$, renderContainer, renderItem, getKey, }) => (React.createElement(itsFine.FiberProvider, null,
108
- React.createElement(DiffableList_, { "items$": items$, renderContainer: renderContainer, renderItem: renderItem, getKey: getKey })));
109
13
  const ItemWrapper = ({ item$, opts, renderItem, }) => {
110
14
  const item = useQuery(item$);
111
15
  return React.createElement(React.Fragment, null, renderItem(item, opts));
112
16
  };
17
+ const ItemWrapperMemo = React.memo(ItemWrapper, (prev, next) => prev.itemKey === next.itemKey &&
18
+ prev.renderItem === prev.renderItem &&
19
+ prev.opts.index === next.opts.index &&
20
+ prev.opts.isInitialListRender === next.opts.isInitialListRender);
113
21
  //# sourceMappingURL=DiffableList.js.map