@graspologic/memstore
A fast, memory efficient, data storage library that utilizes ArrayBuffer instances as its storage medium.
Usage
First we create an implementation that users will directly interact with.
Rectangle Implementation
import {
createLayoutBuilder,
InterpretationHint,
AttributeType,
PropertySpecification,
createReader
} from '@graspologic/memstore'
export const type = Symbol('Rectangle')
export const memoryLayout = createLayoutBuilder()
.addUint32('color')
.addFloat32Vec2('position', { components: ['x', 'y'] })
.addFloat32('width')
.addFloat32('height')
.build()
export interface Rectangle {
color: number
position: [number, number]
width: number
height: number
}
export const RectangleImpl = createReader<Rectangle>(
type,
memoryLayout,
)
Now we create a store around our Rectangle implementation so we can store their data in the
underlying ArrayBuffer
Rectangle Store
import {
ArrayStore,
ArrayStoreImpl,
SlotAllocator,
ReaderStoreImpl,
} from '@graspologic/memstore'
import { RectangleImpl, memoryLayout } from './rectangle'
export function createStore() {
const store: ArrayStore = new ArrayStoreImpl(memoryLayout)
const initialCapacity = 1
const slotAllocator = new SlotAllocator(initialCapacity)
return new ReaderStoreImpl(RectangleImpl, store, slotAllocator)
}
Finally we construct our Rectangle store and add instances of it
Use the store
import { RectangleImpl } from './rectangle'
import { createStore } from './rectangleStore'
const myStore = createStore()
const myRectangle = new RectangleImpl()
myRectangle.color = 0xff00ff
myRectangle.position = [10, 10]
myRectangle.width = 20
myRectangle.height = 200
myStore.receive(myRectangle)
const ab = myStore.store.buffer
for (const rectangle of myStore) {
rectangle.width = 200
rectangle.position = [Math.random() * 100, Math.random() * 100]
}
See the API documentation or examples for additional examples.