live-share-sdk icon indicating copy to clipboard operation
live-share-sdk copied to clipboard

LiveState not initializing (live-share-react)

Open pheidler opened this issue 7 months ago • 3 comments

Please review FAQ and Known issues before filing a new item!

  • [x] I have reviewed the FAQ and known issues and did not find my topic

Please note: any submissions with insufficient reproducible information will be marked as 'Waiting for customer input' and may be closed is there is no response

Describe the bug Initializing live state using a useEffect hook is inconsistent. Setting live state on component load (i.e empty dependency array) results in ActionLiveDataObjectInitializedError: useLiveState setState: liveState is not yet initialized.. Adding the liveState.isInitialized value to the dependency array inconsistently results in isInitialized never being set to true. Attempting to set the value using .initialize(value) results in LiveDataObjectInitializeNotNeededError: LiveState:initialize - initialization is not needed.

To Reproduce

Steps to reproduce the behavior:

const [inMeetingContext, setInMeetingContext, inMeetingContextLiveState] =
    useLiveState<IMeetingContext | undefined>("in-meeting-context", undefined);

React.useEffect(() => {
    if (isPresenter && inMeetingContextLiveState?.isInitialized) {
      const context = parseInMeetingContext();
      if (context) {
        setInMeetingContext(context);
      }
    }
  }, [isPresenter, inMeetingContextLiveState?.isInitialized]);

results in inMeetingContextLiveState?.isInitialized remaining false.

React.useEffect(() => {
    if (isPresenter && inMeetingContextLiveState) {
      const context = parseInMeetingContext();
      if (context) {
        if (!inMeetingContextLiveState.isInitialized) {
          inMeetingContextLiveState.initialize(context);
        } else {
          setInMeetingContext(context);
        }
      }
    }
  }, [isPresenter, inMeetingContextLiveState]);

results in LiveDataObjectInitializeNotNeededError: LiveState:initialize - initialization is not needed. initializeState is pending but must equal needed. To fix this error, ensure you only call .initialize() when initializeState equals needed.

Context object is of type

interface IMeetingContext {
  meetingId: number;
  siteUrl: string;
  channelId: string;
}

Expected behavior

liveState.isInitiliazed to resolve to true and set initial value in useEffect hook

Screenshots

If applicable, add screenshots to help explain your problem.

OS Version[macOS/Windows]: Windows using Teams browser version in Edge Live Share Extension Version: 1.4.0 Target Platform or Language [e.g. Node.js]: SPFx v1.21.1 with bundled TeamsJS v2.32 Node v18.18.0

Add any other context about the problem here. I'd pass the initial value when initializing the state, but this value is only populated from the side panel context (isPresenter in the dep array). I'm using live state as a way to pass this value into the meeting stage context. @ryanbliss

pheidler avatar May 29 '25 18:05 pheidler

@pheidler yeah good catch, this is something we have fixed in our latest version of 2.0.0-internal.10, probably should patch it into the current v1 as well. Good issue, will look into it.

ryanbliss avatar May 29 '25 22:05 ryanbliss

@pheidler feel free to update to 1.4.1 / 1.4.1-preview.0 (for live-share-react and live-share-turbo). Fix should be up. Will close this issue once I've merged the PR.

ryanbliss avatar May 29 '25 23:05 ryanbliss

@ryanbliss Great, thank you!

pheidler avatar May 29 '25 23:05 pheidler