# @graspologic/memstore

Latest version **0.7.0** (published 2021-12-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @graspologic/memstore
pnpm add @graspologic/memstore
yarn add @graspologic/memstore
bun add @graspologic/memstore
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.7.0 |
| Published | 2021-12-08 |
| First published | 2020-09-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 426.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 16 |
| Author | Chris Trevino (chtrevin@microsoft.com) |
| Maintainers | darthtrevino, stopyoukid, essexbot, natoverse |

## Links

- npm: https://www.npmjs.com/package/@graspologic/memstore
- Repository: https://github.com/microsoft/graspologic-js
- npm.io page: https://npm.io/package/@graspologic/memstore

## Dependencies (1)

- [typedoc](https://npm.io/package/typedoc.md) 0.22.10

## Recent versions

- 0.7.0 (latest) — 2021-12-08
- 0.7.0-13 (prerelease) — 2021-03-23
- 0.4.1-beta.14.0 (beta.14) — 2020-09-11
- 0.4.0 (local.1) — 2020-09-10
- 0.7.0-12 — 2021-03-04
- 0.7.0-11 — 2021-03-04
- 0.7.0-10 — 2021-03-01
- 0.7.0-9 — 2021-02-23
- 0.7.0-8 — 2021-02-22
- 0.7.0-7 — 2021-01-11
- 0.7.0-6 — 2021-01-11
- 0.7.0-5 — 2020-12-23
- 0.7.0-4 — 2020-12-23
- 0.7.0-3 — 2020-12-23
- 0.0.7-3 — 2020-12-23
- … 9 more at https://npm.io/package/@graspologic/memstore/versions

## README

# @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

```js
import {
	createLayoutBuilder,
	InterpretationHint,
	AttributeType,
	PropertySpecification,
	createReader
} from '@graspologic/memstore'

// create a unique symbol for our type we are defining
export const type = Symbol('Rectangle')

// Create a memory layout descriptor which describes the
// properties of a rectangle
export const memoryLayout = createLayoutBuilder()
	.addUint32('color')
	.addFloat32Vec2('position', { components: ['x', 'y'] })
	.addFloat32('width')
	.addFloat32('height')

	.build()


// Set up an interface which represents a rectangle that matches
// the data of the memory layout defined above
export interface Rectangle {
	// uint32
	color: number

	// float32[2]
	position: [number, number]

	// float32
	width: number

	// float32
	height: number
}

// Create a class implementation using the layout you created earlier
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

```js
import {
	ArrayStore,
	ArrayStoreImpl,
	SlotAllocator,
	ReaderStoreImpl,
} from '@graspologic/memstore'

// Defined above
import { RectangleImpl, memoryLayout } from './rectangle'

export function createStore() {
	// Constructs an underlying ArrayBuffer backed store for storing data
	// in the format of the memoryLayout you created earlier
	const store: ArrayStore = new ArrayStoreImpl(memoryLayout)

	const initialCapacity = 1
	const slotAllocator = new SlotAllocator(initialCapacity)

	// Constructs a store which interacts directly with your implementation you created earlier
	return new ReaderStoreImpl(RectangleImpl, store, slotAllocator)
}
```

Finally we construct our Rectangle store and add instances of it

### Use the store

```js
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

// Add the rectangle to our store
myStore.receive(myRectangle)

// Underlying ArrayBuffer that is storing the rectangle data.
// The data is in the layout we created earlier in the rectangle module
const ab = myStore.store.buffer

// Iterate through all the rectangles in the store
for (const rectangle of myStore) {
	rectangle.width = 200
	rectangle.position = [Math.random() * 100, Math.random() * 100]
}
```

See the [API documentation](./dist/docs/globals.md) or [examples](../../../examples) for additional examples.

---
_Source: https://npm.io/package/@graspologic/memstore · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
