import { scan } from 'react-scan' // dev-tools for demo
import ReactDOM from 'react-dom/client'
import { useQueuerState } from '@tanstack/react-pacer/queuer'
function App() {
// Queuer that uses React.useState under the hood
const [queueItems, queuer] = useQueuerState({
maxSize: 25,
initialItems: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
wait: 1000, // wait 1 second between processing items - wait is optional!
})
return (
<div>
<h1>TanStack Pacer useQueuerState Example</h1>
<div>Queue Size: {queuer.size()}</div>
<div>Queue Max Size: {25}</div>
<div>Queue Full: {queuer.isFull() ? 'Yes' : 'No'}</div>
<div>Queue Peek: {queuer.peek()}</div>
<div>Queue Empty: {queuer.isEmpty() ? 'Yes' : 'No'}</div>
<div>Queue Idle: {queuer.isIdle() ? 'Yes' : 'No'}</div>
<div>Queuer Status: {queuer.isRunning() ? 'Running' : 'Stopped'}</div>
<div>Items Processed: {queuer.getExecutionCount()}</div>
<div>Queue Items: {queueItems.join(', ')}</div>
<div
style={{
display: 'grid',
gridTemplateColumns: 'repeat(2, 1fr)',
gap: '8px',
maxWidth: '600px',
margin: '16px 0',
}}
>
<button
onClick={() => {
const nextNumber = queueItems.length
? queueItems[queueItems.length - 1] + 1
: 1
queuer.addItem(nextNumber)
}}
disabled={queuer.isFull()}
>
Add Number
</button>
<button
disabled={queuer.isEmpty()}
onClick={() => {
const item = queuer.getNextItem()
console.log('getNextItem item', item)
}}
>
Process Next
</button>
<button onClick={() => queuer.clear()} disabled={queuer.isEmpty()}>
Clear Queue
</button>
<button onClick={() => queuer.reset()} disabled={queuer.isEmpty()}>
Reset Queue
</button>
<button onClick={() => queuer.start()} disabled={queuer.isRunning()}>
Start Processing
</button>
<button onClick={() => queuer.stop()} disabled={!queuer.isRunning()}>
Stop Processing
</button>
</div>
</div>
)
}
const root = ReactDOM.createRoot(document.getElementById('root')!)
root.render(<App />)
scan() // dev-tools for demo
import { scan } from 'react-scan' // dev-tools for demo
import ReactDOM from 'react-dom/client'
import { useQueuerState } from '@tanstack/react-pacer/queuer'
function App() {
// Queuer that uses React.useState under the hood
const [queueItems, queuer] = useQueuerState({
maxSize: 25,
initialItems: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
wait: 1000, // wait 1 second between processing items - wait is optional!
})
return (
<div>
<h1>TanStack Pacer useQueuerState Example</h1>
<div>Queue Size: {queuer.size()}</div>
<div>Queue Max Size: {25}</div>
<div>Queue Full: {queuer.isFull() ? 'Yes' : 'No'}</div>
<div>Queue Peek: {queuer.peek()}</div>
<div>Queue Empty: {queuer.isEmpty() ? 'Yes' : 'No'}</div>
<div>Queue Idle: {queuer.isIdle() ? 'Yes' : 'No'}</div>
<div>Queuer Status: {queuer.isRunning() ? 'Running' : 'Stopped'}</div>
<div>Items Processed: {queuer.getExecutionCount()}</div>
<div>Queue Items: {queueItems.join(', ')}</div>
<div
style={{
display: 'grid',
gridTemplateColumns: 'repeat(2, 1fr)',
gap: '8px',
maxWidth: '600px',
margin: '16px 0',
}}
>
<button
onClick={() => {
const nextNumber = queueItems.length
? queueItems[queueItems.length - 1] + 1
: 1
queuer.addItem(nextNumber)
}}
disabled={queuer.isFull()}
>
Add Number
</button>
<button
disabled={queuer.isEmpty()}
onClick={() => {
const item = queuer.getNextItem()
console.log('getNextItem item', item)
}}
>
Process Next
</button>
<button onClick={() => queuer.clear()} disabled={queuer.isEmpty()}>
Clear Queue
</button>
<button onClick={() => queuer.reset()} disabled={queuer.isEmpty()}>
Reset Queue
</button>
<button onClick={() => queuer.start()} disabled={queuer.isRunning()}>
Start Processing
</button>
<button onClick={() => queuer.stop()} disabled={!queuer.isRunning()}>
Stop Processing
</button>
</div>
</div>
)
}
const root = ReactDOM.createRoot(document.getElementById('root')!)
root.render(<App />)
scan() // dev-tools for demo
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.