@jsb188/react 1.1.8 → 1.1.11

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.
@@ -1,12 +1,88 @@
1
- import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai'
1
+ import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai';
2
+ import type { SetStateAction } from 'react';
3
+
4
+ export type RealtimeConnectionStatus =
5
+ | 'CONNECTED'
6
+ | 'DISCONNECTED'
7
+ | 'REAUTHENTICATING'
8
+ | 'REVOKED';
9
+
10
+ /*
11
+ * Preserve terminal revocation when a recoverable transport path disconnects.
12
+ */
13
+ export function getRealtimeConnectionStatusAfterDisconnect(
14
+ status: RealtimeConnectionStatus,
15
+ ): RealtimeConnectionStatus {
16
+ return status === 'REVOKED' ? status : 'DISCONNECTED';
17
+ }
18
+
19
+ /*
20
+ * Enter optimistic reauthentication only from an authenticated-ready lifecycle.
21
+ */
22
+ export function getRealtimeConnectionStatusAfterAuthReset(
23
+ status: RealtimeConnectionStatus,
24
+ ): RealtimeConnectionStatus {
25
+ return status === 'CONNECTED' || status === 'REAUTHENTICATING'
26
+ ? 'REAUTHENTICATING'
27
+ : status;
28
+ }
29
+
30
+ /*
31
+ * Return whether the app should surface its reconnecting recovery message.
32
+ */
33
+ export function shouldShowRealtimeReconnectingMessage(
34
+ status: RealtimeConnectionStatus,
35
+ ) {
36
+ return status === 'DISCONNECTED';
37
+ }
38
+
39
+ /**
40
+ * realtimeConnectionStatusState; Track transport and authenticated readiness.
41
+ */
42
+
43
+ const realtimeConnectionStatusState =
44
+ atom<RealtimeConnectionStatus>('DISCONNECTED');
2
45
 
3
46
  /**
4
- * connectedToState; Track network connection state;
5
- * Developer should use either WebSockets, or SSE to track this,
6
- * or just keep the value set to {true} on app mount.
47
+ * connectedToServerState; Preserve the strict authenticated-ready boolean API.
7
48
  */
8
49
 
9
- const connectedToServerState = atom(false);
50
+ const connectedToServerState = atom(
51
+ (get) => get(realtimeConnectionStatusState) === 'CONNECTED',
52
+ (get, set, nextValue: SetStateAction<boolean>) => {
53
+ const connected = get(realtimeConnectionStatusState) === 'CONNECTED';
54
+ const nextConnected =
55
+ typeof nextValue === 'function' ? nextValue(connected) : nextValue;
56
+ set(
57
+ realtimeConnectionStatusState,
58
+ nextConnected ? 'CONNECTED' : 'DISCONNECTED',
59
+ );
60
+ },
61
+ );
62
+
63
+ /**
64
+ * optimisticConnectedToServerState; Keep presentation connected during auth rotation.
65
+ */
66
+
67
+ const optimisticConnectedToServerState = atom((get) => {
68
+ const status = get(realtimeConnectionStatusState);
69
+ return status === 'CONNECTED' || status === 'REAUTHENTICATING';
70
+ });
71
+
72
+ /**
73
+ * realtimeReconnectingMessageState; Narrowly observe visible recovery status.
74
+ */
75
+
76
+ const realtimeReconnectingMessageState = atom((get) =>
77
+ shouldShowRealtimeReconnectingMessage(get(realtimeConnectionStatusState)),
78
+ );
79
+
80
+ /**
81
+ * realtimeConnectionGenerationState; Monotonic identity for replacement
82
+ * realtime connections, including reconnects whose false state is batched.
83
+ */
84
+
85
+ const realtimeConnectionGenerationState = atom(0);
10
86
 
11
87
  /**
12
88
  * screenIsFocused; whether or not app is open & focused
@@ -19,7 +95,7 @@ const screenIsFocusedState = atom(true);
19
95
  */
20
96
 
21
97
  export function useSetConnectedToServer() {
22
- return useSetAtom(connectedToServerState);
98
+ return useSetAtom(connectedToServerState);
23
99
  }
24
100
 
25
101
  /**
@@ -27,7 +103,7 @@ export function useSetConnectedToServer() {
27
103
  */
28
104
 
29
105
  export function useConnectedToServerValue() {
30
- return useAtomValue(connectedToServerState);
106
+ return useAtomValue(connectedToServerState);
31
107
  }
32
108
 
33
109
  /**
@@ -35,7 +111,55 @@ export function useConnectedToServerValue() {
35
111
  */
36
112
 
37
113
  export function useConnectedToServer() {
38
- return useAtom(connectedToServerState);
114
+ return useAtom(connectedToServerState);
115
+ }
116
+
117
+ /**
118
+ * Export; get the complete realtime connection lifecycle status.
119
+ */
120
+
121
+ export function useRealtimeConnectionStatusValue() {
122
+ return useAtomValue(realtimeConnectionStatusState);
123
+ }
124
+
125
+ /**
126
+ * Export; set the complete realtime connection lifecycle status.
127
+ */
128
+
129
+ export function useSetRealtimeConnectionStatus() {
130
+ return useSetAtom(realtimeConnectionStatusState);
131
+ }
132
+
133
+ /**
134
+ * Export; get whether realtime should appear connected to the user.
135
+ */
136
+
137
+ export function useOptimisticConnectedToServerValue() {
138
+ return useAtomValue(optimisticConnectedToServerState);
139
+ }
140
+
141
+ /**
142
+ * Export; get whether the app should surface its reconnecting message.
143
+ */
144
+
145
+ export function useShowRealtimeReconnectingMessageValue() {
146
+ return useAtomValue(realtimeReconnectingMessageState);
147
+ }
148
+
149
+ /**
150
+ * Export; advance realtimeConnectionGenerationState
151
+ */
152
+
153
+ export function useSetRealtimeConnectionGeneration() {
154
+ return useSetAtom(realtimeConnectionGenerationState);
155
+ }
156
+
157
+ /**
158
+ * Export; get realtimeConnectionGenerationState value
159
+ */
160
+
161
+ export function useRealtimeConnectionGenerationValue() {
162
+ return useAtomValue(realtimeConnectionGenerationState);
39
163
  }
