Skip to content

Commit

Permalink
wip: Improving CustomAlert functionalities...
Browse files Browse the repository at this point in the history
  • Loading branch information
Byloth committed Feb 23, 2024
1 parent e951f73 commit 8397d25
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 53 deletions.
12 changes: 7 additions & 5 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@
"dist",
"src"
],
"main": "./dist/vuert.umd.cjs",
"module": "./dist/vuert.js",
"main": "dist/vuert.umd.cjs",
"module": "dist/vuert.cjs.js",
"unpkg": "dist/vuert.global.js",
"jsdelivr": "dist/vuert.global.js",
"exports": {
".": {
"import": "./dist/vuert.js",
"require": "./dist/vuert.umd.cjs",
"import": "./dist/vuert.esm.js",
"require": "./dist/vuert.cjs.js",
"types": "./src/index.ts"
}
},
"types": "./src/index.ts",
"types": "src/index.ts",
"scripts": {
"build": "vite build",
"build:types": "vue-tsc --build --emitDeclarationOnly --force",
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/components/AlertHandler.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<Component :is="is">
<slot v-if="context"
:alert="context.alert"
:custom-component="context.component"
:is-open="context.isOpen.value"
:resolve="context.resolve"
:reject="context.reject"></slot>
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export { Action, Alert, Context } from "./models/index.js";

export type { VuertOptions, VuertSubscriber } from "./vuert.js";

export type { IAction, ActionCallback, ActionOptions } from "./types/action.js";
export type { IAction, ActionCallback, ActionOptions } from "./types/action/index.js";
export type { CallbackAction } from "./types/action/callback.js";
export type { ValueAction } from "./types/action/value.js";

export type { IAlert, AlertOptions } from "./types/alert/index.js";
export type { SimpleAlert, BlockingAlert, DismissibleAlert } from "./types/alert/simple.js";
export type { CustomAlert, BlockingCustomAlert, DismissibleCustomAlert } from "./types/alert/custom.js";
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/models/action.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IAction, ActionCallback, ActionOptions } from "../types/action.js";
import type { IAction, ActionCallback, ActionOptions } from "../types/action/index.js";

export default class Action<R = void> implements IAction<R>
{
Expand All @@ -18,6 +18,6 @@ export default class Action<R = void> implements IAction<R>
this.icon = options.icon;
this.label = options.label;

this.callback = options.callback ?? (() => undefined);
this.callback = options.callback ?? (() => options.value);
}
}
17 changes: 5 additions & 12 deletions packages/core/src/models/alert.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import type { Component } from "vue";

import { ValueException } from "@byloth/exceptions";

import type { Props } from "../types/core.js";
import type { IAlert, AlertOptions } from "../types/alert/index.js";

import Action from "./action.js";

