Timeout actions
This commit is contained in:
parent
20397d32e6
commit
921ead0f05
@ -13,11 +13,11 @@ import type {VideoRef, ReactVideoProps, VideoMetadata} from './types';
|
|||||||
|
|
||||||
// Action Queue Class
|
// Action Queue Class
|
||||||
class ActionQueue {
|
class ActionQueue {
|
||||||
private queue: (() => Promise<void>)[] = [];
|
private queue: { action: () => Promise<void>; name: string }[] = [];
|
||||||
private isRunning = false;
|
private isRunning = false;
|
||||||
|
|
||||||
enqueue(action: () => Promise<void>) {
|
enqueue(action: () => Promise<void>, name: string) {
|
||||||
this.queue.push(action);
|
this.queue.push({ action, name });
|
||||||
this.runNext();
|
this.runNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,17 +27,21 @@ class ActionQueue {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.isRunning = true;
|
this.isRunning = true;
|
||||||
console.log("Running an action");
|
const { action, name } = this.queue.shift()!;
|
||||||
const action = this.queue.shift();
|
console.log(`Running action: ${name}`);
|
||||||
if (action) {
|
|
||||||
try {
|
const actionPromise = action();
|
||||||
await action();
|
const timeoutPromise = new Promise<void>((_, reject) =>
|
||||||
} catch (e) {
|
setTimeout(() => reject(new Error(`Action ${name} timed out`)), 2000)
|
||||||
console.error('Error in queued action:', e);
|
);
|
||||||
} finally {
|
|
||||||
this.isRunning = false;
|
try {
|
||||||
this.runNext();
|
await Promise.race([actionPromise, timeoutPromise]);
|
||||||
}
|
} catch (e) {
|
||||||
|
console.error('Error in queued action:', e);
|
||||||
|
} finally {
|
||||||
|
this.isRunning = false;
|
||||||
|
this.runNext();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,7 +120,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
seekTime: time,
|
seekTime: time,
|
||||||
currentTime: nativeRef.current.currentTime,
|
currentTime: nativeRef.current.currentTime,
|
||||||
});
|
});
|
||||||
});
|
}, 'seek');
|
||||||
},
|
},
|
||||||
[onSeek],
|
[onSeek],
|
||||||
);
|
);
|
||||||
@ -127,7 +131,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await nativeRef.current.pause();
|
await nativeRef.current.pause();
|
||||||
});
|
}, 'pause');
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const resume = useCallback(() => {
|
const resume = useCallback(() => {
|
||||||
@ -140,7 +144,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error playing video:', e);
|
console.error('Error playing video:', e);
|
||||||
}
|
}
|
||||||
});
|
}, 'resume');
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const setVolume = useCallback((vol: number) => {
|
const setVolume = useCallback((vol: number) => {
|
||||||
@ -149,7 +153,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
nativeRef.current.volume = Math.max(0, Math.min(vol, 100)) / 100;
|
nativeRef.current.volume = Math.max(0, Math.min(vol, 100)) / 100;
|
||||||
});
|
}, 'setVolume');
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const getCurrentPosition = useCallback(async () => {
|
const getCurrentPosition = useCallback(async () => {
|
||||||
@ -204,7 +208,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
console.error('Could not toggle fullscreen/screen lock status', e);
|
console.error('Could not toggle fullscreen/screen lock status', e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
run();
|
actionQueue.current.enqueue(run, 'setFullScreen');
|
||||||
},
|
},
|
||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
@ -374,7 +378,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
}, 'makeNewShaka');
|
||||||
}, [source, paused, onError]);
|
}, [source, paused, onError]);
|
||||||
|
|
||||||
const nativeRefDefined = !!nativeRef.current;
|
const nativeRefDefined = !!nativeRef.current;
|
||||||
|
Loading…
Reference in New Issue
Block a user