react-realtime-hooks 1.0.0 → 1.0.2
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 +21 -21
- package/README.md +547 -474
- package/dist/index.cjs +56 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +56 -4
- package/dist/index.js.map +1 -1
- package/package.json +86 -86
package/dist/index.js
CHANGED
|
@@ -616,6 +616,7 @@ var useWebSocket = (options) => {
|
|
|
616
616
|
const manualOpenRef = useRef(false);
|
|
617
617
|
const skipCloseReconnectRef = useRef(false);
|
|
618
618
|
const suppressReconnectRef = useRef(false);
|
|
619
|
+
const terminalErrorRef = useRef(null);
|
|
619
620
|
const [openNonce, setOpenNonce] = useState(0);
|
|
620
621
|
const [state, setState] = useState(
|
|
621
622
|
() => createInitialState3(connect ? "connecting" : "idle")
|
|
@@ -699,7 +700,9 @@ var useWebSocket = (options) => {
|
|
|
699
700
|
});
|
|
700
701
|
const handleOpen = useEffectEvent((event, socket2) => {
|
|
701
702
|
manualCloseRef.current = false;
|
|
703
|
+
manualOpenRef.current = false;
|
|
702
704
|
suppressReconnectRef.current = false;
|
|
705
|
+
terminalErrorRef.current = null;
|
|
703
706
|
reconnect.markConnected();
|
|
704
707
|
heartbeat.start();
|
|
705
708
|
commitState((current) => ({
|
|
@@ -723,11 +726,19 @@ var useWebSocket = (options) => {
|
|
|
723
726
|
options.onMessage?.(message, event);
|
|
724
727
|
} catch {
|
|
725
728
|
const parseError = new Event("error");
|
|
729
|
+
terminalErrorRef.current = parseError;
|
|
730
|
+
manualOpenRef.current = false;
|
|
731
|
+
skipCloseReconnectRef.current = true;
|
|
732
|
+
suppressReconnectRef.current = true;
|
|
733
|
+
reconnect.cancel();
|
|
734
|
+
heartbeat.stop();
|
|
726
735
|
commitState((current) => ({
|
|
727
736
|
...current,
|
|
737
|
+
lastChangedAt: Date.now(),
|
|
728
738
|
lastError: parseError,
|
|
729
739
|
status: "error"
|
|
730
740
|
}));
|
|
741
|
+
closeSocket(1003, "parse-error");
|
|
731
742
|
}
|
|
732
743
|
});
|
|
733
744
|
const handleError = useEffectEvent((event) => {
|
|
@@ -744,8 +755,21 @@ var useWebSocket = (options) => {
|
|
|
744
755
|
socketKeyRef.current = null;
|
|
745
756
|
heartbeat.stop();
|
|
746
757
|
updateBufferedAmount();
|
|
758
|
+
const terminalError = terminalErrorRef.current;
|
|
747
759
|
const skipCloseReconnect = skipCloseReconnectRef.current;
|
|
748
760
|
skipCloseReconnectRef.current = false;
|
|
761
|
+
if (terminalError !== null) {
|
|
762
|
+
suppressReconnectRef.current = false;
|
|
763
|
+
commitState((current) => ({
|
|
764
|
+
...current,
|
|
765
|
+
lastChangedAt: Date.now(),
|
|
766
|
+
lastCloseEvent: event,
|
|
767
|
+
lastError: terminalError,
|
|
768
|
+
status: "error"
|
|
769
|
+
}));
|
|
770
|
+
options.onClose?.(event);
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
749
773
|
const shouldReconnect = !suppressReconnectRef.current && !skipCloseReconnect && reconnectEnabled && (options.shouldReconnect?.(event) ?? true);
|
|
750
774
|
commitState((current) => ({
|
|
751
775
|
...current,
|
|
@@ -764,6 +788,7 @@ var useWebSocket = (options) => {
|
|
|
764
788
|
manualCloseRef.current = false;
|
|
765
789
|
manualOpenRef.current = true;
|
|
766
790
|
suppressReconnectRef.current = false;
|
|
791
|
+
terminalErrorRef.current = null;
|
|
767
792
|
reconnect.cancel();
|
|
768
793
|
setOpenNonce((current) => current + 1);
|
|
769
794
|
};
|
|
@@ -772,6 +797,7 @@ var useWebSocket = (options) => {
|
|
|
772
797
|
manualOpenRef.current = true;
|
|
773
798
|
skipCloseReconnectRef.current = true;
|
|
774
799
|
suppressReconnectRef.current = true;
|
|
800
|
+
terminalErrorRef.current = null;
|
|
775
801
|
heartbeat.stop();
|
|
776
802
|
closeSocket();
|
|
777
803
|
suppressReconnectRef.current = false;
|
|
@@ -781,6 +807,7 @@ var useWebSocket = (options) => {
|
|
|
781
807
|
manualCloseRef.current = true;
|
|
782
808
|
manualOpenRef.current = false;
|
|
783
809
|
suppressReconnectRef.current = true;
|
|
810
|
+
terminalErrorRef.current = null;
|
|
784
811
|
reconnect.cancel();
|
|
785
812
|
heartbeat.stop();
|
|
786
813
|
commitState((current) => ({
|
|
@@ -818,7 +845,7 @@ var useWebSocket = (options) => {
|
|
|
818
845
|
}));
|
|
819
846
|
return;
|
|
820
847
|
}
|
|
821
|
-
const shouldConnect = connect && !manualCloseRef.current || manualOpenRef.current || reconnect.status === "running";
|
|
848
|
+
const shouldConnect = terminalErrorRef.current === null && (connect && !manualCloseRef.current || manualOpenRef.current || reconnect.status === "running");
|
|
822
849
|
const nextSocketKey = `${resolvedUrl}::${protocolsDependency}::${options.binaryType ?? "blob"}`;
|
|
823
850
|
if (!shouldConnect) {
|
|
824
851
|
if (socketRef.current !== null) {
|
|
@@ -828,7 +855,7 @@ var useWebSocket = (options) => {
|
|
|
828
855
|
socketKeyRef.current = null;
|
|
829
856
|
commitState((current) => ({
|
|
830
857
|
...current,
|
|
831
|
-
status: manualCloseRef.current ? "closed" : "idle"
|
|
858
|
+
status: terminalErrorRef.current !== null ? "error" : manualCloseRef.current ? "closed" : "idle"
|
|
832
859
|
}));
|
|
833
860
|
return;
|
|
834
861
|
}
|
|
@@ -883,6 +910,7 @@ var useWebSocket = (options) => {
|
|
|
883
910
|
useEffect(() => () => {
|
|
884
911
|
suppressReconnectRef.current = true;
|
|
885
912
|
socketKeyRef.current = null;
|
|
913
|
+
terminalErrorRef.current = null;
|
|
886
914
|
const socket2 = socketRef.current;
|
|
887
915
|
socketRef.current = null;
|
|
888
916
|
if (socket2 === null) {
|
|
@@ -979,6 +1007,7 @@ var useEventSource = (options) => {
|
|
|
979
1007
|
const manualOpenRef = useRef(false);
|
|
980
1008
|
const skipErrorReconnectRef = useRef(false);
|
|
981
1009
|
const suppressReconnectRef = useRef(false);
|
|
1010
|
+
const terminalErrorRef = useRef(null);
|
|
982
1011
|
const [openNonce, setOpenNonce] = useState(0);
|
|
983
1012
|
const [state, setState] = useState(
|
|
984
1013
|
() => createInitialState4(connect ? "connecting" : "idle")
|
|
@@ -1012,7 +1041,9 @@ var useEventSource = (options) => {
|
|
|
1012
1041
|
});
|
|
1013
1042
|
const handleOpen = useEffectEvent((event, source) => {
|
|
1014
1043
|
manualCloseRef.current = false;
|
|
1044
|
+
manualOpenRef.current = false;
|
|
1015
1045
|
suppressReconnectRef.current = false;
|
|
1046
|
+
terminalErrorRef.current = null;
|
|
1016
1047
|
reconnect.markConnected();
|
|
1017
1048
|
commitState((current) => ({
|
|
1018
1049
|
...current,
|
|
@@ -1038,8 +1069,14 @@ var useEventSource = (options) => {
|
|
|
1038
1069
|
options.onMessage?.(message, event);
|
|
1039
1070
|
} catch {
|
|
1040
1071
|
const parseError = new Event("error");
|
|
1072
|
+
terminalErrorRef.current = parseError;
|
|
1073
|
+
manualOpenRef.current = false;
|
|
1074
|
+
suppressReconnectRef.current = true;
|
|
1075
|
+
reconnect.cancel();
|
|
1076
|
+
closeEventSource();
|
|
1041
1077
|
commitState((current) => ({
|
|
1042
1078
|
...current,
|
|
1079
|
+
lastChangedAt: Date.now(),
|
|
1043
1080
|
lastError: parseError,
|
|
1044
1081
|
status: "error"
|
|
1045
1082
|
}));
|
|
@@ -1047,6 +1084,17 @@ var useEventSource = (options) => {
|
|
|
1047
1084
|
}
|
|
1048
1085
|
);
|
|
1049
1086
|
const handleError = useEffectEvent((event, source) => {
|
|
1087
|
+
const terminalError = terminalErrorRef.current;
|
|
1088
|
+
if (terminalError !== null) {
|
|
1089
|
+
suppressReconnectRef.current = false;
|
|
1090
|
+
commitState((current) => ({
|
|
1091
|
+
...current,
|
|
1092
|
+
lastChangedAt: Date.now(),
|
|
1093
|
+
lastError: terminalError,
|
|
1094
|
+
status: "error"
|
|
1095
|
+
}));
|
|
1096
|
+
return;
|
|
1097
|
+
}
|
|
1050
1098
|
const skipErrorReconnect = skipErrorReconnectRef.current;
|
|
1051
1099
|
skipErrorReconnectRef.current = false;
|
|
1052
1100
|
const shouldReconnect = !suppressReconnectRef.current && !skipErrorReconnect && reconnectEnabled && (options.shouldReconnect?.(event) ?? true);
|
|
@@ -1073,6 +1121,7 @@ var useEventSource = (options) => {
|
|
|
1073
1121
|
manualCloseRef.current = false;
|
|
1074
1122
|
manualOpenRef.current = true;
|
|
1075
1123
|
suppressReconnectRef.current = false;
|
|
1124
|
+
terminalErrorRef.current = null;
|
|
1076
1125
|
reconnect.cancel();
|
|
1077
1126
|
setOpenNonce((current) => current + 1);
|
|
1078
1127
|
};
|
|
@@ -1081,6 +1130,7 @@ var useEventSource = (options) => {
|
|
|
1081
1130
|
manualOpenRef.current = true;
|
|
1082
1131
|
skipErrorReconnectRef.current = true;
|
|
1083
1132
|
suppressReconnectRef.current = true;
|
|
1133
|
+
terminalErrorRef.current = null;
|
|
1084
1134
|
closeEventSource();
|
|
1085
1135
|
suppressReconnectRef.current = false;
|
|
1086
1136
|
reconnect.schedule("manual");
|
|
@@ -1089,6 +1139,7 @@ var useEventSource = (options) => {
|
|
|
1089
1139
|
manualCloseRef.current = true;
|
|
1090
1140
|
manualOpenRef.current = false;
|
|
1091
1141
|
suppressReconnectRef.current = true;
|
|
1142
|
+
terminalErrorRef.current = null;
|
|
1092
1143
|
reconnect.cancel();
|
|
1093
1144
|
closeEventSource();
|
|
1094
1145
|
commitState((current) => ({
|
|
@@ -1115,7 +1166,7 @@ var useEventSource = (options) => {
|
|
|
1115
1166
|
}));
|
|
1116
1167
|
return;
|
|
1117
1168
|
}
|
|
1118
|
-
const shouldConnect = connect && !manualCloseRef.current || manualOpenRef.current || reconnect.status === "running";
|
|
1169
|
+
const shouldConnect = terminalErrorRef.current === null && (connect && !manualCloseRef.current || manualOpenRef.current || reconnect.status === "running");
|
|
1119
1170
|
const nextEventSourceKey = [
|
|
1120
1171
|
resolvedUrl,
|
|
1121
1172
|
options.withCredentials ? "credentials" : "anonymous",
|
|
@@ -1129,7 +1180,7 @@ var useEventSource = (options) => {
|
|
|
1129
1180
|
eventSourceKeyRef.current = null;
|
|
1130
1181
|
commitState((current) => ({
|
|
1131
1182
|
...current,
|
|
1132
|
-
status: manualCloseRef.current ? "closed" : "idle"
|
|
1183
|
+
status: terminalErrorRef.current !== null ? "error" : manualCloseRef.current ? "closed" : "idle"
|
|
1133
1184
|
}));
|
|
1134
1185
|
return;
|
|
1135
1186
|
}
|
|
@@ -1191,6 +1242,7 @@ var useEventSource = (options) => {
|
|
|
1191
1242
|
useEffect(() => () => {
|
|
1192
1243
|
suppressReconnectRef.current = true;
|
|
1193
1244
|
eventSourceKeyRef.current = null;
|
|
1245
|
+
terminalErrorRef.current = null;
|
|
1194
1246
|
const source = eventSourceRef.current;
|
|
1195
1247
|
eventSourceRef.current = null;
|
|
1196
1248
|
if (source !== null) {
|