pinia-plugin-subscription 1.0.0-beta.19 → 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 (54) hide show
  1. package/CHANGELOG.md +26 -16
  2. package/README.md +16 -9
  3. package/dist/chunks/setup-context-3G3Hy1ep.js +909 -0
  4. package/dist/dictionaries/plugin-subscription-event.d.ts +1 -0
  5. package/dist/{core/Store.d.ts → enhanced-store/EnhancedStore.d.ts} +11 -11
  6. package/dist/enhanced-store/define-a-store.d.ts +64 -0
  7. package/dist/enhanced-store/setup-context.d.ts +27 -0
  8. package/dist/events/EventsEmitter.d.ts +9 -0
  9. package/dist/events/EventsSubscriber.d.ts +32 -0
  10. package/dist/events/EventsSubscription.d.ts +13 -0
  11. package/dist/events/StoreEvent.d.ts +6 -0
  12. package/dist/events/StoresEventsSubscriptionsRegistry.d.ts +31 -0
  13. package/dist/events/event.d.ts +8 -0
  14. package/dist/factories/events-registry.d.ts +9 -0
  15. package/dist/factories/events-subscriber.d.ts +11 -0
  16. package/dist/factories/messenger-registry.d.ts +16 -0
  17. package/dist/factories/plugin-subscriber.d.ts +2 -2
  18. package/dist/helpers.js +116 -130
  19. package/dist/{helpers/index.d.ts → lib/helpers.d.ts} +5 -6
  20. package/dist/lib/main.d.ts +2 -1
  21. package/dist/orchestration/ExecutionPolicy.d.ts +15 -0
  22. package/dist/orchestration/PluginOrchestrator.d.ts +41 -0
  23. package/dist/orchestration/SubscriberScheduler.d.ts +31 -0
  24. package/dist/pinia-plugin-subscription.js +34 -10
  25. package/dist/plugins/createPlugin.d.ts +2 -2
  26. package/dist/services/messenger.d.ts +28 -0
  27. package/dist/services/messengerProvider.d.ts +45 -0
  28. package/dist/store-hooks/StoreHooksBinder.d.ts +20 -0
  29. package/dist/store-hooks/StoreHooksRegistry.d.ts +34 -0
  30. package/dist/{core/PluginSubscriber.d.ts → subscribers/StoreSubscriber.d.ts} +14 -10
  31. package/dist/tracing/TraceGroupHelper.d.ts +27 -0
  32. package/dist/{events/events-emitter.d.ts → tracing/TraceableEmitter.d.ts} +6 -19
  33. package/dist/tracing/TraceableEventsSubscriber.d.ts +21 -0
  34. package/dist/tracing/TraceableStoreEventsSubscriber.d.ts +16 -0
  35. package/dist/types/core.d.ts +12 -0
  36. package/dist/types/events.d.ts +24 -0
  37. package/dist/types/index.d.ts +5 -5
  38. package/dist/types/messenger.d.ts +20 -0
  39. package/dist/types/plugin.d.ts +11 -12
  40. package/dist/types/store.d.ts +1 -0
  41. package/dist/types/trace.d.ts +2 -2
  42. package/dist/utils/constants.d.ts +2 -2
  43. package/dist/utils/events.d.ts +3 -0
  44. package/dist/utils/id.d.ts +20 -0
  45. package/dist/utils/store.d.ts +7 -87
  46. package/docs/authoring-plugins.md +127 -64
  47. package/docs/using-plugins.md +8 -5
  48. package/package.json +3 -3
  49. package/dist/chunks/PluginSubscription-A8aoeAIQ.js +0 -599
  50. package/dist/core/PluginSubscription.d.ts +0 -95
  51. package/dist/events/events-registry.d.ts +0 -23
  52. package/dist/events/store-events-subscriber-handler.d.ts +0 -9
  53. package/dist/events/store-events-subscriber.d.ts +0 -16
  54. /package/dist/{utils/traceGroups.d.ts → tracing/trace-groups.d.ts} +0 -0
