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