@trakit/react 0.0.5 → 0.0.6
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/context/connectionContext.d.ts +64 -0
- package/index.d.ts +7 -3
- package/package.json +4 -2
- package/trakit-react.min.js +2 -2
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { guid, Machine, nothing, url, User } from '@trakit/objects';
|
|
2
|
+
import { TrakitSyncCommander } from '@trakit/sync';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
/**
|
|
5
|
+
* The result of the `useConnection` hook, providing the synchronizer instance and connection state.
|
|
6
|
+
*/
|
|
7
|
+
export type ConnectionContextType = {
|
|
8
|
+
/**
|
|
9
|
+
* The synchronizer instance used for commands.
|
|
10
|
+
*/
|
|
11
|
+
readonly synchronizer: TrakitSyncCommander;
|
|
12
|
+
/**
|
|
13
|
+
* Indicates whether the connection is ready for use.
|
|
14
|
+
* Will be `true` if there is {@link User|user session} or {@link Machine|API credentials}
|
|
15
|
+
* available and the underlying WebSocket connection has been established,
|
|
16
|
+
* or if there is no authentication details given.
|
|
17
|
+
*/
|
|
18
|
+
ready: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* The session ID taken from a login command.
|
|
21
|
+
*/
|
|
22
|
+
ghostId: guid;
|
|
23
|
+
/**
|
|
24
|
+
* Indicates whether the WebSocket connection is currently online.
|
|
25
|
+
*/
|
|
26
|
+
online: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* The {@link User} information associated with the current session, if available.
|
|
29
|
+
*/
|
|
30
|
+
user: User | null;
|
|
31
|
+
/**
|
|
32
|
+
* The {@link Machine|API credentials} associated with the current session, if available.
|
|
33
|
+
*/
|
|
34
|
+
machine: Machine | null;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Provider component that wraps the application with CookiesProvider.
|
|
38
|
+
* Required for the `useConnection` hook to work properly.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```tsx
|
|
42
|
+
* import { ConnectionProvider, useConnection } from '@trakit/react';
|
|
43
|
+
*
|
|
44
|
+
* function App() {
|
|
45
|
+
* return (
|
|
46
|
+
* <ConnectionProvider>
|
|
47
|
+
* <YourComponent />
|
|
48
|
+
* </ConnectionProvider>
|
|
49
|
+
* );
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function ConnectionProvider({ children, restAddress, socketAddress }: {
|
|
54
|
+
children: ReactNode;
|
|
55
|
+
restAddress?: URL | url | nothing;
|
|
56
|
+
socketAddress?: URL | url | nothing;
|
|
57
|
+
}): import("react").JSX.Element;
|
|
58
|
+
/**
|
|
59
|
+
* A React hook that manages the connection to the Trak-iT synchronization service.
|
|
60
|
+
* It initializes the synchronizer, listens for connection and account events,
|
|
61
|
+
* and provides the current connection state and user/machine information.
|
|
62
|
+
*/
|
|
63
|
+
export declare function useConnection(): ConnectionContextType;
|
|
64
|
+
//# sourceMappingURL=connectionContext.d.ts.map
|
package/index.d.ts
CHANGED
|
@@ -4,15 +4,19 @@
|
|
|
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,
|
|
7
|
+
import { ConnectionProvider, useConnection, type ConnectionContextType } from "./context/connectionContext";
|
|
8
8
|
import useIsOnline from "./hooks/useIsOnline";
|
|
9
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.
|
|
13
|
+
export declare const version = "0.0.6";
|
|
14
14
|
/**
|
|
15
15
|
* Hooks
|
|
16
16
|
*/
|
|
17
|
-
export { useConnection, useIsOnline, useSingle, useSync, type
|
|
17
|
+
export { useConnection, useIsOnline, useSingle, useSync, type UseSyncMultiple, type UseSyncResult, type UseSyncSingle, };
|
|
18
|
+
/**
|
|
19
|
+
* Context provider for connection state and synchronizer instance.
|
|
20
|
+
*/
|
|
21
|
+
export { ConnectionProvider, type ConnectionContextType, };
|
|
18
22
|
//# 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.
|
|
4
|
+
"version": "0.0.6",
|
|
5
5
|
"main": "./trakit-react.min.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
7
7
|
"scripts": {
|
|
@@ -38,7 +38,9 @@
|
|
|
38
38
|
"@trakit/commands": "^0.1.2",
|
|
39
39
|
"@trakit/objects": "^0.1.2",
|
|
40
40
|
"@trakit/sync": "^0.1.2",
|
|
41
|
-
"@types/react": "^19.2.17"
|
|
41
|
+
"@types/react": "^19.2.17"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
42
44
|
"react": "19.2.3",
|
|
43
45
|
"react-cookie": "^8.0.1",
|
|
44
46
|
"react-dom": "19.2.3"
|
package/trakit-react.min.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
function n(n,e){function
|
|
1
|
+
function n({children:n,restAddress:t,socketAddress:r}){return l(A,{children:l(e,{restAddress:t,socketAddress:r,children:n})})}function e({children:n,restAddress:e,socketAddress:t}){function r(n){h(n.online)}function o(n){const{account:e}=n;e.user?.login&&e.expiry>new Date?(c(j,e.ghostId,{...D,expires:e.expiry}),x(e.ghostId||"")):u(j,D),C(e.user||null),e.machine?.key&&e.expiry>new Date?(c(I,e.machine.key,{...D,expires:e.expiry}),c(v,e.machine.secret,{...D,expires:e.expiry})):(u(I,D),u(v,D)),S(e.machine||null)}const i=w(new f(null,e||m.URI_PROD,t||p.URI_PROD)),[s,c,u]=b([j,I,v]),[a,h]=g(i.current.socketOnline),[y,x]=g(s[j]||""),[A,C]=g(i.current.account.user||null),[N,S]=g(i.current.account.machine||s[I]&&new d({key:s[I],secret:s[v]})||null),E=!(!y&&!N)==!(!i.current.account.user&&!i.current.account.machine);return k(()=>{i.current.on("open",r),i.current.on("error",r),i.current.on("close",r),i.current.on("account",o),(y||N)&&i.current.setAuth({ghostId:y,machine:N?.toJSON()})},[]),l(z,{value:{synchronizer:i.current,ready:E,ghostId:y,online:a,user:A,machine:N},children:n})}function t(){const n=y(z);if(!n)throw new Error("useConnection must be used within ConnectionProvider");return n}function r(n){return window.addEventListener("online",n),window.addEventListener("offline",n),()=>{window.removeEventListener("online",n),window.removeEventListener("offline",n)}}function o(){return navigator.onLine}function i(){return!0}function s(){return x(r,o,i)}function c(n,e){const r=w(null),o=w(0),[i,s]=g(null),[c,u]=g({}),{synchronizer:l,ready:d,online:f,user:m,machine:h}=t();return k(()=>{function t(t,r){0!==o.current&&e===r&&n.includes(t)&&u(n=>({...n,[t]:[...a[t].values().filter(n=>n.companyId===e)]}))}function c(n){const{kind:e,companyId:r}=n;t(e,r)}function f(n){const{name:e,body:r}=n;t(p.msgNameToSyncName(e),r.company)}function m(){if(0===o.current){const n=i?.map(n=>n.syncName).filter(n=>!!n);n?.length&&l.desync(e,n),l.off("list",c),l.off("update",c),l.off("delete",c),l.off("message",f)}r.current=null}function h(r){s(r),n.forEach(n=>t(n,e))}if(d&&n?.length&&!isNaN(e))return o.current++,l.on("list",c),l.on("update",c),l.on("delete",c),l.on("message",f),r.current=l.sync(e,n),r.current.then(h,h),r.current.finally(m),()=>{o.current--,r.current||m()}},[e=e??m?.companyId??h?.companyId,n.sort().join(",")]),{loading:!(d&&f&&Object.keys(c).length&&!r.current),replies:i??[],...c}}function u(n,e){const{loading:t,replies:r,[n]:o}=c([n],e);return{loading:t,replies:r,objects:o??[]}}
|
|
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
|
|
7
|
+
*/import{jsx as l}from"react/jsx-runtime";import{Machine as d,storage as a}from"@trakit/objects";import{TrakitSyncCommander as f,TrakitRestfulCommander as m,TrakitSocketCommander as p}from"@trakit/sync";import{createContext as h,useRef as w,useState as g,useEffect as k,useContext as y,useSyncExternalStore as x}from"react";import{CookiesProvider as A,useCookies as b}from"react-cookie";const j="ghostId",I="aK",v="aS",D={path:"/",secure:!0,sameSite:"strict",expires:new Date(0)},z=h(null),C="0.0.6";export{n as ConnectionProvider,t as useConnection,s as useIsOnline,u as useSingle,c as useSync,C as version};
|