package/CHANGELOG.md CHANGED
@@ -7,48 +7,57 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Changed
11
+ - Refactored the `PluginOrchestrator` into focused collaborators: `ExecutionPolicy` and `SubscriberScheduler` in `src/orchestration/`, `StoreHooksRegistry` and `StoreHooksBinder` in `src/store-hooks/`, and `TraceGroupHelper` in `src/tracing/`; this keeps orchestration, per-store hooks, and tracing responsibilities separate without changing subscriber delivery behavior.
12
+ - Updated the public API names to match the reorganized implementation: import `StoreSubscriber`, `EnhancedStore`, and `PluginOrchestrator` from `pinia-plugin-subscription/helpers`, and use `StoreSubscriberInterface` from `pinia-plugin-subscription/types`.
13
+
10
14
  ### Added
11
- - SSR demo subscribers are now class-based (`PluginSubscriber` + a `Store` subclass) and emit traces through the tracer `PluginSubscription` provides (`this.trace()` / `this.traceError()`): hydration start/done, errors, and the `onMutation` trace group, all scoped to the store; the per-request tracer registry also listens on the server, so server traces print in the Node terminal and browser traces in the devtools console
15
+ - SSR demo subscribers are now class-based (`StoreSubscriber` + an `EnhancedStore` subclass) and emit traces through the tracer `PluginOrchestrator` provides (`this.trace()` / `this.traceError()`): hydration start/done, errors, and the `onMutation` trace group, all scoped to the store; the per-request tracer registry also listens on the server, so server traces print in the Node terminal and browser traces in the devtools console
12
16
  - SSR demo shows a concrete case of environment-aware hydration: one store filled by a server-only subscriber (request data) and a client-only deferred subscriber (browser data), with a server/browser panel layout and a hydration timeline; the `Accept-Language` header is validated before being serialized
13
17
  - Added a Vite SSR demo server (`npm run dev:ssr`) with per-request app/plugin instances, async server-side subscriber hydration, and Pinia state serialization for client hydration
14
18
  - Demo: `defineAStoreCtx` enhanced-store example; the demo app now resolves the package to `src/` through Vite aliases (no build needed)
15
19
  - Added `pinia-plugin-subscription` to the package `keywords`
16
- - `traceGroups` glossary, exported from `pinia-plugin-subscription/helpers`, building the `groupId` emitted by `PluginSubscription` (`onActionAfter`, `onActionError`, `onMutation`, `reset`); documented in `docs/tracing.md`
20
+ - `traceGroups` glossary, exported from `pinia-plugin-subscription/helpers`, building the `groupId` emitted by `PluginOrchestrator` (`onActionAfter`, `onActionError`, `onMutation`, `reset`); documented in `packages/devtools/docs/tracing.md`
17
21
  - `createPluginSubscriber(name, invoke, optionalProps?)` factory, exported from `pinia-plugin-subscription/helpers`, to build plain-object subscribers; documented in `docs/authoring-plugins.md` and used by the demo plugin
18
- - Added a dedicated tracing guide, `docs/tracing.md`; the README now only presents tracing and links to it
19
- - Moved the API reference, import subpaths, development, `Store` summary and testing sections from `README.md` to `docs/authoring-plugins.md`
20
- - Added `trace()` and `traceError()` to `Store` and `PluginSubscription`
22
+ - Added a dedicated tracing guide, `packages/devtools/docs/tracing.md`; the README now only presents tracing and links to it
23
+ - Moved the API reference, import subpaths, development, `EnhancedStore` summary and testing sections from `README.md` to `docs/authoring-plugins.md`
24
+ - Added `trace()` and `traceError()` to `EnhancedStore` and `PluginOrchestrator`
21
25
  - Added trace types: `TraceEvent`, `TraceEventMetadata`, `TraceHandler`, `TraceLevel`, `TraceListener`, `TraceListenerRemover` and `TracePayload`
22
26
  - Added `check:leaks` script and `scripts/check-dist-leaks.mjs`, guarding published artifacts against non-peer dependency imports in `dist/` (`.js` and `.d.ts`)
23
27
  - Added the leak guard to `npm run build` and to the CI workflow
