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,19 +1,4 @@
1
1
  import type { AnyObject } from './index';
2
- import type Tracer from '../system/Tracer';
3
- export interface AddTraceListenerOptions {
4
- filterMode?: TraceListenerFilterMode;
5
- groupBy?: TraceListenerGroupBy;
6
- }
7
- export interface ConsoleTraceEventArgs extends Omit<TraceEvent, 'level' | 'timestamp'> {
8
- }
9
- export type ConsoleTraceEvent = Array<string | ConsoleTraceEventArgs>;
10
- /**
11
- * A trace listener that supports grouping of trace events.
12
- */
13
- export interface ConsoleTraceGroupListener extends Omit<TraceListener, 'filter'> {
14
- createGroup: (key: string) => void;
15
- logGroup: (key: string) => void;
16
- }
17
2
  /**
18
3
  * Severity of a trace event.
19
4
  * `error` events are delivered to matching listeners, or reported with `console.error` when none match.
@@ -24,6 +9,13 @@ export type TraceLevel = 'debug' | 'error';
24
9
  * Filters receive this object so that discarded events never allocate a payload.
25
10
  */
26
11
  export interface TraceEventMetadata {
12
+ /** Identifier of the store action this event belongs to, shared by every event of the same action. */
13
+ actionId?: string;
14
+ /**
15
+ * Group (lifecycle) this event belongs to. It is a `/` separated path whose prefixes are the parent groups,
16
+ * e.g. `cart/onAction:add#3/after`, see `traceGroups`.
17
+ */
18
+ groupId?: string;
27
19
  /** Label describing the event, e.g. `Subscription:Delivery`. */
28
20
  label: string;
29
21
  /** Severity of the trace event. */
@@ -32,7 +24,7 @@ export interface TraceEventMetadata {
32
24
  namespace: string;
33
25
  /** Instance the event belongs to, usually a Pinia `store.$id`. */
34
26
  scope?: string;
35
- /** Emitting class name, e.g. `PluginSubscription`. */
27
+ /** Emitting class name, e.g. `PluginOrchestrator`. */
36
28
  source: string;
37
29
  }
38
30
  /**
@@ -41,81 +33,49 @@ export interface TraceEventMetadata {
41
33
  export interface TraceEvent extends TraceEventMetadata {
42
34
  error?: unknown;
43
35
  payload?: AnyObject;
36
+ /** Monotonic position of the event in its registry. Set when a journal is enabled. */
37
+ seq?: number;
38
+ /** Environment that emitted the event. Set when a journal is enabled. */
39
+ side?: TraceSide;
44
40
  timestamp: number;
45
41
  }
46
- type TraceFilterCriterionComparator = 'equals' | 'contains' | 'startsWith' | 'endsWith';
47
- export interface TraceFilterCriterion {
48
- comparator: TraceFilterCriterionComparator;
49
- target: keyof TraceEventMetadata;
50
- value: string;
51
- }
52
- export interface TraceFilterFactoryCriterion extends Omit<TraceFilterCriterion, 'comparator'> {
53
- comparator?: TraceFilterCriterionComparator;
54
- }
55
- export type TraceFilterFactoryParam = {
56
- handler?: TraceHandler;
57
- filter: TraceFilter | TraceFilterFactoryCriterion[];
58
- };
42
+ /** Environment that emitted a trace event. */
43
+ export type TraceSide = 'client' | 'server';
59
44
  /**
60
45
  * Event data. Prefer the function form: it is only invoked when a listener
61
46
  * actually consumes the event, keeping unobserved tracing allocation free.
62
47
  */
63
48
  export type TracePayload = AnyObject | (() => AnyObject);
64
- /**
65
- * Determines whether a trace event should be delivered to a listener.
66
- * @param event The metadata of the trace event.
67
- * @returns `true` if the event should be delivered to the listener, otherwise `false`.
68
- */
69
- export type TraceFilter = (event: TraceEventMetadata) => boolean;
70
- export interface TraceGroupListener extends ConsoleTraceGroupListener, TraceListener {
71
- groupBy: TraceListenerGroupBy;
72
- }
73
49
  /**
74
50
  * Handles a trace event.
75
51
  * @param event The resolved trace event to handle.
76
52
  */
