Skip to content

Commit

Permalink
[docs][LinearBuffer] Fix demo
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrookes committed Jul 5, 2024
1 parent e5030ad commit 28ce82d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
29 changes: 23 additions & 6 deletions docs/data/material/components/progress/LinearBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,42 @@ export default function LinearBuffer() {
const [progress, setProgress] = React.useState(0);
const [buffer, setBuffer] = React.useState(10);

const bufferRef = React.useRef(() => {});
React.useEffect(() => {
bufferRef.current = () => {
if (buffer < 100) {
const newBuffer = buffer + 1 + Math.random() * 10;
setBuffer(newBuffer > 100 ? 100 : newBuffer);
}
};
});

const progressRef = React.useRef(() => {});
React.useEffect(() => {
progressRef.current = () => {
if (progress > 100) {
if (progress === 100) {
setProgress(0);
setBuffer(10);
} else {
const diff = Math.random() * 10;
const diff2 = Math.random() * 10;
setProgress(progress + diff);
setBuffer(progress + diff + diff2);
setProgress(progress + 1);
}
};
});

React.useEffect(() => {
const timer = setInterval(() => {
bufferRef.current();
}, 500);

return () => {
clearInterval(timer);
};
}, []);

React.useEffect(() => {
const timer = setInterval(() => {
progressRef.current();
}, 500);
}, 100);

return () => {
clearInterval(timer);
Expand Down
29 changes: 23 additions & 6 deletions docs/data/material/components/progress/LinearBuffer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,42 @@ export default function LinearBuffer() {
const [progress, setProgress] = React.useState(0);
const [buffer, setBuffer] = React.useState(10);

const bufferRef = React.useRef(() => {});
React.useEffect(() => {
bufferRef.current = () => {
if (buffer < 100) {
const newBuffer = buffer + 1 + Math.random() * 10;
setBuffer(newBuffer > 100 ? 100 : newBuffer);
}
};
});

const progressRef = React.useRef(() => {});
React.useEffect(() => {
progressRef.current = () => {
if (progress > 100) {
if (progress === 100) {
setProgress(0);
setBuffer(10);
} else {
const diff = Math.random() * 10;
const diff2 = Math.random() * 10;
setProgress(progress + diff);
setBuffer(progress + diff + diff2);
setProgress(progress + 1);
}
};
});

React.useEffect(() => {
const timer = setInterval(() => {
bufferRef.current();
}, 500);

return () => {
clearInterval(timer);
};
}, []);

React.useEffect(() => {
const timer = setInterval(() => {
progressRef.current();
}, 500);
}, 100);

return () => {
clearInterval(timer);
Expand Down

0 comments on commit 28ce82d

Please sign in to comment.