npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@muds/store

v0.1.1

Published

Core data structure store library for muds

Downloads

7

Readme

@muds/store

Overview

@muds/store is the core data structure store library for muds, the modular microframework for interactive data-oriented systems. This package is what enables muds to be data-oriented. It provides useful data structures to model your data store and structure your app in Entity component system (ECS) architecture.

API Documentation

http://andykswong.github.io/muds

Usage

npm install --save @muds/store

@muds/store is best used with an ECS architecture. IdGenerator can be used to generate entity IDs, and component data can be stored in maps:

import { IdGenerator, SparseSetMap, uniqueJoin } from '@muds/store';

// Entities are simply IDs
const entityIdGenerator = new IdGenerator();

// Components are stored in maps
// SparseSetMap is good for often used components. for rare components, use GenIdMap instead
const positions = new SparseSetMap();
const velocities = new SparseSetMap();

// Systems are simply functions that operates on the data model (entity and components)
function PhysicsSystem(deltaTimeSec) {
  // Use uniqueJoin(iter, keyFn, ...maps) to perform multi-component left-join queries
  const query = uniqueJoin(velocities.entries(), ([id, vel] => id), positions);

  for ([entityId, vel, pos = [0, 0]] of query) {
    const newPos = [pos[0] + vel[0] * deltaTimeSec, pos[1] + vel[1] * deltaTimeSec];
    positions.set(entityId, newPos);
  }
}

// Create some entities
const entity1 = entityIdGenerator.add();
positions.set(entity1, [10, 10]);
velocities.set(entity1, [1, 2]);
const entity2 = entityIdGenerator.add();
// position of entity2 defaults to [0, 0]
velocities.set(entity2, [3, 4]);

PhysicsSystem(1); // Run the system
// Current positions = { [entity1]: [11, 12], [entity2]: [3, 4] }

Observable collections are available for reacting on collection change events. There is also a UniqueIndex type that automatically builds a unique key index by listening to an observable map:

import { GenIdMap, IdGenerator, ObservableMap, UniqueIndex, onCollectionChange } from '@muds/store';

const entityIdGenerator = new IdGenerator();
const components = new ObservableMap(new GenIdMap());

// You can listen to components.onAdd/onClear/onDelete/onUpdate events
components.onAdd.addListener((components, entityId, value) => console.log(`added component key = ${entityId}, value = ${component}`));

const onChange = onCollectionChange(components); // To listen to all collection event types at once
onChange.addListener((eventType, components, entityId, value, oldValue) => ...);

// Create a reverse mapping index from component value to entityId
const index = UniqueIndex.fromCollection(components, (entityId, value) => value);

const entity1 = entityIdGenerator.add();
components.set(entity1, 1); // This triggers components.onAdd, onChange events and updates index

console.assert(entity1 === index.get(1)); // true

License

This repository and the code inside it is licensed under the MIT License. Read LICENSE for more information.