source: imaps-frontend/node_modules/react-use/esm/factory/createHTMLMediaHook.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 6.9 KB
Line 
1import { __assign } from "tslib";
2import * as React from 'react';
3import { useEffect, useRef } from 'react';
4import useSetState from '../useSetState';
5import parseTimeRanges from '../misc/parseTimeRanges';
6export default function createHTMLMediaHook(tag) {
7 return function (elOrProps) {
8 var element;
9 var props;
10 if (React.isValidElement(elOrProps)) {
11 element = elOrProps;
12 props = element.props;
13 }
14 else {
15 props = elOrProps;
16 }
17 var _a = useSetState({
18 buffered: [],
19 time: 0,
20 duration: 0,
21 paused: true,
22 muted: false,
23 volume: 1,
24 playing: false,
25 }), state = _a[0], setState = _a[1];
26 var ref = useRef(null);
27 var wrapEvent = function (userEvent, proxyEvent) {
28 return function (event) {
29 try {
30 proxyEvent && proxyEvent(event);
31 }
32 finally {
33 userEvent && userEvent(event);
34 }
35 };
36 };
37 var onPlay = function () { return setState({ paused: false }); };
38 var onPlaying = function () { return setState({ playing: true }); };
39 var onWaiting = function () { return setState({ playing: false }); };
40 var onPause = function () { return setState({ paused: true, playing: false }); };
41 var onVolumeChange = function () {
42 var el = ref.current;
43 if (!el) {
44 return;
45 }
46 setState({
47 muted: el.muted,
48 volume: el.volume,
49 });
50 };
51 var onDurationChange = function () {
52 var el = ref.current;
53 if (!el) {
54 return;
55 }
56 var duration = el.duration, buffered = el.buffered;
57 setState({
58 duration: duration,
59 buffered: parseTimeRanges(buffered),
60 });
61 };
62 var onTimeUpdate = function () {
63 var el = ref.current;
64 if (!el) {
65 return;
66 }
67 setState({ time: el.currentTime });
68 };
69 var onProgress = function () {
70 var el = ref.current;
71 if (!el) {
72 return;
73 }
74 setState({ buffered: parseTimeRanges(el.buffered) });
75 };
76 if (element) {
77 element = React.cloneElement(element, __assign(__assign({ controls: false }, props), { ref: ref, onPlay: wrapEvent(props.onPlay, onPlay), onPlaying: wrapEvent(props.onPlaying, onPlaying), onWaiting: wrapEvent(props.onWaiting, onWaiting), onPause: wrapEvent(props.onPause, onPause), onVolumeChange: wrapEvent(props.onVolumeChange, onVolumeChange), onDurationChange: wrapEvent(props.onDurationChange, onDurationChange), onTimeUpdate: wrapEvent(props.onTimeUpdate, onTimeUpdate), onProgress: wrapEvent(props.onProgress, onProgress) }));
78 }
79 else {
80 element = React.createElement(tag, __assign(__assign({ controls: false }, props), { ref: ref, onPlay: wrapEvent(props.onPlay, onPlay), onPlaying: wrapEvent(props.onPlaying, onPlaying), onWaiting: wrapEvent(props.onWaiting, onWaiting), onPause: wrapEvent(props.onPause, onPause), onVolumeChange: wrapEvent(props.onVolumeChange, onVolumeChange), onDurationChange: wrapEvent(props.onDurationChange, onDurationChange), onTimeUpdate: wrapEvent(props.onTimeUpdate, onTimeUpdate), onProgress: wrapEvent(props.onProgress, onProgress) })); // TODO: fix this typing.
81 }
82 // Some browsers return `Promise` on `.play()` and may throw errors
83 // if one tries to execute another `.play()` or `.pause()` while that
84 // promise is resolving. So we prevent that with this lock.
85 // See: https://bugs.chromium.org/p/chromium/issues/detail?id=593273
86 var lockPlay = false;
87 var controls = {
88 play: function () {
89 var el = ref.current;
90 if (!el) {
91 return undefined;
92 }
93 if (!lockPlay) {
94 var promise = el.play();
95 var isPromise = typeof promise === 'object';
96 if (isPromise) {
97 lockPlay = true;
98 var resetLock = function () {
99 lockPlay = false;
100 };
101 promise.then(resetLock, resetLock);
102 }
103 return promise;
104 }
105 return undefined;
106 },
107 pause: function () {
108 var el = ref.current;
109 if (el && !lockPlay) {
110 return el.pause();
111 }
112 },
113 seek: function (time) {
114 var el = ref.current;
115 if (!el || state.duration === undefined) {
116 return;
117 }
118 time = Math.min(state.duration, Math.max(0, time));
119 el.currentTime = time;
120 },
121 volume: function (volume) {
122 var el = ref.current;
123 if (!el) {
124 return;
125 }
126 volume = Math.min(1, Math.max(0, volume));
127 el.volume = volume;
128 setState({ volume: volume });
129 },
130 mute: function () {
131 var el = ref.current;
132 if (!el) {
133 return;
134 }
135 el.muted = true;
136 },
137 unmute: function () {
138 var el = ref.current;
139 if (!el) {
140 return;
141 }
142 el.muted = false;
143 },
144 };
145 useEffect(function () {
146 var el = ref.current;
147 if (!el) {
148 if (process.env.NODE_ENV !== 'production') {
149 if (tag === 'audio') {
150 console.error('useAudio() ref to <audio> element is empty at mount. ' +
151 'It seem you have not rendered the audio element, which it ' +
152 'returns as the first argument const [audio] = useAudio(...).');
153 }
154 else if (tag === 'video') {
155 console.error('useVideo() ref to <video> element is empty at mount. ' +
156 'It seem you have not rendered the video element, which it ' +
157 'returns as the first argument const [video] = useVideo(...).');
158 }
159 }
160 return;
161 }
162 setState({
163 volume: el.volume,
164 muted: el.muted,
165 paused: el.paused,
166 });
167 // Start media, if autoPlay requested.
168 if (props.autoPlay && el.paused) {
169 controls.play();
170 }
171 }, [props.src]);
172 return [element, state, controls, ref];
173 };
174}
Note: See TracBrowser for help on using the repository browser.