Skip to content

Commit

Permalink
workaround for bug in VueDemi
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Marshall committed Jun 3, 2022
1 parent 486ee45 commit e0ab9a3
Show file tree
Hide file tree
Showing 6 changed files with 2,104 additions and 349 deletions.
3 changes: 1 addition & 2 deletions .gitignore
@@ -1,3 +1,2 @@
node_modules
dist
.idea
.idea
13 changes: 13 additions & 0 deletions dist/index.d.ts
@@ -0,0 +1,13 @@
import { PiniaPluginContext } from 'pinia';
import { Ref } from 'vue-demi';

declare function PiniaLoading({ options, store }: PiniaPluginContext): void;
declare module 'pinia' {
interface PiniaCustomProperties<Id, S, G, A> {
$loading: {
[K in keyof A as A[K] extends () => Promise<any> ? K : never]: Ref<Boolean>;
};
}
}

export { PiniaLoading };
53 changes: 53 additions & 0 deletions dist/index.js
@@ -0,0 +1,53 @@
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/index.ts
var src_exports = {};
__export(src_exports, {
PiniaLoading: () => PiniaLoading
});
module.exports = __toCommonJS(src_exports);
var import_vue_demi = require("vue-demi");
function PiniaLoading({ options, store }) {
if (options.actions) {
const $loading = {};
Object.keys(options.actions).forEach((actionKey) => {
const originAction = options.actions[actionKey];
const action = function(...args) {
const rtn = originAction.apply(this, args);
if (rtn instanceof Promise) {
$loading[actionKey] = (0, import_vue_demi.ref)(false);
return new Promise((resolve, reject) => {
$loading[actionKey].value = true;
rtn.then(resolve).catch(reject).finally(() => {
$loading[actionKey].value = false;
});
});
} else {
return rtn;
}
};
store[actionKey] = action;
});
store.$loading = $loading;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PiniaLoading
});
29 changes: 29 additions & 0 deletions dist/index.mjs
@@ -0,0 +1,29 @@
// src/index.ts
import { ref } from "vue-demi";
function PiniaLoading({ options, store }) {
if (options.actions) {
const $loading = {};
Object.keys(options.actions).forEach((actionKey) => {
const originAction = options.actions[actionKey];
const action = function(...args) {
const rtn = originAction.apply(this, args);
if (rtn instanceof Promise) {
$loading[actionKey] = ref(false);
return new Promise((resolve, reject) => {
$loading[actionKey].value = true;
rtn.then(resolve).catch(reject).finally(() => {
$loading[actionKey].value = false;
});
});
} else {
return rtn;
}
};
store[actionKey] = action;
});
store.$loading = $loading;
}
}
export {
PiniaLoading
};

0 comments on commit e0ab9a3

Please sign in to comment.