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

vue-mobx-finegrained

v1.1.1

Published

Vue 3 composable for a complete MobX state management integration.

Downloads

30

Readme

vue-mobx-finegrained

Vue 3 composable for a complete MobX state management integration. Does not require <Observer> to operate.useMobX() hooks into Vue reactive() object functionality directly creating a shadow object that allows seamless integration between MobX and Vue.

Install

yarn add vue-mobx-finegrained

Usage

// LoginRegisterPresenter.js
import { useMobX } from 'vue-mobx-finegrained'

export class LoginRegisterPresenter {
  
  email = null
  password = null
  option = null

  observables = {
    email: observable,
    password: observable,
    option: observable,
    _awesome: observable,
    awesome: computed,
    reset: action,
    login: action,
    register: action,
    logOut: action
  }
  
  constructor () {
    makeObservable(this, this.observables)
  }
    
  get vm () {
    return useMobX(this, this.observables, { attach: 'vm' })
  }

  async login () {}

  async register () {}

  _awesome = {}

  get awesome () {
    return this._awesome
  }

  set awesome (value) {
    this._awesome = value
  }

  reset () {
    this.email = ''
    this.password = ''
    this.option = 'login'
  }

}

In your component:

<script setup>
   import { LoginRegisterPresenter } from "./LoginRegisterPresenter.js";

   // Instantiate the presenter
   const presenter = new LoginRegisterPresenter()
   
   // Vue shadow ViewModel(vm) reactive() object 
   const vm = presenter.vm
</script>

<template>
   <!-- All the variables are reactive without <Observer> -->
   Email: <input type="text" v-model="vm.email" /><br />
   Password: <input type="text" v-model="vm.email" /><br />
   Option: <input type="text" v-model="vm.option" /><br />
   Awesome: <input type="text" v-model="vm.awesome" /><br />
   <!-- All functions are also available -->
   <button @click="vm.login()">Login</button>
</template>

Features

  • Changing values in MobX(presenter) changes the Vue(vm) state
  • Changing values in Vue(vm) state changes the MobX(presenter)
  • Getters and Setters are supported
  • ES6+ Maps and Sets are supported, see more instructions below*
  • Functions are mirrored but use the Presenter context
  • Deep changes are tracked both ways by MobX and Vue (using deepObserve() in MobX & watch(..., { deep: true }) in Vue)
  • MobX deepObserve() propagates changes to objects & arrays to Vue via atomic changes surgically editing only parts of the object rather than overwriting whole objects in this aspect it makes it superior to current Vue implementation of watch() (Vues' watch() function also runs asynchronously and does not provide atomic changes when objects change, it just gives us the new Object and old Object which are actually the same object because the comparison is done by reference, and if you want to make a diff of the changes that happened, you have to do it manually)
  • *Caveat: All functions in vm are converted to async functions because they have to await for Vue values to update the Presenter before using the inner object property values (this is because Vues' watch() function does not run synchronously) while MobX runs synchronously, so changing the Presenter values immediatedly reflects into the Vue (which is really good to push changes from the Repository to the Presenter down to the Vue's ViewModel).

Handling Maps

// Don't do this, this will lose mutual reactivity of the Map between MobX & Vue
vm.mapObject = new Map([...])

// Do this instead to maintain mutual reactivity between MobX & Vue, 
// this is because (MobX converts Maps to an ObservableMap on initialization)
vm.mapObject = new ObservableMap([...])

// But, note that:
// If you are doing reassignment from the Presenter side
// then doing this is fine:
presenter.mapObject = new Map([...])
// because MobX recognizes the change and converts the Map to an ObservableMap() under the hood

Run Unit Tests with Vitest

This package has 60+ tests to verify the integration between MobX & Vue

yarn test