40
164
 
41
165
  /**
@@ -43,7 +167,7 @@ export function useConnectedToServer() {
43
167
  */
44
168
 
45
169
  export function useSetScreenIsFocused() {
46
- return useSetAtom(screenIsFocusedState);
170
+ return useSetAtom(screenIsFocusedState);
47
171
  }
48
172
 
49
173
  /**
@@ -51,7 +175,7 @@ export function useSetScreenIsFocused() {
51
175
  */
52
176
 
53
177
  export function useScreenIsFocusedValue() {
54
- return useAtomValue(screenIsFocusedState);
178
+ return useAtomValue(screenIsFocusedState);
55
179
  }
56
180
 
57
181
  /**
@@ -59,5 +183,5 @@ export function useScreenIsFocusedValue() {
59
183
  */
60
184
 
61
185
  export function useScreenIsFocused() {
62
- return useAtom(screenIsFocusedState);
186
+ return useAtom(screenIsFocusedState);
63
187
  }
package/states/index.ts CHANGED
@@ -4,5 +4,5 @@ export * from './connected';
4
4
  export * from './keyDown';
5
5
  export * from './modal';
6
6
  export * from './popOver';
7
- export * from './toasts';
7
+ export * from './reactiveEvents';
8
8
  export * from './uploads';
package/states/keyDown.ts CHANGED
@@ -6,10 +6,10 @@ import { useCallback } from 'react';
6
6
  */
7
7
 
8
8
  type KeyDownObserverObj = {
9
- metaKey: boolean;
10
- pressed: null | string;
11
- alert: boolean;
12
- modal: boolean;
9
+ metaKey: boolean;
10
+ pressed: null | string;
11
+ alert: boolean;
12
+ modal: boolean;
13
13
  };
14
14
 
15
15
  /**
@@ -17,29 +17,35 @@ type KeyDownObserverObj = {
17
17
  */
18
18
 
19
19
  const keyDownState = atom<KeyDownObserverObj>({
20
- metaKey: false,
21
- pressed: null,
22
- alert: false,
23
- modal: false,
20
+ metaKey: false,
21
+ pressed: null,
22
+ alert: false,
23
+ modal: false,
24
24
  });
25
25
 
26
26
  /**
27
27
  * Compose set key down function
28
28
  */
29
29
 
30
- function composeSetKeyDown(setter: (data: KeyDownObserverObj | ((prev: KeyDownObserverObj) => KeyDownObserverObj)) => void) {
31
- return (data: Partial<KeyDownObserverObj>) => {
32
- if (typeof data.pressed !== 'string' && data.pressed !== null) {
33
- return;
34
- }
30
+ function composeSetKeyDown(
31
+ setter: (
32
+ data:
33
+ | KeyDownObserverObj
34
+ | ((prev: KeyDownObserverObj) => KeyDownObserverObj),
35
+ ) => void,
36
+ ) {
37
+ return (data: Partial<KeyDownObserverObj>) => {
38
+ if (typeof data.pressed !== 'string' && data.pressed !== null) {
39
+ return;
40
+ }
35
41
 
36
- setter((prev: KeyDownObserverObj) => {
37
- return {
38
- ...prev,
39
- ...data,
40
- };
41
- });
42
- };
42
+ setter((prev: KeyDownObserverObj) => {
43
+ return {
44
+ ...prev,
45
+ ...data,
46
+ };
47
+ });
48
+ };
43
49
  }
44
50
 
45
51
  /**
@@ -47,20 +53,19 @@ function composeSetKeyDown(setter: (data: KeyDownObserverObj | ((prev: KeyDownOb
47
53
  */
48
54
 
49
55
  export function useSetKeyDown() {
50
- const setValue = useSetAtom(keyDownState);
51
- const setKeyDown = useCallback( composeSetKeyDown(setValue), []);
56
+ const setValue = useSetAtom(keyDownState);
57
+ const setKeyDown = useCallback(composeSetKeyDown(setValue), []);
52
58
 
53
- return setKeyDown;
59
+ return setKeyDown;
54
60
  }
55
61
 
56
62
  /**
57
63
  * Export; use key down
58
64
  */
59
65
 
60
-
61
66
  export function useKeyDown() {
62
- const [value, setValue] = useAtom(keyDownState);
63
- const setKeyDown = useCallback( composeSetKeyDown(setValue), []);
67
+ const [value, setValue] = useAtom(keyDownState);
68
+ const setKeyDown = useCallback(composeSetKeyDown(setValue), []);
64
69
 
65
- return [value, setKeyDown] as const;
70
+ return [value, setKeyDown] as const;
66
71
  }