1 | "use strict";
|
2 | 'use client';
|
3 |
|
4 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
5 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
6 | Object.defineProperty(exports, "__esModule", {
|
7 | value: true
|
8 | });
|
9 | exports.LazyRipple = void 0;
|
10 | exports.default = useLazyRipple;
|
11 | var React = _interopRequireWildcard(require("react"));
|
12 | var _useLazyRef = _interopRequireDefault(require("@mui/utils/useLazyRef"));
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | class LazyRipple {
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | static create() {
|
29 | return new LazyRipple();
|
30 | }
|
31 | static use() {
|
32 |
|
33 | const ripple = (0, _useLazyRef.default)(LazyRipple.create).current;
|
34 | const [shouldMount, setShouldMount] = React.useState(false);
|
35 | ripple.shouldMount = shouldMount;
|
36 | ripple.setShouldMount = setShouldMount;
|
37 | React.useEffect(ripple.mountEffect, [shouldMount]);
|
38 |
|
39 |
|
40 | return ripple;
|
41 | }
|
42 | constructor() {
|
43 | this.ref = {
|
44 | current: null
|
45 | };
|
46 | this.mounted = null;
|
47 | this.didMount = false;
|
48 | this.shouldMount = false;
|
49 | this.setShouldMount = null;
|
50 | }
|
51 | mount() {
|
52 | if (!this.mounted) {
|
53 | this.mounted = createControlledPromise();
|
54 | this.shouldMount = true;
|
55 | this.setShouldMount(this.shouldMount);
|
56 | }
|
57 | return this.mounted;
|
58 | }
|
59 | mountEffect = () => {
|
60 | if (this.shouldMount && !this.didMount) {
|
61 | if (this.ref.current !== null) {
|
62 | this.didMount = true;
|
63 | this.mounted.resolve();
|
64 | }
|
65 | }
|
66 | };
|
67 |
|
68 |
|
69 |
|
70 | start(...args) {
|
71 | this.mount().then(() => this.ref.current?.start(...args));
|
72 | }
|
73 | stop(...args) {
|
74 | this.mount().then(() => this.ref.current?.stop(...args));
|
75 | }
|
76 | pulsate(...args) {
|
77 | this.mount().then(() => this.ref.current?.pulsate(...args));
|
78 | }
|
79 | }
|
80 | exports.LazyRipple = LazyRipple;
|
81 | function useLazyRipple() {
|
82 | return LazyRipple.use();
|
83 | }
|
84 | function createControlledPromise() {
|
85 | let resolve;
|
86 | let reject;
|
87 | const p = new Promise((resolveFn, rejectFn) => {
|
88 | resolve = resolveFn;
|
89 | reject = rejectFn;
|
90 | });
|
91 | p.resolve = resolve;
|
92 | p.reject = reject;
|
93 | return p;
|
94 | } |
\ | No newline at end of file |