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

lom_atom

v4.0.4

Published

Alternative implementation of eigenmethod mol_atom state management library

Downloads

40

Readme

lom_atom

State management with error handling and reactive cache done right.

Alternative standalone implementation of eigenmethod mol_atom.

  • About 11kb minified
  • Memory-efficient
  • Simpler, less core concept than mobx
  • Loading status / error handling features

Usage examples:

Install npm install --save lom_atom

Observable property

import {mem} from 'lom_atom'
class Todo {
    @mem title = ''
}
const todo = new Todo()
todo.title = '123'

Observable get/set

import {mem} from 'lom_atom'
class Todo {
    @mem set title(next: string) {
        // test next
    }
    @mem get title(): string {
        return 'default'
    }
}
const todo = new Todo()
todo.title = '123'

Computed values

One decorator for all cases.

class TodoList {
    @mem todos = []
    @mem get unfinishedTodos() {
        return this.todos.filter((todo) => !todo.finished)
    }
}

Like mobx, unfinishedTodos is updated automatically when a todo changed.

Side effects

Lom atom memoized property can interact with upstream (server, etc). Each observable or computed property can be used in 4 cases: get, set, cache set, cache reset. Modifiers helps to produce and control side effects for making network requests.

import {mem, addInfo} from 'lom_atom'

class TodoList {
    @mem set todos(todos: Todo | Error) {
        const promise = fetch({
            url: '/todos',
            method: 'POST',
            body: JSON.stringify(todos)
        })

        console.log('set handler')

        throw new addInfo('Loading /todos...', promise)
    }

    @mem get todos(): Todos {
        console.log('get handler')

        throw fetch('/todos')
    }

    @mem.manual get user(): IUser {
        throw fetch('/user')
    }

    set user(next: IUser | Error) {}

    @mem todosWithUser() {
        return {todos: this.todos, user: this.user}
    }

    @mem todosWithUserParallel() {
        return {todos: mem.async(this.todos), user: mem.async(this.user)}
    }
}
const list = new TodoList()
  • this.todos - get value, if cache is empty - invokes get todos and actualize cache.
  • this.todos = data - set value, if cache empty - pass value to set todos() {} and actualize cache.
  • mem.cache(this.todos = data) - set new value or error directly into cache (push).
  • mem.cache(list.todosWithUser) - deep reset cache for todosWithUser all its dependencies (todos) and notify all dependants about changes.
  • @mem.manual get user() {...} - exclude user from deep reset. mem.reset(list.todosWithUser) resets todos but not user. If you want to reset user, use helper directly on user: mem.cache(list.user)
  • mem.async(this.todos) - initiate parallel loading todos and user (wrap error in proxy, do not throw error if data are fetching).
  • throw new Promise(...) - Load data and handle errors and pending State

Key-value

Basic dictionary support. First argument is an key of any type. See eigenmethod mol_mem.

class TodoList {
    @mem.key todo(id: string, next?: Todo): Todo {
        if (next === undefined) {
            // get mode
            return {}
        }

        return next
    }
}
const list = new TodoList()
list.todo('1', {id: 1, title: 'Todo 1'}) // set todo
list.todo('1') // get todo

Actions

Wrapping method in action decorator enables error handling in component event callbacks. Without it, unhandlered exception throws only into console.

State updates are asynchronous, but sometime we need to do transactional synced updates via action helper: @action.sync (Usable for react input, without it cursor position jumps due to asyncronous state updates)

@action.defer runs decorated action on the next tick (Usable for passing valid mounted DOM-node from refs into action in the react).

import {action, mem} from 'lom_atom'
class Some {
    @mem name = ''
    @mem id = ''

    @action set(id: string, name: string) {
        this.id = id
        this.name = name
    }
    @action.sync setSynced(id: string, name: string) {
        this.id = id
        this.name = name
    }
}
const some = new Some()

// View updates on next tick:
some.set('123', 'test')

// View updates in current tick:
some.setSynced('123', 'test2')