24
28
  - Added unit tests covering the tracing system and the leak detection logic
25
29
 
26
30
  ### Changed
27
- - `PluginSubscriber` passes the shared store-events handler to its `Store` instance; both can register store-event subscribers.
28
- - Subscribers now execute in ascending `execution.order` order (default `9`), preserving declaration order for equal priorities; `PluginSubscriber.tracer` exposes the tracer created for that subscriber and passes the same instance to its `Store` factory.
31
+ - `StoreSubscriber` passes the shared event registry to its `EnhancedStore` instance; both can register event subscriptions.
32
+ - Subscribers now execute in ascending `execution.order` order (default `9`), preserving declaration order for equal priorities; `StoreSubscriber.tracer` exposes the tracer created for that subscriber and passes the same instance to its `EnhancedStore` factory.
29
33
  - Trace groups follow the lifecycles of a store: `groupId` is now a `/` separated path (`<storeId>/plugin`, `<storeId>/onAction:<name>#<n>`, with `/after` and `/onError` sub-groups, `<storeId>/onMutation#<n>`, `<storeId>/reset#<n>`, `<storeId>/plugin/deferred:<subscriber>#<n>`). Every trace emitted for a store while a group is open, including those of subscribers and `Store` subclasses, joins the innermost one. `traceGroups` builders take the call counter; `TraceGroupId` is removed, and `groupId` is lifted from the payload to the event like `actionId`
30
34
  - `storeMutationSubscription` now supports several subscribers per store, like `storeOnActionSubscription`: a single `$subscribe` is registered per store and dispatches to every callback. A failing callback is reported through `traceError` without blocking the others, subscriber names are tracked, and the hook is unsubscribed when the store is disposed
31
35
  - `storeOnActionSubscription` now receives the Pinia plugin context; action trace labels include the invoked action name
32
36
  - Subscribers deferred with `hydration: 'defer'` now run inside their own trace group (label `subscriber:deferred`, keyed by store scope), which stays open until their asynchronous `hydrate()` / `afterHydration()` settle, so their traces are no longer emitted outside any group once `plugin()` has closed its `context` group. A hydration that never settles can no longer buffer the store's traces forever: the group is force-closed after `deferredTraceGroupTimeout` ms (new option, default 2500) and a `subscriber:deferred:timeout` error is traced
33
37
  - Moved the demo application from `src/demo/` to the repository-level `demo/` directory, separating example code from the library source and tracking the demo files in version control.
34
38
  - Clarified that SSR tracing registries and plugin instances must be created per request, and added coverage for listener isolation between registries
35
- - Replaced the `Debug` abstract class with composition: `Store` and `PluginSubscription` no longer extend it and now own a `Tracer`
39
+ - Replaced the `Debug` abstract class with composition: `EnhancedStore` and `PluginOrchestrator` no longer extend it and now own a `Tracer`
36
40
  - Trace errors with no matching listener, and failures in listener filters or handlers, are now reported with `console.error` instead of rethrowing the original error
37
41
  - Trace payloads are now lazily resolved, so unobserved tracing no longer builds log arguments
38
- - Internal events now derive namespaces from trace labels, with `PluginSubscription` prefixed by default; store events carry `store.$id` as `scope`
42
+ - Internal events now derive namespaces from trace labels, with `PluginOrchestrator` prefixed by default; store events carry `store.$id` as `scope`
39
43
  - `Tracer` now delegates all output decisions to listeners and no longer owns a console or an enabled state
40
44
  - Error events are delivered to matching listeners and reported with `console.error` when none match
41
45
  - Store trace labels can include the store class name by default, and callers can override the event namespace
42
46
  - Excluded `scripts/**` from coverage thresholds, as build tooling is not shipped to consumers
43
- - `Store`, `PluginSubscriber` and `PluginSubscription` share store-event emission and tracing helpers; `Store` receives the shared event handler from its subscriber.
47
+ - `EnhancedStore`, `StoreSubscriber` and `PluginOrchestrator` share store-event emission and tracing helpers; `EnhancedStore` receives the shared event handler from its subscriber.
44
48
  - `Tracer` listener matching no longer allocates intermediate arrays beyond the matched set, and duplicated unhandled-error reporting was factored out
