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

mobx-log

v2.2.3

Published

[![Npm Version](https://badge.fury.io/js/mobx-log.svg)](https://badge.fury.io/js/mobx-log) [![NPM downloads](http://img.shields.io/npm/dm/mobx-log.svg)](https://www.npmjs.com/package/mobx-log) [![Tests](https://github.com/kubk/mobx-log/actions/workflows/m

Downloads

12,259

Readme

mobx-log

Npm Version NPM downloads Tests code style: prettier

Logger + Redux devtools for Mobx 6+. Works only in dev mode. screenshot

Installation

npm i mobx-log

Usage with Redux Devtools

  1. Make sure Redux Devtools are installed
  2. Add makeLoggable to each store you'd like to debug:
import { makeLoggable } from 'mobx-log'

class SomeStore {
  ...

  constructor() {
    makeAutoObservable(this)
+   makeLoggable(this)
  }
}

Usage with browser logger

  1. Add makeLoggable to a store:
class SomeStore {
  ...

  constructor() {
    makeAutoObservable(this)
+   makeLoggable(this)
  }
}
  1. In Chrome DevTools, press F1 to load the Settings. Scroll down to the Console section and check "Enable custom formatters":

alt text

You'll only need to do it once.

  1. If you have Redux devtools installed then you need to deactivate it. The mobx-log package tries to detect automatically what you'd like to use - browser console or Redux devtools. Installed Redux devtools take precedence over browser console.

Use only browser formatters

mobx-log uses custom Chrome formatters, so you won't see awkward [Proxy, Proxy] in your console anymore. All your console.log(store) calls will be nicely formatted. If it is the only feature you need from this package, you can use just formatters:

import { applyFormatters } from 'mobx-log';

applyFormatters();

Make sure this function is called at the very top of your code

Customize

Access stores as global variables in browser console.

By default, all the stores marked as loggable become accessible as global variables. Example:

class AuthStore {
  constructor() {
    makeLoggable(this);
  }
}

Then you can type store.authStore in your browser console. Feel free to log store, call actions and computeds in the console. Works only in dev mode.

If you'd like to disable this feature use this:

import { configureLogger } from 'mobx-log';

configureLogger({
  storeConsoleAccess: false,
});

Make sure this function is called at the very top of your code

Log observables / computeds / actions conditionally

An example how to log only actions and computeds:

import { configureLogger } from 'mobx-log';

configureLogger({
  filters: {
    computeds: true,
    actions: true,
    observables: false,
  },
});

Log observables / computeds / actions of a specific store.

An example how to log only changes in computeds of a store Counter.

import { makeLoggable } from 'mobx-log';

class Counter {
  value = 0;

  constructor() {
    makeAutoObservable(this);
    makeLoggable(this, {
      filters: {
        events: {
          computeds: true,
          observables: false,
          actions: false,
        },
      },
    });
  }
}

Customize logger output

Example - add time for each log entry:

import { configureLogger, DefaultLogger, LogWriter } from 'mobx-log';

export const now = () => {
  const time = new Date();
  const h = time.getHours().toString().padStart(2, '0');
  const m = time.getMinutes().toString().padStart(2, '0');
  const s = time.getSeconds().toString().padStart(2, '0');
  const ms = time.getMilliseconds().toString().padStart(3, '0');

  return `${h}:${m}:${s}.${ms}`;
};

class MyLogWriter implements LogWriter {
  write(...messages: unknown[]) {
    console.log(now(), ...messages);
  }
}

configureLogger({
  logger: new DefaultLogger(new MyLogWriter()),
});

Usage with factory functions

With Mobx 6 you can create stores without classes using makeAutoObservable / makeObservable:

export const createDoubler = () => {
  return makeAutoObservable({
    value: 0,
    get double() {
      return this.value * 2;
    },
    increment() {
      this.value++;
    },
  });
};

You can also log such stores using makeLoggable:

export const createDoubler = () => {
  return makeLoggable(
    makeAutoObservable({
      loggableName: 'doubler', // <-- Required
      value: 0,
      get double() {
        return this.value * 2;
      },
      increment() {
        this.value++;
      },
    })
  );
};

The store also become available in console if you turn on storeConsoleAccess option.

Usage with useLocalObservable

Before:

import { useLocalObservable } from 'mobx-react-lite'

...

const App = observer(() => {
  const counterStore = useLocalObservable(() => {
    count: 0,
    increment: () => this.count++,
  })

  return ...
})

After:

import { useMakeLoggable } from 'mobx-log'

...

const App = observer(() => {
  const counterStore = useLocalObservable(() => {
    count: 0,
    increment: () => this.count++,
  })
  useMakeLoggable(counterStore, 'counter')

  return ...
})

The store also become available in console if you turn on storeConsoleAccess option.

How it is different from alternatives?

Example project

This library has example project located in ./example folder. It is used for development purposes. To run it go to ./example folder and run npm run start.

Store destructuring

Prior to Mobx 6.3.4 bound actions didn't appear in the log. If you are using store destructuring and want your actions appear in the log please update your Mobx to 6.3.4. See the discussion for details: https://github.com/mobxjs/mobx/discussions/3140