pinia-plugin-subscription 1.0.0-beta.2 → 1.0.0-beta.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/CHANGELOG.md +73 -12
  2. package/CONTRIBUTING.md +10 -1
  3. package/README.md +48 -293
  4. package/SECURITY.md +6 -6
  5. package/dist/chunks/setup-context-3G3Hy1ep.js +909 -0
  6. package/dist/dictionaries/plugin-subscription-event.d.ts +20 -0
  7. package/dist/{core/Store.d.ts → enhanced-store/EnhancedStore.d.ts} +24 -29
  8. package/dist/enhanced-store/define-a-store.d.ts +64 -0
  9. package/dist/enhanced-store/setup-context.d.ts +27 -0
  10. package/dist/events/EventsEmitter.d.ts +9 -0
  11. package/dist/events/EventsSubscriber.d.ts +32 -0
  12. package/dist/events/EventsSubscription.d.ts +13 -0
  13. package/dist/events/StoreEvent.d.ts +6 -0
  14. package/dist/events/StoresEventsSubscriptionsRegistry.d.ts +31 -0
  15. package/dist/events/event.d.ts +8 -0
  16. package/dist/factories/events-registry.d.ts +9 -0
  17. package/dist/factories/events-subscriber.d.ts +11 -0
  18. package/dist/factories/messenger-registry.d.ts +16 -0
  19. package/dist/factories/plugin-subscriber.d.ts +17 -0
  20. package/dist/helpers.js +204 -312
  21. package/dist/lib/helpers.d.ts +9 -0
  22. package/dist/lib/main.d.ts +3 -12
  23. package/dist/orchestration/ExecutionPolicy.d.ts +15 -0
  24. package/dist/orchestration/PluginOrchestrator.d.ts +41 -0
  25. package/dist/orchestration/SubscriberScheduler.d.ts +31 -0
  26. package/dist/pinia-plugin-subscription.js +31 -426
  27. package/dist/plugins/createPlugin.d.ts +3 -13
  28. package/dist/services/messenger.d.ts +28 -0
  29. package/dist/services/messengerProvider.d.ts +45 -0
  30. package/dist/store-hooks/StoreHooksBinder.d.ts +20 -0
  31. package/dist/store-hooks/StoreHooksRegistry.d.ts +34 -0
  32. package/dist/{core/PluginSubscriber.d.ts → subscribers/StoreSubscriber.d.ts} +39 -28
  33. package/dist/tracing/TraceGroupHelper.d.ts +27 -0
  34. package/dist/tracing/TraceableEmitter.d.ts +60 -0
  35. package/dist/tracing/TraceableEventsSubscriber.d.ts +21 -0
  36. package/dist/tracing/TraceableStoreEventsSubscriber.d.ts +16 -0
  37. package/dist/tracing/trace-groups.d.ts +9 -0
  38. package/dist/types/core.d.ts +12 -0
  39. package/dist/types/events.d.ts +24 -0
  40. package/dist/types/index.d.ts +8 -34
  41. package/dist/types/messenger.d.ts +20 -0
  42. package/dist/types/plugin.d.ts +39 -20
  43. package/dist/types/store.d.ts +1 -9
  44. package/dist/types/trace.d.ts +35 -75
  45. package/dist/utils/constants.d.ts +4 -0
  46. package/dist/utils/events.d.ts +3 -0
  47. package/dist/utils/id.d.ts +20 -0
  48. package/dist/utils/store.d.ts +9 -93
  49. package/docs/authoring-plugins.md +330 -78
  50. package/docs/using-plugins.md +89 -53
  51. package/package.json +14 -8
  52. package/dist/chunks/constantes-CaTNAbzX.js +0 -261
  53. package/dist/chunks/constantes-CaTNAbzX.js.map +0 -1
  54. package/dist/core/PluginSubscription.d.ts +0 -92
  55. package/dist/core/RollbackStateSnapshotsHandler.d.ts +0 -65
  56. package/dist/factories/console-trace-listener.d.ts +0 -7
  57. package/dist/factories/state-snapshot.d.ts +0 -3
  58. package/dist/factories/trace-listener.d.ts +0 -16
  59. package/dist/factories/trace-registry.d.ts +0 -17
  60. package/dist/helpers/index.d.ts +0 -9
  61. package/dist/helpers.js.map +0 -1
  62. package/dist/pinia-plugin-subscription.js.map +0 -1
  63. package/dist/plugins/createHydrationPlugin.d.ts +0 -10
  64. package/dist/system/Tracer.d.ts +0 -20
  65. package/dist/system/log.d.ts +0 -35
  66. package/dist/tests/defineAStore.test-d.d.ts +0 -1
  67. package/dist/tests/storeOptionsExtensions.test-d.d.ts +0 -12
  68. package/dist/types/comparison.d.ts +0 -3
  69. package/dist/types/list.d.ts +0 -13
  70. package/dist/types/log.d.ts +0 -8
  71. package/dist/types/resourceId.d.ts +0 -4
  72. package/dist/utils/Performance.d.ts +0 -9
  73. package/dist/utils/constantes.d.ts +0 -1
  74. package/dist/utils/deep-clone.d.ts +0 -7
  75. package/dist/utils/defineStoreId.d.ts +0 -1
  76. package/dist/utils/object.d.ts +0 -4
  77. package/dist/utils/validation/object.d.ts +0 -9