77
53
  export type TraceHandler = (event: TraceEvent) => void;
78
- export interface TraceListener {
79
- /** Evaluated on every event; return `true` to receive it. */
80
- filter: TraceFilter;
81
- /** Handles the trace event when it passes the filter. */
82
- handler: TraceHandler;
54
+ /**
55
+ * Emits structured trace events. Implemented by tracing packages such as `pinia-plugin-subscription-devtools`.
56
+ * The plugin only emits: it never reads, stores or displays events.
57
+ */
58
+ export interface Tracer {
59
+ /** Instance the emitted events belong to, usually a Pinia `store.$id`. */
60
+ scope?: string;
61
+ readonly source: string;
62
+ debug: (label: string, namespace: string, payload?: TracePayload, scope?: string) => void;
63
+ error: (label: string, namespace: string, error: unknown, payload?: TracePayload, scope?: string) => void;
83
64
  }
84
- export type TraceListenerFilterMode = 'every' | 'some';
85
- export type TraceListenerGroupBy = keyof TraceEventMetadata;
86
- /** Removes a previously registered listener. */
87
- export type TraceListenerRemover = () => void;
88
- export type TraceRegistry = {
89
- /**
90
- * Registers a trace listener and returns its remover.
91
- * Listeners are evaluated in registration order.
92
- * @param listener The trace listener to register.
93
- * @returns A function that removes the registered listener when called.
94
- */
95
- addTraceListener: (listener: TraceListener | TraceFilterFactoryParam, filterMode?: 'every' | 'some') => TraceListenerRemover;
96
- /**
97
- * Clears all registered trace listeners.
98
- */
99
- clearTraceListeners: () => void;
65
+ /**
66
+ * Contract between the plugin and a tracing package, passed through `PluginOrchestratorOptions.tracerHandler`.
67
+ * Without a handler, tracing is inactive and costs nothing.
68
+ */
69
+ export interface TracerHandler {
70
+ /** Closes the group of the event's group key. */
71
+ closeGroup: (payload: TraceEvent) => void;
100
72
  /**
101
73
  * Creates a new tracer instance.
102
74
  * @param source The source of the tracer, usually the class name.
103
- * @param options Optional tracer options.
104
- * @returns A new tracer instance.
105
- */
106
- createTracer: (source: string, options?: TracerOptions) => Tracer;
107
- /**
108
- * Checks if there are any registered trace listeners.
109
- * @returns True if there are registered trace listeners, otherwise false.
110
75
  */
111
- hasTraceListeners: () => boolean;
112
- };
113
- export interface TracerHandler {
114
- closeGroup: (payload: TraceEvent) => undefined;
115
76
  createTracer: (source: string) => Tracer;
116
- startGroup: (payload: TraceEvent) => undefined;
117
- }
118
- export interface TracerOptions {
119
- scope?: string;
77
+ /** Reports whether this handler currently consumes events. Lets the plugin skip building them when `false`. */
78
+ hasTraceListeners?: () => boolean;
79
+ /** Opens a group for the event's group key. */
80
+ startGroup: (payload: TraceEvent) => void;
120
81
  }
121
- export {};
@@ -0,0 +1,4 @@
1
+ export declare const pluginName: string;
2
+ export declare const pluginOrchestratorClassName = "PluginOrchestrator";
3
+ /** Prefix of every namespace emitted by `PluginOrchestrator` (labels are lowercased into namespaces). */
4
+ export declare const pluginSubscriptionNamespace: string;
@@ -0,0 +1,3 @@
1
+ import type { Store } from 'pinia';
2
+ export declare function defineStoreEventSubscriberId(prefix: string, store: Store): string;
3
+ export declare function definePPSEventSubscriberId(store: Store): string;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Generate a unique ID for a given string.
3
+ * @param id The base string to generate a unique ID for.
4
+ * @returns A unique ID string.
5
+ */
6
+ export declare function generateUniqueId(id: string): string;
7
+ /**
8
+ * Get the unique ID assigned to an instance.
9
+ * @param instance The instance to get the unique ID for.
10
+ * @returns The unique ID of the instance, or undefined if not registered.
11
+ */
12
+ export declare function getInstanceUid(instance: object): string | undefined;
13
+ /**
14
+ * Register an instance with a unique ID.
15
+ * @param id The base string to generate a unique ID for.
16
+ * @param instance The instance to register.
17
+ * @returns The unique ID assigned to the instance.
18
+ */
19
+ export declare function registerInstance(id: string, instance: object): string;
20
+ export declare function unregisterInstance(instance: object): void;
@@ -1,73 +1,4 @@
1
- import type { DefineStoreOptions, SetupStoreDefinition, StateTree, Store, StoreDefinition, _GettersTree } from "pinia";
2
- import type { AnyObject } from "../types";
3
- import type { DefineAStoreDefinition, DefineAStoreSetupContext, EmptyExtensions, StoreOptions } from "../types/store";
4
- import type { PluginStoreOptions } from "../types/plugin";
5
- export declare const itemState: {
6
- '@id': undefined;
7
- id: undefined;
8
- };
9
- type DefineAStoreSetup<SS, TExtraExtensions extends Record<string, unknown> = EmptyExtensions> = (ctx: DefineAStoreSetupContext<SS, TExtraExtensions>) => SS;
10
- /**
11
- * Define a store with the setup API. State, getters and actions are inferred
12
- * from the setup function return, exactly like pinia's `defineStore`, so
13
- * store members are listed by IDEs and action prototypes are preserved.
14
- * @template Id The type of the store identifier.
15
- * @template SS The type of the setup function return.
16
- * @param id The unique identifier for the store.
17
- * @param storeDefinition The setup function defining the store.
18
- * @param options Optional store options.
19
- * @returns The defined store.
20
- */
21
- export declare function defineAStore<Id extends string, SS>(id: Id, storeDefinition: (ctx?: DefineAStoreSetupContext<AnyObject>) => SS, options?: StoreOptions): SetupStoreDefinition<Id, SS>;
22
- /**
23
- * Define a store with the options API, mirroring pinia's `defineStore` inference.
24
- * @template Id The type of the store identifier.
25
- * @template S The type of the store's state.
26
- * @template G The type of the store's getters.
27
- * @template A The type of the store's actions.
28
- * @param id The unique identifier for the store.
29
- * @param storeDefinition The store options definition.
30
- * @param options Optional store options.
31
- * @returns The defined store.
32
- */
33
- export declare function defineAStore<Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(id: Id, storeDefinition: Omit<DefineStoreOptions<Id, S, G, A>, 'id'>, options?: StoreOptions): StoreDefinition<Id, S, G, A>;
34
- /**
35
- * Define a store with explicit store/state types, e.g. when plugins augment
36
- * the store with members not returned by the setup function.
37
- * @template Sto The type of the store.
38
- * @template Sta The type of the store's state.
39
- * @param id The unique identifier for the store.
40
- * @param storeDefinition The store definition, either as an options object or a setup function.
41
- * @param options Optional store options.
42
- * @returns The defined store.
43
- */
44
- export declare function defineAStore<Sto, Sta>(id: string, storeDefinition: Omit<DefineStoreOptions<string, StateTree & Sta, AnyObject, Partial<Sto>>, 'id'> | ((ctx?: DefineAStoreSetupContext<Sto & Sta>) => AnyObject), options?: StoreOptions): DefineAStoreDefinition<Sto, Sta>;
45
- /**
46
- * Define a store with a required, fully typed setup context. State, getters
47
- * and actions are inferred from the setup function return.
48
- * @template Id The type of the store identifier.
49
- * @template SS The type of the setup function return.
50
- * @template TExtraExtensions The type of the store's extra extensions.
51
- * @param id The unique identifier for the store.
52
- * @param storeDefinition The setup function defining the store.
53
- * @param options Optional store options.
54
- * @returns The defined store with setup context.
55
- */
56
- export declare function defineAStoreCtx<Id extends string, SS, TExtraExtensions extends Record<string, unknown> = EmptyExtensions>(id: Id, storeDefinition: (ctx: DefineAStoreSetupContext<AnyObject, TExtraExtensions>) => SS, options?: StoreOptions): SetupStoreDefinition<Id, SS>;
57
- /**
58
- * Define a store with a setup context and explicit store/state types, e.g.
59
- * when plugins augment the store with members not returned by the setup function.
60
- * @template Sto The type of the store.
61
- * @template Sta The type of the store's state.
62
- * @template TExtraExtensions The type of the store's extra extensions.
63
- * @param id The unique identifier for the store.
64
- * @param storeDefinition The setup function defining the store.
65
- * @param options Optional store options.
66
- * @returns The defined store with setup context.
67
- */
68
- export declare function defineAStoreCtx<Sto, Sta, TExtraExtensions extends Record<string, unknown> = EmptyExtensions>(id: string, storeDefinition: (ctx: DefineAStoreSetupContext<Sto & Sta, TExtraExtensions>) => AnyObject, options?: StoreOptions): DefineAStoreDefinition<Sto, Sta>;
69
- export declare function defineAStoreSetup<Id extends string, SS>(id: Id, storeDefinition: DefineAStoreSetup<SS>, options: PluginStoreOptions): SetupStoreDefinition<Id, SS>;
70
- export declare function defineAStoreOptionApi<Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(id: Id, storeDefinition: Omit<DefineStoreOptions<Id, S, G, A>, 'id'>, options?: PluginStoreOptions): StoreDefinition<Id, S, G, A>;
1
+ import type { Store } from "pinia";
71
2
  /**
72
3
  * Check if a property has a denied first character : ['_', '$'].
73
4
  * @param property The property name to check.
@@ -75,29 +6,11 @@ export declare function defineAStoreOptionApi<Id extends string, S extends State
75
6
  */
76
7
  export declare function hasDeniedFirstChar(property: string): boolean;
77
8
  /**
78
- * Get the setup context associated with a store instance (or pending store id).
79
- * @param store The store instance, or an object exposing the store `$id`.
80
- * @returns The setup context when the store was defined with `enhancedStore` enabled, undefined otherwise.
81
- */
82
- export declare function getDefineAStoreSetupContext(store: object): DefineAStoreSetupContext<AnyObject> | undefined;
83
- /**
84
- * Get the enhanced store from the context.
85
- * @template TEnhancedStore The type of the enhanced store.
86
- * @param ctx The context containing the extensions.
87
- * @returns The enhanced store casted to the specified types.
88
- */
89
- export declare function getEnhancedStore<TEnhancedStore>(ctx: DefineAStoreSetupContext<TEnhancedStore>): TEnhancedStore;
90
- /**
91
- * Set the enhanced store in context with deprecated alias support.
92
- * @template TEnhancedStore The type of the enhanced store.
93
- * @param ctx The context containing the extensions.
94
- * @param store The store to expose as enhancement.
95
- */
96
- export declare function setEnhancedStore<TEnhancedStore>(ctx: DefineAStoreSetupContext<TEnhancedStore>, store: TEnhancedStore): void;
97
- /**
98
- * @deprecated Use getEnhancedStore instead.
9
+ * Get the unique ID assigned to a store instance.
10
+ * @param store The store instance to get the unique ID for.
11
+ * @returns The unique ID of the store instance.
99
12
  */
100
- export declare function getExtendingStore<TEnhancedStore>(ctx: DefineAStoreSetupContext<TEnhancedStore>): TEnhancedStore;
13
+ export declare function getStoreInstanceId(store: Store): string;
101
14
  /**
102
15
  * Get the storage key for a given action and store combination.
103
16
  * @param actionName The name of the action.
@@ -105,4 +18,7 @@ export declare function getExtendingStore<TEnhancedStore>(ctx: DefineAStoreSetup
105
18
  * @returns The storage key for the action and store combination.
106
19
  */
107
20
  export declare function getActionStoreKey(actionName: string, store: Store): string;
108
- export {};
21
+ export declare function getStoreKeyPrefix(store: Pick<Store, '$id'>): string;
22
+ export declare function getStoreScopedKey(value: string, store: Pick<Store, '$id'>): string;
23
+ export declare function getStoreInstanceKeyPrefix(store: Store): string;
24
+ export declare function getStoreInstanceScopedKey(value: string, store: Store): string;