fixed-size-list
Immutable fixed-length list (a.k.a circular buffer) with an event emitter for Typescript and Javascript
Quick start
npm i fixed-size-list
// Now it's emptyfixedSizeList.add1// Not it's [ 1 ]fixedSizeList.add2// Now it's [ 2, 1 ]fixedSizeList.add3// Now it's [ 3, 2, 1 ]fixedSizeList.add4// Now it's [ 4, 3, 2 ] console.logfixedSizeList.data// logs [4,3,2] fixedSizeList.reset// Now it's []
Initial list
You can set initial values easily passing them to the constructor
// Now it's [ 1, 2, 3 ]
Be aware that the initial list is truncated if it's longer than maxSize
// Now it's [ 1, 2 ]// 3 was truncated
Events
FixedSizeList has an event emitter. You can listen to specific events. WARNING! on
returns an unsubscribe function. Do not forget to call it when you no longer need the subscription to unsubscribe.
eventNewItem
It emits an added item
fixedSizeList.add5// logs 'item added 5' // later onunsubscribe
eventTruncate
It emits an array of removed items
fixedSizeList.add5fixedSizeList.add4fixedSizeList.add3// logs 'items removed [ 5 ]' // later onunsubscribe
eventReset
fixedSizeList.reset// logs 'list reset' // later onunsubscribe
eventCreated
We can add the third optional parameter of FixedSizeList's constructor and pass a custom event emitter
// logs 'list created' // later onunsubscribe
All
We can subscribe to all events at once
// later onunsubscribe