@arcote.tech/arc-react 0.0.20 → 0.0.21
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 +39 -7
- package/dist/reactModel.d.ts +4 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -46,6 +46,8 @@ class IDBReadTransaction {
|
|
|
46
46
|
return new Promise((resolve) => {
|
|
47
47
|
const result = this.transaction.objectStore(collection.name).get(id);
|
|
48
48
|
result.onsuccess = (e) => {
|
|
49
|
+
if (!e.target.result)
|
|
50
|
+
return resolve(undefined);
|
|
49
51
|
const deserialized = collection.deserialize(e.target.result);
|
|
50
52
|
resolve(deserialized);
|
|
51
53
|
};
|
|
@@ -123,10 +125,14 @@ class IDBAdapter {
|
|
|
123
125
|
}
|
|
124
126
|
}
|
|
125
127
|
// reactModel.tsx
|
|
128
|
+
import {
|
|
129
|
+
ArcSubModel
|
|
130
|
+
} from "@arcote.tech/arc";
|
|
126
131
|
import { createContext, useContext, useEffect, useState } from "react";
|
|
127
132
|
import { jsx } from "react/jsx-runtime";
|
|
128
133
|
var reactModel = (model) => {
|
|
129
134
|
const LiveModelContext = createContext(null);
|
|
135
|
+
const LocalModelContext = createContext(null);
|
|
130
136
|
return [
|
|
131
137
|
function LiveModelProvider(props) {
|
|
132
138
|
return /* @__PURE__ */ jsx(LiveModelContext.Provider, {
|
|
@@ -136,19 +142,36 @@ var reactModel = (model) => {
|
|
|
136
142
|
children: props.children
|
|
137
143
|
}, undefined, false, undefined, this);
|
|
138
144
|
},
|
|
145
|
+
function LocalModelProvider(props) {
|
|
146
|
+
const liveModelContext = useContext(LiveModelContext);
|
|
147
|
+
if (!liveModelContext) {
|
|
148
|
+
throw new Error("LocalModelProvider must be used within a LiveModelProvider");
|
|
149
|
+
}
|
|
150
|
+
const [subModel] = useState(() => new ArcSubModel(liveModelContext.model));
|
|
151
|
+
return /* @__PURE__ */ jsx(LocalModelContext.Provider, {
|
|
152
|
+
value: {
|
|
153
|
+
model: subModel
|
|
154
|
+
},
|
|
155
|
+
children: props.children
|
|
156
|
+
}, undefined, false, undefined, this);
|
|
157
|
+
},
|
|
139
158
|
function useQuery(queryBuilder, dependencies = []) {
|
|
140
|
-
const
|
|
159
|
+
const localContext = useContext(LocalModelContext);
|
|
160
|
+
const liveContext = useContext(LiveModelContext);
|
|
161
|
+
const context = localContext || liveContext;
|
|
141
162
|
if (!context)
|
|
142
|
-
throw new Error("useQuery must be used within a LiveModelProvider");
|
|
143
|
-
if (!context.model)
|
|
144
|
-
throw new Error("model not found");
|
|
163
|
+
throw new Error("useQuery must be used within a LiveModelProvider or LocalModelProvider");
|
|
145
164
|
const [result, setResult] = useState(null);
|
|
146
165
|
const [loading, setLoading] = useState(true);
|
|
147
166
|
useEffect(() => {
|
|
148
167
|
const query = context.model.query(queryBuilder);
|
|
168
|
+
if (localContext)
|
|
169
|
+
console.log("local query", query);
|
|
149
170
|
query.result$.subscribe((result2) => {
|
|
150
171
|
setResult(result2);
|
|
151
172
|
setLoading(false);
|
|
173
|
+
if (localContext)
|
|
174
|
+
console.log("local query result change", result2);
|
|
152
175
|
});
|
|
153
176
|
return () => {
|
|
154
177
|
query.unsubscribe();
|
|
@@ -157,9 +180,11 @@ var reactModel = (model) => {
|
|
|
157
180
|
return [result, loading];
|
|
158
181
|
},
|
|
159
182
|
function useCommands() {
|
|
160
|
-
const
|
|
183
|
+
const localContext = useContext(LocalModelContext);
|
|
184
|
+
const liveContext = useContext(LiveModelContext);
|
|
185
|
+
const context = localContext || liveContext;
|
|
161
186
|
if (!context)
|
|
162
|
-
throw new Error("useCommands must be used within a LiveModelProvider");
|
|
187
|
+
throw new Error("useCommands must be used within a LiveModelProvider or LocalModelProvider");
|
|
163
188
|
return context.model.commands();
|
|
164
189
|
},
|
|
165
190
|
function query(queryBuilder) {
|
|
@@ -175,6 +200,13 @@ var reactModel = (model) => {
|
|
|
175
200
|
}
|
|
176
201
|
});
|
|
177
202
|
});
|
|
203
|
+
},
|
|
204
|
+
function useLocalModel() {
|
|
205
|
+
const localContext = useContext(LocalModelContext);
|
|
206
|
+
if (!localContext) {
|
|
207
|
+
throw new Error("useLocalModel must be used within a LocalModelProvider");
|
|
208
|
+
}
|
|
209
|
+
return localContext.model;
|
|
178
210
|
}
|
|
179
211
|
];
|
|
180
212
|
};
|
|
@@ -184,4 +216,4 @@ export {
|
|
|
184
216
|
formResolver
|
|
185
217
|
};
|
|
186
218
|
|
|
187
|
-
//# debugId=
|
|
219
|
+
//# debugId=9C5FFF223A0A726064756E2164756E21
|
package/dist/reactModel.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { type ArcModelAny, type GetAnyCollectionQueryResult, type QueryFactoryFunction } from "@arcote.tech/arc";
|
|
1
|
+
import { type ArcModelAny, type GetAnyCollectionQueryResult, type QueryFactoryFunction, ArcSubModel } from "@arcote.tech/arc";
|
|
2
2
|
export declare const reactModel: <Model extends ArcModelAny>(model: Model) => readonly [(props: {
|
|
3
3
|
children: React.ReactNode;
|
|
4
|
-
}) => import("react/jsx-dev-runtime").JSX.Element,
|
|
4
|
+
}) => import("react/jsx-dev-runtime").JSX.Element, (props: {
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
}) => import("react/jsx-dev-runtime").JSX.Element, <QueryBuilderFn extends QueryFactoryFunction<Model["context"]>>(queryBuilder: QueryBuilderFn, dependencies?: any[]) => [GetAnyCollectionQueryResult<QueryBuilderFn>, false] | [null, true], () => ReturnType<Model["commands"]>, <QueryBuilderFn extends QueryFactoryFunction<Model["context"]>>(queryBuilder: QueryBuilderFn) => Promise<GetAnyCollectionQueryResult<QueryBuilderFn> | null>, () => ArcSubModel<Model["context"]>];
|
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.
|
|
7
|
+
"version": "0.0.21",
|
|
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.",
|