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

epic-mobx

v0.5.1

Published

Makes it easy to work with lists of nested stores in MobX.

Downloads

67

Readme

epic-mobx

Makes it easy to work with lists of nested stores in MobX.

  • Automatic store instantiation
  • Remove nested stores without parent reference

Demo npm

Usage

import { makeAutoObservable } from 'mobx'
import { nestable } from 'epic-mobx'

class Item {
  constructor(value: number) {
    this.count = value
    makeAutoObservable(this, {}, { autoBind: true })
  }
}

class Container {
  list = nestable([1, 2], Item)

  constructor() {
    makeAutoObservable(this, {}, { autoBind: true })
  }
}

const Store = new StoreClass()

// Initial values automatically initialized with NestedClass
Store.list[0].count === 1
Store.list[1].count === 2

// Instead of Store.list.push(new NestedClass(3))
Store.list.extend(3)

Store.list[2].count === 3

// Remove individual elements without a reference to the containing store.
Store.list[1].remove()

Methods

Create

nestable(['hello', 'world'], Item)
// Without generic type inferrable from first array argument.
nestable<string, typeof Item>([], Item)
// To avoid specifying both generic types, type the first argument.
nestable([] as string[], Test)

Creates a MobX observable that can later quickly be extended with the store constructor passed in the second argument.

Extend

const myList = nestable([{ color: 'red', speed: 50 }], Item)

myList.extend({ color: 'blue', speed: 100 })
myList.extend({ color: 'green', speed: 75 })

This plugin will create a new instance of the store defined at the start.

Find

const myList = nestable([{ id: '1', color: 'red', speed: 50 }, { id: '2', color: 'green', speed: 25 }], Item)

myList.byId('2').remove()

byId will find the first element in the list with a matching id and return it.

Update

const myList = nestable([{ id: '1', color: 'red', speed: 50 }, { id: '2', color: 'green', speed: 25 }], Item)

myList[1].update({ color: 'blue', speed: 75, newProperty: false })

Using the placeAll() method this will update any passed properties on the list item. When an update method is already present on the item it will have precedence.

Remove

myList[1].remove() // => Item({ color: 'blue', speed: 100 }) removed from the myList list

Removing items this way avoids the need for a refrence to the observable. This is especially useful when .map'ing over a list in React and then removing individual elements in their specific component without access to the list anymore.

import { NestableItem } from 'epic-mobx'

const Item = observer(({ item }: { item: Item & NestableItem }) => (
  <Button onClick={item.remove}>Remove</Button>
))

To ensure the added methods like remove are available in typescript you can add them with the exported NestableItem class.

replaceAll

Use this function instead of observable.replace to replace all items with new instances of the nestable class.

const myNestable = nestable([{ color: 'red', speed: 50 }], Item)

nestable.replaceAll([
  { color: 'blue', speed: 100 },
  { color: 'green', speed: 75 },
])

Structuring Stores

To keep in line with the idea of one class per file the following structure has proven useful.

my-app
├── data
│   ├── index.ts
│   ├── project.ts
│   └── user.ts
├── markup
|   └── Button.tsx
└── index.tsx

Place all MobX Stores inside a folder we now call data and export a single root store with all the nested stores imported from different files accessible by importing the root store instance.

import { makeAutoObservable } from 'mobx'
import { nestable } from 'epic-mobx'
import { Project } from './project'
import { User } from './user'

class Store {
  user = nestable([{ name: 'Jimmy' }], User)
  project = nestable([], Project)

  constructor() {
    makeAutoObservable(this, {}, { autoBind: true })
  }

  randomProject() {
    return this.project[Math.floor(Math.random() * this.project.length) + 1]
  }
}

export const Data = new Store()

Utility Method: placeAll

Use this method to avoid assigning long lists of initial values onto an instance. Make sure to call it before makeAutoObservable.

import { placeAll } from 'epic-mobx'

type ItemInput = { title: string; text: string; date: string }

class Item {
  constructor({ title, text, date }: ItemInput) {
    this.title = title
    this.text = text
    this.date = date
  }
  // =>
  constructor(data: ItemInput) {
    placeAll(this, data)
  }
  // Also works with multiple arguments.
  constructor(...args) {
    placeAll(this, args)
  }
}

Experimental: Usage with Objects

import { makeAutoObservable } from 'mobx'
import { nestableObject } from 'epic-mobx'

const createItem = (count: number) => ({
  count,
})

const createContainer = () => ({
  // Use nestableObject instead of nestable when not using classes.
  list: nestableObject([1, 2], createItem),
})

const store = makeAutoObservable(createContainer(), undefined, { autoBind: true })

// Initial values automatically initialized with NestedClass
store.list[0].count === 1
store.list[1].count === 2

// Instead of store.list.push(new NestedClass(3))
store.list.extend(3)

store.list[2].count === 3

// Remove individual elements without a reference to the containing store.
store.list[1].remove()