@lensmcp/valtio-instrumentation 1.0.0
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.
- package/LICENSE +202 -0
- package/README.md +115 -0
- package/index.d.ts +6 -0
- package/index.d.ts.map +1 -0
- package/index.js +10 -0
- package/lib/proxy.d.ts +13 -0
- package/lib/proxy.d.ts.map +1 -0
- package/lib/proxy.js +94 -0
- package/lib/snapshot.d.ts +17 -0
- package/lib/snapshot.d.ts.map +1 -0
- package/lib/snapshot.js +136 -0
- package/lib/store-registry.d.ts +25 -0
- package/lib/store-registry.d.ts.map +1 -0
- package/lib/store-registry.js +58 -0
- package/lib/types.d.ts +38 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +1 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# @lensmcp/valtio-instrumentation
|
|
2
|
+
|
|
3
|
+
Drop-in instrumented Valtio for LensMCP — per-path subscription and mutation tracking.
|
|
4
|
+
|
|
5
|
+
`@lensmcp/valtio-instrumentation` is a transparent, drop-in replacement for [`valtio`](https://github.com/pmndrs/valtio). It exposes the exact same surface (`proxy`, `useSnapshot`, `snapshot`, `subscribe`, `ref`, …), but the `proxy` and `useSnapshot` you get are instrumented: every store creation, every state mutation, and every per-path component subscription is observed and published as a LensMCP event. This is what lets the [LensMCP](https://github.com/kiwiapps-ltd/lensmcp) lens show frontend state changes inside a flow/trace — which store changed, on which path, and which mounted components were subscribed to it at the time.
|
|
6
|
+
|
|
7
|
+
The instrumentation is path-aware. `proxy()` registers each store and subscribes at the root with Valtio's path-tracking ops enabled, emitting one `state-update` per touched leaf. `useSnapshot()` inlines Valtio v2's `proxy-compare` "affected" map so it knows exactly which `(storeId, path)` pairs a component actually read, and registers one subscription per path instead of a single store-wide one. You normally don't import this package by hand — [`@lensmcp/vite-plugin`](https://github.com/kiwiapps-ltd/lensmcp) aliases `valtio → @lensmcp/valtio-instrumentation` at dev time, so your existing Valtio code is observed with no source changes.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
yarn add @lensmcp/valtio-instrumentation
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
In a LensMCP-enabled Vite app you usually don't need this: `@lensmcp/vite-plugin` aliases `valtio` to this package automatically, so `import { proxy, useSnapshot } from 'valtio'` resolves here in dev. Install it directly only if you want to import `@lensmcp/valtio-instrumentation` explicitly or use the LensMCP-specific store-registry exports.
|
|
16
|
+
|
|
17
|
+
`react` (`^18 || ^19`) and `valtio` (`^1 || ^2`) are peer dependencies, and it depends on `@lensmcp/react-instrumentation` for event publishing and flow context.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
Because the surface matches Valtio exactly, existing code works unchanged — it's just now observed:
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
// With @lensmcp/vite-plugin, this import resolves to @lensmcp/valtio-instrumentation in dev.
|
|
25
|
+
import { proxy, useSnapshot } from 'valtio';
|
|
26
|
+
|
|
27
|
+
// `proxy()` registers the store and emits a `store-created` event.
|
|
28
|
+
const state = proxy({ count: 0, user: { name: 'Ada' } });
|
|
29
|
+
|
|
30
|
+
function Counter() {
|
|
31
|
+
// `useSnapshot()` infers the paths this component reads (here: `count`)
|
|
32
|
+
// and registers one subscription per path.
|
|
33
|
+
const snap = useSnapshot(state);
|
|
34
|
+
return <button onClick={() => state.count++}>{snap.count}</button>;
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Mutating `state.count` emits a `state-update` event for path `count`, tagged with the components currently subscribed to that path (and its parents). No callbacks, providers, or annotations are required beyond mounting your tree under `LensmcpRoot` from `@lensmcp/react-instrumentation`.
|
|
39
|
+
|
|
40
|
+
If you import this package directly, you can pass LensMCP options to `proxy()`:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { proxy } from '@lensmcp/valtio-instrumentation';
|
|
44
|
+
|
|
45
|
+
const cart = proxy(
|
|
46
|
+
{ items: [] },
|
|
47
|
+
{ storeId: 'cart', source: 'src/cart.ts:12', emitInitialPreview: true },
|
|
48
|
+
);
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
For cases where you want to declare a read path explicitly rather than infer it, use `useTrackedSnapshot`:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { useTrackedSnapshot } from '@lensmcp/valtio-instrumentation';
|
|
55
|
+
|
|
56
|
+
const snap = useTrackedSnapshot(cart, { storeId: 'cart', path: 'items' });
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## API
|
|
60
|
+
|
|
61
|
+
### Valtio-compatible surface
|
|
62
|
+
|
|
63
|
+
| Export | Description |
|
|
64
|
+
| --- | --- |
|
|
65
|
+
| `proxy(initial, opts?)` | Instrumented `proxy`. Registers the store, emits `store-created`, and emits a `state-update` per mutated path. Accepts `LensmcpProxyOptions`. |
|
|
66
|
+
| `useSnapshot(target)` | Instrumented `useSnapshot` with the same signature as Valtio's. Infers read paths via `proxy-compare` and registers one subscription per path. |
|
|
67
|
+
| `useTrackedSnapshot(target, opts)` | Like `useSnapshot`, but the caller declares `{ storeId, path }` explicitly instead of inferring it. Accepts `UseSnapshotOptions`. |
|
|
68
|
+
| `snapshot` | Re-exported from `valtio/vanilla` (uninstrumented). |
|
|
69
|
+
| `subscribe` | Re-exported from `valtio/vanilla`. |
|
|
70
|
+
| `ref` | Re-exported from `valtio/vanilla`. |
|
|
71
|
+
| `getVersion` | Re-exported from `valtio/vanilla`. |
|
|
72
|
+
| `unstable_enableOp` | Re-exported from `valtio/vanilla`. |
|
|
73
|
+
| `unstable_getInternalStates` | Re-exported from `valtio/vanilla`. |
|
|
74
|
+
| `unstable_replaceInternalFunction` | Re-exported from `valtio/vanilla`. |
|
|
75
|
+
| `LensmcpProxyOptions` | Options for `proxy()`: `storeId?`, `source?`, `emitInitialPreview?`. |
|
|
76
|
+
| `UseSnapshotOptions` | Options for `useTrackedSnapshot()`: `storeId` (required), `path?`. |
|
|
77
|
+
|
|
78
|
+
> `proxy` and `useSnapshot` are the instrumented versions; everything else passes through from the real Valtio (imported via the `valtio/vanilla` subpath so this module never aliases itself).
|
|
79
|
+
|
|
80
|
+
### Store registry (LensMCP-specific)
|
|
81
|
+
|
|
82
|
+
Module-level bookkeeping shared by `proxy`, `useSnapshot`, and the LensMCP reducer. Most apps never call these directly.
|
|
83
|
+
|
|
84
|
+
| Export | Description |
|
|
85
|
+
| --- | --- |
|
|
86
|
+
| `registerStore(storeId, source?)` | Record a store and its source location. |
|
|
87
|
+
| `tagProxy(proxyObject, storeId)` | Associate a proxy object with its `storeId` so `useSnapshot(store)` can recover the id with no extra args. |
|
|
88
|
+
| `storeIdOf(proxyObject)` | Look up the `storeId` previously tagged onto a proxy. |
|
|
89
|
+
| `addSubscription(entry)` | Register a `{ storeId, path, componentInstanceId }` subscription. |
|
|
90
|
+
| `removeSubscription(entry)` | Remove a subscription entry. |
|
|
91
|
+
| `subscribersFor(storeId, path)` | List `componentInstanceId`s subscribed to an exact path. |
|
|
92
|
+
| `subscribersForPathAndParents(storeId, path)` | List subscribers of a path unioned with every parent prefix and the root. |
|
|
93
|
+
| `resetRegistry()` | Clear all stores and subscriptions (useful in tests). |
|
|
94
|
+
| `SubscriptionEntry` | `{ storeId, path, componentInstanceId }`. |
|
|
95
|
+
|
|
96
|
+
### Event payload types
|
|
97
|
+
|
|
98
|
+
The wire-shape payloads emitted by this package (consumed by the LensMCP Valtio reducer via `event.raw.kind`):
|
|
99
|
+
|
|
100
|
+
| Export | Description |
|
|
101
|
+
| --- | --- |
|
|
102
|
+
| `ValtioPublishPayload` | Union of `store-created`, `state-update`, `subscription-added`, and `subscription-removed`. |
|
|
103
|
+
| `ValtioStoreRecord` | `{ storeId, source?, initialPreview? }`. |
|
|
104
|
+
| `ValtioUpdateRecord` | `{ storeId, path, changedKeys, beforeHash, afterHash, preview?, subscribers }`. |
|
|
105
|
+
| `ValtioSubscriptionRecord` | `{ storeId, path, componentInstanceId? }`. |
|
|
106
|
+
|
|
107
|
+
## How it fits
|
|
108
|
+
|
|
109
|
+
- **Aliased by [`@lensmcp/vite-plugin`](https://github.com/kiwiapps-ltd/lensmcp).** The plugin rewrites `valtio` to this package at dev time, so instrumentation is zero-touch for app code.
|
|
110
|
+
- **Pairs with [`@lensmcp/react-instrumentation`](https://github.com/kiwiapps-ltd/lensmcp).** This package publishes events through its `publish()` API and reads the active flow/component context from `useLensmcpContext()` / `LensmcpRoot`, so state updates are correlated with the render and effect timeline in the same trace.
|
|
111
|
+
- **Speaks [`@lensmcp/protocol-types`](https://github.com/kiwiapps-ltd/lensmcp).** Events ride the LensMCP publish envelope (`source: 'valtio'`, `category: 'state'`) defined by the shared protocol, so the lens and reducers can render store creation, mutations, and per-path subscriptions in a unified flow view.
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
Part of [LensMCP](https://github.com/kiwiapps-ltd/lensmcp). Apache-2.0.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { ref, snapshot, subscribe, getVersion, unstable_enableOp, unstable_getInternalStates, unstable_replaceInternalFunction, } from 'valtio/vanilla';
|
|
2
|
+
export { proxy, type LensmcpProxyOptions } from './lib/proxy.js';
|
|
3
|
+
export { useSnapshot, useTrackedSnapshot, type UseSnapshotOptions } from './lib/snapshot.js';
|
|
4
|
+
export { registerStore, tagProxy, storeIdOf, addSubscription, removeSubscription, subscribersFor, subscribersForPathAndParents, resetRegistry, type SubscriptionEntry, } from './lib/store-registry.js';
|
|
5
|
+
export type { ValtioUpdateRecord, ValtioStoreRecord, ValtioSubscriptionRecord, ValtioPublishPayload, } from './lib/types.js';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,GAAG,EACH,QAAQ,EACR,SAAS,EACT,UAAU,EACV,iBAAiB,EACjB,0BAA0B,EAC1B,gCAAgC,GACjC,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,KAAK,EAAE,KAAK,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACjE,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAG7F,OAAO,EACL,aAAa,EACb,QAAQ,EACR,SAAS,EACT,eAAe,EACf,kBAAkB,EAClB,cAAc,EACd,4BAA4B,EAC5B,aAAa,EACb,KAAK,iBAAiB,GACvB,MAAM,yBAAyB,CAAC;AACjC,YAAY,EACV,kBAAkB,EAClB,iBAAiB,EACjB,wBAAwB,EACxB,oBAAoB,GACrB,MAAM,gBAAgB,CAAC"}
|
package/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Drop-in for `valtio`: re-exports the full main-entry surface so the
|
|
2
|
+
// Vite plugin can alias `valtio → @lensmcp/valtio-instrumentation` in dev
|
|
3
|
+
// with no caller changes. `proxy` + `useSnapshot` are the instrumented
|
|
4
|
+
// versions; the rest pass through from the real valtio (imported via the
|
|
5
|
+
// `valtio/vanilla` subpath so this module never aliases itself).
|
|
6
|
+
export { ref, snapshot, subscribe, getVersion, unstable_enableOp, unstable_getInternalStates, unstable_replaceInternalFunction, } from 'valtio/vanilla';
|
|
7
|
+
export { proxy } from './lib/proxy.js';
|
|
8
|
+
export { useSnapshot, useTrackedSnapshot } from './lib/snapshot.js';
|
|
9
|
+
// LensMCP-specific extras (not part of the valtio surface).
|
|
10
|
+
export { registerStore, tagProxy, storeIdOf, addSubscription, removeSubscription, subscribersFor, subscribersForPathAndParents, resetRegistry, } from './lib/store-registry.js';
|
package/lib/proxy.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface LensmcpProxyOptions {
|
|
2
|
+
/** Stable storeId; defaults to `valtio:store:<n>`. */
|
|
3
|
+
storeId?: string;
|
|
4
|
+
/** Source path (file:line) the proxy was created at. */
|
|
5
|
+
source?: string;
|
|
6
|
+
/** Include first-level state in `store-created` events. */
|
|
7
|
+
emitInitialPreview?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Create a tracked Valtio proxy.
|
|
11
|
+
*/
|
|
12
|
+
export declare function proxy<T extends object>(initial: T, opts?: LensmcpProxyOptions): T;
|
|
13
|
+
//# sourceMappingURL=proxy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../../src/lib/proxy.ts"],"names":[],"mappings":"AAmCA,MAAM,WAAW,mBAAmB;IAClC,sDAAsD;IACtD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,wDAAwD;IACxD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,2DAA2D;IAC3D,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B;AAID;;GAEG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,MAAM,EACpC,OAAO,EAAE,CAAC,EACV,IAAI,GAAE,mBAAwB,GAC7B,CAAC,CAkEH"}
|
package/lib/proxy.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `proxy(initial, opts)` — wraps the actual Valtio proxy and emits
|
|
3
|
+
* `store-created` + per-mutation `state-update` events.
|
|
4
|
+
*
|
|
5
|
+
* Valtio is a peer dependency; we import it statically so bundlers
|
|
6
|
+
* resolve it the same way as user code.
|
|
7
|
+
*/
|
|
8
|
+
// Import the REAL valtio via the `valtio/vanilla` subpath — never bare
|
|
9
|
+
// `valtio` — so the dev-time `valtio → @lensmcp/valtio-instrumentation`
|
|
10
|
+
// alias (Phase 8) doesn't make this module import itself.
|
|
11
|
+
// `unstable_enableOp` turns on path-aware ops; without it `subscribe()`
|
|
12
|
+
// callbacks receive empty op arrays.
|
|
13
|
+
import { proxy as valtioProxy, subscribe as valtioSubscribe, unstable_enableOp, } from 'valtio/vanilla';
|
|
14
|
+
import { publish, depsHash } from '@lensmcp/react-instrumentation';
|
|
15
|
+
import { registerStore, subscribersForPathAndParents, tagProxy, } from './store-registry.js';
|
|
16
|
+
let opsEnabled = false;
|
|
17
|
+
function ensureOpsEnabled() {
|
|
18
|
+
if (opsEnabled)
|
|
19
|
+
return;
|
|
20
|
+
try {
|
|
21
|
+
unstable_enableOp(true);
|
|
22
|
+
opsEnabled = true;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
/* older valtio without the flag — fall through, paths just won't populate */
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
let storeCounter = 0;
|
|
29
|
+
/**
|
|
30
|
+
* Create a tracked Valtio proxy.
|
|
31
|
+
*/
|
|
32
|
+
export function proxy(initial, opts = {}) {
|
|
33
|
+
ensureOpsEnabled();
|
|
34
|
+
const storeId = opts.storeId ?? `valtio:store:${++storeCounter}`;
|
|
35
|
+
registerStore(storeId, opts.source);
|
|
36
|
+
const proxied = valtioProxy(initial);
|
|
37
|
+
// Remember which store this proxy is, so the drop-in `useSnapshot(store)`
|
|
38
|
+
// can recover the id with no extra args.
|
|
39
|
+
tagProxy(proxied, storeId);
|
|
40
|
+
publish({
|
|
41
|
+
source: 'valtio',
|
|
42
|
+
category: 'state',
|
|
43
|
+
severity: 'info',
|
|
44
|
+
title: `store-created ${storeId}`,
|
|
45
|
+
fingerprint: `valtio-store:${storeId}`,
|
|
46
|
+
raw: {
|
|
47
|
+
kind: 'store-created',
|
|
48
|
+
store: {
|
|
49
|
+
storeId,
|
|
50
|
+
source: opts.source,
|
|
51
|
+
initialPreview: opts.emitInitialPreview === false ? undefined : safePreview(initial),
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
// Subscribe at the root to all changes; Valtio's ops carry the path
|
|
56
|
+
// tuple so we can emit one `state-update` per touched leaf.
|
|
57
|
+
valtioSubscribe(proxied, (ops) => {
|
|
58
|
+
for (const op of ops) {
|
|
59
|
+
const [, pathParts, value, prevValue] = op;
|
|
60
|
+
const path = (pathParts ?? []).map(String).join('.');
|
|
61
|
+
if (!path)
|
|
62
|
+
continue;
|
|
63
|
+
const changedKeys = (pathParts ?? []).map(String);
|
|
64
|
+
publish({
|
|
65
|
+
source: 'valtio',
|
|
66
|
+
category: 'state',
|
|
67
|
+
severity: 'info',
|
|
68
|
+
title: `state-update ${storeId}.${path}`,
|
|
69
|
+
fingerprint: `valtio-update:${storeId}:${path}`,
|
|
70
|
+
raw: {
|
|
71
|
+
kind: 'state-update',
|
|
72
|
+
update: {
|
|
73
|
+
storeId,
|
|
74
|
+
path,
|
|
75
|
+
changedKeys,
|
|
76
|
+
beforeHash: depsHash([prevValue]),
|
|
77
|
+
afterHash: depsHash([value]),
|
|
78
|
+
preview: safePreview(value),
|
|
79
|
+
subscribers: subscribersForPathAndParents(storeId, path),
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}, true);
|
|
85
|
+
return proxied;
|
|
86
|
+
}
|
|
87
|
+
function safePreview(v) {
|
|
88
|
+
try {
|
|
89
|
+
return JSON.parse(JSON.stringify(v));
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface UseSnapshotOptions {
|
|
2
|
+
/** Path string declared by the caller (e.g. "dashboard.cards"). Optional —
|
|
3
|
+
* without it we fall back to the empty root path. */
|
|
4
|
+
path?: string;
|
|
5
|
+
/** Store id (set by `proxy()`); the snapshot wrapper can't recover this
|
|
6
|
+
* from the proxy alone, so callers must supply it. */
|
|
7
|
+
storeId: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function useTrackedSnapshot<T extends object>(target: T, opts: UseSnapshotOptions): T;
|
|
10
|
+
/**
|
|
11
|
+
* Drop-in `useSnapshot(store)` matching valtio's signature exactly. Infers
|
|
12
|
+
* the read paths from `proxy-compare`'s affected map and registers one
|
|
13
|
+
* subscription per path. Recovers the storeId tagged by the instrumented
|
|
14
|
+
* `proxy()`; falls back to a single root subscription when nothing is read.
|
|
15
|
+
*/
|
|
16
|
+
export declare function useSnapshot<T extends object>(target: T): T;
|
|
17
|
+
//# sourceMappingURL=snapshot.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../../src/lib/snapshot.ts"],"names":[],"mappings":"AAoCA,MAAM,WAAW,kBAAkB;IACjC;0DACsD;IACtD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;2DACuD;IACvD,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,kBAAkB,CAAC,CAAC,SAAS,MAAM,EACjD,MAAM,EAAE,CAAC,EACT,IAAI,EAAE,kBAAkB,GACvB,CAAC,CAcH;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAgF1D"}
|
package/lib/snapshot.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `useSnapshot` — the drop-in replacement for valtio's `useSnapshot`, with
|
|
3
|
+
* **per-path subscription inference**.
|
|
4
|
+
*
|
|
5
|
+
* valtio's own re-render optimisation is a `proxy-compare` "affected"
|
|
6
|
+
* WeakMap recording which snapshot paths a component reads. We inline
|
|
7
|
+
* valtio v2's `useSnapshot` here so we *own* that `affected` map, then read
|
|
8
|
+
* it back with `affectedToPathList` after commit to learn exactly which
|
|
9
|
+
* (`storeId`, `path`) pairs the component depends on — and register one
|
|
10
|
+
* subscription per path instead of a single store-root subscription.
|
|
11
|
+
*
|
|
12
|
+
* `useTrackedSnapshot(target, { storeId, path })` stays available for
|
|
13
|
+
* callers that want to declare a path explicitly.
|
|
14
|
+
*/
|
|
15
|
+
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useSyncExternalStore, } from 'react';
|
|
16
|
+
// Real valtio via the `valtio/vanilla` subpath (never bare `valtio`), so the
|
|
17
|
+
// dev-time alias doesn't make this module import itself.
|
|
18
|
+
import { snapshot as valtioSnapshot, subscribe as valtioSubscribe, } from 'valtio/vanilla';
|
|
19
|
+
import { useSnapshot as valtioUseSnapshot } from 'valtio/react';
|
|
20
|
+
import { affectedToPathList, createProxy, isChanged } from 'proxy-compare';
|
|
21
|
+
import { useLensmcpContext, publish } from '@lensmcp/react-instrumentation';
|
|
22
|
+
import { addSubscription, removeSubscription, storeIdOf } from './store-registry.js';
|
|
23
|
+
// Shared across components for proxy reuse, exactly as valtio does.
|
|
24
|
+
const targetCache = new WeakMap();
|
|
25
|
+
export function useTrackedSnapshot(target, opts) {
|
|
26
|
+
const ctx = useLensmcpContext();
|
|
27
|
+
const path = opts.path ?? '';
|
|
28
|
+
const componentInstanceId = ctx.componentInstanceId ?? `react:component:anonymous`;
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
emitSubscription('added', opts.storeId, path, componentInstanceId);
|
|
31
|
+
return () => {
|
|
32
|
+
emitSubscription('removed', opts.storeId, path, componentInstanceId);
|
|
33
|
+
};
|
|
34
|
+
}, [opts.storeId, path, componentInstanceId]);
|
|
35
|
+
return valtioUseSnapshot(target);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Drop-in `useSnapshot(store)` matching valtio's signature exactly. Infers
|
|
39
|
+
* the read paths from `proxy-compare`'s affected map and registers one
|
|
40
|
+
* subscription per path. Recovers the storeId tagged by the instrumented
|
|
41
|
+
* `proxy()`; falls back to a single root subscription when nothing is read.
|
|
42
|
+
*/
|
|
43
|
+
export function useSnapshot(target) {
|
|
44
|
+
const storeId = storeIdOf(target) ?? 'valtio:store:anonymous';
|
|
45
|
+
const ctx = useLensmcpContext();
|
|
46
|
+
const componentInstanceId = ctx.componentInstanceId ?? 'react:component:anonymous';
|
|
47
|
+
// ---- valtio v2's useSnapshot, inlined so we own `affected` ----
|
|
48
|
+
const affected = useMemo(() => target && new WeakMap(), [target]);
|
|
49
|
+
const lastSnapshot = useRef(undefined);
|
|
50
|
+
let inRender = true;
|
|
51
|
+
const currSnapshot = useSyncExternalStore(useCallback((callback) => {
|
|
52
|
+
const unsub = valtioSubscribe(target, callback, false);
|
|
53
|
+
callback();
|
|
54
|
+
return unsub;
|
|
55
|
+
}, [target]), () => {
|
|
56
|
+
const next = valtioSnapshot(target);
|
|
57
|
+
try {
|
|
58
|
+
if (!inRender &&
|
|
59
|
+
lastSnapshot.current &&
|
|
60
|
+
affected &&
|
|
61
|
+
!isChanged(lastSnapshot.current, next, affected, new WeakMap())) {
|
|
62
|
+
return lastSnapshot.current;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
/* fall through to the fresh snapshot */
|
|
67
|
+
}
|
|
68
|
+
return next;
|
|
69
|
+
}, () => valtioSnapshot(target));
|
|
70
|
+
inRender = false;
|
|
71
|
+
useLayoutEffect(() => {
|
|
72
|
+
lastSnapshot.current = currSnapshot;
|
|
73
|
+
});
|
|
74
|
+
const proxyCache = useMemo(() => new WeakMap(), []);
|
|
75
|
+
// ---- LensMCP: reconcile per-path subscriptions to the read set ----
|
|
76
|
+
const subscribedRef = useRef(undefined);
|
|
77
|
+
if (!subscribedRef.current)
|
|
78
|
+
subscribedRef.current = new Set();
|
|
79
|
+
// Runs after every commit — `affected` now reflects this render's reads.
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
const paths = affected
|
|
82
|
+
? affectedToPathList(currSnapshot, affected, true).map((parts) => parts.map(String).join('.'))
|
|
83
|
+
: [];
|
|
84
|
+
const next = new Set(paths.length ? paths : ['']);
|
|
85
|
+
reconcileSubscriptions(subscribedRef.current, next, storeId, componentInstanceId);
|
|
86
|
+
});
|
|
87
|
+
// Drop all subscriptions on unmount.
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
const subscribed = subscribedRef.current;
|
|
90
|
+
return () => {
|
|
91
|
+
for (const path of [...subscribed]) {
|
|
92
|
+
emitSubscription('removed', storeId, path, componentInstanceId);
|
|
93
|
+
}
|
|
94
|
+
subscribed.clear();
|
|
95
|
+
};
|
|
96
|
+
}, [storeId, componentInstanceId]);
|
|
97
|
+
return affected
|
|
98
|
+
? createProxy(currSnapshot, affected, proxyCache, targetCache)
|
|
99
|
+
: currSnapshot;
|
|
100
|
+
}
|
|
101
|
+
/** Add/remove subscriptions so the registry matches `next` exactly. */
|
|
102
|
+
function reconcileSubscriptions(prev, next, storeId, componentInstanceId) {
|
|
103
|
+
for (const path of next) {
|
|
104
|
+
if (!prev.has(path)) {
|
|
105
|
+
emitSubscription('added', storeId, path, componentInstanceId);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (const path of [...prev]) {
|
|
109
|
+
if (!next.has(path)) {
|
|
110
|
+
emitSubscription('removed', storeId, path, componentInstanceId);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
prev.clear();
|
|
114
|
+
for (const path of next)
|
|
115
|
+
prev.add(path);
|
|
116
|
+
}
|
|
117
|
+
function emitSubscription(kind, storeId, path, componentInstanceId) {
|
|
118
|
+
if (kind === 'added') {
|
|
119
|
+
addSubscription({ storeId, path, componentInstanceId });
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
removeSubscription({ storeId, path, componentInstanceId });
|
|
123
|
+
}
|
|
124
|
+
const verb = kind === 'added' ? 'sub' : 'unsub';
|
|
125
|
+
publish({
|
|
126
|
+
source: 'valtio',
|
|
127
|
+
category: 'state',
|
|
128
|
+
severity: 'debug',
|
|
129
|
+
title: `subscription-${kind} ${storeId}.${path}`,
|
|
130
|
+
fingerprint: `valtio-${verb}:${storeId}:${path}:${componentInstanceId}`,
|
|
131
|
+
raw: {
|
|
132
|
+
kind: `subscription-${kind}`,
|
|
133
|
+
subscription: { storeId, path, componentInstanceId },
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module-level bookkeeping shared by `proxy`/`subscribe`/`useSnapshot`.
|
|
3
|
+
* The map from path → subscriber set is what `valtio://path/{path}/subscribers`
|
|
4
|
+
* shows; the reducer reads it indirectly via `subscription-added`
|
|
5
|
+
* events but we also keep a local copy so `useSnapshot` can attach the
|
|
6
|
+
* subscriber list inline to a `state-update`.
|
|
7
|
+
*/
|
|
8
|
+
export interface SubscriptionEntry {
|
|
9
|
+
storeId: string;
|
|
10
|
+
path: string;
|
|
11
|
+
componentInstanceId: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function registerStore(storeId: string, source?: string): void;
|
|
14
|
+
export declare function tagProxy(proxyObject: object, storeId: string): void;
|
|
15
|
+
export declare function storeIdOf(proxyObject: object): string | undefined;
|
|
16
|
+
export declare function isStoreRegistered(storeId: string): boolean;
|
|
17
|
+
export declare function addSubscription(entry: SubscriptionEntry): void;
|
|
18
|
+
export declare function removeSubscription(entry: SubscriptionEntry): void;
|
|
19
|
+
export declare function subscribersFor(storeId: string, path: string): string[];
|
|
20
|
+
/** Walk every prefix of `path` and union the subscribers. Used when a
|
|
21
|
+
* state-update lands on a nested path but parent-path snapshot users
|
|
22
|
+
* also need to be notified. */
|
|
23
|
+
export declare function subscribersForPathAndParents(storeId: string, path: string): string[];
|
|
24
|
+
export declare function resetRegistry(): void;
|
|
25
|
+
//# sourceMappingURL=store-registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-registry.d.ts","sourceRoot":"","sources":["../../src/lib/store-registry.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,mBAAmB,EAAE,MAAM,CAAC;CAC7B;AAQD,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAEpE;AAED,wBAAgB,QAAQ,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAEnE;AAED,wBAAgB,SAAS,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAEjE;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAE1D;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI,CAQ9D;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI,CAOjE;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAEtE;AAED;;gCAEgC;AAChC,wBAAgB,4BAA4B,CAC1C,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,GACX,MAAM,EAAE,CAUV;AAED,wBAAgB,aAAa,IAAI,IAAI,CAGpC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const subs = new Map(); // key: `${storeId}::${path}` → set of componentInstanceIds
|
|
2
|
+
const stores = new Map();
|
|
3
|
+
// proxy object → its storeId, so the drop-in `useSnapshot(store)` can
|
|
4
|
+
// recover the store id without the caller passing it.
|
|
5
|
+
const proxyStoreIds = new WeakMap();
|
|
6
|
+
export function registerStore(storeId, source) {
|
|
7
|
+
stores.set(storeId, { storeId, source });
|
|
8
|
+
}
|
|
9
|
+
export function tagProxy(proxyObject, storeId) {
|
|
10
|
+
proxyStoreIds.set(proxyObject, storeId);
|
|
11
|
+
}
|
|
12
|
+
export function storeIdOf(proxyObject) {
|
|
13
|
+
return proxyStoreIds.get(proxyObject);
|
|
14
|
+
}
|
|
15
|
+
export function isStoreRegistered(storeId) {
|
|
16
|
+
return stores.has(storeId);
|
|
17
|
+
}
|
|
18
|
+
export function addSubscription(entry) {
|
|
19
|
+
const key = `${entry.storeId}::${entry.path}`;
|
|
20
|
+
let set = subs.get(key);
|
|
21
|
+
if (!set) {
|
|
22
|
+
set = new Set();
|
|
23
|
+
subs.set(key, set);
|
|
24
|
+
}
|
|
25
|
+
set.add(entry.componentInstanceId);
|
|
26
|
+
}
|
|
27
|
+
export function removeSubscription(entry) {
|
|
28
|
+
const key = `${entry.storeId}::${entry.path}`;
|
|
29
|
+
const set = subs.get(key);
|
|
30
|
+
if (set) {
|
|
31
|
+
set.delete(entry.componentInstanceId);
|
|
32
|
+
if (set.size === 0)
|
|
33
|
+
subs.delete(key);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export function subscribersFor(storeId, path) {
|
|
37
|
+
return [...(subs.get(`${storeId}::${path}`) ?? [])];
|
|
38
|
+
}
|
|
39
|
+
/** Walk every prefix of `path` and union the subscribers. Used when a
|
|
40
|
+
* state-update lands on a nested path but parent-path snapshot users
|
|
41
|
+
* also need to be notified. */
|
|
42
|
+
export function subscribersForPathAndParents(storeId, path) {
|
|
43
|
+
const out = new Set();
|
|
44
|
+
const parts = path.split('.');
|
|
45
|
+
for (let i = parts.length; i > 0; i--) {
|
|
46
|
+
const candidate = parts.slice(0, i).join('.');
|
|
47
|
+
for (const id of subscribersFor(storeId, candidate))
|
|
48
|
+
out.add(id);
|
|
49
|
+
}
|
|
50
|
+
// root subscribers
|
|
51
|
+
for (const id of subscribersFor(storeId, ''))
|
|
52
|
+
out.add(id);
|
|
53
|
+
return [...out];
|
|
54
|
+
}
|
|
55
|
+
export function resetRegistry() {
|
|
56
|
+
subs.clear();
|
|
57
|
+
stores.clear();
|
|
58
|
+
}
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire-shape payloads emitted by `@lensmcp/valtio-instrumentation`. The
|
|
3
|
+
* apps/valtio reducer consumes these via `event.raw.kind`.
|
|
4
|
+
*/
|
|
5
|
+
export interface ValtioUpdateRecord {
|
|
6
|
+
storeId: string;
|
|
7
|
+
path: string;
|
|
8
|
+
changedKeys: string[];
|
|
9
|
+
beforeHash: string;
|
|
10
|
+
afterHash: string;
|
|
11
|
+
preview?: unknown;
|
|
12
|
+
/** Components subscribing to this path at update time. */
|
|
13
|
+
subscribers: string[];
|
|
14
|
+
}
|
|
15
|
+
export interface ValtioStoreRecord {
|
|
16
|
+
storeId: string;
|
|
17
|
+
source?: string;
|
|
18
|
+
initialPreview?: unknown;
|
|
19
|
+
}
|
|
20
|
+
export interface ValtioSubscriptionRecord {
|
|
21
|
+
storeId: string;
|
|
22
|
+
path: string;
|
|
23
|
+
componentInstanceId?: string;
|
|
24
|
+
}
|
|
25
|
+
export type ValtioPublishPayload = {
|
|
26
|
+
kind: 'state-update';
|
|
27
|
+
update: ValtioUpdateRecord;
|
|
28
|
+
} | {
|
|
29
|
+
kind: 'store-created';
|
|
30
|
+
store: ValtioStoreRecord;
|
|
31
|
+
} | {
|
|
32
|
+
kind: 'subscription-added';
|
|
33
|
+
subscription: ValtioSubscriptionRecord;
|
|
34
|
+
} | {
|
|
35
|
+
kind: 'subscription-removed';
|
|
36
|
+
subscription: ValtioSubscriptionRecord;
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,0DAA0D;IAC1D,WAAW,EAAE,MAAM,EAAE,CAAC;CACvB;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED,MAAM,WAAW,wBAAwB;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED,MAAM,MAAM,oBAAoB,GAC5B;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,MAAM,EAAE,kBAAkB,CAAA;CAAE,GACpD;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,KAAK,EAAE,iBAAiB,CAAA;CAAE,GACnD;IAAE,IAAI,EAAE,oBAAoB,CAAC;IAAC,YAAY,EAAE,wBAAwB,CAAA;CAAE,GACtE;IAAE,IAAI,EAAE,sBAAsB,CAAC;IAAC,YAAY,EAAE,wBAAwB,CAAA;CAAE,CAAC"}
|
package/lib/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lensmcp/valtio-instrumentation",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"module": "./index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
"./package.json": "./package.json",
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./index.d.ts",
|
|
12
|
+
"import": "./index.js",
|
|
13
|
+
"default": "./index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@lensmcp/react-instrumentation": "1.0.0",
|
|
18
|
+
"proxy-compare": "^3.0.0",
|
|
19
|
+
"tslib": "^2.3.0"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
23
|
+
"valtio": "^1.0.0 || ^2.0.0"
|
|
24
|
+
},
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
|
+
"homepage": "https://github.com/kiwiapps-ltd/lensmcp#readme",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/kiwiapps-ltd/lensmcp/issues"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=18"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"description": "Drop-in instrumented Valtio for LensMCP — per-path subscription tracking.",
|
|
37
|
+
"keywords": [
|
|
38
|
+
"lensmcp",
|
|
39
|
+
"mcp",
|
|
40
|
+
"observability",
|
|
41
|
+
"ai-agents",
|
|
42
|
+
"claude",
|
|
43
|
+
"valtio",
|
|
44
|
+
"state"
|
|
45
|
+
],
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/kiwiapps-ltd/lensmcp.git",
|
|
49
|
+
"directory": "libs/valtio-instrumentation"
|
|
50
|
+
}
|
|
51
|
+
}
|