Skip to content

Instantly share code, notes, and snippets.

@karol-majewski
Created August 9, 2021 14:24
Show Gist options
  • Save karol-majewski/62a3e4bb54562ade9b5cff219fcc6a71 to your computer and use it in GitHub Desktop.
Save karol-majewski/62a3e4bb54562ade9b5cff219fcc6a71 to your computer and use it in GitHub Desktop.
import React from 'react';
import { autobind } from '@typed-decorators/autobind';
class Rolodex<T> {
index: number;
length: number;
constructor(private sequence: NonEmptyArray<T>) {
this.sequence = sequence;
this.index = 0;
this.length = sequence.length;
}
@autobind
next(): T {
return this.sequence[((++this.index % this.length) + this.length) % this.length]!;
}
}
export function useRolodex<T>(initialSequence: NonEmptyArray<T>): Rolodex<T> {
const slider = React.useRef(new Rolodex(initialSequence));
React.useDebugValue(slider);
return slider.current;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment