@veltdev/react 1.0.118 → 1.0.119

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.
@@ -33,4 +33,7 @@ export declare function useEditorAccessRequestHandler(): {
33
33
  /**
34
34
  * @beta This hook is in beta
35
35
  */
36
- export declare function useLiveState<T>(liveStateDataId: string, initialValue?: any, debounceTime?: number): [T, (value: T) => void];
36
+ export declare function useLiveState<T>(liveStateDataId: string, initialValue?: any, options?: {
37
+ syncDuration?: number;
38
+ resetLiveState?: boolean;
39
+ }): [T, (value: T) => void];
package/esm/index.js CHANGED
@@ -1984,18 +1984,45 @@ function useEditorAccessRequestHandler() {
1984
1984
  /**
1985
1985
  * @beta This hook is in beta
1986
1986
  */
1987
- function useLiveState(liveStateDataId, initialValue, debounceTime) {
1988
- if (debounceTime === void 0) { debounceTime = 50; }
1987
+ function useLiveState(liveStateDataId, initialValue, options) {
1989
1988
  var liveStateSyncElement = useLiveStateSyncUtils();
1990
- var _a = React.useState(initialValue), data = _a[0], setData = _a[1];
1989
+ var client = useVeltClient().client;
1990
+ var _a = React.useState(null), documentId = _a[0], setDocumentId = _a[1];
1991
+ var _b = React.useState(null), user = _b[0], setUser = _b[1];
1992
+ var _c = React.useState(initialValue), data = _c[0], setData = _c[1];
1993
+ useEffect(function () {
1994
+ resetLiveStateData();
1995
+ }, [documentId, user]);
1996
+ var resetLiveStateData = function () {
1997
+ if (user && documentId) {
1998
+ liveStateSyncElement === null || liveStateSyncElement === void 0 ? void 0 : liveStateSyncElement.setLiveStateData(liveStateDataId, data);
1999
+ }
2000
+ };
1991
2001
  useEffect(function () {
1992
2002
  if (!(liveStateSyncElement === null || liveStateSyncElement === void 0 ? void 0 : liveStateSyncElement.getLiveStateData))
1993
2003
  return;
2004
+ var documentPathsSubscription, userSubscription;
2005
+ if (options === null || options === void 0 ? void 0 : options.resetLiveState) {
2006
+ documentPathsSubscription = client.docService.getDocumentPaths$().subscribe(function (paths) {
2007
+ if (paths) {
2008
+ setDocumentId(paths === null || paths === void 0 ? void 0 : paths.documentId);
2009
+ documentPathsSubscription === null || documentPathsSubscription === void 0 ? void 0 : documentPathsSubscription.unsubscribe();
2010
+ }
2011
+ });
2012
+ userSubscription = client.authService.getUser$().subscribe(function (user) {
2013
+ if (user) {
2014
+ setUser(user);
2015
+ userSubscription === null || userSubscription === void 0 ? void 0 : userSubscription.unsubscribe();
2016
+ }
2017
+ });
2018
+ }
1994
2019
  var subscription = liveStateSyncElement.getLiveStateData(liveStateDataId).subscribe(function (res) {
1995
2020
  setData(res);
1996
2021
  });
1997
2022
  return function () {
1998
2023
  subscription.unsubscribe();
2024
+ documentPathsSubscription === null || documentPathsSubscription === void 0 ? void 0 : documentPathsSubscription.unsubscribe();
2025
+ userSubscription === null || userSubscription === void 0 ? void 0 : userSubscription.unsubscribe();
1999
2026
  };
2000
2027
  }, [liveStateSyncElement === null || liveStateSyncElement === void 0 ? void 0 : liveStateSyncElement.getLiveStateData]);
2001
2028
  var useDebounce = function (callback) {
@@ -2016,7 +2043,7 @@ function useLiveState(liveStateDataId, initialValue, debounceTime) {
2016
2043
  };
2017
2044
  var setDataFunction = function (value) {
2018
2045
  setData(value);
2019
- debouncedOnInput(value, debounceTime); // default 50ms debounce time
2046
+ debouncedOnInput(value, (options === null || options === void 0 ? void 0 : options.syncDuration) >= 0 ? options === null || options === void 0 ? void 0 : options.syncDuration : 50); // default 50ms debounce time
2020
2047
  };
2021
2048
  var debouncedOnInput = useDebounce(function (value) {
2022
2049
  if (liveStateSyncElement === null || liveStateSyncElement === void 0 ? void 0 : liveStateSyncElement.setLiveStateData) {