@capybara-devtools/core 0.2.0 → 0.3.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/README.md +2 -2
- package/dist/client.d.ts +3 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +9 -4
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/plugins/state/context.d.ts +102 -0
- package/dist/plugins/state/context.d.ts.map +1 -0
- package/dist/plugins/state/context.js +765 -0
- package/dist/plugins/state/context.js.map +1 -0
- package/dist/plugins/state/engine.d.ts +71 -0
- package/dist/plugins/state/engine.d.ts.map +1 -0
- package/dist/plugins/state/engine.js +710 -0
- package/dist/plugins/state/engine.js.map +1 -0
- package/dist/plugins/state/index.d.ts +59 -0
- package/dist/plugins/state/index.d.ts.map +1 -0
- package/dist/plugins/state/index.js +52 -0
- package/dist/plugins/state/index.js.map +1 -0
- package/dist/plugins/state/redux.d.ts +43 -0
- package/dist/plugins/state/redux.d.ts.map +1 -0
- package/dist/plugins/state/redux.js +580 -0
- package/dist/plugins/state/redux.js.map +1 -0
- package/dist/plugins/state/registry.d.ts +22 -0
- package/dist/plugins/state/registry.d.ts.map +1 -0
- package/dist/plugins/state/registry.js +109 -0
- package/dist/plugins/state/registry.js.map +1 -0
- package/dist/plugins/state/swr.d.ts +49 -0
- package/dist/plugins/state/swr.d.ts.map +1 -0
- package/dist/plugins/state/swr.js +359 -0
- package/dist/plugins/state/swr.js.map +1 -0
- package/dist/plugins/state/tanstack.d.ts +31 -0
- package/dist/plugins/state/tanstack.d.ts.map +1 -0
- package/dist/plugins/state/tanstack.js +461 -0
- package/dist/plugins/state/tanstack.js.map +1 -0
- package/dist/plugins/state/zustand.d.ts +21 -0
- package/dist/plugins/state/zustand.d.ts.map +1 -0
- package/dist/plugins/state/zustand.js +70 -0
- package/dist/plugins/state/zustand.js.map +1 -0
- package/package.json +11 -2
- package/src/client.ts +12 -6
- package/src/index.ts +19 -0
- package/src/plugins/state/context.ts +831 -0
- package/src/plugins/state/engine.ts +739 -0
- package/src/plugins/state/index.ts +112 -0
- package/src/plugins/state/redux.ts +609 -0
- package/src/plugins/state/registry.ts +126 -0
- package/src/plugins/state/swr.ts +375 -0
- package/src/plugins/state/tanstack.ts +488 -0
- package/src/plugins/state/zustand.ts +82 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
StateChangePayload,
|
|
3
|
+
StateCommands,
|
|
4
|
+
StateEntryOp,
|
|
5
|
+
StateEntrySummary,
|
|
6
|
+
StateStoreInfo,
|
|
7
|
+
} from '@capybara-devtools/protocol';
|
|
8
|
+
import { getActiveClient, type Plugin } from '../../client.js';
|
|
9
|
+
import { serialize } from '../../serialize.js';
|
|
10
|
+
import { trackContexts, type ContextTracker } from './context.js';
|
|
11
|
+
import { createStateEngine, diffState, readPath, type StateEngine } from './engine.js';
|
|
12
|
+
import { installReduxShim } from './redux.js';
|
|
13
|
+
import { attach } from './registry.js';
|
|
14
|
+
|
|
15
|
+
/** Where a driver reports changes. Each call is O(1), only keeping a reference or marking a key, and never throws. */
|
|
16
|
+
export interface StateSink {
|
|
17
|
+
/** The store's new state, and the action that made it when there is one. */
|
|
18
|
+
record(id: string, state: unknown, action?: unknown, cause?: StateChangePayload['cause']): void;
|
|
19
|
+
/** An entry changed. `low` for noise that can wait a second, such as observer updates. */
|
|
20
|
+
dirty(id: string, key: string, low?: boolean): void;
|
|
21
|
+
removed(id: string, key: string): void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* One store, query client or cache, as an adapter registers it. Stores with a timeline (Redux, Zustand,
|
|
26
|
+
* Context) have `getState`; caches (TanStack Query, SWR) have `keys` and `summarize`.
|
|
27
|
+
*/
|
|
28
|
+
export interface StoreDriver {
|
|
29
|
+
info: Omit<StateStoreInfo, 'id'>;
|
|
30
|
+
/** Read when the store is attached, as the base of its first diff, and on each `get`. */
|
|
31
|
+
getState?(): unknown;
|
|
32
|
+
/** Keys of the entries to show. */
|
|
33
|
+
keys?(): Iterable<string>;
|
|
34
|
+
summarize?(key: string): StateEntrySummary | undefined;
|
|
35
|
+
data?(key: string): unknown;
|
|
36
|
+
act?(key: string, op: StateEntryOp): Promise<{ done: boolean; note?: string }>;
|
|
37
|
+
/**
|
|
38
|
+
* Runs only while a client runs, so a store registered without one costs nothing. `id` is the one
|
|
39
|
+
* `register` returns, given here too because `start` can run before `register` has returned.
|
|
40
|
+
* Returns a function that stops reporting.
|
|
41
|
+
*/
|
|
42
|
+
start(sink: StateSink, id: string): (() => void) | void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface StatePluginOptions {
|
|
46
|
+
/**
|
|
47
|
+
* Stand in for the Redux DevTools extension, or wrap it when installed, so stores from Redux Toolkit's
|
|
48
|
+
* configureStore() and zustand's devtools() show up on their own. Default true. Where the extension is
|
|
49
|
+
* missing, this turns devtools() on, and each zustand `set` without an action name then builds a stack
|
|
50
|
+
* trace to find one; pass false to avoid that.
|
|
51
|
+
*/
|
|
52
|
+
redux?: boolean;
|
|
53
|
+
/** Records past this many per second for one store are folded into the next one. Default 30. */
|
|
54
|
+
maxActionsPerSecond?: number;
|
|
55
|
+
/** Action types to keep off the timeline; a string matches the whole type. Their changes show in the next record. */
|
|
56
|
+
actionsDenylist?: Array<string | RegExp>;
|
|
57
|
+
/** Runs before a state is diffed or read, e.g. to blank out tokens; only what it returns leaves the app. */
|
|
58
|
+
sanitizeState?: (state: unknown, store: StateStoreInfo) => unknown;
|
|
59
|
+
sanitizeAction?: (action: unknown, store: StateStoreInfo) => unknown;
|
|
60
|
+
/** Leave out keys that hold functions, such as Zustand actions. Default true. */
|
|
61
|
+
hideFunctions?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Show React Context providers, each change of their value with a diff, read through React DevTools' global
|
|
64
|
+
* hook from development builds of React. Default true. Without React DevTools or Fast Refresh, `init()` must
|
|
65
|
+
* run before react-dom loads.
|
|
66
|
+
*/
|
|
67
|
+
context?: boolean;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function statePlugin(options: StatePluginOptions = {}): Plugin {
|
|
71
|
+
let engine: StateEngine | null = null;
|
|
72
|
+
let contexts: ContextTracker | null = null;
|
|
73
|
+
const running = (): StateEngine => {
|
|
74
|
+
if (!engine) throw new Error('The state plugin is not running');
|
|
75
|
+
return engine;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
name: 'state',
|
|
80
|
+
setup(ctx) {
|
|
81
|
+
// Here rather than at import: the packages declare no side effects, so bundlers may drop such an import.
|
|
82
|
+
const unshim = options.redux === false ? () => {} : installReduxShim();
|
|
83
|
+
const tracker = options.context === false ? null : trackContexts(ctx);
|
|
84
|
+
contexts = tracker;
|
|
85
|
+
// The client becomes the active one only after setup, so this is read when the engine flushes.
|
|
86
|
+
const connected = () => getActiveClient()?.connected === true;
|
|
87
|
+
const current = createStateEngine(ctx.emit, { ...options, connected }, serialize, diffState, readPath);
|
|
88
|
+
engine = current;
|
|
89
|
+
const detach = attach(current);
|
|
90
|
+
// Drivers stay registered, so the next client picks them up again; context stores are found again instead.
|
|
91
|
+
return () => {
|
|
92
|
+
unshim();
|
|
93
|
+
tracker?.stop();
|
|
94
|
+
if (contexts === tracker) contexts = null;
|
|
95
|
+
detach();
|
|
96
|
+
current.dispose();
|
|
97
|
+
if (engine === current) engine = null;
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
commands: {
|
|
101
|
+
stores: (): StateStoreInfo[] => {
|
|
102
|
+
const list = running().stores();
|
|
103
|
+
const tracker = contexts;
|
|
104
|
+
return tracker ? list.map((store) => (store.library === 'context' ? tracker.info(store) : store)) : list;
|
|
105
|
+
},
|
|
106
|
+
get: (params: StateCommands['get']['params']) => running().read(params?.store, params?.path),
|
|
107
|
+
entries: (params: StateCommands['entries']['params']) => running().entries(params?.store),
|
|
108
|
+
entry: (params: StateCommands['entry']['params']) => running().entry(params?.store, params?.key, params?.path),
|
|
109
|
+
act: (params: StateCommands['act']['params']) => running().act(params?.store, params?.key, params?.op),
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|