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

vuex-scaffold

v0.2.0

Published

Scaffolding methods to compose your vuex store

Downloads

4

Readme

vuex-scaffold

Scaffolding methods to compose your Vuex store

createAction

(
  type: string,
  payloadCreator?: Function,
  metaCreator?: Function,
) => (
  ...args
) => {
  type: string,
  payload: Object,
  meta: Object,
}

Aside from the standard dispatch(type, payload), Vuex also supports an action object, i.e. dispatch({ type: 'foo' }). A common pattern is to use an action creator to generate the action object.

createAction allows you to compose this action creator.

const fetch = createAction(
  'fetch',
  (id) => ({ id }),
  (id, resolve, reject) => ({ resolve, reject }),
);

// then:
const action = fetch(4, res, rej);
store.dispatch(action);

createPayload

(
  keys: Array<string>,
  offset?: number,
) => (
  ...values
) => Object

A helper that creates a payload object from an array of keys. Use it in conjunction with createAction.

const fetch = createAction(
  'fetch',
  createPayload([ 'id' ]),
  createPayload([ 'resolve', 'reject' ], 1),
);

fetch(4, res, rej)

createDispatch

(
  type: string,
  payloadCreator?: Function,
  metaCreator?: Function,
  store?: Object,
) => (...args) => Promise

Similar to createAction expect it also expects a store object. When you all the action it will automatically dispatch it as well.

const fetch = createDispatch('fetch', (x) => x, store);

fetch('x').then(...);

Store is a curried parameter, meaning if you don't pass it in, it will return a function that takes the store. Meaning you can do something like this:

const fetch = createDispatch('fetch');

// later
const dispatcher = fetch(store);
// later still
dispatcher().then(...);

createCommit

(
  type: string,
  payloadCreator?: Function,
  metaCreator?: Function,
  store?: Object,
) => (...args) => void

Similar to createDispatch but for triggering a commit.

combineActions

(...actions: Array<Object>) => Object

Takes a number of objects that contain actions and combines them. Any same-keyed actions are run together.

const actions = combineActions(
  {
    fetch: (context, payload) => { /* ... */ },
  },
  {
    fetch: (context, payload) => { /* ... */ },
  },
);

// later
store.dispatch('fetch');
// will dispatch both fetch actions

combineActionArray

(actions: Array<Object>) => Object

An array version of combineActions.

combineMutations

(...mutations: Array<Object>) => Object

Takes a number of objects that contain mutations and combines them. Any same-keyed mutations are run together.

const mutations = combineMutations(
  {
    FETCH: (state, payload) => { /* ... */ },
  },
  {
    FETCH: (state, payload) => { /* ... */ },
  },
);

// later
store.dispatch('FETCH');
// will dispatch both fetch mutations

combineMutationArray

(mutations: Array<Object) => Object

An array version of combineMutations

combineModules

(...modules: Array<Object>) => Object

Takes a number of modules and combines them.

combineModuleArray

(modules: Array<Object>) => Object

An array version of combineModules

mapToCommit

(key: string) => Function

Takes the key of a mutation and automatically commits it when the specified action is caled.

const actions = {
  fetch: mapToCommit('FETCH'),
};

filter

(
  predicate: (...args) => boolean,
  fn: (...args) => any,
) => Function

wraps an action or commit in a function that only executes when the given predicate passes.

const actions = {
  fetch: filter(
    (context, payload) => payload.key === 4,
    (context, payload) => { /* ... */ },
  ),
};

Filter is also curried, meaning you can create helper functions:

const onlyForKey = filter((context, payload) => payload.key === 4);

const actions = {
  fetch: onlyForKey(() => { /* ... */ }),
};

dispatchCommits

(module: Object) => Object

takes all mutations in a module and creates an action for each one.

const module = dispatchCommits({
  mutations: {
    fetch: (state, payload) => { /* ... */ },
  },
});

store.dispatch('fetch'); // will trigger the fetch mutation

This means you no longer have to differentiate between your actions and mutations. It also means you can isolate your action as you don't need to know what is a mutation or an action.

const mutations = {
  submitting: () => { /* ... */ },
  submitted: () => { /* ... */ },
};
const actions = {
  doSomething: () => { /* ... */ },
  submitted: () => { /* ... */ },
  submit: async ({ commit, dispatch }) => {
    commit('submitting');
    await dispatch('doSomething');
    commit('submitted');
    await dispatch('submitted');
  },
};

In the above example, the submit action has to be aware of the fact that submitting is a mutation, doSomething is an action, and submitted is both an action and a mutation.

It would be much better if your action could dispatch actions and not have to be aware of the context. Using dispatchCommits, submit would look something like:

({ commit, dispatch }) => {
  dispatch('submitting');
  dispatch('doSomething');
  dispatch('submitted');
}