@arcote.tech/arc-react 0.0.23 → 0.0.25

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.
package/dist/index.js CHANGED
@@ -54,6 +54,10 @@ class IDBReadTransaction {
54
54
  }
55
55
  async findById(store, id) {
56
56
  return new Promise((resolve) => {
57
+ if (!id) {
58
+ resolve(undefined);
59
+ return;
60
+ }
57
61
  const result = this.transaction.objectStore(store).get(id);
58
62
  result.onsuccess = (e) => {
59
63
  resolve(e.target.result);
@@ -137,16 +141,28 @@ import {
137
141
  MasterDataStorage,
138
142
  rtcClientFactory
139
143
  } from "@arcote.tech/arc";
140
- import { createContext, useContext, useEffect, useRef, useState } from "react";
144
+ import {
145
+ createContext,
146
+ useContext,
147
+ useEffect,
148
+ useMemo,
149
+ useRef,
150
+ useState
151
+ } from "react";
141
152
  import { jsx } from "react/jsx-runtime";
153
+ "use client";
142
154
  var reactModel = (arcContext, databaseName) => {
143
155
  const LiveModelContext = createContext(null);
144
156
  const LocalModelContext = createContext(null);
145
- let dataStorage = null;
157
+ let modelMasterDataStorage = null;
146
158
  return [
147
159
  function LiveModelProvider(props) {
148
- const dbAdapterPromise = idbAdapterFactory(databaseName, arcContext.version)(arcContext);
149
- dataStorage = new MasterDataStorage(dbAdapterPromise, rtcClientFactory, arcContext);
160
+ const dataStorage = useMemo(() => {
161
+ const dbAdapterPromise = idbAdapterFactory(databaseName, arcContext.version)(arcContext);
162
+ const dataStorage2 = new MasterDataStorage(dbAdapterPromise, rtcClientFactory, arcContext);
163
+ modelMasterDataStorage = dataStorage2;
164
+ return dataStorage2;
165
+ }, [arcContext, databaseName, arcContext.version]);
150
166
  const [syncProgress, setSyncProgress] = useState([]);
151
167
  const [syncDone, setSyncDone] = useState(false);
152
168
  useEffect(() => {
@@ -159,7 +175,11 @@ var reactModel = (arcContext, databaseName) => {
159
175
  sync();
160
176
  }, []);
161
177
  return /* @__PURE__ */ jsx(LiveModelContext.Provider, {
162
- value: { dataStorage, dbAdapterPromise },
178
+ value: {
179
+ dataStorage,
180
+ client: props.client,
181
+ catchErrorCallback: props.catchErrorCallback
182
+ },
163
183
  children: syncDone ? props.children : /* @__PURE__ */ jsx(props.syncView, {
164
184
  progress: syncProgress
165
185
  }, undefined, false, undefined, this)
@@ -171,11 +191,14 @@ var reactModel = (arcContext, databaseName) => {
171
191
  throw new Error("LocalModelProvider must be used within a LiveModelProvider");
172
192
  }
173
193
  const [localContext] = useState(() => ({
174
- dbAdapterPromise: parentContext.dbAdapterPromise,
175
194
  dataStorage: parentContext.dataStorage.fork()
176
195
  }));
177
196
  return /* @__PURE__ */ jsx(LocalModelContext.Provider, {
178
- value: localContext,
197
+ value: {
198
+ dataStorage: localContext.dataStorage,
199
+ client: parentContext.client,
200
+ catchErrorCallback: parentContext.catchErrorCallback
201
+ },
179
202
  children
180
203
  }, undefined, false, undefined, this);
181
204
  },
@@ -188,6 +211,8 @@ var reactModel = (arcContext, databaseName) => {
188
211
  const [loading, setLoading] = useState(true);
189
212
  const queryRef = useRef(null);
190
213
  useEffect(() => {
214
+ if (queryRef.current)
215
+ queryRef.current.unsubscribe();
191
216
  const queryBuilder = arcContext.queryBuilder();
192
217
  const query = queryBuilderFn(queryBuilder).toQuery();
193
218
  queryRef.current = query;
@@ -212,35 +237,20 @@ var reactModel = (arcContext, databaseName) => {
212
237
  if (!context) {
213
238
  throw new Error("useQuery must be used within a ModelProvider");
214
239
  }
215
- const commands = arcContext.commands;
216
- return new Proxy({}, {
217
- get: (_, name) => {
218
- if (name in commands) {
219
- return async (...args) => {
220
- const dataStorage2 = context.dataStorage.fork();
221
- const commandContext = arcContext.commandContext(dataStorage2);
222
- const result = await commands[name](commandContext, ...args);
223
- await dataStorage2.merge();
224
- return result;
225
- };
226
- }
227
- console.warn(`Command '${name}' not found in the context.`);
228
- return;
229
- }
230
- });
240
+ return arcContext.commandsClient(context.client, context.dataStorage, context.catchErrorCallback);
231
241
  },
232
242
  async function query(queryBuilderFn) {
233
243
  const queryBuilder = arcContext.queryBuilder();
234
244
  const query = queryBuilderFn(queryBuilder).toQuery();
235
- if (!dataStorage)
245
+ if (!modelMasterDataStorage)
236
246
  throw new Error("dataStorage not found");
237
- const result = await query.run(dataStorage);
247
+ const result = await query.run(modelMasterDataStorage);
238
248
  return result;
239
249
  },
240
250
  function useLocalDataStorage() {
241
251
  const context = useContext(LocalModelContext);
242
252
  if (!context) {
243
- throw new Error("hook must be used within a ModelProvider");
253
+ return null;
244
254
  }
245
255
  return context.dataStorage;
246
256
  }
@@ -252,4 +262,4 @@ export {
252
262
  formResolver
253
263
  };
254
264
 
255
- //# debugId=4A9F9D29F4CCB1FA64756E2164756E21
265
+ //# debugId=1A90F216CD67D17164756E2164756E21
@@ -1,13 +1,15 @@
1
1
  import { ForkedDataStorage, type ArcContextAny, type CommandsClient, type QueryBuilderFunctionResult, type QueryFactoryFunction } from "@arcote.tech/arc";
2
2
  export declare const reactModel: <C extends ArcContextAny>(arcContext: C, databaseName: string) => readonly [(props: {
3
3
  children: React.ReactNode;
4
+ client: string;
4
5
  syncView: React.ComponentType<{
5
6
  progress: {
6
7
  store: string;
7
8
  size: number;
8
9
  }[];
9
10
  }>;
11
+ catchErrorCallback: (error: any) => void;
10
12
  }) => import("react/jsx-dev-runtime").JSX.Element, ({ children }: {
11
13
  children: React.ReactNode;
12
- }) => import("react/jsx-dev-runtime").JSX.Element, <QueryBuilderFn extends QueryFactoryFunction<C>>(queryBuilderFn: QueryBuilderFn, dependencies?: any[]) => [QueryBuilderFunctionResult<QueryBuilderFn>, boolean], () => CommandsClient<C["commands"]>, <QueryBuilderFn extends QueryFactoryFunction<C>>(queryBuilderFn: QueryBuilderFn) => Promise<QueryBuilderFunctionResult<QueryBuilderFn>>, () => ForkedDataStorage];
14
+ }) => import("react/jsx-dev-runtime").JSX.Element, <QueryBuilderFn extends QueryFactoryFunction<C>>(queryBuilderFn: QueryBuilderFn, dependencies?: any[]) => [QueryBuilderFunctionResult<QueryBuilderFn>, boolean], () => CommandsClient<C["commands"]>, <QueryBuilderFn extends QueryFactoryFunction<C>>(queryBuilderFn: QueryBuilderFn) => Promise<QueryBuilderFunctionResult<QueryBuilderFn>>, () => ForkedDataStorage | null];
13
15
  //# sourceMappingURL=reactModel.d.ts.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
7
- "version": "0.0.23",
7
+ "version": "0.0.25",
8
8
  "private": false,
9
9
  "author": "Przemysław Krasiński [arcote.tech]",
10
10
  "description": "React client for the Arc framework, providing utilities for querying data and executing commands, enhancing the development of reactive and efficient user interfaces.",