UNPKG

4.93 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7function _interopNamespace(e) {
8 if (e && e.__esModule) { return e; } else {
9 var n = {};
10 if (e) {
11 Object.keys(e).forEach(function (k) {
12 var d = Object.getOwnPropertyDescriptor(e, k);
13 Object.defineProperty(n, k, d.get ? d : {
14 enumerable: true,
15 get: function () {
16 return e[k];
17 }
18 });
19 });
20 }
21 n['default'] = e;
22 return n;
23 }
24}
25
26var React = require('react');
27var React__default = _interopDefault(React);
28
29function _extends() {
30 _extends = Object.assign || function (target) {
31 for (var i = 1; i < arguments.length; i++) {
32 var source = arguments[i];
33
34 for (var key in source) {
35 if (Object.prototype.hasOwnProperty.call(source, key)) {
36 target[key] = source[key];
37 }
38 }
39 }
40
41 return target;
42 };
43
44 return _extends.apply(this, arguments);
45}
46
47function _objectWithoutPropertiesLoose(source, excluded) {
48 if (source == null) return {};
49 var target = {};
50 var sourceKeys = Object.keys(source);
51 var key, i;
52
53 for (i = 0; i < sourceKeys.length; i++) {
54 key = sourceKeys[i];
55 if (excluded.indexOf(key) >= 0) continue;
56 target[key] = source[key];
57 }
58
59 return target;
60}
61
62function useOnMount(callback) {
63 React.useEffect(callback, []);
64}
65
66function useSound(url, _ref) {
67 if (_ref === void 0) {
68 _ref = {};
69 }
70
71 var _ref2 = _ref,
72 _ref2$volume = _ref2.volume,
73 volume = _ref2$volume === void 0 ? 1 : _ref2$volume,
74 _ref2$playbackRate = _ref2.playbackRate,
75 playbackRate = _ref2$playbackRate === void 0 ? 1 : _ref2$playbackRate,
76 _ref2$soundEnabled = _ref2.soundEnabled,
77 soundEnabled = _ref2$soundEnabled === void 0 ? true : _ref2$soundEnabled,
78 _ref2$interrupt = _ref2.interrupt,
79 interrupt = _ref2$interrupt === void 0 ? false : _ref2$interrupt,
80 delegated = _objectWithoutPropertiesLoose(_ref2, ["volume", "playbackRate", "soundEnabled", "interrupt"]);
81
82 var HowlConstructor = React__default.useRef(null);
83
84 var _React$useState = React__default.useState(false),
85 isPlaying = _React$useState[0],
86 setIsPlaying = _React$useState[1];
87
88 var _React$useState2 = React__default.useState(null),
89 sound = _React$useState2[0],
90 setSound = _React$useState2[1]; // We want to lazy-load Howler, since sounds can't play on load anyway.
91
92
93 useOnMount(function () {
94 new Promise(function (resolve) { resolve(_interopNamespace(require('howler'))); }).then(function (mod) {
95 HowlConstructor.current = mod.Howl;
96 var sound = new HowlConstructor.current(_extends({
97 src: [url],
98 volume: volume
99 }, delegated));
100 setSound(sound);
101 });
102 }); // When the URL changes, we have to do a whole thing where we recreate
103 // the Howl instance. This is because Howler doesn't expose a way to
104 // tweak the sound
105
106 React__default.useEffect(function () {
107 if (HowlConstructor.current && sound) {
108 setSound(new HowlConstructor.current(_extends({
109 src: [url],
110 volume: volume
111 }, delegated)));
112 } // The linter wants to run this effect whenever ANYTHING changes,
113 // but very specifically I only want to recreate the Howl instance
114 // when the `url` changes. Other changes should have no effect.
115 // eslint-disable-next-line react-hooks/exhaustive-deps
116
117 }, [url]); // Whenever volume/playbackRate are changed, change those properties
118 // on the sound instance.
119
120 React__default.useEffect(function () {
121 if (sound) {
122 sound.volume(volume);
123 sound.rate(playbackRate);
124 } // A weird bug means that including the `sound` here can trigger an
125 // error on unmount, where the state loses track of the sprites??
126 // No idea, but anyway I don't need to re-run this if only the `sound`
127 // changes.
128 // eslint-disable-next-line react-hooks/exhaustive-deps
129
130 }, [volume, playbackRate]);
131 var play = React__default.useCallback(function (options) {
132 if (options === void 0) {
133 options = {};
134 }
135
136 if (!sound || !soundEnabled && !options.forceSoundEnabled) {
137 return;
138 }
139
140 if (interrupt) {
141 sound.stop();
142 }
143
144 if (options.playbackRate) {
145 sound.rate(options.playbackRate);
146 }
147
148 sound.play(options.id);
149 sound.once('end', function () {
150 return setIsPlaying(false);
151 });
152 setIsPlaying(true);
153 }, [sound, soundEnabled, interrupt]);
154 var stop = React__default.useCallback(function () {
155 if (!sound) {
156 return;
157 }
158
159 sound.stop();
160 setIsPlaying(false);
161 }, [sound]);
162 var returnedValue = [play, {
163 sound: sound,
164 stop: stop,
165 isPlaying: isPlaying
166 }];
167 return returnedValue;
168}
169
170exports.default = useSound;
171//# sourceMappingURL=use-sound.cjs.development.js.map