@@ -1,8 +1,8 @@
1
- import Store from "./Store";
1
+ import TraceableEventsSubscriber from "../tracing/TraceableEventsSubscriber";
2
+ import EnhancedStore from "../enhanced-store/EnhancedStore";
2
3
  import type { PiniaPluginContext, Store as PiniaStore } from "pinia";
3
- import type { PluginExecutionOptions, PluginHydrationScheduler, PluginSubscriber as PluginSubscriberInterface, PluginSubscriptions, StoreClassConstructorParams, StoreMutationSubscription, StoreOnActionSubscription } from "../types/plugin";
4
- import type Tracer from "../system/Tracer";
5
- type CreateInstance<Instance = Store> = (store: PiniaStore, options: StoreClassConstructorParams) => Instance | undefined;
4
+ import type { PluginExecutionOptions, PluginHydrationScheduler, PluginOptions, StoreSubscriberInterface, PluginSubscriptions, EnhancedStoreOptions, StoreMutationSubscription, StoreOnActionSubscription } from "../types/plugin";
5
+ type CreateInstance<Instance = EnhancedStore> = (store: PiniaStore, options: EnhancedStoreOptions) => Instance | undefined;
6
6
  /**
7
7
  * Type definition for the function used to create an instance of a store.
8
8
  * @template Instance The type of the store instance, defaulting to the base Store class.
@@ -10,53 +10,64 @@ type CreateInstance<Instance = Store> = (store: PiniaStore, options: StoreClassC
10
10
  * @param options Additional options for creating the store instance.
11
11
  * @returns The created store instance or undefined if creation fails.
12
12
  */
