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-vuex-promise-store

v1.0.5

Published

Wraps promises to save resolved data to vuex store.

Downloads

37

Readme

vue-vuex-promise-store

Version License Build Status CircleCI Status Coverage Status Code Climate Status codecov Dependency Status devDependency Status peerDependency Status js-standard-style

Wraps promises to save resolved data to vuex store.

Usage

import PromiseStore from 'vue-vuex-promise-store'

const store = new Vuex.Store({
  plugins: [PromiseStore.plugin({
    moduleName: 'promise'
  })]
})

const uniqueKey = 'unique key is required'

// first time for the unique key
async function initialInvocation() {
  const context = store.getters['promise/init'](uniqueKey, (resolve) => {
    console.log('first')

    setTimeout(() => {
      console.log('third')
      resolve(1)
    }, 0)
  })
    .thenSync((value) => {
      console.log('fourth', value) // 1
      
      return 'hello'
    })
    .thenSync((value) => {
      console.log('fifth', value) // 'hello'
      
      return 'world'
    })
    .thenSync((value) => {
      console.log('sixth', value) // 'world'
      
      return '!'
    })

  console.log('second')

  // context.promise is a Promise
  const last = await context.promise

  console.log('seventh', last) // '!'
}

await initialInvocation()

async function secondInvocation() {
  // second time for the unique key
  const context = store.getters['promise/init'](uniqueKey, (resolve => {
    // a result for the `uniqueKey` is already in the store.
    // this promise executor is not invoked,
    // and following thenSync()s are executed synchronously.
  }))
    .thenSync((value) => {
      console.log('first', value) // 1
      
      return 'HELLO'
    })

  console.log('second')

  const last = await p.promise

  console.log('third', last) // 'HELLO'
}

Usage (resolve, reject, wrap)

import PromiseStore, { resolve, reject, wrap } from 'vue-vuex-promise-store'

const store = new Vuex.Store({
  plugins: [PromiseStore.plugin()]
})

let cache = null

function fetchRemoteData (useCache = false, ignoreStore = false) {
  if (useCache && cache) {
    return cache.status ? resolve(cache.value) : reject(cache.value)
  }
  
  const cb = resolve => resolve(fetch('http://example.com/path/to/remoteData'))

  if (ignoreStore) return wrap(cb)
  
  return store.getters['promise/init']('remoteData', cb)
    .thenSync((value) => {
      cache = { value, status: true }
      
      return value
    }, (reason) => {
      cache = { status: false, value: reason }
      
      throw reason
    })
}

fetchRemoteData().thenSync((value) => {
  // ...
}, (reason) => {
  // ...
})

API

Types

type OnFulfilledSync<T, U> = (value: T) => U
type OnRejectedSync<T> = (reason: Error) => T

type CatchSync<T> = (onRejected: OnRejectedSync<T>) => Context<T>
type ThenSync<T> = (onFulfilled: OnFulfilled<T>, onRejected?: OnRejected<T>) => Context<T>

type Context<T> = {
  isFulfilled: boolean, // true if or when the promise is fulfilled
  isPending: boolean, // ditto but is nether fulfilled nor rejected
  isRejected: boolean, // ditto but is rejected
  promise: Promise<T> | void,
  reason: Error | void, // the result of the rejected promise
  value: T | void, // the result of the fulfilled promise

  catchSync<U>: CatchSync<U> | void
  thenSync<U>: ThenSync<U> | void
}

type Resolve<T> = (value: T) => void
type Reject = (reason: Error) => void
type Executor<T> = (resolve: Resolve<T>, reject?: Reject) => void
type PromiseOrExecutor<T> = Promise<T> | Executor<T>

type ContextOptions = {
  refresh?: boolean
}

type PluginOptions = {
  moduleName?: string
}

type PluginInstaller = (store: Vuex.Store) => void

Exports

  • MODULE_NAME: string
    • is the default module name (is 'promise').
  • VERSION: string
    • is the version number (like '1.0.0').
  • plugin: (options?: PluginOptions) => PluginInstaller
    • returns a plugin installer function with given options.
  • reject: (reason: Error) => Context<void>
  • resolve: (value: T) => Context<T>
  • wrap: (promise: Promise<T>) => Context<T>
    • returns a new Context object without stores.

State

  • contexts: { [string]: Context }
    • Context objects.
  • disabled: boolean
    • true if store binding is disabled.
    • @see disable() action

Actions

  • enable() => Promise<void>
    • enables the store binding.
  • disable() => Promise<void>
    • disables the store binding.
    • After disabling the store binding, Context objects are not stored in state.contexts.
  • finalize() => Promise<void>
    • resolves all Context objects in state.contexts, then set promise catchSync thenSync to undefined.
    • Use this action to serialize state.
  • resolveAll() => Promise<void>
    • resolves all pending Context objects.

Getters

  • hasPendingPromises: boolean
    • true if any pending Context objects are in state.contexts.
  • init: (key: string, promiseOrExecutor: PromiseOrExecutor<T>, options?: ContextOptions) => Context<T>
    • is a function creates a new Context object, stores it in state.contexts with a given key if the store binding is not disabled, and finally returns it.
    • If a given key exists in state.contexts then returns it (instead of creating and storing a new object).
  • pendingPromises: Array<Context>
    • is an array of pending Context objects in state.contexts.