UNPKG

pinia-class-component

Version:

ECMAScript / TypeScript decorator for class-style Pinia store.

6 lines 1.2 kB
/** * pinia-class-component v0.9.4 * (c) 2022-present PentaTea * @license MIT */ import{defineStore}from"pinia";function Store(t){return"function"==typeof t?storeFactory(t):function(e){return storeFactory(e,t)}}var $internalHooks=[];function storeFactory(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};e.name=e.name||e.id||t.name,e.state||(e.state={}),e.actions||(e.actions={}),e.getters||(e.getters={});var r=t.prototype;Object.getOwnPropertyNames(r).forEach(function(t){if("constructor"!==t)if($internalHooks.indexOf(t)>-1)e[t]=r[t];else{var o=Object.getOwnPropertyDescriptor(r,t);void 0!==o.value?"function"==typeof o.value?e.actions[t]=o.value:e.state[t]=o.value:o.get&&(e.getters[t]=o.get)}});var o=new t;Object.keys(o).forEach(t=>{e.state[t]=o[t]});var n=t.__decorators__;n&&(n.forEach(t=>t(e)),delete t.__decorators__);var a=e.state;e.state=(()=>a);var c=defineStore(e.name,e);return function(){return c()}}function createDecorator(t){return(e,r,o)=>{var n="function"==typeof e?e:e.constructor;n.__decorators__||(n.__decorators__=[]),"number"!=typeof o&&(o=void 0),n.__decorators__.push(e=>t(e,r,o))}}var Pinia=function(){};export{Pinia,Store,createDecorator};