13
- export default abstract class PluginSubscriber<Instance extends Store> implements PluginSubscriberInterface {
13
+ export default abstract class PluginSubscriber<Instance extends EnhancedStore> extends TraceableEventsSubscriber implements StoreSubscriberInterface {
14
14
  private _createInstance;
15
- private _name;
16
- private _pluginOptions?;
17
- private _storeInstance?;
18
- /** Callback function to reset the store instance, if needed. */
19
- protected _resetStoreCallback?: (store?: PiniaStore) => void;
20
- private _storeOnActionSubscription?;
21
- private _storeMutationSubscription?;
22
- private _subscriptions?;
15
+ /** Options for the plugin subscriber's execution. */
16
+ execution?: PluginExecutionOptions;
17
+ /** Scheduler for handling the hydration process of the plugin subscriber. */
18
+ hydrationScheduler?: PluginHydrationScheduler;
23
19
  /**
24
20
  * Called when the plugin subscriber has been created for a specific store.
25
21
  * This callback can be used to perform any initialization or setup required for the plugin subscriber.
26
22
  */
27
23
  protected pluginCreated?: (store: PiniaStore) => void;
28
- /** Options for the plugin subscriber's execution. */
29
- execution?: PluginExecutionOptions;
30
- /** Scheduler for handling the hydration process of the plugin subscriber. */
31
- hydrationScheduler?: PluginHydrationScheduler;
32
- get name(): string;
33
- get storeInstance(): Instance | undefined;
34
- get pluginOptions(): StoreClassConstructorParams | undefined;
35
- set pluginOptions(options: StoreClassConstructorParams | undefined);
36
- get resetStoreCallback(): ((store?: PiniaStore) => void) | undefined;
24
+ private _pluginOptions?;
25
+ private _storeInstances;
26
+ /**
27
+ * Subscription for store actions.
28
+ * This subscription is used to listen for actions dispatched in the store.
29
+ */
30
+ protected _storeOnActionSubscription?: StoreOnActionSubscription;
31
+ /**
32
+ * Subscription for store mutations.
33
+ * This subscription is used to listen for mutations occurring in the store.
34
+ */
35
+ protected _storeMutationSubscription?: StoreMutationSubscription;
36
+ /**
37
+ * Subscriptions for the plugin subscriber.
38
+ * This is used to manage and track all active subscriptions related to the plugin subscriber.
39
+ */
40
+ protected _subscriptions?: PluginSubscriptions;
41
+ get pluginOptions(): EnhancedStoreOptions | undefined;
42
+ set pluginOptions(options: EnhancedStoreOptions | undefined);
43
+ /**
44
+ * Returns the wrapper created for a specific Pinia store.
45
+ * @param store The Pinia store passed to the subscriber hook.
46
+ */
47
+ getStoreInstance(store: PiniaStore): Instance | undefined;
37
48
  get storeOnActionSubscription(): StoreOnActionSubscription | undefined;
38
49
  get storeMutationSubscription(): StoreMutationSubscription | undefined;
39
50
  get subscriptions(): PluginSubscriptions | undefined;
40
51
  set subscriptions(subscriptions: PluginSubscriptions);
41
- constructor(pluginName: string, createInstanceFunction: CreateInstance);
52
+ constructor(createInstanceFunction: CreateInstance<Instance>);
42
53
  /**
43
54
  * Called after the store has been hydrated.
44
55
  * @param context The Pinia plugin context containing the store and options.
45
56
  * @returns A promise that resolves when the post-hydration process is complete, or void if synchronous.
46
57
  */
47
- afterHydration(context: PiniaPluginContext): void | Promise<void>;
58
+ afterHydration(_context: PiniaPluginContext): void | Promise<void>;
48
59
  /**
49
60
  * Called when the plugin subscriber needs to hydrate its state.
50
61
  * @param context The Pinia plugin context containing the store and options.
51
62
  * @returns A promise that resolves when the hydration process is complete, or void if synchronous.
52
63
  */
53
- hydrate(context: PiniaPluginContext): void | Promise<void>;
64
+ hydrate(_context: PiniaPluginContext): void | Promise<void>;
54
65
  /**
55
- * Invokes the plugin subscriber with the given context and optional tracer creation function.
66
+ * Invokes the plugin subscriber with the given context and optional plugin options.
56
67
  * @param context The Pinia plugin context containing the store and options.
57
- * @param createTracer Optional function to create a tracer for the plugin.
68
+ * @param pluginOptions Optional tracer factory and events handler.
58
69
  * @returns True if the plugin was successfully invoked, otherwise false.
59
70
  */
60
- invoke({ store, options }: PiniaPluginContext, createTracer?: (name: string) => Tracer): boolean;
71
+ invoke({ store, options }: PiniaPluginContext, pluginOptions?: PluginOptions): boolean;
61
72
  }
62
73
  export {};
@@ -0,0 +1,27 @@
1
+ import type { PluginOrchestratorOptions } from "../types/plugin";
2
+ import type { TraceReporter } from "../types/core";
3
+ import type { TracePayload } from "../types/trace";
4
+ export declare const defaultDeferredTraceGroupTimeout = 2500;
5
+ /** Opens/closes trace groups and runs callbacks while tracing their failures. */
6
+ export default class TraceGroupHelper {
7
+ private _options;
8
+ private _reporter;
9
+ private _seq;
10
+ constructor(options: PluginOrchestratorOptions | undefined, reporter: TraceReporter);
11
+ /** Returns a sequence number, unique per plugin instance, used to build group ids. */
12
+ nextSeq(): number;
13
+ start(payload: TracePayload, scope: string, label?: string, emitTrace?: boolean): void;
14
+ end(payload: TracePayload, scope: string): void;
15
+ /** Closes a trace group once all async callbacks are settled (immediately if none is pending). */
16
+ endAfter(pending: (Promise<void> | undefined)[], end: () => void): void;
17
+ /**
18
+ * Closes a trace group once `pending` settles, or after the configured timeout so a promise
19
+ * that never settles cannot keep buffering the traces of the store scope.
20
+ */
21
+ closeWhenSettled(pending: Promise<unknown>, closeGroup: () => void, storeId: string, subscriberName: string): void;
22
+ /**
23
+ * Runs a callback, tracing sync errors and async rejections.
24
+ * @returns a never-rejecting promise when the callback is asynchronous, undefined otherwise.
25
+ */
26
+ runSafely(callback: () => unknown, errorLabel: string, storeId: string, extra?: () => Record<string, unknown>): Promise<void> | undefined;
27
+ }
@@ -0,0 +1,60 @@
1
+ import type { Store } from "pinia";
2
+ import type { TracePayload, Tracer } from '../types/trace';
3
+ import type { EventRegistry } from '../types/events';
4
+ /**
5
+ * Abstract class for Plugin implementations.
6
+ */
7
+ export default abstract class TraceableEmitter {
8
+ protected abstract _name: string;
9
+ protected _eventsRegistry?: EventRegistry;
10
+ protected traceOptions: {
11
+ prefixLabelWithName: boolean;
12
+ };
13
+ /**
14
+ * Tracer for structured logging and debugging.
15
+ */
16
+ protected _tracer?: Tracer;
17
+ get name(): string;
18
+ /** Returns the structured trace emitter, if tracing is enabled. */
19
+ get tracer(): Tracer | undefined;
20
+ /**
21
+ * Initializes the store events emitter with the given name and trace options.
22
+ * @param name The name of the emitter.
23
+ * @param prefixLabelWithName Whether to prefix trace labels with the emitter name.
24
+ */
25
+ constructor({ prefixLabelWithName }?: {
26
+ prefixLabelWithName?: boolean;
27
+ });
28
+ /**
29
+ * Emits an event to all subscribers of the given store.
30
+ * @param store The store instance to emit the event for.
31
+ * @param event The name of the event to emit.
32
+ * @param payload Optional payload factory function for the event.
33
+ */
34
+ protected emit(store: Store, event: string, payload?: () => any): void;
35
+ protected storeHasEventSubscription(store: Store, eventName: string): boolean;
36
+ /**
37
+ * Emits a structured trace event.
38
+ * Prefer the factory form for `payload`: it is only resolved when consumed.
39
+ * @param label The label for the trace event.
40
+ * @param scope Optional scope for the trace event.
41
+ * @param payload Optional payload for the trace event.
42
+ * @param namespace Optional namespace for the trace event.
43
+ */
44
+ protected trace(label: string, scope?: string, payload?: TracePayload, namespace?: string): void;
45
+ /**
46
+ * Emits a structured error event, or reports it with `console.error` when tracing is disabled.
47
+ * @param label The label for the error event.
48
+ * @param error The error object.
49
+ * @param scope Optional scope for the error event.
50
+ * @param payload Optional payload for the error event.
51
+ * @param namespace Optional namespace for the error event.
52
+ */
53
+ protected traceError(label: string, error: unknown, scope?: string, payload?: TracePayload, namespace?: string): void;
54
+ /**
55
+ * Formats the trace label according to the trace options.
56
+ * @param label The original trace label.
57
+ * @returns The formatted trace label.
58
+ */
59
+ protected traceLabelFormatter(label: string): string;
60
+ }
@@ -0,0 +1,21 @@
1
+ import EventSubscription from '../events/EventsSubscription';
2
+ import TraceableEmitter from './TraceableEmitter';
3
+ import type EventSubscriber from '../events/EventsSubscriber';
4
+ import type { StoreEventConstructorOptions } from '../types/events';
5
+ export default abstract class TraceableEventsSubscriber extends TraceableEmitter {
6
+ protected _eventsSubscriber: EventSubscriber | undefined;
7
+ /**
8
+ * Initializes the event subscriber with the given ID and optional event subscriptions.
9
+ * @param id The identifier for the event subscriber.
10
+ * @param eventSubscriptions Optional array of event subscriptions to initialize the subscriber with.
11
+ * @returns void
12
+ */
13
+ protected initEventsSubscriber(id: string, eventSubscriptions?: EventSubscription[]): void;
14
+ /**
15
+ * Adds a subscription for store events.
16
+ * @param eventName The name of the event to subscribe to.
17
+ * @param consumer The consumer function to handle the event.
18
+ * @param options Optional event options.
19
+ */
20
+ protected addEventSubscription(eventName: string, consumer: (...args: any[]) => void | Promise<void>, options?: StoreEventConstructorOptions): void;
21
+ }
@@ -0,0 +1,16 @@
1
+ import EventSubscription from '../events/EventsSubscription';
2
+ import TraceableEventsSubscriber from './TraceableEventsSubscriber';
3
+ import type { Store } from "pinia";
4
+ import type { StoreEventConstructorOptions } from '../types/events';
5
+ export default abstract class TraceableStoreEventsSubscriber extends TraceableEventsSubscriber {
6
+ protected abstract _store: Store;
7
+ protected addEventSubscription(eventName: string, consumer: (...args: any[]) => void | Promise<void>, options?: StoreEventConstructorOptions): void;
8
+ protected initEventsSubscriber(id: string, eventSubscriptions?: EventSubscription[]): void;
9
+ /**
10
+ * Registers a callback to be invoked when the specified store is disposed.
11
+ * @param store The store to watch for disposal.
12
+ * @param callback The callback to invoke on store disposal.
13
+ */
14
+ protected onDispose(callback: (store: Store) => void): void;
15
+ protected onStoreDispose(): void;
16
+ }
@@ -0,0 +1,9 @@
1
+ export declare const traceGroups: {
2
+ onAction: (storeId: string, actionName: string, callId: number) => string;
3
+ onActionAfter: (storeId: string, actionName: string, callId: number) => string;
4
+ onActionError: (storeId: string, actionName: string, callId: number) => string;
5
+ onMutation: (storeId: string, callId: number) => string;
6
+ plugin: (storeId: string) => string;
7
+ pluginDeferred: (storeId: string, subscriberName: string, callId: number) => string;
8
+ reset: (storeId: string, callId: number) => string;
9
+ };
@@ -0,0 +1,12 @@
1
+ import type { Store } from "pinia";
2
+ import type { TracePayload } from "./trace";
3
+ /** Reports traces on behalf of the owning `PluginOrchestrator` (label prefix and tracer are handled by the owner). */
4
+ export interface TraceReporter {
5
+ trace(label: string, scope?: string, payload?: TracePayload): void;
6
+ traceError(label: string, error: unknown, scope?: string, payload?: TracePayload): void;
7
+ }
8
+ /** Store events API of the owning `PluginOrchestrator`. */
9
+ export interface StoreEventsGateway {
10
+ emit(store: Store, event: string, payload?: () => any): void;
11
+ hasEventSubscription(store: Store, eventName: string): boolean;
12
+ }
@@ -0,0 +1,24 @@
1
+ import type EventSubscriber from '../events/EventsSubscriber';
2
+ import type { MessengerRegistry } from "../types/messenger";
3
+ import type StoresEventsSubscriptionsRegistry from '../events/StoresEventsSubscriptionsRegistry';
4
+ export interface Event {
5
+ name: string;
6
+ payload?: EventPayload;
7
+ scope?: string;
8
+ source?: string;
9
+ }
10
+ export type EventConstructorOptions = Omit<Event, 'name'>;
11
+ export type EventPayload<T = Record<string, unknown> | unknown[]> = () => T | T;
12
+ export interface EventRegistry extends Omit<MessengerRegistry, "createSubscriber"> {
13
+ createSubscriber: EventsSubscriberFactory;
14
+ subscriptionsRegistry: StoresEventsSubscriptionsRegistry;
15
+ }
16
+ export interface EventSubscription extends Partial<StoreEvent> {
17
+ consume: (event: Event | StoreEvent) => void;
18
+ subscribed: (event: Event | StoreEvent) => boolean;
19
+ }
20
+ export type EventsSubscriberFactory = (id: string, eventSubscriptions?: EventSubscription[]) => EventSubscriber;
21
+ export interface StoreEvent extends Event {
22
+ instanceId?: string;
23
+ }
24
+ export type StoreEventConstructorOptions = Omit<StoreEvent, 'name'>;
@@ -1,4 +1,6 @@
1
- import type { PiniaPluginContext, Store as PiniaStore } from 'pinia';
1
+ import type { Store as PiniaStore } from 'pinia';
2
+ import type { StoreSubscriberInterface } from './plugin';
3
+ import type { StoreOptions } from './store';
2
4
  /**
3
5
  * Centralized extension point for per-store plugin options.
4
6
  *
@@ -10,43 +12,15 @@ import type { PiniaPluginContext, Store as PiniaStore } from 'pinia';
10
12
  */
11
13
  export interface StoreOptionsExtensions {
12
14
  }
13
- import type { PluginExecutionOptions, PluginHydrationScheduler, PluginSubscriptions, StoreMutationSubscription, StoreOnActionSubscription } from './plugin';
14
- import type { StoreOptions } from './store';
15
15
  export interface AnyObject {
16
16
  [key: number | string | symbol]: any;
17
17
  }
18
- export interface SearchCollectionCriteria {
19
- [key: number | string | symbol]: boolean | number | string;
20
- }
21
- export interface Console {
22
- log(...args: any): void;
23
- error(...args: any): void;
24
- }
25
- export type LogType = 'error' | 'info';
26
- export type StyleDefinitionKeys = 'bgColor' | 'color' | 'icon';
27
- export type StyleDefinition = Record<StyleDefinitionKeys, string>;
28
- export type StyleDefinitions = Record<LogType, Record<StyleDefinitionKeys, string>>;
29
- export type ConsoleStyleDefinition = StyleDefinition;
30
- export type ConsoleStyleDefinitionKeys = StyleDefinitionKeys;
31
- export type ConsoleStyleDefinitions = StyleDefinitions;
32
- export type { List, ListTypes, ListTypesMap, PartialList } from './list';
33
- export type { TraceEvent, TraceEventMetadata, TraceFilter, TraceHandler, TraceLevel, TraceListener, TraceListenerRemover, TracePayload, TraceRegistry, TracerOptions } from './trace';
18
+ export type { TraceEvent, TraceEventMetadata, TraceHandler, TraceLevel, TracePayload, TraceSide, Tracer, TracerHandler } from './trace';
34
19
  export type { CustomStore, DefineAStoreSetupContext, DefineAStoreSetupExtensions, EmptyExtensions, StatePropertyValue, StoreOptions, StoreOptionsPropertyValue } from './store';
35
- export type { NativePiniaSubscription, NativePiniaSubscriptionReturn, PluginExecutionEnvironment, PluginExecutionOptions, PluginHydrationScheduler, PluginHydrationTiming, PluginRuntimeEnvironment, PluginStoreOptions, PluginSubscriber, PluginSubscription, PluginSubscriptionOptions, PluginSubscriptions, StoreClassConstructorParams, StoreMutationSubscription, StoreMutationSubscriptionCallback, StoreMutationSubscriptionReturn, StoreOnActionCallbackParameters, StoreOnActionSubscription, StoreOnActionSubscriptionCallback, StoreOnActionSubscriptionReturn, } from './plugin';
36
- export interface PluginSubscriberInterface {
37
- execution?: PluginExecutionOptions;
38
- hydrate?: (context: PiniaPluginContext) => void | Promise<void>;
39
- hydrationScheduler?: PluginHydrationScheduler;
40
- invoke: (context: PiniaPluginContext) => boolean;
41
- name: string;
42
- resetStoreCallback?: (store?: PiniaStore) => void;
43
- storeOnActionSubscription?: StoreOnActionSubscription;
44
- storeMutationSubscription?: StoreMutationSubscription;
45
- subscriptions?: PluginSubscriptions;
46
- afterHydration?: (context: PiniaPluginContext) => void | Promise<void>;
47
- }
48
- export interface PluginSubscriptionDefinition {
20
+ export type { NativePiniaSubscription, NativePiniaSubscriptionReturn, PluginExecutionEnvironment, PluginExecutionOptions, PluginHydrationTiming, PluginOptions, PluginRuntimeEnvironment, PluginStoreOptions, StoreSubscriberInvoke, PluginHydrationScheduler, PluginOrchestrator, PluginOrchestratorOptions, PluginSubscriptions, EnhancedStoreOptions, StoreMutationSubscription, StoreMutationSubscriptionCallback, StoreMutationSubscriptionReturn, StoreOnActionCallbackParameters, StoreOnActionSubscription, StoreOnActionSubscriptionCallback, StoreOnActionSubscriptionReturn, } from './plugin';
21
+ export type { StoreSubscriberInterface };
22
+ export interface PluginOrchestratorDefinition {
49
23
  stores?: PiniaStore[];
50
- subscription: PluginSubscriberInterface;
24
+ subscription: StoreSubscriberInterface;
51
25
  subscriptionOptions?: StoreOptions;
52
26
  }
@@ -0,0 +1,20 @@
1
+ import Messenger from "../services/messenger";
2
+ export type SubscriberFactory = (...optionalArgs: unknown[]) => MessengerSubscriber;
3
+ export type CreateSubscriberFactory = (messenger: Messenger) => SubscriberFactory;
4
+ export interface MessengerEmitter {
5
+ emit(payload: unknown): void;
6
+ }
7
+ export interface MessengerProvider {
8
+ messenger: Messenger;
9
+ messengerChannel: string;
10
+ }
11
+ export interface MessengerRegistry {
12
+ emitter: MessengerEmitter;
13
+ createSubscriber: CreateSubscriberFactory;
14
+ }
15
+ export interface MessengerSubscriber {
16
+ id: string;
17
+ receive(payload: unknown): void;
18
+ clear(): void;
19
+ unsubscribe(): void;
20
+ }
@@ -1,8 +1,7 @@
1
+ import type { EventRegistry } from "./events";
1
2
  import type { PiniaPluginContext, StateTree, Store, SubscriptionCallbackMutation } from "pinia";
2
3
  import type { StoreOptions } from "./store";
3
- import type { PluginSubscriberInterface } from ".";
4
- import Tracer from "../system/Tracer";
5
- export type CreateStateSnapshotKeys = (keyof StateTree)[] | 'all';
4
+ import type { Tracer, TracerHandler } from "./trace";
6
5
  export type PluginHydrationScheduler = (callback: () => void) => void;
7
6
  export type PluginHydrationTiming = 'defer' | 'immediate';
8
7
  export type PluginRuntimeEnvironment = 'client' | 'server';
@@ -18,6 +17,16 @@ export interface PluginExecutionOptions {
18
17
  * @type {PluginHydrationTiming | undefined}
19
18
  */
20
19
  hydration?: PluginHydrationTiming;
20
+ /**
21
+ * Order of execution for the plugin subscriber.
22
+ * Lower numbers are executed first.
23
+ * @type {number | undefined}
24
+ */
25
+ order?: number;
26
+ }
27
+ export interface PluginOptions {
28
+ eventsRegistry?: EventRegistry;
29
+ createTracer?: (source: string) => Tracer;
21
30
  }
22
31
  export interface PluginStoreOptions {
23
32
  /**
@@ -26,14 +35,12 @@ export interface PluginStoreOptions {
26
35
  */
27
36
  storeOptions?: StoreOptions;
28
37
  }
29
- export interface PluginSubscriptionOptions {
38
+ export interface PluginOrchestratorOptions {
30
39
  /**
31
- * Creates a new tracer instance for the plugin subscriber.
32
- * @param source The source string used to create the tracer.
33
- * @returns A new instance of the Tracer class.
34
- * @see Tracer for more details on the tracer implementation.
40
+ * Tracer handler for the plugin subscriber.
41
+ * Provided by a tracing package such as `pinia-plugin-subscription-devtools`; tracing is inactive without it.
35
42
  */
36
- createTracer?: (source: string) => Tracer;
43
+ tracerHandler?: TracerHandler;
37
44
  /**
38
45
  * Execution options for the plugin subscriber.
39
46
  * This determines how and when the plugin subscriber should be executed.
@@ -50,6 +57,13 @@ export interface PluginSubscriptionOptions {
50
57
  * @type {PluginHydrationScheduler | undefined}
51
58
  */
52
59
  hydrationScheduler?: PluginHydrationScheduler;
60
+ /**
61
+ * Maximum time (ms) a `hydration: 'defer'` subscriber keeps its trace group open while its
62
+ * asynchronous hydration hooks are pending. Once elapsed, the group is closed and a
63
+ * `subscriber:deferred:timeout` error is traced. Defaults to 2500.
64
+ * @type {number | undefined}
65
+ */
66
+ deferredTraceGroupTimeout?: number;
53
67
  /**
54
68
  * Runtime environment for the plugin subscriber.
55
69
  * @type {PluginRuntimeEnvironment | undefined}
@@ -62,7 +76,7 @@ export interface PluginSubscriptionOptions {
62
76
  */
63
77
  subscriberExecution?: Record<string, PluginExecutionOptions>;
64
78
  }
65
- export interface PluginSubscriber {
79
+ export interface StoreSubscriberInterface {
66
80
  /**
67
81
  * Execution options for the plugin subscriber.
68
82
  * This determines how and when the plugin subscriber should be executed.
@@ -82,16 +96,15 @@ export interface PluginSubscriber {
82
96
  * Function to invoke the plugin subscriber.
83
97
  * Returns a boolean indicating whether the invocation was successful.
84
98
  */
85
- invoke: (context: PiniaPluginContext, createTracer?: (source: string) => Tracer) => boolean;
99
+ invoke: StoreSubscriberInvoke;
86
100
  /**
87
101
  * Name of the plugin subscriber.
88
102
  */
89
103
  name: string;
90
104
  /**
91
- * Callback to reset the store associated with the plugin subscriber.
92
- * This is typically used to clean up or reinitialize the store.
105
+ * Options the plugin subscriber was created with (used for tracing).
93
106
  */
94
- resetStoreCallback?: (store?: Store) => void;
107
+ pluginOptions?: EnhancedStoreOptions;
95
108
  /**
96
109
  * Subscription for store actions.
97
110
  * This is typically used to listen for and respond to actions dispatched in the store.
@@ -107,12 +120,18 @@ export interface PluginSubscriber {
107
120
  * This is typically used to manage and respond to various plugin-specific events or changes.
108
121
  */
109
122
  subscriptions?: PluginSubscriptions;
123
+ /**
124
+ * Tracer for the plugin subscriber.
125
+ * This is typically used for tracing and debugging purposes.
126
+ */
127
+ tracer?: Tracer;
110
128
  /**
111
129
  * Callback to be executed after the hydration of the plugin subscriber.
112
130
  * This is typically used to perform any post-hydration logic.
113
131
  */
114
132
  afterHydration?: (context: PiniaPluginContext) => void | Promise<void>;
115
133
  }
134
+ export type StoreSubscriberInvoke = (context: PiniaPluginContext, pluginOptions?: PluginOptions) => boolean;
116
135
  /**
117
136
  * Return type for native Pinia subscriptions.
118
137
  * @template Callback - The type of the callback function for the subscription.
@@ -129,7 +148,7 @@ export interface NativePiniaSubscriptionReturn<Callback> {
129
148
  */
130
149
  callback: Callback;
131
150
  }
132
- export type StoreClassConstructorParams = Omit<PluginSubscriptionOptions, 'createTracer'> & {
151
+ export type EnhancedStoreOptions = Omit<PluginOrchestratorOptions, 'createTracer'> & Omit<Partial<PluginOptions>, 'createTracer'> & {
133
152
  createTracer?: () => Tracer;
134
153
  storeOptions?: StoreOptions;
135
154
  };
@@ -157,16 +176,16 @@ export interface StoreOnActionCallbackParameters {
157
176
  */
158
177
  onError: StoreOnActionOnErrorCallbackParameter;
159
178
  }
160
- export interface PluginSubscription {
179
+ export interface PluginOrchestrator {
161
180
  stores?: Store[];
162
- subscription: PluginSubscriberInterface;
181
+ subscription: StoreSubscriberInterface;
163
182
  subscriptionOptions?: StoreOptions;
164
183
  }
165
- export type PluginSubscriptions = Record<string, PluginSubscription>;
184
+ export type PluginSubscriptions = Record<string, PluginOrchestrator>;
166
185
  export type NativePiniaSubscription<Callback> = () => NativePiniaSubscriptionReturn<Callback>;
167
186
  export type StoreOnActionSubscriptionCallback = (params: StoreOnActionCallbackParameters) => void;
168
187
  export type StoreOnActionSubscriptionReturn = NativePiniaSubscriptionReturn<StoreOnActionSubscriptionCallback>;
169
- export type StoreOnActionSubscription = NativePiniaSubscription<StoreOnActionSubscriptionCallback>;
170
- export type StoreMutationSubscriptionCallback = (mutation: SubscriptionCallbackMutation<StateTree>) => void;
188
+ export type StoreOnActionSubscription = (context: PiniaPluginContext) => StoreOnActionSubscriptionReturn;
189
+ export type StoreMutationSubscriptionCallback = (mutation: SubscriptionCallbackMutation<StateTree>) => void | Promise<void>;
171
190
  export type StoreMutationSubscriptionReturn = NativePiniaSubscriptionReturn<StoreMutationSubscriptionCallback>;
172
191
  export type StoreMutationSubscription = NativePiniaSubscription<StoreMutationSubscriptionCallback>;
@@ -1,8 +1,8 @@
1
1
  import type { AnyObject, StoreOptionsExtensions } from ".";
2
- import type { CreateStateSnapshotKeys } from "./plugin";
3
2
  import type { Pinia, PiniaCustomProperties, StateTree, Store, StoreDefinition, StoreGeneric } from "pinia";
4
3
  import type { Ref } from "vue";
5
4
  export type CustomStore<TStore, TState> = Store & TStore & TState & PiniaCustomProperties & StoreDefinition;
5
+ export type DefineAStoreSetup<S, TExtraExtensions extends Record<string, unknown> = EmptyExtensions> = (ctx: DefineAStoreSetupContext<S, TExtraExtensions>) => S;
6
6
  /**
7
7
  * Store definition returned by `defineAStore`/`defineAStoreCtx` when explicit
8
8
  * store/state types are provided, e.g. for stores augmented by plugins with
@@ -44,13 +44,6 @@ export interface DefineAStoreSetupContext<TEnhancedStore, TExtraExtensions exten
44
44
  export type DefineAugmentedStore<TStore, TState> = (args?: any) => CustomStore<TStore, TState>;
45
45
  export type ObjectBaseProperty = Record<string, OptionBaseProperty | OptionBaseProperty[]>;
46
46
  export type OptionBaseProperty = string | number | boolean | null | undefined | object | Function;
47
- export interface RollbackActionParams {
48
- action: string;
49
- stateKeys?: CreateStateSnapshotKeys;
50
- }
51
- export type RollbackAfterFailureParams = {
52
- [key: string]: CreateStateSnapshotKeys;
53
- };
54
47
  export type StatePropertyValue = StdStatePropertyValue | Ref<StdStatePropertyValue> | StdStatePropertyValue[] | Ref<StdStatePropertyValue[]> | Ref<StdStatePropertyValue>[];
55
48
  type StdStatePropertyValue = AnyObject | boolean | null | number | string | undefined;
56
49
  /**
@@ -64,7 +57,6 @@ type StdStatePropertyValue = AnyObject | boolean | null | number | string | unde
64
57
  */
65
58
  export type StoreOptions = StoreOptionsExtensions & {
66
59
  [key: string]: StoreOptionsPropertyValue;
67
- rollbackAfterFailure?: RollbackAfterFailureParams;
68
60
  };
69
61
  export type StoreOptionsPropertyValue = OptionBaseProperty | OptionBaseProperty[] | ObjectBaseProperty | ObjectBaseProperty[];
70
62
  export {};