@trakit/react 0.0.1 → 0.0.3
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/README.md +11 -3
- package/hooks/useSync.d.ts +33 -10
- package/index.d.ts +3 -3
- package/package.json +5 -4
- package/trakit-react.min.js +2 -2
package/README.md
CHANGED
|
@@ -4,17 +4,25 @@ This library contains ReactJS controls, hooks, and contexts to assist in working
|
|
|
4
4
|
|
|
5
5
|
### Prerequisites
|
|
6
6
|
|
|
7
|
-
The `@trakit/objects` and `@trakit/
|
|
7
|
+
The `@trakit/objects`, `@trakit/commands` and `@trakit/sync` packages are required since they contain the definitions for all the commands required to manipulate the objects.
|
|
8
8
|
|
|
9
|
-
In order to build this project, you need to install the RollupJS, and plugins for TypeScript and Minifying.
|
|
9
|
+
In order to build this project, you need to install the ReactJS types, RollupJS, and plugins for TypeScript and Minifying.
|
|
10
10
|
```
|
|
11
|
-
npm i rollup rollup-plugin-typescript2 @rollup/plugin-terser
|
|
11
|
+
npm i @types/react rollup rollup-plugin-typescript2 @rollup/plugin-terser
|
|
12
12
|
```
|
|
13
13
|
After those have been installed, build the project normally.
|
|
14
14
|
```
|
|
15
15
|
rollup --config rollup.config.js
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
+
### `useConnection` hook
|
|
19
|
+
|
|
20
|
+
Used as the main hook for keeping a persistent connection.
|
|
21
|
+
|
|
22
|
+
### `useSync` hook
|
|
23
|
+
|
|
24
|
+
Used to synchronize the specified type(s) of objects with the system. Internally, this hook uses `useConnection`.
|
|
25
|
+
|
|
18
26
|
## Questions and Feedback
|
|
19
27
|
|
|
20
28
|
If you have any questions, please start for the project on GitHub
|
package/hooks/useSync.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
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
|
|
6
|
+
export interface UseSyncResult {
|
|
7
7
|
/**
|
|
8
|
-
* Whether the hook is currently loading data. This will be `true` until the initial synchronization
|
|
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
11
|
ready: boolean;
|
|
11
12
|
/**
|
|
@@ -13,19 +14,41 @@ export type UseSyncResult<T> = {
|
|
|
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
|
|
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
|
|
24
|
-
*
|
|
25
|
-
*
|
|
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
|
|
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
|
@@ -6,13 +6,13 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import useConnection, { UseConnectionResult } from "./hooks/useConnection";
|
|
8
8
|
import useIsOnline from "./hooks/useIsOnline";
|
|
9
|
-
import 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.
|
|
13
|
+
export declare const version = "0.0.3";
|
|
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.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"main": "./trakit-react.min.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
7
7
|
"scripts": {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"url": "https://github.com/trakitwireless/trakit-ts-react/issues"
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/trakitwireless/trakit-ts-react#readme",
|
|
29
|
-
"description": "
|
|
29
|
+
"description": "ReactJS controls, hooks, and contexts to assist in working with Trak-iT's APIs.",
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@rollup/plugin-terser": "^1.0.0",
|
|
32
32
|
"rollup": "^4.35.0",
|
|
@@ -37,7 +37,8 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@trakit/commands": "^0.1.2",
|
|
39
39
|
"@trakit/objects": "^0.1.2",
|
|
40
|
-
"@trakit/sync": "^0.1.
|
|
41
|
-
"@types/react": "^19.2.17"
|
|
40
|
+
"@trakit/sync": "^0.1.2",
|
|
41
|
+
"@types/react": "^19.2.17",
|
|
42
|
+
"react-cookie": "^8.1.2"
|
|
42
43
|
}
|
|
43
44
|
}
|
package/trakit-react.min.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
function n(n,
|
|
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(g,e.machine.key,{...x,expires:e.expiry}),c(k,e.machine.secret,{...x,expires:e.expiry})):(u(g,x),u(k,x)),S(e.machine||null)}const r=d(new a(null,n||l.URI_PROD,e||f.URI_PROD)),[i,c,u]=y([h,g,k]),[w,b]=p(r.current.socketOnline),[I,j]=p(i[h]||""),[D,z]=p(r.current.account.user||null),[N,S]=p(r.current.account.machine||i[g]&&new s({key:i[g],secret:i[k]})||null),v=w===!(!I&&!N);return m(()=>(r.current.on("open",t),r.current.on("error",t),r.current.on("close",t),r.current.on("account",o),(I||N)&&r.current.setAuth({ghostId:I,machine:N?.toJSON()}),()=>r.current.dispose()),[]),{synchronizer:r.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 r(){return w(e,t,o)}function i(e,t){const o=d(null),r=d(0),[i,c]=p(null),[s,a]=p({}),{synchronizer:l,ready:w,online:y,user:h,machine:g}=n();return m(()=>{function n(n,o){0!==r.current&&t===o&&e.includes(n)&&a(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===r.current){const n=i?.map(n=>n.syncName).filter(n=>!!n);n?.length&&l.desync(t,n),l.off("list",s),l.off("update",s),l.off("delete",s),l.off("message",d)}o.current=null}function m(o){c(o),e.forEach(e=>n(e,t))}if(w&&e?.length&&!isNaN(t))return r.current++,l.on("list",s),l.on("update",s),l.on("delete",s),l.on("message",d),o.current=l.sync(t,e),o.current.then(m,m),o.current.finally(p),()=>{r.current--,o.current||p()}},[t=t??h?.companyId??g?.companyId,e.sort().join(",")]),{ready:w&&y&&!!Object.keys(s).length&&!o.current,replies:i??[],...s}}function c(n,e){const{ready:t,replies:o,[n]:r}=i([n],e);return{ready:t,replies:o,objects:r??[]}}
|
|
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{TrakitSyncCommander as
|
|
7
|
+
*/import{Machine as s,storage as u}from"@trakit/objects";import{TrakitSyncCommander as a,TrakitRestfulCommander as l,TrakitSocketCommander as f}from"@trakit/sync";import{useRef as d,useState as p,useEffect as m,useSyncExternalStore as w}from"react";import{useCookies as y}from"react-cookie";const h="ghostId",g="aK",k="aS",x={path:"/",secure:!0,sameSite:"strict",expires:new Date(0)},b="0.0.3";export{n as useConnection,r as useIsOnline,c as useSingle,i as useSync,b as version};
|