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

@nashlabs/expecto

v0.1.0

Published

Manage your loading states (with first-class integration with Vue.js)

Downloads

8

Readme

Expecto

Manage your loading states (with first-class integration with Vue.js)

Install

Wit NPM:

$ npm install @nashlabs/expecto

With Yarn:

$ yarn add @nashlabs/expecto

Usage

With pure JS:

import { expecto } from '@nashlabs/expecto'

// Start a loader (when no argument is passed, '' will be used as the default loader ID)
expecto.startLoading('someLoaderID') // now `expecto.isLoading('someLoaderID')` & `expecto.anyLoading` return `true`

// Stop a loader
expecto.stopLoading('someLoaderID')

// With a callback Expecto will automatically stop the loader
const processedResponse = await expecto.startLoading('someLoaderID', () => { // The cb must return a Promise-like object or it will throw a `TypeError`
  return fetchData().then(response => processData(response))
})

With pure Vue.js:

import Vue from 'vue'
import Vuex from 'vuex'
import { createVuexPlugin, createVuePlugin } from '@nashlabs/expecto'


const store = new Vuex.Store({
  // ...
  plugins: [createVuexPlugin()] // this will register a new namespaced module to manage your loading states. You can pass a different namespace as first argument (default: 'loaders')
})


Vue.use(createVuePlugin({ // This needs vuex plugin to be installed
  namespace, // default: 'loaders' 
  componentName, // default: 'VWait'
  className // an array of CSS classes to apply on component (default: ['expecto'])
}))
// This will add `$isLoading`, `$anyLoading`, `$startLoading` & `$stopLoading` to `Vue.prototype`. Use `createComponent` instead if you don't want `Vue.prototype` to be poluted

Then in your components:

// VWait Component (needs vuex plugin to be installed)
<VWait :when="/* same arguments as expecto.isLoading or directly pass true/false */" :empty="true">
  { slot content }

  <template v-slot:emtpy>
    This will be displayed <strong>in place of the default slot</strong> only if you pass `true` to the `:empty` prop.
    It can be helpful to display a 'No data' component after loading.
  </template>
</VWait>

This is what you need to know about the module that is registered in the store when you call createVuexPlugin

store.registerModule(namespace, {
  // ...
  getters: {
    isLoading : state => ..., // same thing as calling expecto.isLoading(...)
    anyLoading: state => ..., // same thing as calling expecto.anyLoading
  },
  actions: {
    startLoading: ({commit}, { loaderId, callback }) => ..., // same thing as calling expecto.startLoading(loaderId, callback)
    stopLoading: ({commit}, { loaderId }) => ..., // same thing as calling expecto.stopLoading(loaderId)
  },
  // ...
})

Tips

Tip 1: isLoading can take a function as an argument instead of a loaderId.

In that case, it will behave exactly like the function argument passed to Array.prototype.findIndex. Each currently loading loaderId is be passed to the function until it returns true and the loop stops. isLoading will return true only if the function returned true for at least one id.

expecto.isLoading(id => /^UID-.*/.test) // returns `true` if one id starts with 'UID-'

Tip 2: Need to call vuex manually ?

$store.dispatch(`<vuex module name>/startLoading`, { loaderId, callback }, { root: true })

Tip 3: The class used under the hood (ExpectoWrapper) is also exposed.

By creating new instances of it, you can have different and isolated loading state managers.

$store.dispatch(`<vuex module name>/startLoading`, { loaderId, callback }, { root: true })

License

MIT © Honoré Nintunze