diff --git a/src/useSubscription/models/channel.ts b/src/useSubscription/models/channel.ts index 5d5a3043..5a40c8af 100644 --- a/src/useSubscription/models/channel.ts +++ b/src/useSubscription/models/channel.ts @@ -1,8 +1,8 @@ /* eslint-disable max-classes-per-file */ import { isEqual } from 'lodash' import { effectScope } from 'vue' -import Manager from '@/useSubscription/models/manager' -import Subscription from '@/useSubscription/models/subscription' +import { SubscriptionManager } from '@/useSubscription/models/manager' +import { Subscription } from '@/useSubscription/models/subscription' import { Action, ActionArguments, @@ -38,10 +38,10 @@ class ChannelSignatureManager { } } -export default class Channel { +export class SubscriptionChannel { public readonly signature: ChannelSignature - private readonly manager: Manager + private readonly manager: SubscriptionManager private readonly action: T private readonly args: ActionArguments private readonly subscriptions: Map> = new Map() @@ -56,7 +56,7 @@ export default class Channel { private _error: unknown = null private _response: ActionResponse | undefined = undefined - public constructor(manager: Manager, action: T, args: ActionArguments) { + public constructor(manager: SubscriptionManager, action: T, args: ActionArguments) { this.signature = ChannelSignatureManager.get(action, args) this.manager = manager this.action = action diff --git a/src/useSubscription/models/index.ts b/src/useSubscription/models/index.ts index 84100246..bb4fedcc 100644 --- a/src/useSubscription/models/index.ts +++ b/src/useSubscription/models/index.ts @@ -1,3 +1,3 @@ -export { default as channel } from './channel' -export { default as manager } from './manager' -export { default as subscription } from './subscription' +export * from './channel' +export * from './manager' +export * from './subscription' diff --git a/src/useSubscription/models/manager.ts b/src/useSubscription/models/manager.ts index d97312c3..72ff927f 100644 --- a/src/useSubscription/models/manager.ts +++ b/src/useSubscription/models/manager.ts @@ -1,5 +1,5 @@ -import Channel from '@/useSubscription/models/channel' -import Subscription from '@/useSubscription/models/subscription' +import { SubscriptionChannel } from '@/useSubscription/models/channel' +import { Subscription } from '@/useSubscription/models/subscription' import { Action, ActionArguments, @@ -8,8 +8,8 @@ import { import { SubscriptionOptions } from '@/useSubscription/types/subscription' import * as useSubscriptionDevtools from '@/useSubscription/useSubscriptionDevtools' -export default class Manager { - private readonly channels: Map = new Map() +export class SubscriptionManager { + private readonly channels: Map = new Map() public subscribe( action: T, @@ -48,11 +48,11 @@ export default class Manager { private getChannel( action: T, args: ActionArguments, - ): Channel { - const channel = new Channel(this, action, args) + ): SubscriptionChannel { + const channel = new SubscriptionChannel(this, action, args) if (this.channels.has(channel.signature)) { - return this.channels.get(channel.signature)! as Channel + return this.channels.get(channel.signature)! as SubscriptionChannel } this.addChannel(channel) @@ -60,7 +60,7 @@ export default class Manager { return channel } - private addChannel(channel: Channel): void { + private addChannel(channel: SubscriptionChannel): void { this.channels.set(channel.signature, channel) useSubscriptionDevtools.addChannel(channel) diff --git a/src/useSubscription/models/subscription.ts b/src/useSubscription/models/subscription.ts index 29339363..9fb09dff 100644 --- a/src/useSubscription/models/subscription.ts +++ b/src/useSubscription/models/subscription.ts @@ -1,6 +1,6 @@ /* eslint-disable max-classes-per-file */ import { ref, Ref, watch } from 'vue' -import Channel from '@/useSubscription/models/channel' +import { SubscriptionChannel } from '@/useSubscription/models/channel' import { Action, ActionResponse } from '@/useSubscription/types/action' import { SubscriptionOptions } from '@/useSubscription/types/subscription' import * as useSubscriptionDevtools from '@/useSubscription/useSubscriptionDevtools' @@ -13,7 +13,7 @@ class SubscriptionIdManager { } } -export default class Subscription { +export class Subscription { public readonly id: number public readonly options: SubscriptionOptions public loading: Ref = ref(false) @@ -24,9 +24,9 @@ export default class Subscription { public paused: Ref = ref(false) public late: Ref = ref(false) - private readonly channel: Channel + private readonly channel: SubscriptionChannel - public constructor(channel: Channel, options: SubscriptionOptions) { + public constructor(channel: SubscriptionChannel, options: SubscriptionOptions) { this.id = SubscriptionIdManager.get() this.channel = channel this.options = options diff --git a/src/useSubscription/types/subscription.ts b/src/useSubscription/types/subscription.ts index 41231863..1d9c7183 100644 --- a/src/useSubscription/types/subscription.ts +++ b/src/useSubscription/types/subscription.ts @@ -1,6 +1,6 @@ import { MaybeRef } from '@/types/maybe' -import Manager from '@/useSubscription/models/manager' -import Subscription from '@/useSubscription/models/subscription' +import { SubscriptionManager } from '@/useSubscription/models/manager' +import { Subscription } from '@/useSubscription/models/subscription' import { Action, ActionArguments, ActionParamsRequired, ActionResponse } from '@/useSubscription/types/action' export type SubscribeArguments = ActionParamsRequired extends never[] @@ -10,7 +10,7 @@ export type SubscribeArguments = ActionParamsRequired exten export type SubscriptionOptions = { /** The maximum time in milliseconds before the subscription is considered stale and refreshes. Defaults to `Infinity` */ interval?: number, - manager?: Manager, + manager?: SubscriptionManager, lifecycle?: 'component' | 'app', onError?: (error: unknown) => void, } diff --git a/src/useSubscription/useSubscription.spec.ts b/src/useSubscription/useSubscription.spec.ts index bcfe111b..366addba 100644 --- a/src/useSubscription/useSubscription.spec.ts +++ b/src/useSubscription/useSubscription.spec.ts @@ -2,7 +2,7 @@ import { render } from '@testing-library/vue' import { test, describe, it, expect, afterEach, vi } from 'vitest' import { computed, h, reactive, ref } from 'vue' import { useSubscription } from '@/useSubscription' -import Manager from '@/useSubscription/models/manager' +import { SubscriptionManager } from '@/useSubscription/models/manager' import { SubscriptionOptions, UseSubscription } from '@/useSubscription/types/subscription' import { timeout, uniqueSubscribe } from '@/utilities/tests' @@ -120,7 +120,7 @@ describe('subscribe', () => { }) it('executes the action once when two subscriptions are created', () => { - const manager = new Manager() + const manager = new SubscriptionManager() const action = vi.fn() useSubscription(action, [], { manager }) @@ -132,7 +132,7 @@ describe('subscribe', () => { it('calculates the poll interval correctly', () => { vi.useFakeTimers() - const manager = new Manager() + const manager = new SubscriptionManager() const action = vi.fn() const initialExecutions = 1 const additionalExecutions = 3 @@ -150,7 +150,7 @@ describe('subscribe', () => { it('calculates the poll interval correctly when a subscription is unsubscribed', () => { vi.useFakeTimers() - const manager = new Manager() + const manager = new SubscriptionManager() const action = vi.fn() const initialExecutions = 1 const additionalExecutions = 3 @@ -171,7 +171,7 @@ describe('subscribe', () => { it('stops polling when all subscriptions with interval unsubscribe', () => { vi.useFakeTimers() - const manager = new Manager() + const manager = new SubscriptionManager() const action = vi.fn() const minInterval = 10 const maxInterval = 20 @@ -361,7 +361,7 @@ describe('subscribe', () => { }) it('does not update subscription when unsubscribed', async () => { - const manager = new Manager() + const manager = new SubscriptionManager() let int = 0 function action(): number { @@ -481,7 +481,7 @@ describe('subscribe', () => { return 0 } - const manager = new Manager() + const manager = new SubscriptionManager() useSubscription(action, [], { manager }) @@ -503,7 +503,7 @@ describe('subscribe', () => { it('correctly sets executed on additional subscriptions', async () => { const action = vi.fn() - const manager = new Manager() + const manager = new SubscriptionManager() useSubscription(action, [], { manager }) @@ -556,7 +556,7 @@ describe('subscribe', () => { }) it('unsubscribes nested subscriptions automatically', () => { - const manager = new Manager() + const manager = new SubscriptionManager() const action = vi.fn() let childSubscription: UseSubscription @@ -573,7 +573,7 @@ describe('subscribe', () => { it('unsubscribes nested subscriptions when changing args', async () => { const number = ref(0) - const manager = new Manager() + const manager = new SubscriptionManager() const action = vi.fn() let childSubscription: UseSubscription @@ -593,7 +593,7 @@ describe('subscribe', () => { it('it refreshes nested subscriptions when calling refresh', async () => { const action = vi.fn() - const manager = new Manager() + const manager = new SubscriptionManager() const subscription = useSubscription((): void => { useSubscription(action, [], { manager }) }, [], { manager }) @@ -649,7 +649,7 @@ describe('subscribe', () => { vi.useFakeTimers() const interval = 1000 - const manager = new Manager() + const manager = new SubscriptionManager() const action = vi.fn() const subscription = useSubscription(action, [], { manager, interval }) diff --git a/src/useSubscription/useSubscription.ts b/src/useSubscription/useSubscription.ts index b1007426..888b025f 100644 --- a/src/useSubscription/useSubscription.ts +++ b/src/useSubscription/useSubscription.ts @@ -1,5 +1,5 @@ import { reactive, unref } from 'vue' -import Manager from '@/useSubscription/models/manager' +import { SubscriptionManager } from '@/useSubscription/models' import { Action, ActionArguments } from '@/useSubscription/types/action' import { SubscribeArguments, UseSubscription } from '@/useSubscription/types/subscription' import { mapSubscription } from '@/useSubscription/utilities/subscriptions' @@ -7,7 +7,7 @@ import { getValidWatchSource } from '@/utilities/getValidWatchSource' import { tryOnScopeDispose } from '@/utilities/tryOnScopeDispose' import { uniqueValueWatcher } from '@/utilities/uniqueValueWatcher' -export const defaultSubscriptionManager = new Manager() +export const defaultSubscriptionManager = new SubscriptionManager() /** * The `useSubscription` composition manages data sharing across components. Multiple components can subscribe to an `action` (any method or function) and share the response value. diff --git a/src/useSubscription/useSubscriptionDevtools.ts b/src/useSubscription/useSubscriptionDevtools.ts index 6d90a5e4..8e5674de 100644 --- a/src/useSubscription/useSubscriptionDevtools.ts +++ b/src/useSubscription/useSubscriptionDevtools.ts @@ -7,7 +7,7 @@ import type { ExtractSettingsTypes } from '@vue/devtools-api' import { ComponentInternalInstance, getCurrentInstance, nextTick } from 'vue' -import Channel from '@/useSubscription/models/channel' +import { SubscriptionChannel } from '@/useSubscription/models/channel' function throttle(fn: () => void, wait: number): () => void { let isThrottled = false @@ -53,8 +53,8 @@ export const SUBSCRIPTION_DEVTOOLS_SETTINGS = { export type SubscriptionDevtoolsSettings = ExtractSettingsTypes -const channelNodes: Map = new Map() -const subscribedComponents: Map> = new Map() +const channelNodes: Map = new Map() +const subscribedComponents: Map> = new Map() let API: DevtoolsPluginApi> | null = null export function init(api: DevtoolsPluginApi): void { @@ -110,7 +110,7 @@ const refresh: () => void = throttle(() => { }, 100) }, 200) -export function addChannel(channel: Channel): void { +export function addChannel(channel: SubscriptionChannel): void { if (!initialized()) { return } @@ -124,7 +124,7 @@ export type UpdateChannelEventTypes = 'Loading' | 'Error' | 'Executed' | 'Respon type UpdateChannelEvent = { [K in UpdateChannelEventTypes]: CreateSubscriptionDevtoolsTimelineEvent }[UpdateChannelEventTypes] -export function updateChannel(channel: Channel, event: UpdateChannelEvent): void { +export function updateChannel(channel: SubscriptionChannel, event: UpdateChannelEvent): void { if (!initialized()) { return } @@ -134,7 +134,7 @@ export function updateChannel(channel: Channel, event: UpdateChannelEvent): void refresh() } -export function removeChannel(channel: Channel): void { +export function removeChannel(channel: SubscriptionChannel): void { if (!initialized()) { return } @@ -144,7 +144,7 @@ export function removeChannel(channel: Channel): void { refresh() } -export function registerChannelSubscription(channel: Channel, subscriptionId: number): void { +export function registerChannelSubscription(channel: SubscriptionChannel, subscriptionId: number): void { if (!initialized()) { return } @@ -158,7 +158,7 @@ export function registerChannelSubscription(channel: Channel, subscriptionId: nu refresh() } -export function removeChannelSubscription(channel: Channel, subscriptionId: number): void { +export function removeChannelSubscription(channel: SubscriptionChannel, subscriptionId: number): void { if (!initialized()) { return } @@ -178,7 +178,7 @@ type SubscriptionsInspectorState = CustomInspectorState & { } async function getCustomInspectorState(nodeId: string): Promise { - const { channel } = channelNodes.get(nodeId as Channel['signature']) ?? {} + const { channel } = channelNodes.get(nodeId as SubscriptionChannel['signature']) ?? {} if (!channel) { return { 'Error': [{ key: 'message', value: 'Channel not found.' }], 'State': [], 'Subscribed Components': [] } } @@ -212,17 +212,17 @@ type CreateSubscriptionDevtoolsTimelineEvent = Omi } type EventTypeToDataMap = { - 'Channel created': { channel: Channel, action: string }, - 'Channel removed': { channel: Channel, action: string }, - 'Subscription created': { channel: Channel, action: string, subscriptionId: number }, - 'Subscription removed': { channel: Channel, action: string, subscriptionId: number }, - 'Loading': { channel: Channel, action: string, loading: boolean }, - 'Error': { channel: Channel, action: string, error: unknown }, - 'Executed': { channel: Channel, action: string, executed: boolean }, - 'Response': { channel: Channel, action: string, response: unknown }, - 'Refresh': { channel: Channel, action: string }, - 'Paused': { channel: Channel, action: string, paused: boolean }, - 'Late': { channel: Channel, action: string, late: boolean }, + 'Channel created': { channel: SubscriptionChannel, action: string }, + 'Channel removed': { channel: SubscriptionChannel, action: string }, + 'Subscription created': { channel: SubscriptionChannel, action: string, subscriptionId: number }, + 'Subscription removed': { channel: SubscriptionChannel, action: string, subscriptionId: number }, + 'Loading': { channel: SubscriptionChannel, action: string, loading: boolean }, + 'Error': { channel: SubscriptionChannel, action: string, error: unknown }, + 'Executed': { channel: SubscriptionChannel, action: string, executed: boolean }, + 'Response': { channel: SubscriptionChannel, action: string, response: unknown }, + 'Refresh': { channel: SubscriptionChannel, action: string }, + 'Paused': { channel: SubscriptionChannel, action: string, paused: boolean }, + 'Late': { channel: SubscriptionChannel, action: string, late: boolean }, } type SubscriptionDevtoolsTimelineEvent = { @@ -239,7 +239,7 @@ function addTimelineEvent(event: SubscriptionDevtoolsTimelineEvent): void { }) } -function mapChannelToInspectorNode(channel: Channel): CustomInspectorNode { +function mapChannelToInspectorNode(channel: SubscriptionChannel): CustomInspectorNode { return { id: channel.signature, label: `${channel.actionName} ${channel.signature}`, diff --git a/src/useSubscription/utilities/subscriptions.ts b/src/useSubscription/utilities/subscriptions.ts index 11fcf0b2..d8cbe184 100644 --- a/src/useSubscription/utilities/subscriptions.ts +++ b/src/useSubscription/utilities/subscriptions.ts @@ -1,5 +1,5 @@ import { reactive } from 'vue' -import Subscription from '@/useSubscription/models/subscription' +import { Subscription } from '@/useSubscription/models' import { Action } from '@/useSubscription/types/action' import { MappedSubscription, SubscriptionPromise } from '@/useSubscription/types/subscription' diff --git a/src/utilities/tests.ts b/src/utilities/tests.ts index 0c88c8cb..63e45d1d 100644 --- a/src/utilities/tests.ts +++ b/src/utilities/tests.ts @@ -1,6 +1,6 @@ import { computed, unref } from 'vue' import { useSubscription } from '@/useSubscription' -import Manager from '@/useSubscription/models/manager' +import { SubscriptionManager } from '@/useSubscription/models/manager' import { Action } from '@/useSubscription/types/action' import { SubscribeArguments, UseSubscription } from '@/useSubscription/types/subscription' @@ -14,7 +14,7 @@ export function uniqueSubscribe(...[action, args, optionsArg = return { ...options, - manager: new Manager(), + manager: new SubscriptionManager(), } })