Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Update FlowManager in Managed Player state if a new one is passed in #585

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions react/player/src/manager/managed-player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ManagedState {

/** the config to use when creating a player */
playerConfig: ReactPlayerOptions;
}) {
}): this {
const initialState: ManagedPlayerState = {
value: "not_started",
context: {
Expand All @@ -75,7 +75,7 @@ class ManagedState {
}

/** reset starts from nothing */
public reset() {
public reset(): void {
if (this.state?.value === "error") {
const { playerConfig, manager } = this.state.context;
this.start({ playerConfig, manager });
Expand All @@ -85,7 +85,7 @@ class ManagedState {
}

/** restart starts from the last result */
public restart() {
public restart(): void {
if (this.state?.value === "error") {
const { playerConfig, manager, prevResult, reactPlayer } =
this.state.context;
Expand Down Expand Up @@ -208,6 +208,18 @@ const managedPlayerStateMachines = new WeakMap<
ManagedState
>();

function getOrCreateNewManagedState(
key: ManagedPlayerStateKey,
middleware?: ManagerMiddleware,
): ManagedState {
const newManagedState =
managedPlayerStateMachines.get(key) ??
new ManagedState({ middleware: middleware });
managedPlayerStateMachines.set(key, newManagedState);

return newManagedState;
}

/** Creates an x-state state machine that persists when this component is no longer renders (due to Suspense) */
export const usePersistentStateMachine = (options: {
/** the flow manager to use */
Expand All @@ -218,17 +230,30 @@ export const usePersistentStateMachine = (options: {

/** Any middleware for the manager */
middleware?: ManagerMiddleware;
}) => {
}): { state: ManagedPlayerState | undefined; managedState: ManagedState } => {
const keyRef = React.useRef<ManagedPlayerStateKey>({
_key: Symbol("managed-player"),
});

const managedState =
managedPlayerStateMachines.get(keyRef.current) ??
new ManagedState({ middleware: options.middleware });
managedPlayerStateMachines.set(keyRef.current, managedState);
const initialManagedState = getOrCreateNewManagedState(
keyRef.current,
options.middleware,
);

const managedStateRef = React.useRef(initialManagedState);
let managedState = managedStateRef.current;
const [state, setState] = React.useState(managedState.state);

React.useEffect(() => {
if (state?.value === "completed") {
const newManagedState = getOrCreateNewManagedState(
keyRef.current,
options.middleware,
);
managedState = newManagedState;
}
}, [options.manager, options.playerConfig]);

React.useEffect(() => {
const unsub = managedState.addListener({
onState: (s) => {
Expand All @@ -241,9 +266,9 @@ export const usePersistentStateMachine = (options: {
}

return unsub;
}, []);
}, [managedState]);

return { managedState, state };
return { state, managedState };
};

/**
Expand All @@ -252,7 +277,9 @@ export const usePersistentStateMachine = (options: {
*
* `suspense` must be enabled to wait for results in flight.
*/
export const ManagedPlayer = (props: ManagedPlayerProps) => {
export const ManagedPlayer = (
props: ManagedPlayerProps,
): React.ReactElement | null => {
const { withRequestTime, RequestTimeMetricsPlugin } = useRequestTime();

const { state, managedState } = usePersistentStateMachine({
Expand Down