Skip to content

Commit

Permalink
[IMP] Now you can access Vuert instance from globally across the app.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Bilotta committed Jun 11, 2023
1 parent 06fc714 commit 544267f
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 319 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docs-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ jobs:
run: yarn ci
- name: Coding standard check
run: yarn lint:prod
# - name: Types integrity check
# run: yarn check:types
- name: Types integrity check
run: yarn check:types
- name: Source code build
run: yarn build:docs
- name: Artifact upload
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/template-build-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ jobs:
run: yarn ci
- name: Coding standard check
run: yarn lint:prod
# - name: Types integrity check
# run: yarn check:types
- name: Types integrity check
run: yarn check:types
- name: Source code build
run: yarn build:core
- name: Artifact creation
Expand Down
2 changes: 1 addition & 1 deletion docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineConfig({
{ text: "Guide", link: "/guide/" },
{ text: "Configs", link: "/config/" },
{
text: "1.0.1",
text: "1.0.2",
items: [{ text: "Releases", link: `${REPO_HOME}/releases` }]
}
],
Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@byloth/vuert",
"version": "1.0.1",
"version": "1.0.2",
"description": "The headless alerts, notifications & popups library for Vue.js craftsmen. ℹ",
"keywords": [
"Alert",
Expand Down Expand Up @@ -54,28 +54,28 @@
"build:docs": "vitepress build docs",
"preview": "vitepress serve docs",
"check:types": "vue-tsc",
"lint": "eslint --ext .js,.json,.ts,.vue --ignore-path .gitignore .",
"lint": "eslint --ext .cjs,.js,.json,.ts,.vue --ignore-path .gitignore .",
"lint:prod": "export NODE_ENV=\"production\" && yarn lint",
"ci": "yarn install --frozen-lockfile"
},
"dependencies": {
"@byloth/exceptions": "^2.0.2",
"@byloth/exceptions": "^2.0.3",
"vue": "^3.3.4"
},
"devDependencies": {
"@byloth/eslint-config-typescript": "^2.5.2",
"@byloth/eslint-config-vue": "^2.5.2",
"@fortawesome/fontawesome-free": "^6.4.0",
"@typescript-eslint/eslint-plugin": "^5.59.7",
"@typescript-eslint/parser": "^5.59.7",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"@vitejs/plugin-vue": "^4.2.3",
"eslint": "^8.41.0",
"eslint": "^8.42.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-vue": "^9.14.0",
"sass": "^1.62.1",
"typescript": "^5.0.4",
"vite": "^4.3.8",
"vitepress": "^1.0.0-beta.1",
"eslint-plugin-vue": "^9.14.1",
"sass": "^1.63.3",
"typescript": "^5.1.3",
"vite": "^4.3.9",
"vitepress": "^1.0.0-beta.2",
"vue-eslint-parser": "^9.3.0",
"vue-tsc": "^1.6.5"
}
Expand Down
8 changes: 4 additions & 4 deletions src/components/AlertHandler.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@
});
const emit = defineEmits({
opening: <R>(alert: Alert<R>) => alert instanceof Alert<R>,
opened: <R>(alert: Alert<R>) => alert instanceof Alert<R>,
closing: <R>(alert: Alert<R>) => alert instanceof Alert<R>,
closed: <R>(alert: Alert<R>) => alert instanceof Alert<R>
opening: <R>(alert: Alert<R>) => (alert instanceof Alert),
opened: <R>(alert: Alert<R>) => (alert instanceof Alert),
closing: <R>(alert: Alert<R>) => (alert instanceof Alert),
closed: <R>(alert: Alert<R>) => (alert instanceof Alert)
});
const contexts: Context<any>[] = [];
Expand Down
24 changes: 0 additions & 24 deletions src/exceptions.ts

This file was deleted.

28 changes: 21 additions & 7 deletions src/functions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { inject } from "vue";
import { inject, getCurrentScope } from "vue";
import type { App, ComponentPublicInstance, Plugin } from "vue";

import { RuntimeException } from "@byloth/exceptions";
import { handle, RuntimeException } from "@byloth/exceptions";

import { InjectionKeys } from "./core.js";
import { handle } from "./helpers.js";

import Vuert from "./vuert.js";
import type { VuertOptions } from "./vuert.js";
Expand All @@ -23,6 +22,19 @@ interface DisabledErrorsHandling

type ErrorsHandling = EnabledErrorsHandling | DisabledErrorsHandling;

let _activeVuert: Vuert | undefined = undefined;

const _setActiveVuert = (vuert: Vuert): void => { _activeVuert = vuert; };
const _getActiveVuert = (): Vuert | undefined =>
{
if (getCurrentScope())
{
return inject(InjectionKeys.$vuert);
}

return _activeVuert;
};

export type PluginOptions = Partial<VuertOptions> & ErrorsHandling;
export const createVuert = (options?: PluginOptions): Plugin =>
{
Expand All @@ -31,14 +43,16 @@ export const createVuert = (options?: PluginOptions): Plugin =>
{
const $vuert = new Vuert(options);

_setActiveVuert($vuert);

config.globalProperties.$vuert = $vuert;
provide(InjectionKeys.$vuert, $vuert);

if (options?.enableErrorsHandling)
{
config.errorHandler = (error: unknown, instance: ComponentPublicInstance | null, info: string) =>
{
handle($vuert, error, (exc) =>
handle(error, (exc) =>
{
// eslint-disable-next-line no-console
console.error(exc);
Expand All @@ -49,19 +63,19 @@ export const createVuert = (options?: PluginOptions): Plugin =>

window.addEventListener("unhandledrejection", ({ reason }: PromiseRejectionEvent) =>
{
handle($vuert, reason, () => $vuert.emit(options.defaultAlertOptions));
handle(reason, () => $vuert.emit(options.defaultAlertOptions));
});
}
}
};
};
export const useVuert = (): Vuert =>
{
const $vuert = inject(InjectionKeys.$vuert);
const $vuert = _getActiveVuert();
if (!$vuert)
{
throw new RuntimeException(
"`useVuert` was called with no active instance. " +
"`useVuert()` was called but there was not active Vuert. " +
"Did you forget to install `Vuert` plugin in your App?"
);
}
Expand Down
27 changes: 0 additions & 27 deletions src/helpers.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import AlertHandler from "./components/AlertHandler.vue";
import Vuert from "./vuert.js";

export { AlertInterrupt } from "./exceptions.js";
export { createVuert, useVuert } from "./functions.js";
export { handle } from "./helpers.js";
export { Action, Alert, Context } from "./models/index.js";

export type { VuertOptions } from "./vuert.js";
Expand Down
9 changes: 9 additions & 0 deletions src/types/vue.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module "*.vue"
{
import type { DefineComponent } from "vue";

// eslint-disable-next-line @typescript-eslint/ban-types
const Component: DefineComponent<{ }, { }, any>;

export default Component;
}
2 changes: 1 addition & 1 deletion src/vuert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface VuertOptions

export default class Vuert
{
public static readonly VERSION: string = "1.0.1";
public static readonly VERSION: string = "1.0.2";

public static get DEFAULT_OPTS(): VuertOptions
{
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@
"src/**/*",
"src/**/*.json",
"src/**/*.vue"
],
"exclude": [
"dist",
"node_modules"
]
}
Loading

0 comments on commit 544267f

Please sign in to comment.