@topgunbuild/react 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,97 @@
1
+ Business Source License 1.1
2
+
3
+ Parameters
4
+
5
+ Licensor: TopGun Contributors
6
+ Licensed Work: TopGun
7
+ The Licensed Work is (c) 2024 TopGun Contributors.
8
+ Additional Use Grant: You may make production use of the Licensed Work,
9
+ provided Your use does not include offering the
10
+ Licensed Work to third parties as a commercial
11
+ managed database service, database-as-a-service,
12
+ or similar hosted database offering that competes
13
+ with TopGun products or services.
14
+
15
+ For purposes of this license:
16
+ - "Managed database service" means a service that
17
+ allows third parties to create, manage, or operate
18
+ databases using TopGun as the underlying technology.
19
+ - Internal use within your organization is permitted.
20
+ - Using TopGun as part of your application's backend
21
+ (not exposed as a database service) is permitted.
22
+ - Consulting and professional services around TopGun
23
+ are permitted.
24
+
25
+ Change Date: Four years from the date of each version release
26
+ Change License: Apache License, Version 2.0
27
+
28
+ For information about alternative licensing arrangements for the Licensed Work,
29
+ please contact the Licensor.
30
+
31
+ Notice
32
+
33
+ Business Source License 1.1
34
+
35
+ Terms
36
+
37
+ The Licensor hereby grants you the right to copy, modify, create derivative
38
+ works, redistribute, and make non-production use of the Licensed Work. The
39
+ Licensor may make an Additional Use Grant, above, permitting limited production use.
40
+
41
+ Effective on the Change Date, or the fourth anniversary of the first publicly
42
+ available distribution of a specific version of the Licensed Work under this
43
+ License, whichever comes first, the Licensor hereby grants you rights under
44
+ the terms of the Change License, and the rights granted in the paragraph
45
+ above terminate.
46
+
47
+ If your use of the Licensed Work does not comply with the requirements
48
+ currently in effect as described in this License, you must purchase a
49
+ commercial license from the Licensor, its affiliated entities, or authorized
50
+ resellers, or you must refrain from using the Licensed Work.
51
+
52
+ All copies of the original and modified Licensed Work, and derivative works
53
+ of the Licensed Work, are subject to this License. This License applies
54
+ separately for each version of the Licensed Work and the Change Date may vary
55
+ for each version of the Licensed Work released by Licensor.
56
+
57
+ You must conspicuously display this License on each original or modified copy
58
+ of the Licensed Work. If you receive the Licensed Work in original or
59
+ modified form from a third party, the terms and conditions set forth in this
60
+ License apply to your use of that work.
61
+
62
+ Any use of the Licensed Work in violation of this License will automatically
63
+ terminate your rights under this License for the current and all other
64
+ versions of the Licensed Work.
65
+
66
+ This License does not grant you any right in any trademark or logo of
67
+ Licensor or its affiliates (provided that you may use a trademark or logo of
68
+ Licensor as expressly required by this License).
69
+
70
+ TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
71
+ AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
72
+ EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
73
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
74
+ TITLE.
75
+
76
+ MariaDB hereby grants you permission to use this License's text to license
77
+ your works, and to refer to it using the trademark "Business Source License",
78
+ as long as you comply with the Covenants of Licensor below.
79
+
80
+ Covenants of Licensor
81
+
82
+ In consideration of the right to use this License's text and the "Business
83
+ Source License" name and trademark, Licensor covenants to MariaDB, and to all
84
+ other recipients of the licensed work to be provided by Licensor:
85
+
86
+ 1. To specify as the Change License the GPL Version 2.0 or any later version,
87
+ or a license that is compatible with GPL Version 2.0 or a later version,
88
+ where "compatible" means that software provided under the Change License can
89
+ be included in a program with software provided under GPL Version 2.0 or a
90
+ later version. Licensor may specify additional Change Licenses without
91
+ limitation.
92
+
93
+ 2. To either: (a) specify an Additional Use Grant (above) that does not impose
94
+ any additional restriction on the right granted in this License, as the
95
+ Additional Use Grant; or (b) insert the text "None" to specify a Change Date.
96
+
97
+ 3. Not to modify this License in any other way.
@@ -0,0 +1,34 @@
1
+ import React, { ReactNode } from 'react';
2
+ import * as _topgunbuild_client from '@topgunbuild/client';
3
+ import { TopGunClient, QueryResultItem, QueryFilter, TopicCallback } from '@topgunbuild/client';
4
+ import { LWWMap, ORMap } from '@topgunbuild/core';
5
+
6
+ interface TopGunProviderProps {
7
+ client: TopGunClient;
8
+ children: ReactNode;
9
+ }
10
+ declare const TopGunProvider: React.FC<TopGunProviderProps>;
11
+ declare function useClient(): TopGunClient;
12
+
13
+ interface UseQueryResult<T> {
14
+ data: QueryResultItem<T>[];
15
+ loading: boolean;
16
+ error: Error | null;
17
+ }
18
+ declare function useQuery<T = any>(mapName: string, query?: QueryFilter): UseQueryResult<T>;
19
+
20
+ interface UseMutationResult<T, K = string> {
21
+ create: (key: K, value: T) => void;
22
+ update: (key: K, value: T) => void;
23
+ remove: (key: K) => void;
24
+ map: any;
25
+ }
26
+ declare function useMutation<T = any, K = string>(mapName: string): UseMutationResult<T, K>;
27
+
28
+ declare function useMap<K = string, V = any>(mapName: string): LWWMap<K, V>;
29
+
30
+ declare function useORMap<K = string, V = any>(mapName: string): ORMap<K, V>;
31
+
32
+ declare function useTopic(topicName: string, callback?: TopicCallback): _topgunbuild_client.TopicHandle;
33
+
34
+ export { TopGunProvider, type TopGunProviderProps, type UseMutationResult, type UseQueryResult, useClient, useMap, useMutation, useORMap, useQuery, useTopic };
@@ -0,0 +1,34 @@
1
+ import React, { ReactNode } from 'react';
2
+ import * as _topgunbuild_client from '@topgunbuild/client';
3
+ import { TopGunClient, QueryResultItem, QueryFilter, TopicCallback } from '@topgunbuild/client';
4
+ import { LWWMap, ORMap } from '@topgunbuild/core';
5
+
6
+ interface TopGunProviderProps {
7
+ client: TopGunClient;
8
+ children: ReactNode;
9
+ }
10
+ declare const TopGunProvider: React.FC<TopGunProviderProps>;
11
+ declare function useClient(): TopGunClient;
12
+
13
+ interface UseQueryResult<T> {
14
+ data: QueryResultItem<T>[];
15
+ loading: boolean;
16
+ error: Error | null;
17
+ }
18
+ declare function useQuery<T = any>(mapName: string, query?: QueryFilter): UseQueryResult<T>;
19
+
20
+ interface UseMutationResult<T, K = string> {
21
+ create: (key: K, value: T) => void;
22
+ update: (key: K, value: T) => void;
23
+ remove: (key: K) => void;
24
+ map: any;
25
+ }
26
+ declare function useMutation<T = any, K = string>(mapName: string): UseMutationResult<T, K>;
27
+
28
+ declare function useMap<K = string, V = any>(mapName: string): LWWMap<K, V>;
29
+
30
+ declare function useORMap<K = string, V = any>(mapName: string): ORMap<K, V>;
31
+
32
+ declare function useTopic(topicName: string, callback?: TopicCallback): _topgunbuild_client.TopicHandle;
33
+
34
+ export { TopGunProvider, type TopGunProviderProps, type UseMutationResult, type UseQueryResult, useClient, useMap, useMutation, useORMap, useQuery, useTopic };
package/dist/index.js ADDED
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ TopGunProvider: () => TopGunProvider,
24
+ useClient: () => useClient,
25
+ useMap: () => useMap,
26
+ useMutation: () => useMutation,
27
+ useORMap: () => useORMap,
28
+ useQuery: () => useQuery,
29
+ useTopic: () => useTopic
30
+ });
31
+ module.exports = __toCommonJS(index_exports);
32
+
33
+ // src/TopGunProvider.tsx
34
+ var import_react = require("react");
35
+ var import_jsx_runtime = require("react/jsx-runtime");
36
+ var TopGunContext = (0, import_react.createContext)(null);
37
+ var TopGunProvider = ({ client, children }) => {
38
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TopGunContext.Provider, { value: client, children });
39
+ };
40
+ function useClient() {
41
+ const client = (0, import_react.useContext)(TopGunContext);
42
+ if (!client) {
43
+ throw new Error("useClient must be used within a TopGunProvider");
44
+ }
45
+ return client;
46
+ }
47
+
48
+ // src/hooks/useQuery.ts
49
+ var import_react2 = require("react");
50
+ function useQuery(mapName, query = {}) {
51
+ const client = useClient();
52
+ const [data, setData] = (0, import_react2.useState)([]);
53
+ const [loading, setLoading] = (0, import_react2.useState)(true);
54
+ const [error, setError] = (0, import_react2.useState)(null);
55
+ const isMounted = (0, import_react2.useRef)(true);
56
+ const queryJson = JSON.stringify(query);
57
+ (0, import_react2.useEffect)(() => {
58
+ isMounted.current = true;
59
+ setLoading(true);
60
+ try {
61
+ const handle = client.query(mapName, query);
62
+ const unsubscribe = handle.subscribe((results) => {
63
+ if (isMounted.current) {
64
+ setData(results);
65
+ setLoading(false);
66
+ }
67
+ });
68
+ return () => {
69
+ isMounted.current = false;
70
+ unsubscribe();
71
+ };
72
+ } catch (err) {
73
+ if (isMounted.current) {
74
+ setError(err instanceof Error ? err : new Error(String(err)));
75
+ setLoading(false);
76
+ }
77
+ return () => {
78
+ isMounted.current = false;
79
+ };
80
+ }
81
+ }, [client, mapName, queryJson]);
82
+ return { data, loading, error };
83
+ }
84
+
85
+ // src/hooks/useMutation.ts
86
+ var import_react3 = require("react");
87
+ function useMutation(mapName) {
88
+ const client = useClient();
89
+ const map = client.getMap(mapName);
90
+ const create = (0, import_react3.useCallback)((key, value) => {
91
+ map.set(key, value);
92
+ }, [map]);
93
+ const update = (0, import_react3.useCallback)((key, value) => {
94
+ map.set(key, value);
95
+ }, [map]);
96
+ const remove = (0, import_react3.useCallback)((key) => {
97
+ map.remove(key);
98
+ }, [map]);
99
+ return { create, update, remove, map };
100
+ }
101
+
102
+ // src/hooks/useMap.ts
103
+ var import_react4 = require("react");
104
+ function useMap(mapName) {
105
+ const client = useClient();
106
+ const map = client.getMap(mapName);
107
+ const [, setTick] = (0, import_react4.useState)(0);
108
+ const isMounted = (0, import_react4.useRef)(true);
109
+ (0, import_react4.useEffect)(() => {
110
+ isMounted.current = true;
111
+ const unsubscribe = map.onChange(() => {
112
+ if (isMounted.current) {
113
+ setTick((t) => t + 1);
114
+ }
115
+ });
116
+ return () => {
117
+ isMounted.current = false;
118
+ unsubscribe();
119
+ };
120
+ }, [map]);
121
+ return map;
122
+ }
123
+
124
+ // src/hooks/useORMap.ts
125
+ var import_react5 = require("react");
126
+ function useORMap(mapName) {
127
+ const client = useClient();
128
+ const map = client.getORMap(mapName);
129
+ const [, setTick] = (0, import_react5.useState)(0);
130
+ const isMounted = (0, import_react5.useRef)(true);
131
+ (0, import_react5.useEffect)(() => {
132
+ isMounted.current = true;
133
+ const unsubscribe = map.onChange(() => {
134
+ if (isMounted.current) {
135
+ setTick((t) => t + 1);
136
+ }
137
+ });
138
+ return () => {
139
+ isMounted.current = false;
140
+ unsubscribe();
141
+ };
142
+ }, [map]);
143
+ return map;
144
+ }
145
+
146
+ // src/hooks/useTopic.ts
147
+ var import_react6 = require("react");
148
+ function useTopic(topicName, callback) {
149
+ const client = useClient();
150
+ const topic = client.topic(topicName);
151
+ const isMounted = (0, import_react6.useRef)(true);
152
+ const callbackRef = (0, import_react6.useRef)(callback);
153
+ (0, import_react6.useEffect)(() => {
154
+ callbackRef.current = callback;
155
+ }, [callback]);
156
+ (0, import_react6.useEffect)(() => {
157
+ isMounted.current = true;
158
+ if (!callback) return;
159
+ const unsubscribe = topic.subscribe((data, context) => {
160
+ if (isMounted.current && callbackRef.current) {
161
+ callbackRef.current(data, context);
162
+ }
163
+ });
164
+ return () => {
165
+ isMounted.current = false;
166
+ unsubscribe();
167
+ };
168
+ }, [topic, callback]);
169
+ return topic;
170
+ }
171
+ // Annotate the CommonJS export names for ESM import in node:
172
+ 0 && (module.exports = {
173
+ TopGunProvider,
174
+ useClient,
175
+ useMap,
176
+ useMutation,
177
+ useORMap,
178
+ useQuery,
179
+ useTopic
180
+ });
181
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/TopGunProvider.tsx","../src/hooks/useQuery.ts","../src/hooks/useMutation.ts","../src/hooks/useMap.ts","../src/hooks/useORMap.ts","../src/hooks/useTopic.ts"],"sourcesContent":["export * from './TopGunProvider';\nexport * from './hooks/useClient';\nexport * from './hooks/useQuery';\nexport * from './hooks/useMutation';\nexport * from './hooks/useMap';\nexport * from './hooks/useORMap';\nexport * from './hooks/useTopic';\n\n","import React, { createContext, useContext, ReactNode } from 'react';\nimport { TopGunClient } from '@topgunbuild/client';\n\nconst TopGunContext = createContext<TopGunClient | null>(null);\n\nexport interface TopGunProviderProps {\n client: TopGunClient;\n children: ReactNode;\n}\n\nexport const TopGunProvider: React.FC<TopGunProviderProps> = ({ client, children }) => {\n return (\n <TopGunContext.Provider value={client}>\n {children}\n </TopGunContext.Provider>\n );\n};\n\nexport function useClient(): TopGunClient {\n const client = useContext(TopGunContext);\n if (!client) {\n throw new Error('useClient must be used within a TopGunProvider');\n }\n return client;\n}\n\n","import { useState, useEffect, useRef } from 'react';\nimport { QueryFilter, QueryResultItem } from '@topgunbuild/client';\nimport { useClient } from './useClient';\n\nexport interface UseQueryResult<T> {\n data: QueryResultItem<T>[];\n loading: boolean;\n error: Error | null;\n}\n\nexport function useQuery<T = any>(mapName: string, query: QueryFilter = {}): UseQueryResult<T> {\n const client = useClient();\n const [data, setData] = useState<QueryResultItem<T>[]>([]);\n const [loading, setLoading] = useState(true);\n const [error, setError] = useState<Error | null>(null);\n\n // Use a ref to track if the component is mounted to avoid state updates on unmounted components\n const isMounted = useRef(true);\n\n // We serialize the query to use it as a stable dependency for the effect\n const queryJson = JSON.stringify(query);\n\n useEffect(() => {\n isMounted.current = true;\n setLoading(true);\n \n try {\n const handle = client.query<T>(mapName, query);\n\n const unsubscribe = handle.subscribe((results) => {\n if (isMounted.current) {\n setData(results);\n setLoading(false);\n }\n });\n\n return () => {\n isMounted.current = false;\n unsubscribe();\n };\n } catch (err) {\n if (isMounted.current) {\n setError(err instanceof Error ? err : new Error(String(err)));\n setLoading(false);\n }\n return () => {\n isMounted.current = false;\n };\n }\n }, [client, mapName, queryJson]);\n\n return { data, loading, error };\n}\n\n","import { useCallback } from 'react';\nimport { useClient } from './useClient';\n\nexport interface UseMutationResult<T, K = string> {\n create: (key: K, value: T) => void;\n update: (key: K, value: T) => void;\n remove: (key: K) => void;\n map: any; // Expose map instance if needed\n}\n\nexport function useMutation<T = any, K = string>(mapName: string): UseMutationResult<T, K> {\n const client = useClient();\n // We get the map instance. Note: getMap is synchronous but might trigger async restore.\n // LWWMap is the default assumption for simple mutations.\n const map = client.getMap<K, T>(mapName);\n\n const create = useCallback((key: K, value: T) => {\n map.set(key, value);\n }, [map]);\n\n const update = useCallback((key: K, value: T) => {\n map.set(key, value);\n }, [map]);\n\n const remove = useCallback((key: K) => {\n map.remove(key);\n }, [map]);\n\n return { create, update, remove, map };\n}\n\n","import { useState, useEffect, useRef } from 'react';\nimport { LWWMap } from '@topgunbuild/core';\nimport { useClient } from './useClient';\n\nexport function useMap<K = string, V = any>(mapName: string): LWWMap<K, V> {\n const client = useClient();\n // Get the map instance. This is stable for the same mapName.\n const map = client.getMap<K, V>(mapName);\n\n // We use a dummy state to trigger re-renders when the map changes\n const [, setTick] = useState(0);\n const isMounted = useRef(true);\n\n useEffect(() => {\n isMounted.current = true;\n\n // Subscribe to map changes\n const unsubscribe = map.onChange(() => {\n if (isMounted.current) {\n setTick(t => t + 1);\n }\n });\n\n return () => {\n isMounted.current = false;\n unsubscribe();\n };\n }, [map]);\n\n return map;\n}\n","import { useState, useEffect, useRef } from 'react';\nimport { ORMap } from '@topgunbuild/core';\nimport { useClient } from './useClient';\n\nexport function useORMap<K = string, V = any>(mapName: string): ORMap<K, V> {\n const client = useClient();\n const map = client.getORMap<K, V>(mapName);\n\n const [, setTick] = useState(0);\n const isMounted = useRef(true);\n\n useEffect(() => {\n isMounted.current = true;\n\n const unsubscribe = map.onChange(() => {\n if (isMounted.current) {\n setTick(t => t + 1);\n }\n });\n\n return () => {\n isMounted.current = false;\n unsubscribe();\n };\n }, [map]);\n\n return map;\n}\n","import { useEffect, useRef } from 'react';\nimport { TopicCallback } from '@topgunbuild/client';\nimport { useClient } from './useClient';\n\nexport function useTopic(topicName: string, callback?: TopicCallback) {\n const client = useClient();\n const topic = client.topic(topicName);\n const isMounted = useRef(true);\n\n // Keep callback ref stable to avoid re-subscribing if callback function identity changes\n const callbackRef = useRef(callback);\n useEffect(() => {\n callbackRef.current = callback;\n }, [callback]);\n\n useEffect(() => {\n isMounted.current = true;\n\n if (!callback) return;\n\n const unsubscribe = topic.subscribe((data, context) => {\n if (isMounted.current && callbackRef.current) {\n callbackRef.current(data, context);\n }\n });\n\n return () => {\n isMounted.current = false;\n unsubscribe();\n };\n }, [topic, callback]); // Re-subscribe if topic handle changes (rare) or if callback presence toggles\n\n return topic;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA4D;AAYxD;AATJ,IAAM,oBAAgB,4BAAmC,IAAI;AAOtD,IAAM,iBAAgD,CAAC,EAAE,QAAQ,SAAS,MAAM;AACrF,SACE,4CAAC,cAAc,UAAd,EAAuB,OAAO,QAC5B,UACH;AAEJ;AAEO,SAAS,YAA0B;AACxC,QAAM,aAAS,yBAAW,aAAa;AACvC,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI,MAAM,gDAAgD;AAAA,EAClE;AACA,SAAO;AACT;;;ACxBA,IAAAA,gBAA4C;AAUrC,SAAS,SAAkB,SAAiB,QAAqB,CAAC,GAAsB;AAC7F,QAAM,SAAS,UAAU;AACzB,QAAM,CAAC,MAAM,OAAO,QAAI,wBAA+B,CAAC,CAAC;AACzD,QAAM,CAAC,SAA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AACZ,cAAU,UAAU;AAEpB,QAAI,CAAC,SAAU;AAEf,UAAM,cAAc,MAAM,UAAU,CAAC,MAAM,YAAY;AACnD,UAAI,UAAU,WAAW,YAAY,SAAS;AAC1C,oBAAY,QAAQ,MAAM,OAAO;AAAA,MACrC;AAAA,IACJ,CAAC;AAED,WAAO,MAAM;AACT,gBAAU,UAAU;AACpB,kBAAY;AAAA,IAChB;AAAA,EACJ,GAAG,CAAC,OAAO,QAAQ,CAAC;AAEpB,SAAO;AACX;","names":["import_react","import_react","import_react","import_react","import_react"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,148 @@
1
+ // src/TopGunProvider.tsx
2
+ import { createContext, useContext } from "react";
3
+ import { jsx } from "react/jsx-runtime";
4
+ var TopGunContext = createContext(null);
5
+ var TopGunProvider = ({ client, children }) => {
6
+ return /* @__PURE__ */ jsx(TopGunContext.Provider, { value: client, children });
7
+ };
8
+ function useClient() {
9
+ const client = useContext(TopGunContext);
10
+ if (!client) {
11
+ throw new Error("useClient must be used within a TopGunProvider");
12
+ }
13
+ return client;
14
+ }
15
+
16
+ // src/hooks/useQuery.ts
17
+ import { useState, useEffect, useRef } from "react";
18
+ function useQuery(mapName, query = {}) {
19
+ const client = useClient();
20
+ const [data, setData] = useState([]);
21
+ const [loading, setLoading] = useState(true);
22
+ const [error, setError] = useState(null);
23
+ const isMounted = useRef(true);
24
+ const queryJson = JSON.stringify(query);
25
+ useEffect(() => {
26
+ isMounted.current = true;
27
+ setLoading(true);
28
+ try {
29
+ const handle = client.query(mapName, query);
30
+ const unsubscribe = handle.subscribe((results) => {
31
+ if (isMounted.current) {
32
+ setData(results);
33
+ setLoading(false);
34
+ }
35
+ });
36
+ return () => {
37
+ isMounted.current = false;
38
+ unsubscribe();
39
+ };
40
+ } catch (err) {
41
+ if (isMounted.current) {
42
+ setError(err instanceof Error ? err : new Error(String(err)));
43
+ setLoading(false);
44
+ }
45
+ return () => {
46
+ isMounted.current = false;
47
+ };
48
+ }
49
+ }, [client, mapName, queryJson]);
50
+ return { data, loading, error };
51
+ }
52
+
53
+ // src/hooks/useMutation.ts
54
+ import { useCallback } from "react";
55
+ function useMutation(mapName) {
56
+ const client = useClient();
57
+ const map = client.getMap(mapName);
58
+ const create = useCallback((key, value) => {
59
+ map.set(key, value);
60
+ }, [map]);
61
+ const update = useCallback((key, value) => {
62
+ map.set(key, value);
63
+ }, [map]);
64
+ const remove = useCallback((key) => {
65
+ map.remove(key);
66
+ }, [map]);
67
+ return { create, update, remove, map };
68
+ }
69
+
70
+ // src/hooks/useMap.ts
71
+ import { useState as useState2, useEffect as useEffect2, useRef as useRef2 } from "react";
72
+ function useMap(mapName) {
73
+ const client = useClient();
74
+ const map = client.getMap(mapName);
75
+ const [, setTick] = useState2(0);
76
+ const isMounted = useRef2(true);
77
+ useEffect2(() => {
78
+ isMounted.current = true;
79
+ const unsubscribe = map.onChange(() => {
80
+ if (isMounted.current) {
81
+ setTick((t) => t + 1);
82
+ }
83
+ });
84
+ return () => {
85
+ isMounted.current = false;
86
+ unsubscribe();
87
+ };
88
+ }, [map]);
89
+ return map;
90
+ }
91
+
92
+ // src/hooks/useORMap.ts
93
+ import { useState as useState3, useEffect as useEffect3, useRef as useRef3 } from "react";
94
+ function useORMap(mapName) {
95
+ const client = useClient();
96
+ const map = client.getORMap(mapName);
97
+ const [, setTick] = useState3(0);
98
+ const isMounted = useRef3(true);
99
+ useEffect3(() => {
100
+ isMounted.current = true;
101
+ const unsubscribe = map.onChange(() => {
102
+ if (isMounted.current) {
103
+ setTick((t) => t + 1);
104
+ }
105
+ });
106
+ return () => {
107
+ isMounted.current = false;
108
+ unsubscribe();
109
+ };
110
+ }, [map]);
111
+ return map;
112
+ }
113
+
114
+ // src/hooks/useTopic.ts
115
+ import { useEffect as useEffect4, useRef as useRef4 } from "react";
116
+ function useTopic(topicName, callback) {
117
+ const client = useClient();
118
+ const topic = client.topic(topicName);
119
+ const isMounted = useRef4(true);
120
+ const callbackRef = useRef4(callback);
121
+ useEffect4(() => {
122
+ callbackRef.current = callback;
123
+ }, [callback]);
124
+ useEffect4(() => {
125
+ isMounted.current = true;
126
+ if (!callback) return;
127
+ const unsubscribe = topic.subscribe((data, context) => {
128
+ if (isMounted.current && callbackRef.current) {
129
+ callbackRef.current(data, context);
130
+ }
131
+ });
132
+ return () => {
133
+ isMounted.current = false;
134
+ unsubscribe();
135
+ };
136
+ }, [topic, callback]);
137
+ return topic;
138
+ }
139
+ export {
140
+ TopGunProvider,
141
+ useClient,
142
+ useMap,
143
+ useMutation,
144
+ useORMap,
145
+ useQuery,
146
+ useTopic
147
+ };
148
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/TopGunProvider.tsx","../src/hooks/useQuery.ts","../src/hooks/useMutation.ts","../src/hooks/useMap.ts","../src/hooks/useORMap.ts","../src/hooks/useTopic.ts"],"sourcesContent":["import React, { createContext, useContext, ReactNode } from 'react';\nimport { TopGunClient } from '@topgunbuild/client';\n\nconst TopGunContext = createContext<TopGunClient | null>(null);\n\nexport interface TopGunProviderProps {\n client: TopGunClient;\n children: ReactNode;\n}\n\nexport const TopGunProvider: React.FC<TopGunProviderProps> = ({ client, children }) => {\n return (\n <TopGunContext.Provider value={client}>\n {children}\n </TopGunContext.Provider>\n );\n};\n\nexport function useClient(): TopGunClient {\n const client = useContext(TopGunContext);\n if (!client) {\n throw new Error('useClient must be used within a TopGunProvider');\n }\n return client;\n}\n\n","import { useState, useEffect, useRef } from 'react';\nimport { QueryFilter, QueryResultItem } from '@topgunbuild/client';\nimport { useClient } from './useClient';\n\nexport interface UseQueryResult<T> {\n data: QueryResultItem<T>[];\n loading: boolean;\n error: Error | null;\n}\n\nexport function useQuery<T = any>(mapName: string, query: QueryFilter = {}): UseQueryResult<T> {\n const client = useClient();\n const [data, setData] = useState<QueryResultItem<T>[]>([]);\n const [loading, setLoading] = useState(true);\n const [error, setError] = useState<Error | null>(null);\n\n // Use a ref to track if the component is mounted to avoid state updates on unmounted components\n const isMounted = useRef(true);\n\n // We serialize the query to use it as a stable dependency for the effect\n const queryJson = JSON.stringify(query);\n\n useEffect(() => {\n isMounted.current = true;\n setLoading(true);\n \n try {\n const handle = client.query<T>(mapName, query);\n\n const unsubscribe = handle.subscribe((results) => {\n if (isMounted.current) {\n setData(results);\n setLoading(false);\n }\n });\n\n return () => {\n isMounted.current = false;\n unsubscribe();\n };\n } catch (err) {\n if (isMounted.current) {\n setError(err instanceof Error ? err : new Error(String(err)));\n setLoading(false);\n }\n return () => {\n isMounted.current = false;\n };\n }\n }, [client, mapName, queryJson]);\n\n return { data, loading, error };\n}\n\n","import { useCallback } from 'react';\nimport { useClient } from './useClient';\n\nexport interface UseMutationResult<T, K = string> {\n create: (key: K, value: T) => void;\n update: (key: K, value: T) => void;\n remove: (key: K) => void;\n map: any; // Expose map instance if needed\n}\n\nexport function useMutation<T = any, K = string>(mapName: string): UseMutationResult<T, K> {\n const client = useClient();\n // We get the map instance. Note: getMap is synchronous but might trigger async restore.\n // LWWMap is the default assumption for simple mutations.\n const map = client.getMap<K, T>(mapName);\n\n const create = useCallback((key: K, value: T) => {\n map.set(key, value);\n }, [map]);\n\n const update = useCallback((key: K, value: T) => {\n map.set(key, value);\n }, [map]);\n\n const remove = useCallback((key: K) => {\n map.remove(key);\n }, [map]);\n\n return { create, update, remove, map };\n}\n\n","import { useState, useEffect, useRef } from 'react';\nimport { LWWMap } from '@topgunbuild/core';\nimport { useClient } from './useClient';\n\nexport function useMap<K = string, V = any>(mapName: string): LWWMap<K, V> {\n const client = useClient();\n // Get the map instance. This is stable for the same mapName.\n const map = client.getMap<K, V>(mapName);\n\n // We use a dummy state to trigger re-renders when the map changes\n const [, setTick] = useState(0);\n const isMounted = useRef(true);\n\n useEffect(() => {\n isMounted.current = true;\n\n // Subscribe to map changes\n const unsubscribe = map.onChange(() => {\n if (isMounted.current) {\n setTick(t => t + 1);\n }\n });\n\n return () => {\n isMounted.current = false;\n unsubscribe();\n };\n }, [map]);\n\n return map;\n}\n","import { useState, useEffect, useRef } from 'react';\nimport { ORMap } from '@topgunbuild/core';\nimport { useClient } from './useClient';\n\nexport function useORMap<K = string, V = any>(mapName: string): ORMap<K, V> {\n const client = useClient();\n const map = client.getORMap<K, V>(mapName);\n\n const [, setTick] = useState(0);\n const isMounted = useRef(true);\n\n useEffect(() => {\n isMounted.current = true;\n\n const unsubscribe = map.onChange(() => {\n if (isMounted.current) {\n setTick(t => t + 1);\n }\n });\n\n return () => {\n isMounted.current = false;\n unsubscribe();\n };\n }, [map]);\n\n return map;\n}\n","import { useEffect, useRef } from 'react';\nimport { TopicCallback } from '@topgunbuild/client';\nimport { useClient } from './useClient';\n\nexport function useTopic(topicName: string, callback?: TopicCallback) {\n const client = useClient();\n const topic = client.topic(topicName);\n const isMounted = useRef(true);\n\n // Keep callback ref stable to avoid re-subscribing if callback function identity changes\n const callbackRef = useRef(callback);\n useEffect(() => {\n callbackRef.current = callback;\n }, [callback]);\n\n useEffect(() => {\n isMounted.current = true;\n\n if (!callback) return;\n\n const unsubscribe = topic.subscribe((data, context) => {\n if (isMounted.current && callbackRef.current) {\n callbackRef.current(data, context);\n }\n });\n\n return () => {\n isMounted.current = false;\n unsubscribe();\n };\n }, [topic, callback]); // Re-subscribe if topic handle changes (rare) or if callback presence toggles\n\n return topic;\n}\n"],"mappings":";AAAA,SAAgB,eAAe,kBAA6B;AAYxD;AATJ,IAAM,gBAAgB,cAAmC,IAAI;AAOtD,IAAM,iBAAgD,CAAC,EAAE,QAAQ,SAAS,MAAM;AACrF,SACE,oBAAC,cAAc,UAAd,EAAuB,OAAO,QAC5B,UACH;AAEJ;AAEO,SAAS,YAA0B;AACxC,QAAM,SAAS,WAAW,aAAa;AACvC,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI,MAAM,gDAAgD;AAAA,EAClE;AACA,SAAO;AACT;;;ACxBA,SAAS,UAAU,WAAW,cAAc;AAUrC,SAAS,SAAkB,SAAiB,QAAqB,CAAC,GAAsB;AAC7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AM,SAAS;AACpC,QAAM,YAAYC,QAAO,IAAI;AAG7B,QAAM,cAAcA,QAAO,QAAQ;AACnC,EAAAC,WAAU,MAAM;AACZ,gBAAY,UAAU;AAAA,EAC1B,GAAG,CAAC,QAAQ,CAAC;AAEb,EAAAA,WAAU,MAAM;AACZ,cAAU,UAAU;AAEpB,QAAI,CAAC,SAAU;AAEf,UAAM,cAAc,MAAM,UAAU,CAAC,MAAM,YAAY;AACnD,UAAI,UAAU,WAAW,YAAY,SAAS;AAC1C,oBAAY,QAAQ,MAAM,OAAO;AAAA,MACrC;AAAA,IACJ,CAAC;AAED,WAAO,MAAM;AACT,gBAAU,UAAU;AACpB,kBAAY;AAAA,IAChB;AAAA,EACJ,GAAG,CAAC,OAAO,QAAQ,CAAC;AAEpB,SAAO;AACX;","names":["useState","useEffect","useRef","useState","useRef","useEffect","useState","useEffect","useRef","useState","useRef","useEffect","useEffect","useRef","useRef","useEffect"]}
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@topgunbuild/react",
3
+ "version": "0.1.0",
4
+ "main": "./dist/index.js",
5
+ "module": "./dist/index.mjs",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "sideEffects": false,
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "peerDependencies": {
22
+ "react": ">=16.8.0",
23
+ "react-dom": ">=16.8.0"
24
+ },
25
+ "dependencies": {
26
+ "@topgunbuild/client": "0.1.0",
27
+ "@topgunbuild/core": "0.1.0"
28
+ },
29
+ "devDependencies": {
30
+ "@testing-library/react": "^14.0.0",
31
+ "@types/jest": "^29.5.14",
32
+ "@types/react": "^18.0.28",
33
+ "@types/react-dom": "^18.0.11",
34
+ "jest": "^29.7.0",
35
+ "jest-environment-jsdom": "^29.7.0",
36
+ "react": "^18.2.0",
37
+ "react-dom": "^18.2.0",
38
+ "react-test-renderer": "^18.2.0",
39
+ "ts-jest": "^29.1.0",
40
+ "typescript": "^5.0.0"
41
+ },
42
+ "engines": {
43
+ "node": ">=18"
44
+ },
45
+ "license": "BSL-1.1",
46
+ "scripts": {
47
+ "build": "tsup",
48
+ "test": "jest",
49
+ "test:coverage": "jest --coverage"
50
+ }
51
+ }