Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A more useful plugin API #1

Closed
samzlab opened this issue Oct 4, 2020 · 1 comment
Closed

A more useful plugin API #1

samzlab opened this issue Oct 4, 2020 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@samzlab
Copy link
Owner

samzlab commented Oct 4, 2020

To be able to persist/dump stores, watch/log changes, etc

@samzlab samzlab added the enhancement New feature or request label Oct 4, 2020
@samzlab samzlab self-assigned this Oct 4, 2020
@samzlab
Copy link
Owner Author

samzlab commented Oct 5, 2020

Plugins was not explained in the video, so outside of the provide function so the hooks will be different in Vuex 5

Helpers:

These functions can be imported from the library.

getStoreState(storeInstance) => {[key: string]: Proxy|Ref}: returns all of the watchable Proxy or Ref objects in the store instance

getStoreActions(storeInstance) => {[key: string]: Function}: returns all the callable function on the store instance.

getCurrentStoreInstance() => storeInstance: returns the currently initializing store instance. Only works during the store initialization.

Provide

Plugins can provide utility function to be exposed in the storeContext. These functions are passed during the store initialization:

// api-plugin.js
export default (provide) => {
    provide('api', (uri) => fetch(`https://localhost:5000/${uri}`));
}

// app.js
import apiPlugin from './api-plugin.js';
const vueStore = createVueStore({
    plugins: [ apiPlugin ]
});

// my-store.js
export default defineStore(('my-store', ({ api }) => {
    // setup logic
    return {};
}));

Hooks

The usable hooks are passed in the second argument of the plugin.

export default function myPlugin(provide, hooks) => { /* plugin code */};

Arguments passed to the callback functions:

  • The storeInstance contains the exact object which is returned from the initializer function passed to defineStore().
  • The context contains all the provided functions by plugins.

onUse(callback: (storeName, storeInstance, context) => void)

Called every time a store is being accessed by the useStore() or composed via use().

Example:

// my-store-logger.js
export default function logger(provide, { onUse }) {
    onUse((name, instance, context) => {
        console.log(`Used: "${name}"`);
    });
}

onInitialized(callback: (storeName, storeInstance, context) => void)

Called once per store per app after the first time usage of useStore() or composed via use()

// my-store-logger.js
export default function logger(provide, { onInitialized }) {
    onInitialized((name, instance, context) => {
        console.log(`[Initialized] Store name: "${name}"`);
    });
}

onAction(callback: (storeName, storeInstance, actionName, context) => void)

Called after a store function invoked.

// my-store-logger.js
export default function logger(provide, { onAction }) {
    onAction((name, instance, action, context) => {
        console.log(`[Action invoked] Store action: "${name}.${action}"`);
    });
}

onMutated(callback: (storeName, storeInstance, stateKey, oldValue, context) => void)

Called when any watchable store property mutated (the value changed).

// my-store-logger.js
export default function logger(provide, { onMutated }) {
    onMutated((name, instance, key, newValue, oldValue, context) => {
        const oldJSON = JSON.encode(oldValue);
        const newJSON = JSON.encode(newValue);
        console.log(`[State mutated] "${name}.${key}" is being mutated from ${oldJSON} to ${newJSON}`);
    });
}

samzlab added a commit that referenced this issue Oct 6, 2020
…#1

commit 72a0e8dd4f17fb1ab2aa37d4a30a513d1ab994ad
Merge: a85ba85 9d8e476
Author: Kövesdi György <[email protected]>
Date:   Tue Oct 6 11:24:33 2020 +0200

    Merge branch 'feature/plugin-hooks' into develop

commit 9d8e47601bf6f1629d3319cdc00bf04fbe77a322
Author: Kövesdi György <[email protected]>
Date:   Tue Oct 6 11:24:04 2020 +0200

    update docs

commit 628d8b380648b6ea9b73737a0ba00907ab1cec50
Author: Kövesdi György <[email protected]>
Date:   Tue Oct 6 11:23:50 2020 +0200

    onMutate hook, cleanup

commit 1451b9530e04cfc642674b1d05e932cabefe4c82
Author: Kövesdi György <[email protected]>
Date:   Tue Oct 6 09:43:38 2020 +0200

    test fix

commit d396b2498184df8dd4dc629605afe6bfb4cc77c4
Author: Kövesdi György <[email protected]>
Date:   Tue Oct 6 09:41:40 2020 +0200

    WIP: keep types in wrapper

commit cc41b5e
Author: Kövesdi György <[email protected]>
Date:   Mon Oct 5 21:30:36 2020 +0200

    WIP - onInitialized, onUse, onAction implemented/tested

commit 61e4998
Author: Kövesdi György <[email protected]>
Date:   Mon Oct 5 18:43:57 2020 +0200

    WIP
@samzlab samzlab closed this as completed Oct 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant