beamable-network-depin
Version:
Beamable DePIN SDK for Solana blockchain interactions
29 lines (28 loc) • 905 B
JavaScript
import { Endian, getArrayCodec, getStructCodec, getU64Codec, getU8Codec } from "gill";
export class RingBuffer64 {
buffer;
current_index;
capacity;
static getDataCodec(size) {
return getStructCodec([
["buffer", getArrayCodec(getU64Codec({ endian: Endian.Little }), { size })],
["current_index", getU8Codec()]
]);
}
static getLen(size) {
return (8 * size) + 1; // 8 bytes for each u64 + 1 byte for current_index
}
constructor(fields, capacity) {
this.buffer = fields.buffer;
this.current_index = fields.current_index;
this.capacity = capacity;
}
getAllValues() {
const result = [];
for (let i = 0; i < this.capacity; i++) {
const index = (this.current_index + i) % this.capacity;
result.push(this.buffer[index]);
}
return result;
}
}