45
- - The `pluginsubscription` namespace prefix is now a shared constant (`pluginSubscriptionNamespace`) instead of a hard-coded string
49
+ - Devtools now recognizes the `pluginorchestrator` namespace emitted by `PluginOrchestrator` when including context events in filtered traces.
46
50
 
47
51
  ### Changed (breaking)
48
- - Class-based subscribers now define a unique `_name` instead of passing a name to `super()`; `PluginSubscriber` takes only its instance factory. `Store` subclasses use `_name` instead of `_className`. `trace()` and `traceError()` are protected, and subclasses configure label prefixes through `traceOptions.prefixLabelWithName` instead of `classNameAddedToTraceNamespace`.
49
- - Removed `PluginSubscriberWithoutInstance`; use a plain-object subscriber or `PluginSubscriber` with a `Store` subclass.
52
+ - **Breaking:** renamed the helper exports `PluginSubscriber`, `Store`, and `PluginSubscription` to `StoreSubscriber`, `EnhancedStore`, and `PluginOrchestrator`; the subscriber interface is now `StoreSubscriberInterface`. Update imports from the `helpers` and `types` subpaths.
53
+ - Action event consumers receive an event whose lazy payload is an object with named fields rather than positional callback arguments. See the [event registration guide](docs/authoring-plugins.md#store-event-registration).
54
+ - Registering multiple callbacks for the same event now keeps all callbacks rather than replacing the previous one.
55
+ - The second argument to a subscriber's `invoke()` is now an options object containing `createTracer` and `eventsRegistry`, rather than the tracer factory itself.
56
+ - Replace `PluginSubscriber.storeInstance` with `getStoreInstance(store)` so asynchronous hooks can retrieve the wrapper for their own Pinia store.
57
+ - Class-based subscribers now define a unique `_name` instead of passing a name to `super()`; `StoreSubscriber` takes only its instance factory. `EnhancedStore` subclasses use `_name` instead of `_className`. `trace()` and `traceError()` are protected, and subclasses configure label prefixes through `traceOptions.prefixLabelWithName` instead of `classNameAddedToTraceNamespace`.
58
+ - Removed `PluginSubscriberWithoutInstance`; use a plain-object subscriber or `StoreSubscriber` with an `EnhancedStore` wrapper.
50
59
  - Move automatic and manual rollback, including their types, to `pinia-plugin-state-snapshots`; register its subscriber explicitly.
51
- - Move `$reset` ownership and reset callback execution to the state-snapshots package; callbacks are registered with `$onResetStore()`, while `PluginSubscription` only orchestrates subscribers.
60
+ - Move `$reset` ownership and reset callback execution to the state-snapshots package; replace subscriber `resetStoreCallback` with `$onResetStore()`. Callbacks run after state restoration; `PluginOrchestrator` only orchestrates subscribers.
52
61
  - Store-event subscriber failures no longer propagate to the emitter, and `shouldPropagateError` has been removed. Synchronous failures and async rejections are logged; async subscribers are not awaited.
53
62
  - removed the built-in console output of traces (`createConsoleGroupTraceListener`, `ConsoleTraceGroupListener`, `TraceGroupListener`, `TraceListenerGroupBy`, the `groupBy` option of `addListener()`, and the internal console printers). Listeners now only forward events to their own `handler` (a no-op when omitted); `startGroup` / `closeGroup` only maintain the open-group stack that sets `groupId`. Fixed `tracerHandler.journal` not being exposed by the registry
54
63
  - The tracing solution moved to the new `pinia-plugin-subscription-devtools` package (`packages/devtools/`, same repository). `pinia-plugin-subscription` now only emits trace events through the `Tracer` / `TracerHandler` contract exported from `pinia-plugin-subscription/types`; without a `tracerHandler` it emits nothing. The compressed package drops from about 117 kB to about 50 kB, and the new package weighs about 19 kB.
@@ -62,6 +71,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
62
71
  - Reference implementation section lists the demo files and what each one illustrates; README links the demo and the `pinia-plugin-subscription` topic
63
72
 
64
73
  ### Fixed
74
+ - Track subscriber delivery, deferred subscriptions, action callbacks, and cleanup per store instance, including stores with the same `$id`; generate distinct IDs for prototype property names.
65
75
  - Correlate state snapshots with individual action invocations, clean them up on store disposal, and emit action completion events when actions actually settle.
66
76
  - Publish workflow: `scripts/publish-unpublished.mjs` publishes each package (root, then workspaces) only when its `name@version` is not already on npm, so a re-run after a partial failure no longer fails on versions already published
67
77
  - `defineAStoreSetup` now creates a separate setup context for each Pinia store instance, preventing enhanced-store extensions from being shared across SSR requests
@@ -81,7 +91,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
81
91
  - **Breaking:** removed `createHydrationPlugin()`; use `createPlugin(subscribers, options)` with the hydration and runtime options directly
82
92
  - **Breaking:** removed `getExtendingStore()`; use `getEnhancedStore()` and `ctx.extensions.enhancedStore` instead. `getEnhancedStore()` no longer falls back to the deprecated `ctx.extensions.extending` property
83
93
  - Removed the `itemState` export from `src/utils/store`
84
- - **Breaking:** removed `debugLog()` and `logError()` from `Store` and `PluginSubscription` — use `trace()` and `traceError()` instead (see the migration snippet in `docs/tracing.md`)
94
+ - **Breaking:** removed `debugLog()` and `logError()` from the former `Store` and `PluginSubscription` helpers — use `trace()` and `traceError()` instead (see the migration snippet in `packages/devtools/docs/tracing.md`)
85
95
  - **Breaking:** removed the obsolete `debug` and custom-console arguments from `createPlugin()`, `PluginSubscription`, `PluginSubscriber`, `Store`, and `Store.customizeStore()`
86
96
  - **Breaking:** removed the `debug` argument from subscriber `invoke()`, `hydrate()`, and `afterHydration()` hooks; tracing is now configured exclusively through listeners
87
97
  - **Breaking:** removed the legacy `debug` and `console` accessors from `Store` and `PluginSubscription`
@@ -155,7 +165,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
155
165
  ### Added
156
166
  - Added `execution` and `hydrationScheduler` support on `PluginSubscriber` / `PluginSubscriberInterface`.
157
167
  - Added `createHydrationPlugin()` as the dedicated Nuxt / SSR hydration helper.
158
- - Added `PluginSubscriptionOptions` for hydration-specific runtime overrides without changing the default `createPlugin()` API.
168
+ - Added `PluginOrchestratorOptions` for hydration-specific runtime overrides without changing the default `createPlugin()` API.
159
169
  - Added optional `hydrate()` and `afterHydration()` lifecycle hooks to the subscriber and base `Store` contract for SSR-safe initialization logic.
160
170
 
161
171
  ### Changed
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # pinia-plugin-subscription
2
2
 
3
- Pinia plugin for Vue.js that helps building Pinia plugins by centralizing subscriber registration and providing a `Store` base class for store helpers.
3
+ Pinia plugin for Vue.js that helps building Pinia plugins by centralizing subscriber registration and providing an `EnhancedStore` base class for store helpers.
4
4
 
5
5
  This project provides:
6
6
  - a lightweight mechanism to declare "subscribers" that are invoked when stores are registered or updated by Pinia;
7
- - a `Store` base class (helper wrapper) to ease interacting with Pinia stores from subscribers or other plugin code;
7
+ - an `EnhancedStore` base class (helper wrapper) to ease interacting with Pinia stores from subscribers or other plugin code;
8
8
  - an API to create a Pinia plugin from a list of subscribers;
9
9
  - optional `$reset` support and state rollback through the separate [`pinia-plugin-state-snapshots`](packages/state-snapshots/) package.
10
10
 
@@ -35,7 +35,7 @@ The package serves two audiences:
35
35
  | You want to… | Guide |
36
36
  | --- | --- |
37
37
  | Use a compatible plugin (e.g. `pinia-plugin-extending-store`, `pinia-plugin-persist-state`, `pinia-plugin-state-snapshots`) or built-in store features (`defineAStore`, `$reset`) | [Using compatible plugins](docs/using-plugins.md) |
38
- | Build your own pinia plugin on top of `pinia-plugin-subscription` (API reference, `Store` class, import subpaths, development and testing) | [Plugin authoring guide](docs/authoring-plugins.md) |
38
+ | Build your own pinia plugin on top of `pinia-plugin-subscription` (API reference, `EnhancedStore` class, import subpaths, development and testing) | [Plugin authoring guide](docs/authoring-plugins.md) |
39
39
  | Debug stores and plugins with structured trace events | [Tracing](packages/devtools/docs/tracing.md) |
40
40
 
41
41
  A runnable example lives in [`demo/`](demo/) (`npm run dev` for SPA mode or `npm run dev:ssr` for Vite SSR). The SSR example hydrates **one store from two environments**: a server-only subscriber loads request data (awaited before rendering and serializing Pinia state) and a client-only, deferred subscriber fills browser data after hydration, with a visual timeline; see the [reference implementation](docs/authoring-plugins.md#reference-implementation). Plugins built on this package are discoverable through the [`pinia-plugin-subscription` GitHub topic](https://github.com/topics/pinia-plugin-subscription).
@@ -79,18 +79,25 @@ Discover more compatible plugins on the [`pinia-plugin-subscription` GitHub topi
79
79
 
80
80
  ## Advanced Features
81
81
 
82
- - **Structured tracing:** the plugin and `Store` emit typed trace events through an optional `tracerHandler`; without one, no trace events are emitted. See [Tracing](#tracing).
82
+ - **Structured tracing:** the plugin and `EnhancedStore` emit typed trace events through an optional `tracerHandler`; without one, no trace events are emitted. See [Tracing](#tracing).
83
83
  - **SSR and hydration control:** Configure whether each subscribed plugin runs on the server, the client, or only after client hydration.
84
84
  - **Lifecycle safety:** Internal hydration ordering is now guarded so `hydrate()` completes before `afterHydration()` and disposed stores do not retain stale subscriber metadata.
85
85
 
86
- For subscriber authors, store action events are emitted even without a `storeOnActionSubscription`:
87
- `ACTION_BEFORE` receives `(name, invocationKey)`, `ACTION_AFTER` receives
88
- `(name, invocationKey)` after success, and `ACTION_ON_ERROR` receives `(name, error, invocationKey)`
89
- after failure. The symbol identifies one invocation.
86
+ Subscriber delivery and cleanup tracking are scoped per Pinia store instance,
87
+ even when stores from different Pinia instances share the same `$id`.
88
+ Class-based subscribers can use `getStoreInstance(context.store)` in lifecycle
89
+ hooks to access the wrapper associated with that specific store.
90
+
91
+ For subscriber authors, store action events are emitted even without a `storeOnActionSubscription`.
92
+ The consumer receives an event; its `payload()` returns an object:
93
+ `ACTION_BEFORE` has `{ action, args, invocationKey }`, `ACTION_AFTER` has
94
+ `{ action, result, invocationKey }`, and `ACTION_ON_ERROR` has
95
+ `{ action, error, invocationKey }`. When present, the symbol identifies one
96
+ invocation; it may be `undefined` if the store has no event subscriber.
90
97
  Event subscribers cannot interrupt the emitter: synchronous failures and async
91
98
  rejections are reported with `console.error`, and other subscribers still run.
92
99
  Async subscribers are not awaited.
93
- Class-based subscribers define a unique `_name`; their `Store` wrappers can
100
+ Class-based subscribers define a unique `_name`; their `EnhancedStore` wrappers can
94
101
  register event callbacks through protected helpers using the shared handler.
95
102
  The orchestrator clears those subscriptions when the store is disposed. See
96
103
  the [plugin authoring guide](docs/authoring-plugins.md#store-event-registration).