-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
Helpers:These functions can be imported from the library.
ProvidePlugins can provide utility function to be exposed in the // 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 {};
})); HooksThe 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:
Called every time a store is being accessed by the Example: // my-store-logger.js
export default function logger(provide, { onUse }) {
onUse((name, instance, context) => {
console.log(`Used: "${name}"`);
});
}
Called once per store per app after the first time usage of // my-store-logger.js
export default function logger(provide, { onInitialized }) {
onInitialized((name, instance, context) => {
console.log(`[Initialized] Store name: "${name}"`);
});
}
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}"`);
});
}
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}`);
});
} |
…#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
To be able to persist/dump stores, watch/log changes, etc
The text was updated successfully, but these errors were encountered: