@trakit/react 0.0.2 → 0.0.4

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.
@@ -37,5 +37,5 @@ export type UseConnectionResult = {
37
37
  * It initializes the synchronizer, listens for connection and account events,
38
38
  * and provides the current connection state and user/machine information.
39
39
  */
40
- export default function useConnection(restAddress?: URL | url | nothing, socketAddress?: URL | url | nothing): UseConnectionResult;
40
+ export declare function useConnection(restAddress?: URL | url | nothing, socketAddress?: URL | url | nothing): UseConnectionResult;
41
41
  //# sourceMappingURL=useConnection.d.ts.map
@@ -1,31 +1,54 @@
1
1
  import { Reply } from '@trakit/commands';
2
2
  import { IBelongCompany, IRequestable, nothing, SyncName, ulong } from '@trakit/objects';
3
3
  /**
4
- * The result of the `useSync` hook.
4
+ * The result of the `useSync` hook and `useSingle` hook.
5
5
  */
6
- export type UseSyncResult<T> = {
6
+ export interface UseSyncResult {
7
7
  /**
8
- * Whether the hook is currently loading data. This will be `true` until the initial synchronization is complete, and may briefly become `true` again if the connection is lost and re-established.
8
+ * Whether the hook is currently loading data. This will be `true` until the initial synchronization
9
+ * is complete, and may briefly become `true` again if the connection is lost and re-established.
9
10
  */
10
- ready: boolean;
11
+ loading: boolean;
11
12
  /**
12
13
  * The list of replies received from the synchronization process.
13
14
  * This is normally not needed, but can be useful for debugging or error handling,
14
15
  * as it contains the {@link Reply} classes with {@link ErrorCode} and {@link ErrorDetail}.
15
16
  */
16
- replies: Reply[];
17
+ replies: Reply[] | null;
18
+ }
19
+ /**
20
+ * The result of the `useSync` hook.
21
+ * This object behaves like a dictionary, where the {@link UseSyncResult} properties are combined with
22
+ * the synchronized objects of type `T` for each requested types.
23
+ */
24
+ export type UseSyncMultiple<T extends IRequestable & IBelongCompany> = UseSyncResult & {
25
+ [key in SyncName]?: T[] | nothing;
26
+ };
27
+ /**
28
+ * The result of the `useSingle` hook.
29
+ * This is a combination of the {@link UseSyncResult} and the synchronized objects of type `T`
30
+ * for the given type, which are stored in an array.
31
+ */
32
+ export type UseSyncSingle<T extends IRequestable & IBelongCompany> = UseSyncResult & {
17
33
  /**
18
- * The list of synchronized objects of type `T`.
34
+ * The list of synchronized objects of the requested type.
19
35
  */
20
- objects: T[];
36
+ objects: T[] | nothing;
21
37
  };
22
38
  /**
23
- * Subscribes to the given sync types for the current company, returning the
24
- * live list of synchronized objects and re-rendering the consumer whenever
25
- * that list changes.
39
+ * Subscribes to the given sync types for the given company, returning the live list of synchronized
40
+ * objects and re-rendering whenever any object, or part of that list changes.
41
+ * @param types The {@link SyncName} array to subscribe to.
42
+ * @param companyId Optional company ID to filter the synchronized objects. Default is your own company.
43
+ * @returns An object containing the loading state, the list of replies, and the list(s) of synchronized objects.
44
+ */
45
+ export declare function useSync<T extends IRequestable & IBelongCompany>(types: SyncName[], companyId?: ulong | nothing): UseSyncMultiple<T>;
46
+ /**
47
+ * Subscribes to the given sync type for the given company, returning the live list of synchronized
48
+ * objects and re-rendering whenever any object, or part of that list changes.
26
49
  * @param type The {@link SyncName} to subscribe to.
27
50
  * @param companyId Optional company ID to filter the synchronized objects. Default is your own company.
28
51
  * @returns An object containing the loading state, the list of replies, and the list of synchronized objects.
29
52
  */
30
- export default function useSync<T extends IRequestable & IBelongCompany>(type: SyncName, companyId?: ulong | nothing): UseSyncResult<T>;
53
+ export declare function useSingle<T extends IRequestable & IBelongCompany>(type: SyncName, companyId?: ulong | nothing): UseSyncSingle<T>;
31
54
  //# sourceMappingURL=useSync.d.ts.map
package/index.d.ts CHANGED
@@ -4,15 +4,15 @@
4
4
  * Last updated on Thu June 11 2026 15:43:01
5
5
  * @copyright Trak-iT Wireless Inc. 2026
6
6
  */
7
- import useConnection, { UseConnectionResult } from "./hooks/useConnection";
7
+ import { useConnection, UseConnectionResult } from "./hooks/useConnection";
8
8
  import useIsOnline from "./hooks/useIsOnline";
9
- import useSync, { UseSyncResult } from "./hooks/useSync";
9
+ import { useSingle, useSync, UseSyncMultiple, UseSyncResult, UseSyncSingle } from "./hooks/useSync";
10
10
  /**
11
11
  * Version number for this release.
12
12
  */
13
- export declare const version = "0.0.2";
13
+ export declare const version = "0.0.4";
14
14
  /**
15
15
  * Hooks
16
16
  */
17
- export { useConnection, useIsOnline, useSync, type UseConnectionResult, type UseSyncResult };
17
+ export { useConnection, useIsOnline, useSingle, useSync, type UseConnectionResult, type UseSyncMultiple, type UseSyncResult, type UseSyncSingle };
18
18
  //# sourceMappingURL=index.d.ts.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@trakit/react",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.0.4",
5
5
  "main": "./trakit-react.min.js",
6
6
  "types": "./index.d.ts",
7
7
  "scripts": {
@@ -1,7 +1,7 @@
1
- function n(n,e){function t(n){I(n.online)}function o(n){const{account:e}=n;e.user?.login&&e.expiry>new Date?(u(y,e.ghostId,{...k,expires:e.expiry}),D(e.ghostId||"")):m(y,k),z(e.user||null),e.machine?.key&&e.expiry>new Date?(u(h,e.machine.key,{...k,expires:e.expiry}),u(g,e.machine.secret,{...k,expires:e.expiry})):(m(h,k),m(g,k)),S(e.machine||null)}const i=f(new s(null,n||l.URI_PROD,e||a.URI_PROD)),[r,u,m]=w([y,h,g]),[x,I]=d(i.current.socketOnline),[b,D]=d(r[y]||""),[j,z]=d(i.current.account.user||null),[N,S]=d(i.current.account.machine||r[h]&&new c({key:r[h],secret:r[g]})||null),v=x===!(!b&&!N);return p(()=>(i.current.on("open",t),i.current.on("error",t),i.current.on("close",t),i.current.on("account",o),(b||N)&&i.current.setAuth({ghostId:b,machine:N?.toJSON()}),()=>i.current.dispose()),[]),{synchronizer:i.current,ready:v,ghostId:b,online:x,user:j,machine:N}}function e(n){return window.addEventListener("online",n),window.addEventListener("offline",n),()=>{window.removeEventListener("online",n),window.removeEventListener("offline",n)}}function t(){return navigator.onLine}function o(){return!0}function i(){return m(e,t,o)}function r(e,t){const o=f(null),i=f(0),[r,c]=d(null),[s,l]=d(null),{synchronizer:m,ready:w,online:y,user:h,machine:g}=n();return p(()=>{function n(n,o){0!==i.current&&t===o&&e===n&&l([...u[n].values().filter(n=>n.companyId===t)])}function s(e){const{kind:t,companyId:o}=e;n(t,o)}function f(e){const{name:t,body:o}=e;n(a.msgNameToSyncName(t),o.company)}function d(){if(0===i.current){const n=r?.map(n=>n.syncName).filter(n=>!!n);n?.length&&m.desync(t,n),m.off("list",s),m.off("update",s),m.off("delete",s),m.off("message",f)}o.current=null}function p(o){c(o),n(e,t)}if(w&&e&&!isNaN(t))return i.current++,m.on("list",s),m.on("update",s),m.on("delete",s),m.on("message",f),o.current=m.sync(t,[e]),o.current.then(p,p),o.current.finally(d),()=>{i.current--,o.current||d()}},[w,y,t=t??h?.companyId??g?.companyId,e]),{ready:w&&y&&!!s&&!o.current,replies:r??[],objects:s??[]}}
1
+ function n(n,e){function t(n){b(n.online)}function o(n){const{account:e}=n;e.user?.login&&e.expiry>new Date?(c(h,e.ghostId,{...x,expires:e.expiry}),j(e.ghostId||"")):u(h,x),z(e.user||null),e.machine?.key&&e.expiry>new Date?(c(y,e.machine.key,{...x,expires:e.expiry}),c(k,e.machine.secret,{...x,expires:e.expiry})):(u(y,x),u(k,x)),S(e.machine||null)}const i=d(new l(null,n||a.URI_PROD,e||f.URI_PROD)),[r,c,u]=g([h,y,k]),[w,b]=p(i.current.socketOnline),[I,j]=p(r[h]||""),[D,z]=p(i.current.account.user||null),[N,S]=p(i.current.account.machine||r[y]&&new s({key:r[y],secret:r[k]})||null),v=w===!(!I&&!N);return m(()=>(i.current.on("open",t),i.current.on("error",t),i.current.on("close",t),i.current.on("account",o),(I||N)&&i.current.setAuth({ghostId:I,machine:N?.toJSON()}),()=>i.current.dispose()),[]),{synchronizer:i.current,ready:v,ghostId:I,online:w,user:D,machine:N}}function e(n){return window.addEventListener("online",n),window.addEventListener("offline",n),()=>{window.removeEventListener("online",n),window.removeEventListener("offline",n)}}function t(){return navigator.onLine}function o(){return!0}function i(){return w(e,t,o)}function r(e,t){const o=d(null),i=d(0),[r,c]=p(null),[s,l]=p({}),{synchronizer:a,ready:w,online:g,user:h,machine:y}=n();return m(()=>{function n(n,o){0!==i.current&&t===o&&e.includes(n)&&l(e=>({...e,[n]:[...u[n].values().filter(n=>n.companyId===t)]}))}function s(e){const{kind:t,companyId:o}=e;n(t,o)}function d(e){const{name:t,body:o}=e;n(f.msgNameToSyncName(t),o.company)}function p(){if(0===i.current){const n=r?.map(n=>n.syncName).filter(n=>!!n);n?.length&&a.desync(t,n),a.off("list",s),a.off("update",s),a.off("delete",s),a.off("message",d)}o.current=null}function m(o){c(o),e.forEach(e=>n(e,t))}if(w&&e?.length&&!isNaN(t))return i.current++,a.on("list",s),a.on("update",s),a.on("delete",s),a.on("message",d),o.current=a.sync(t,e),o.current.then(m,m),o.current.finally(p),()=>{i.current--,o.current||p()}},[t=t??h?.companyId??y?.companyId,e.sort().join(",")]),{loading:!(w&&g&&Object.keys(s).length&&!o.current),replies:r??[],...s}}function c(n,e){const{loading:t,replies:o,[n]:i}=r([n],e);return{loading:t,replies:o,objects:i??[]}}
2
2
  /**
3
3
  * ReactJS library.
4
4
  * {@link https://github.com/trakitwireless/trakit-ts-react|ReactJS controls.}
5
5
  * Last updated on Thu June 11 2026 15:43:01
6
6
  * @copyright Trak-iT Wireless Inc. 2026
7
- */import{Machine as c,storage as u}from"@trakit/objects";import{TrakitSyncCommander as s,TrakitRestfulCommander as l,TrakitSocketCommander as a}from"@trakit/sync";import{useRef as f,useState as d,useEffect as p,useSyncExternalStore as m}from"react";import{useCookies as w}from"react-cookie";const y="ghostId",h="apiKey",g="apiSec",k={path:"/",secure:!0,sameSite:"strict",expires:new Date(0)},x="0.0.2";export{n as useConnection,i as useIsOnline,r as useSync,x as version};
7
+ */import{Machine as s,storage as u}from"@trakit/objects";import{TrakitSyncCommander as l,TrakitRestfulCommander as a,TrakitSocketCommander as f}from"@trakit/sync";import{useRef as d,useState as p,useEffect as m,useSyncExternalStore as w}from"react";import{useCookies as g}from"react-cookie";const h="ghostId",y="aK",k="aS",x={path:"/",secure:!0,sameSite:"strict",expires:new Date(0)},b="0.0.4";export{n as useConnection,i as useIsOnline,c as useSingle,r as useSync,b as version};