export default class Alert<R = void> implements IAlert<R>
export default class Alert<R = void, P extends Record<string, unknown> = never> implements IAlert<R, P>
{
public readonly id: symbol;

Expand All @@ -18,16 +15,14 @@ export default class Alert<R = void> implements IAlert<R>
public readonly title?: string;

public readonly message?: string;

public readonly component?: Component;
public readonly props?: Props;
public readonly payload?: P;

public readonly actions: Action<R>[];
public readonly dismissible: boolean;

public readonly dismissible: boolean;
public readonly timeout: number;

public constructor(options: AlertOptions<R>, cause?: unknown)
public constructor(options: AlertOptions<R, P>)
{
this.id = options.id ?? Symbol();

Expand All @@ -44,9 +39,7 @@ export default class Alert<R = void> implements IAlert<R>
}

this.message = options.message;

this.component = options.component;
this.props = options.props;
this.payload = options.payload;

this.actions = options.actions?.map((a) => new Action(a)) ?? [];

Expand Down
15 changes: 9 additions & 6 deletions packages/core/src/models/context.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { computed, ref } from "vue";
import type { ComputedRef, Ref } from "vue";
import type { Component, ComputedRef, Ref } from "vue";

import { delay, DeferredPromise, Subscribers } from "@byloth/core";
import type { MaybePromise, Timeout } from "@byloth/core";

import type { Duration } from "../types/index.js";
import type { ActionCallback } from "../types/action.js";
import type { ActionCallback } from "../types/action/index.js";
import type { AlertOptions } from "../types/alert/index.js";

import Action from "./action.js";
import Alert from "./alert.js";

export type ContextResult<R> = Action<R> | ActionCallback<R | undefined> | MaybePromise<R | undefined>;

export default class Context<T = void> extends DeferredPromise<T>
export default class Context<T = void, P extends Record<string, unknown> = never> extends DeferredPromise<T>
{
protected _duration: Duration;
protected _timeoutId?: Timeout;
Expand All @@ -25,10 +25,11 @@ export default class Context<T = void> extends DeferredPromise<T>

protected readonly _isOpen: Ref<boolean>;

public readonly alert: Alert<T>;
public readonly alert: Alert<T, P>;
public readonly isOpen: ComputedRef<boolean>;
public readonly component?: Component;

public constructor(options: AlertOptions<T>, duration: number | Duration)
public constructor(options: AlertOptions<T, P>, duration: number | Duration)
{
const _close = async (): Promise<void> =>
{
Expand Down Expand Up @@ -98,10 +99,12 @@ export default class Context<T = void> extends DeferredPromise<T>
this._closingSubscribers = new Subscribers();
this._closedSubscribers = new Subscribers();

this.alert = new Alert<T>(options);
this.alert = new Alert<T, P>(options as AlertOptions<T>);

this._isOpen = ref(false);
this.isOpen = computed((): boolean => this._isOpen.value);

this.component = options.component;
}

public async open(): Promise<void>
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/types/action/callback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { ActionCallback, CoreAction } from "./core";

export interface CallbackAction<R = void> extends CoreAction
{
callback: ActionCallback<R>;
value?: never;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { MaybePromise } from "@byloth/core";

export type ActionCallback<T> = () => MaybePromise<T>;

export interface IAction<R>
export interface IAction<R = void>
{
id: symbol;
type: "primary" | "secondary" | "alternative";
Expand All @@ -13,11 +13,10 @@ export interface IAction<R>
}

type PartialAction<R> = Partial<IAction<R>>;
type OmittedAction = Omit<PartialAction<never>, "callback">;
type OmittedProperty = "callback";
type OmittedAction = Omit<PartialAction<never>, OmittedProperty>;

export interface ActionOptions<R = void> extends OmittedAction
export interface CoreAction extends OmittedAction
{
label: string;

callback?: ActionCallback<R>;
}
6 changes: 6 additions & 0 deletions packages/core/src/types/action/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { CallbackAction } from "./callback";
import type { ValueAction } from "./value";

export type { ActionCallback, IAction } from "./core";

export type ActionOptions<R = void> = CallbackAction<R> | ValueAction<R>;
7 changes: 7 additions & 0 deletions packages/core/src/types/action/value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { CoreAction } from "./core";

export interface ValueAction<R = void> extends CoreAction
{
callback?: never;
value: R;
}
19 changes: 7 additions & 12 deletions packages/core/src/types/alert/core.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { Component } from "vue";
import type { IAction, ActionOptions } from "../action/index.js";

import type { Props } from "../core.js";
import type { IAction, ActionOptions } from "../action.js";

export interface IAlert<R = void>
export interface IAlert<R = void, P extends Record<string, unknown> = never>
{
id: symbol;

Expand All @@ -14,20 +11,18 @@ export interface IAlert<R = void>
title?: string;

message?: string;

component?: Component;
props?: Props;
payload?: P;

actions: IAction<R>[];

dismissible: boolean;
timeout: number;
}

type PartialAlert<R> = Partial<IAlert<R>>;
type OmittedProperty = "message" | "component" | "props" | "actions";
type OmittedAlert = Omit<PartialAlert<never>, OmittedProperty>;
export interface CoreAlert<R = void> extends OmittedAlert
type PartialAlert<R, P extends Record<string, unknown>> = Partial<IAlert<R, P>>;
type OmittedProperty = "message" | "actions";
type OmittedAlert<P extends Record<string, unknown>> = Omit<PartialAlert<never, P>, OmittedProperty>;
export interface CoreAlert<R = void, P extends Record<string, unknown> = never> extends OmittedAlert<P>
{
actions?: ActionOptions<R>[];
}
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/types/alert/custom.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { Component } from "vue";

import type { Props } from "../core.js";
import type { CoreAlert, BlockingMixin, DismissibleMixin } from "./core.js";

export interface CustomAlert<R = void> extends CoreAlert<R>
export interface CustomAlert<R = void, P extends Record<string, unknown> = never> extends CoreAlert<R, P>
{
message?: never;

message?: string;
component: Component;
props?: Props;
}

export type BlockingCustomAlert<R = void> = CustomAlert<R> & BlockingMixin;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/alert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import type { CustomAlert } from "./custom.js";

export type { IAlert } from "./core.js";

export type AlertOptions<R = void> = SimpleAlert<R> | CustomAlert<R>;
export type AlertOptions<R = void, P extends Record<string, unknown> = never> = SimpleAlert<R, P> | CustomAlert<R, P>;
4 changes: 1 addition & 3 deletions packages/core/src/types/alert/simple.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { CoreAlert, BlockingMixin, DismissibleMixin } from "./core.js";

export interface SimpleAlert<R = void> extends CoreAlert<R>
export interface SimpleAlert<R = void, P extends Record<string, unknown> = never> extends CoreAlert<R, P>
{
message: string;

component?: never;
props?: never;
}

export type BlockingAlert<R = void> = SimpleAlert<R> & BlockingMixin;
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/types/core.ts

This file was deleted.

10 changes: 10 additions & 0 deletions packages/core/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ export default defineConfig({
build: {
lib: {
entry: resolve("./src/index.ts"),
fileName: (format) =>
{
if (format === "cjs") { return `vuert.cjs`; }
if (format === "es") { return `vuert.esm.js`; }
if (format === "iife") { return `vuert.global.js`; }
if (format === "umd") { return `vuert.umd.cjs`; }

throw new Error(`Unknown build format: ${format}`);
},
formats: ["cjs", "es", "iife", "umd"],
name: "Vuert"
},
rollupOptions: {
Expand Down

0 comments on commit 8397d25

Please sign in to comment.