@bkbdev/plugin-zustand 0.0.0-stage → 1.1.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 +21 -0
- package/README.md +15 -2
- package/dist/index.cjs +348 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.js +328 -0
- package/dist/index.js.map +7 -0
- package/index.d.ts +44 -0
- package/package.json +62 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bobby Echo Prasetyo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bkbdev/plugin-zustand
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Zustand state changes for the BKB desktop: which store changed, which action did it, and which
|
|
4
|
+
paths moved from what to what. Install it next to `@bkbdev/core`, then list it in the Babel plugin
|
|
5
|
+
options and restart Metro with `--reset-cache`:
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
// babel.config.js
|
|
9
|
+
plugins: [['@bkbdev/core/babel', { bkbPlugins: ['@bkbdev/plugin-zustand'] }]],
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
The Babel plugin wraps your `create(...)` and `createStore(...)` calls in development builds, so
|
|
13
|
+
no store needs to change. Release builds never load this package. Stores are named after their
|
|
14
|
+
variable (`useCartStore` is shown as `cart`) and actions after the state key that holds them.
|
|
15
|
+
|
|
16
|
+
Values pass through BKB's redaction: keys that look like credentials are never shown.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __spreadValues = (a, b) => {
|
|
10
|
+
for (var prop in b || (b = {}))
|
|
11
|
+
if (__hasOwnProp.call(b, prop))
|
|
12
|
+
__defNormalProp(a, prop, b[prop]);
|
|
13
|
+
if (__getOwnPropSymbols)
|
|
14
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
15
|
+
if (__propIsEnum.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
}
|
|
18
|
+
return a;
|
|
19
|
+
};
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
23
|
+
};
|
|
24
|
+
var __copyProps = (to, from, except, desc) => {
|
|
25
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
26
|
+
for (let key of __getOwnPropNames(from))
|
|
27
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
28
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
29
|
+
}
|
|
30
|
+
return to;
|
|
31
|
+
};
|
|
32
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
33
|
+
|
|
34
|
+
// packages/plugins/zustand/src/index.ts
|
|
35
|
+
var src_exports = {};
|
|
36
|
+
__export(src_exports, {
|
|
37
|
+
default: () => src_default,
|
|
38
|
+
droppedUpdateCount: () => droppedUpdateCount,
|
|
39
|
+
listStores: () => listStores,
|
|
40
|
+
onStoreUpdate: () => onStoreUpdate,
|
|
41
|
+
trackStore: () => trackStore
|
|
42
|
+
});
|
|
43
|
+
module.exports = __toCommonJS(src_exports);
|
|
44
|
+
|
|
45
|
+
// packages/plugins/zustand/src/commands.ts
|
|
46
|
+
var import_plugin2 = require("@bkbdev/core/plugin");
|
|
47
|
+
|
|
48
|
+
// packages/plugins/zustand/src/hub.ts
|
|
49
|
+
var import_plugin = require("@bkbdev/core/plugin");
|
|
50
|
+
|
|
51
|
+
// packages/plugins/zustand/src/text.ts
|
|
52
|
+
function clip(text, max) {
|
|
53
|
+
return text.length > max ? text.slice(0, max - 1) + "\u2026" : text;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// packages/plugins/zustand/src/hub.ts
|
|
57
|
+
var REGISTRY_KEY = "__BKB_STATE_STORES_V1__";
|
|
58
|
+
var MAX_TRACKED_STORES = 64;
|
|
59
|
+
var updates = (0, import_plugin.createEventHub)("__BKB_STATE_STORE_UPDATES_V1__");
|
|
60
|
+
function registry() {
|
|
61
|
+
const existing = global[REGISTRY_KEY];
|
|
62
|
+
if (existing) return existing;
|
|
63
|
+
const created = { stores: /* @__PURE__ */ new Map(), apis: /* @__PURE__ */ new Map(), labelCounts: /* @__PURE__ */ new Map() };
|
|
64
|
+
global[REGISTRY_KEY] = created;
|
|
65
|
+
return created;
|
|
66
|
+
}
|
|
67
|
+
function storeLabel(variable, source) {
|
|
68
|
+
if (variable) {
|
|
69
|
+
const label = variable.replace(/^use(?=[A-Z])/, "").replace(/Store$/, "");
|
|
70
|
+
return clip(label.charAt(0).toLowerCase() + label.slice(1) || variable, 128);
|
|
71
|
+
}
|
|
72
|
+
return source ? clip(source.file, 200) + ":" + source.line : "store";
|
|
73
|
+
}
|
|
74
|
+
function registerStore(variable, source, store) {
|
|
75
|
+
var _a;
|
|
76
|
+
const state = registry();
|
|
77
|
+
const label = storeLabel(variable, source);
|
|
78
|
+
const count = ((_a = state.labelCounts.get(label)) != null ? _a : 0) + 1;
|
|
79
|
+
state.labelCounts.set(label, count);
|
|
80
|
+
const info = {
|
|
81
|
+
storeId: count === 1 ? label : label + "#" + count,
|
|
82
|
+
store: label,
|
|
83
|
+
source
|
|
84
|
+
};
|
|
85
|
+
state.stores.set(info.storeId, info);
|
|
86
|
+
state.apis.set(info.storeId, store);
|
|
87
|
+
for (const oldest of [...state.stores.keys()].slice(
|
|
88
|
+
0,
|
|
89
|
+
Math.max(0, state.stores.size - MAX_TRACKED_STORES)
|
|
90
|
+
)) {
|
|
91
|
+
state.stores.delete(oldest);
|
|
92
|
+
state.apis.delete(oldest);
|
|
93
|
+
}
|
|
94
|
+
return info;
|
|
95
|
+
}
|
|
96
|
+
function readableStore(storeId) {
|
|
97
|
+
return registry().apis.get(storeId);
|
|
98
|
+
}
|
|
99
|
+
function listStores() {
|
|
100
|
+
return [...registry().stores.values()];
|
|
101
|
+
}
|
|
102
|
+
var publishUpdate = updates.publish;
|
|
103
|
+
var onStoreUpdate = updates.subscribe;
|
|
104
|
+
var droppedUpdateCount = updates.droppedCount;
|
|
105
|
+
|
|
106
|
+
// packages/plugins/zustand/src/state-values.ts
|
|
107
|
+
var MAX_DEPTH = 4;
|
|
108
|
+
function isPlainRecord(value) {
|
|
109
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
110
|
+
const prototype = Object.getPrototypeOf(value);
|
|
111
|
+
return prototype === Object.prototype || prototype === null;
|
|
112
|
+
}
|
|
113
|
+
function withoutFunctions(value, depth = 0) {
|
|
114
|
+
if (depth >= MAX_DEPTH) return value;
|
|
115
|
+
if (Array.isArray(value)) return value.map((item) => withoutFunctions(item, depth + 1));
|
|
116
|
+
if (!isPlainRecord(value)) return value;
|
|
117
|
+
const copy = {};
|
|
118
|
+
for (const key of Object.keys(value)) {
|
|
119
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
120
|
+
if (!descriptor || !("value" in descriptor) || typeof descriptor.value === "function") continue;
|
|
121
|
+
copy[key] = withoutFunctions(descriptor.value, depth + 1);
|
|
122
|
+
}
|
|
123
|
+
return copy;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// packages/plugins/zustand/src/commands.ts
|
|
127
|
+
function field(value, key) {
|
|
128
|
+
if (typeof value !== "object" || value === null) return void 0;
|
|
129
|
+
try {
|
|
130
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
131
|
+
return descriptor && "value" in descriptor ? descriptor.value : void 0;
|
|
132
|
+
} catch (e) {
|
|
133
|
+
return void 0;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function refused(reason) {
|
|
137
|
+
return { outcome: "refused", reason };
|
|
138
|
+
}
|
|
139
|
+
function handleStateCommand(command) {
|
|
140
|
+
const operation = field(command, "operation");
|
|
141
|
+
const storeId = field(command, "storeId");
|
|
142
|
+
if (operation !== "read-state" || typeof storeId !== "string" || !storeId || storeId.length > 256)
|
|
143
|
+
return refused("The request was not understood.");
|
|
144
|
+
const store = readableStore(storeId);
|
|
145
|
+
if (!store) return refused("That store is not tracked.");
|
|
146
|
+
try {
|
|
147
|
+
const { data, truncated } = (0, import_plugin2.snapshotValue)(withoutFunctions(store.getState()));
|
|
148
|
+
return { outcome: "data", data, truncated };
|
|
149
|
+
} catch (e) {
|
|
150
|
+
return refused("The state could not be read.");
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// packages/plugins/zustand/src/provider.ts
|
|
155
|
+
function stateStoreProvider() {
|
|
156
|
+
return {
|
|
157
|
+
capability: "stateStores",
|
|
158
|
+
instanceId: "zustand-" + Date.now().toString(36) + Math.random().toString(36).slice(2, 8),
|
|
159
|
+
subscribe: (listener) => onStoreUpdate((update) => listener(update)),
|
|
160
|
+
handleCommand: handleStateCommand,
|
|
161
|
+
droppedCount: droppedUpdateCount
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// packages/plugins/zustand/src/track.ts
|
|
166
|
+
var import_plugin4 = require("@bkbdev/core/plugin");
|
|
167
|
+
|
|
168
|
+
// packages/plugins/zustand/src/actions.ts
|
|
169
|
+
function isPromiseLike(value) {
|
|
170
|
+
return typeof value === "object" && value !== null && typeof value.then === "function";
|
|
171
|
+
}
|
|
172
|
+
function isPlainRecord2(value) {
|
|
173
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
174
|
+
const prototype = Object.getPrototypeOf(value);
|
|
175
|
+
return prototype === Object.prototype || prototype === null;
|
|
176
|
+
}
|
|
177
|
+
function createActionTracker() {
|
|
178
|
+
let active = null;
|
|
179
|
+
function wrap(name, action) {
|
|
180
|
+
return function wrapped(...args) {
|
|
181
|
+
const previous = active;
|
|
182
|
+
active = { name, isAsync: false };
|
|
183
|
+
let result;
|
|
184
|
+
try {
|
|
185
|
+
result = action.apply(this, args);
|
|
186
|
+
} catch (error) {
|
|
187
|
+
active = previous;
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
if (!isPromiseLike(result)) {
|
|
191
|
+
active = previous;
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
const pending = { name, isAsync: true };
|
|
195
|
+
active = pending;
|
|
196
|
+
const release = () => {
|
|
197
|
+
if (active === pending) active = previous;
|
|
198
|
+
};
|
|
199
|
+
result.then(release, release);
|
|
200
|
+
return result;
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function patchFor(state) {
|
|
204
|
+
const patch = {};
|
|
205
|
+
for (const key of Object.keys(state)) {
|
|
206
|
+
const value = state[key];
|
|
207
|
+
if (typeof value === "function") {
|
|
208
|
+
patch[key] = wrap(key, value);
|
|
209
|
+
} else if (isPlainRecord2(value)) {
|
|
210
|
+
const nested = __spreadValues({}, value);
|
|
211
|
+
let changed = false;
|
|
212
|
+
for (const name of Object.keys(value)) {
|
|
213
|
+
const inner = value[name];
|
|
214
|
+
if (typeof inner !== "function") continue;
|
|
215
|
+
nested[name] = wrap(key + "." + name, inner);
|
|
216
|
+
changed = true;
|
|
217
|
+
}
|
|
218
|
+
if (changed) patch[key] = nested;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return patch;
|
|
222
|
+
}
|
|
223
|
+
return { current: () => active, patchFor };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// packages/plugins/zustand/src/diff.ts
|
|
227
|
+
var import_plugin3 = require("@bkbdev/core/plugin");
|
|
228
|
+
var MAX_DIFF_DEPTH = 3;
|
|
229
|
+
var MAX_CHANGES_PER_UPDATE = 12;
|
|
230
|
+
var MAX_ARRAY_ITEMS = 100;
|
|
231
|
+
var MAX_PATH_CHARACTERS = 256;
|
|
232
|
+
function isRecord(value) {
|
|
233
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
234
|
+
const prototype = Object.getPrototypeOf(value);
|
|
235
|
+
return prototype === Object.prototype || prototype === null;
|
|
236
|
+
}
|
|
237
|
+
function ownValue(container, key) {
|
|
238
|
+
try {
|
|
239
|
+
const descriptor = Object.getOwnPropertyDescriptor(container, key);
|
|
240
|
+
if (!descriptor || !("value" in descriptor) || typeof descriptor.value === "function")
|
|
241
|
+
return { found: false };
|
|
242
|
+
return { found: true, value: descriptor.value };
|
|
243
|
+
} catch (e) {
|
|
244
|
+
return { found: false };
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function objectPath(parent, key) {
|
|
248
|
+
const path = /^[A-Za-z_$][\w$]*$/.test(key) ? parent ? parent + "." + key : key : parent + "[" + JSON.stringify(key) + "]";
|
|
249
|
+
return clip(path, MAX_PATH_CHARACTERS);
|
|
250
|
+
}
|
|
251
|
+
function record(out, change) {
|
|
252
|
+
if (out.changes.length < MAX_CHANGES_PER_UPDATE) out.changes.push(change);
|
|
253
|
+
else out.omittedChanges += 1;
|
|
254
|
+
}
|
|
255
|
+
function leaf(out, path, before, after) {
|
|
256
|
+
if (before.found && after.found) {
|
|
257
|
+
if (!Object.is(before.value, after.value))
|
|
258
|
+
record(out, {
|
|
259
|
+
path,
|
|
260
|
+
kind: "changed",
|
|
261
|
+
before: (0, import_plugin3.previewValue)(before.value),
|
|
262
|
+
after: (0, import_plugin3.previewValue)(after.value)
|
|
263
|
+
});
|
|
264
|
+
} else if (after.found)
|
|
265
|
+
record(out, { path, kind: "added", before: null, after: (0, import_plugin3.previewValue)(after.value) });
|
|
266
|
+
else if (before.found)
|
|
267
|
+
record(out, { path, kind: "removed", before: (0, import_plugin3.previewValue)(before.value), after: null });
|
|
268
|
+
}
|
|
269
|
+
function keysOf(before, after) {
|
|
270
|
+
return [.../* @__PURE__ */ new Set([...Object.keys(before), ...Object.keys(after)])].filter(
|
|
271
|
+
(key) => key !== "__proto__" && key !== "constructor" && key !== "prototype"
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
function walk(out, before, after, path, depth) {
|
|
275
|
+
if (before.found && after.found && Object.is(before.value, after.value)) return;
|
|
276
|
+
if (before.found && after.found && depth < MAX_DIFF_DEPTH) {
|
|
277
|
+
const b = before.value;
|
|
278
|
+
const a = after.value;
|
|
279
|
+
if (isRecord(b) && isRecord(a)) {
|
|
280
|
+
for (const name of keysOf(b, a))
|
|
281
|
+
walk(out, ownValue(b, name), ownValue(a, name), objectPath(path, name), depth + 1);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (Array.isArray(b) && Array.isArray(a)) {
|
|
285
|
+
const length = Math.min(Math.max(b.length, a.length), MAX_ARRAY_ITEMS);
|
|
286
|
+
for (let index = 0; index < length; index++)
|
|
287
|
+
walk(
|
|
288
|
+
out,
|
|
289
|
+
ownValue(b, String(index)),
|
|
290
|
+
ownValue(a, String(index)),
|
|
291
|
+
path + "[" + index + "]",
|
|
292
|
+
depth + 1
|
|
293
|
+
);
|
|
294
|
+
if (Math.max(b.length, a.length) > MAX_ARRAY_ITEMS) out.omittedChanges += 1;
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
leaf(out, path, before, after);
|
|
299
|
+
}
|
|
300
|
+
function diffState(before, after) {
|
|
301
|
+
const out = { changes: [], omittedChanges: 0 };
|
|
302
|
+
walk(out, { found: true, value: before }, { found: true, value: after }, "", 0);
|
|
303
|
+
return out;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// packages/plugins/zustand/src/track.ts
|
|
307
|
+
var tracked = /* @__PURE__ */ new WeakSet();
|
|
308
|
+
function isRecord2(value) {
|
|
309
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
310
|
+
}
|
|
311
|
+
function trackStore(store, meta) {
|
|
312
|
+
if (typeof __DEV__ !== "undefined" && !__DEV__) return store;
|
|
313
|
+
if (tracked.has(store)) return store;
|
|
314
|
+
tracked.add(store);
|
|
315
|
+
const source = (0, import_plugin4.trackSource)(meta);
|
|
316
|
+
const info = registerStore(meta ? meta.variable : null, source, store);
|
|
317
|
+
const actions = createActionTracker();
|
|
318
|
+
try {
|
|
319
|
+
const state = store.getState();
|
|
320
|
+
const patch = isRecord2(state) ? actions.patchFor(state) : {};
|
|
321
|
+
if (Object.keys(patch).length) store.setState(patch);
|
|
322
|
+
} catch (e) {
|
|
323
|
+
}
|
|
324
|
+
store.subscribe((state, previous) => {
|
|
325
|
+
const diff = diffState(previous, state);
|
|
326
|
+
if (!diff.changes.length && !diff.omittedChanges) return;
|
|
327
|
+
const action = actions.current();
|
|
328
|
+
publishUpdate({
|
|
329
|
+
storeId: info.storeId,
|
|
330
|
+
store: info.store,
|
|
331
|
+
action: clip(action ? action.name : "setState", 128),
|
|
332
|
+
duringAsyncAction: action ? action.isAsync : false,
|
|
333
|
+
changes: diff.changes,
|
|
334
|
+
omittedChanges: diff.omittedChanges,
|
|
335
|
+
timestamp: Date.now(),
|
|
336
|
+
source
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
return store;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// packages/plugins/zustand/src/index.ts
|
|
343
|
+
var plugin = {
|
|
344
|
+
name: "zustand",
|
|
345
|
+
extend: () => ({ pluginProviders: [stateStoreProvider()] })
|
|
346
|
+
};
|
|
347
|
+
var src_default = plugin;
|
|
348
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts", "../src/commands.ts", "../src/hub.ts", "../src/text.ts", "../src/state-values.ts", "../src/provider.ts", "../src/track.ts", "../src/actions.ts", "../src/diff.ts"],
|
|
4
|
+
"sourcesContent": ["import type { BkbPlugin } from '@bkbdev/core/register';\nimport { stateStoreProvider } from './provider';\n\nexport { trackStore } from './track';\nexport type { TrackableStore } from './track';\nexport type { TrackMeta } from '@bkbdev/core/plugin';\nexport { onStoreUpdate, listStores, droppedUpdateCount } from './hub';\n\n// Zustand state changes for the BKB desktop. Listing this package in the `bkbPlugins` option of\n// `@bkbdev/core/babel` makes the Babel plugin wrap the app's `create` and `createStore` calls with\n// `trackStore`; release builds never load it.\nconst plugin: BkbPlugin = {\n name: 'zustand',\n extend: () => ({ pluginProviders: [stateStoreProvider()] }),\n};\n\nexport default plugin;\n", "import { snapshotValue } from '@bkbdev/core/plugin';\nimport { readableStore } from './hub';\nimport { withoutFunctions } from './state-values';\n\nfunction field(value: unknown, key: string): unknown {\n if (typeof value !== 'object' || value === null) return undefined;\n try {\n const descriptor = Object.getOwnPropertyDescriptor(value, key);\n return descriptor && 'value' in descriptor ? (descriptor.value as unknown) : undefined;\n } catch {\n return undefined;\n }\n}\n\nfunction refused(reason: string): { outcome: 'refused'; reason: string } {\n return { outcome: 'refused', reason };\n}\n\n// Serves the one request BKB makes to the plugin: read the state a store holds now, without its\n// actions, bounded like every other value that leaves the app.\nexport function handleStateCommand(command: unknown): unknown {\n const operation = field(command, 'operation');\n const storeId = field(command, 'storeId');\n if (operation !== 'read-state' || typeof storeId !== 'string' || !storeId || storeId.length > 256)\n return refused('The request was not understood.');\n const store = readableStore(storeId);\n if (!store) return refused('That store is not tracked.');\n try {\n const { data, truncated } = snapshotValue(withoutFunctions(store.getState()));\n return { outcome: 'data', data, truncated };\n } catch {\n return refused('The state could not be read.');\n }\n}\n", "import { createEventHub } from '@bkbdev/core/plugin';\nimport { clip } from './text';\nimport type {\n StateSource,\n StoreInfo,\n StoreUpdate,\n} from '../../../protocol/src/state-store-provider';\n\n// The registry of tracked stores lives on the global object, not in this module: Metro can load\n// the package once for the project's own import and once for the registration, and both must\n// share one set of stores.\nconst REGISTRY_KEY = '__BKB_STATE_STORES_V1__';\n// A store made again and again (inside a component or a factory) must not pile up for the whole\n// session: past this many, the oldest is forgotten.\nexport const MAX_TRACKED_STORES = 64;\n\n// All the hub needs of a store to answer a read.\nexport interface ReadableStore {\n getState: () => unknown;\n}\n\ninterface Registry {\n stores: Map<string, StoreInfo>;\n apis: Map<string, ReadableStore>;\n labelCounts: Map<string, number>;\n}\n\ndeclare const global: { __BKB_STATE_STORES_V1__?: Registry };\n\nconst updates = createEventHub<StoreUpdate>('__BKB_STATE_STORE_UPDATES_V1__');\n\nfunction registry(): Registry {\n const existing = global[REGISTRY_KEY];\n if (existing) return existing;\n const created: Registry = { stores: new Map(), apis: new Map(), labelCounts: new Map() };\n global[REGISTRY_KEY] = created;\n return created;\n}\n\n// `useCartStore` is shown as `cart`; a store created without a variable (inside a factory,\n// as a default export) is named by the place that creates it.\nexport function storeLabel(variable: string | null, source: StateSource | null): string {\n if (variable) {\n const label = variable.replace(/^use(?=[A-Z])/, '').replace(/Store$/, '');\n return clip(label.charAt(0).toLowerCase() + label.slice(1) || variable, 128);\n }\n return source ? clip(source.file, 200) + ':' + source.line : 'store';\n}\n\nexport function registerStore(\n variable: string | null,\n source: StateSource | null,\n store: ReadableStore,\n): StoreInfo {\n const state = registry();\n const label = storeLabel(variable, source);\n const count = (state.labelCounts.get(label) ?? 0) + 1;\n state.labelCounts.set(label, count);\n const info: StoreInfo = {\n storeId: count === 1 ? label : label + '#' + count,\n store: label,\n source,\n };\n state.stores.set(info.storeId, info);\n state.apis.set(info.storeId, store);\n for (const oldest of [...state.stores.keys()].slice(\n 0,\n Math.max(0, state.stores.size - MAX_TRACKED_STORES),\n )) {\n state.stores.delete(oldest);\n state.apis.delete(oldest);\n }\n return info;\n}\n\nexport function readableStore(storeId: string): ReadableStore | undefined {\n return registry().apis.get(storeId);\n}\n\nexport function listStores(): StoreInfo[] {\n return [...registry().stores.values()];\n}\n\nexport const publishUpdate = updates.publish;\n// Listens to every store update. Updates made before the first listener are delivered first.\nexport const onStoreUpdate = updates.subscribe;\nexport const droppedUpdateCount = updates.droppedCount;\n", "// Names that reach the desktop have a length limit; longer ones are cut, never rejected.\nexport function clip(text: string, max: number): string {\n return text.length > max ? text.slice(0, max - 1) + '\u2026' : text;\n}\n", "const MAX_DEPTH = 4;\n\nfunction isPlainRecord(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;\n const prototype: unknown = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n\n// A store's state without its actions: functions are dropped at every level of plain objects\n// and arrays (to four levels), so the tree the desktop shows is the data, not the methods. Only\n// own data properties are read; a getter is skipped rather than run.\nexport function withoutFunctions(value: unknown, depth = 0): unknown {\n if (depth >= MAX_DEPTH) return value;\n if (Array.isArray(value)) return value.map((item) => withoutFunctions(item, depth + 1));\n if (!isPlainRecord(value)) return value;\n const copy: Record<string, unknown> = {};\n for (const key of Object.keys(value)) {\n const descriptor = Object.getOwnPropertyDescriptor(value, key);\n if (!descriptor || !('value' in descriptor) || typeof descriptor.value === 'function') continue;\n copy[key] = withoutFunctions(descriptor.value, depth + 1);\n }\n return copy;\n}\n", "import type { PluginProvider } from '@bkbdev/core/register';\nimport { handleStateCommand } from './commands';\nimport { droppedUpdateCount, onStoreUpdate } from './hub';\n\n// Hands the store updates to the logger: it sends each one on the `bkb.state-stores` channel\n// and reports the plugin to BKB in its hello message.\nexport function stateStoreProvider(): PluginProvider {\n return {\n capability: 'stateStores',\n instanceId: 'zustand-' + Date.now().toString(36) + Math.random().toString(36).slice(2, 8),\n subscribe: (listener) => onStoreUpdate((update) => listener(update)),\n handleCommand: handleStateCommand,\n droppedCount: droppedUpdateCount,\n };\n}\n", "import { trackSource } from '@bkbdev/core/plugin';\nimport type { TrackMeta } from '@bkbdev/core/plugin';\nimport { createActionTracker } from './actions';\nimport { clip } from './text';\nimport { diffState } from './diff';\nimport { publishUpdate, registerStore } from './hub';\n\n// The part of a Zustand store the plugin relies on. It is the same in zustand 3, 4 and 5, with\n// or without middleware, because the plugin only watches the finished store.\nexport interface TrackableStore {\n getState: () => unknown;\n // `replace?: never` accepts zustand's overloads (`replace?: false` and `replace: true`).\n setState: (partial: never, replace?: never) => void;\n subscribe: (listener: (state: unknown, previous: unknown) => void) => () => void;\n}\n\ndeclare const __DEV__: boolean | undefined;\nconst tracked = new WeakSet<object>();\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n// Reports every state change of the store to BKB, labelled with the action that caused it, and\n// returns the same store. Calling it again for a store it already tracks does nothing, and in a\n// release build it does nothing at all.\nexport function trackStore<T extends TrackableStore>(store: T, meta?: TrackMeta): T {\n if (typeof __DEV__ !== 'undefined' && !__DEV__) return store;\n if (tracked.has(store)) return store;\n tracked.add(store);\n const source = trackSource(meta);\n const info = registerStore(meta ? meta.variable : null, source, store);\n const actions = createActionTracker();\n // Wrapping happens before the subscription below, so it is not reported as an update.\n try {\n const state = store.getState();\n const patch = isRecord(state) ? actions.patchFor(state) : {};\n if (Object.keys(patch).length) store.setState(patch as never);\n } catch {\n /* Without wrappers every update is still reported, labelled setState. */\n }\n store.subscribe((state, previous) => {\n const diff = diffState(previous, state);\n if (!diff.changes.length && !diff.omittedChanges) return;\n const action = actions.current();\n publishUpdate({\n storeId: info.storeId,\n store: info.store,\n action: clip(action ? action.name : 'setState', 128),\n duringAsyncAction: action ? action.isAsync : false,\n changes: diff.changes,\n omittedChanges: diff.omittedChanges,\n timestamp: Date.now(),\n source,\n });\n });\n return store;\n}\n", "// Labels the `set` calls of a store with the action that made them: the action functions kept\n// in the state are replaced by wrappers that name themselves while they run.\n\nexport interface ActionContext {\n name: string;\n isAsync: boolean;\n}\n\nexport interface ActionTracker {\n current: () => ActionContext | null;\n // The state keys to patch so every action (and every action one object level down, like\n // `actions.reset`) runs through the tracker.\n patchFor: (state: Record<string, unknown>) => Record<string, unknown>;\n}\n\nfunction isPromiseLike(value: unknown): value is PromiseLike<unknown> {\n return (\n typeof value === 'object' &&\n value !== null &&\n typeof (value as { then?: unknown }).then === 'function'\n );\n}\n\nfunction isPlainRecord(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;\n const prototype: unknown = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n\nexport function createActionTracker(): ActionTracker {\n let active: ActionContext | null = null;\n\n function wrap(name: string, action: (...args: unknown[]) => unknown) {\n return function wrapped(this: unknown, ...args: unknown[]): unknown {\n const previous = active;\n active = { name, isAsync: false };\n let result: unknown;\n try {\n result = action.apply(this, args);\n } catch (error) {\n active = previous;\n throw error;\n }\n if (!isPromiseLike(result)) {\n active = previous;\n return result;\n }\n // Until the promise settles, updates are attributed to this action: after an `await`\n // the action is no longer on the call stack, so nothing else says where they come from.\n const pending: ActionContext = { name, isAsync: true };\n active = pending;\n const release = (): void => {\n if (active === pending) active = previous;\n };\n result.then(release, release);\n return result;\n };\n }\n\n function patchFor(state: Record<string, unknown>): Record<string, unknown> {\n const patch: Record<string, unknown> = {};\n for (const key of Object.keys(state)) {\n const value = state[key];\n if (typeof value === 'function') {\n patch[key] = wrap(key, value as (...args: unknown[]) => unknown);\n } else if (isPlainRecord(value)) {\n const nested: Record<string, unknown> = { ...value };\n let changed = false;\n for (const name of Object.keys(value)) {\n const inner = value[name];\n if (typeof inner !== 'function') continue;\n nested[name] = wrap(key + '.' + name, inner as (...args: unknown[]) => unknown);\n changed = true;\n }\n if (changed) patch[key] = nested;\n }\n }\n return patch;\n }\n\n return { current: () => active, patchFor };\n}\n", "import { previewValue } from '@bkbdev/core/plugin';\nimport { clip } from './text';\nimport type { StateChange } from '../../../protocol/src/state-store-provider';\n\nexport const MAX_DIFF_DEPTH = 3;\n// Twelve, not more: the serializer that carries an update keeps 200 nodes, and one change\n// takes about eleven of them.\nexport const MAX_CHANGES_PER_UPDATE = 12;\nconst MAX_ARRAY_ITEMS = 100;\nconst MAX_PATH_CHARACTERS = 256;\n\nexport interface StateDiff {\n changes: StateChange[];\n omittedChanges: number;\n}\n\ninterface Collector {\n changes: StateChange[];\n omittedChanges: number;\n}\n\ntype Own = { found: true; value: unknown } | { found: false };\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;\n const prototype: unknown = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n\n// A data property only: a getter could run application code or throw, and functions are the\n// store's actions, not its state.\nfunction ownValue(container: object, key: string): Own {\n try {\n const descriptor = Object.getOwnPropertyDescriptor(container, key);\n if (!descriptor || !('value' in descriptor) || typeof descriptor.value === 'function')\n return { found: false };\n return { found: true, value: descriptor.value };\n } catch {\n return { found: false };\n }\n}\n\nfunction objectPath(parent: string, key: string): string {\n const path = /^[A-Za-z_$][\\w$]*$/.test(key)\n ? parent\n ? parent + '.' + key\n : key\n : parent + '[' + JSON.stringify(key) + ']';\n return clip(path, MAX_PATH_CHARACTERS);\n}\n\nfunction record(out: Collector, change: StateChange): void {\n if (out.changes.length < MAX_CHANGES_PER_UPDATE) out.changes.push(change);\n else out.omittedChanges += 1;\n}\n\nfunction leaf(out: Collector, path: string, before: Own, after: Own): void {\n if (before.found && after.found) {\n if (!Object.is(before.value, after.value))\n record(out, {\n path,\n kind: 'changed',\n before: previewValue(before.value),\n after: previewValue(after.value),\n });\n } else if (after.found)\n record(out, { path, kind: 'added', before: null, after: previewValue(after.value) });\n else if (before.found)\n record(out, { path, kind: 'removed', before: previewValue(before.value), after: null });\n}\n\nfunction keysOf(before: object, after: object): string[] {\n return [...new Set([...Object.keys(before), ...Object.keys(after)])].filter(\n (key) => key !== '__proto__' && key !== 'constructor' && key !== 'prototype',\n );\n}\n\nfunction walk(out: Collector, before: Own, after: Own, path: string, depth: number): void {\n if (before.found && after.found && Object.is(before.value, after.value)) return;\n if (before.found && after.found && depth < MAX_DIFF_DEPTH) {\n const b = before.value;\n const a = after.value;\n if (isRecord(b) && isRecord(a)) {\n for (const name of keysOf(b, a))\n walk(out, ownValue(b, name), ownValue(a, name), objectPath(path, name), depth + 1);\n return;\n }\n if (Array.isArray(b) && Array.isArray(a)) {\n const length = Math.min(Math.max(b.length, a.length), MAX_ARRAY_ITEMS);\n for (let index = 0; index < length; index++)\n walk(\n out,\n ownValue(b, String(index)),\n ownValue(a, String(index)),\n path + '[' + index + ']',\n depth + 1,\n );\n if (Math.max(b.length, a.length) > MAX_ARRAY_ITEMS) out.omittedChanges += 1;\n return;\n }\n }\n leaf(out, path, before, after);\n}\n\n// The paths that differ between two states, to three levels deep and at most twelve of them.\n// Object keys and array indexes are compared one by one; anything deeper, or not a plain object\n// or array, is compared by identity and shown as one changed value.\nexport function diffState(before: unknown, after: unknown): StateDiff {\n const out: Collector = { changes: [], omittedChanges: 0 };\n walk(out, { found: true, value: before }, { found: true, value: after }, '', 0);\n return out;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,iBAA8B;;;ACA9B,oBAA+B;;;ACCxB,SAAS,KAAK,MAAc,KAAqB;AACtD,SAAO,KAAK,SAAS,MAAM,KAAK,MAAM,GAAG,MAAM,CAAC,IAAI,WAAM;AAC5D;;;ADQA,IAAM,eAAe;AAGd,IAAM,qBAAqB;AAelC,IAAM,cAAU,8BAA4B,gCAAgC;AAE5E,SAAS,WAAqB;AAC5B,QAAM,WAAW,OAAO,YAAY;AACpC,MAAI,SAAU,QAAO;AACrB,QAAM,UAAoB,EAAE,QAAQ,oBAAI,IAAI,GAAG,MAAM,oBAAI,IAAI,GAAG,aAAa,oBAAI,IAAI,EAAE;AACvF,SAAO,YAAY,IAAI;AACvB,SAAO;AACT;AAIO,SAAS,WAAW,UAAyB,QAAoC;AACtF,MAAI,UAAU;AACZ,UAAM,QAAQ,SAAS,QAAQ,iBAAiB,EAAE,EAAE,QAAQ,UAAU,EAAE;AACxE,WAAO,KAAK,MAAM,OAAO,CAAC,EAAE,YAAY,IAAI,MAAM,MAAM,CAAC,KAAK,UAAU,GAAG;AAAA,EAC7E;AACA,SAAO,SAAS,KAAK,OAAO,MAAM,GAAG,IAAI,MAAM,OAAO,OAAO;AAC/D;AAEO,SAAS,cACd,UACA,QACA,OACW;AArDb;AAsDE,QAAM,QAAQ,SAAS;AACvB,QAAM,QAAQ,WAA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,UAAU,UAAU,KAAK;AACtC,QAAI,CAAC,KAAK,QAAQ,UAAU,CAAC,KAAK,eAAgB;AAClD,UAAM,SAAS,QAAQ,QAAQ;AAC/B,kBAAc;AAAA,MACZ,SAAS,KAAK;AAAA,MACd,OAAO,KAAK;AAAA,MACZ,QAAQ,KAAK,SAAS,OAAO,OAAO,YAAY,GAAG;AAAA,MACnD,mBAAmB,SAAS,OAAO,UAAU;AAAA,MAC7C,SAAS,KAAK;AAAA,MACd,gBAAgB,KAAK;AAAA,MACrB,WAAW,KAAK,IAAI;AAAA,MACpB;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AACD,SAAO;AACT;;;AN9CA,IAAM,SAAoB;AAAA,EACxB,MAAM;AAAA,EACN,QAAQ,OAAO,EAAE,iBAAiB,CAAC,mBAAmB,CAAC,EAAE;AAC3D;AAEA,IAAO,cAAQ;",
|
|
6
|
+
"names": ["import_plugin", "import_plugin", "isPlainRecord", "import_plugin", "isRecord"]
|
|
7
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// packages/plugins/zustand/src/commands.ts
|
|
19
|
+
import { snapshotValue } from "@bkbdev/core/plugin";
|
|
20
|
+
|
|
21
|
+
// packages/plugins/zustand/src/hub.ts
|
|
22
|
+
import { createEventHub } from "@bkbdev/core/plugin";
|
|
23
|
+
|
|
24
|
+
// packages/plugins/zustand/src/text.ts
|
|
25
|
+
function clip(text, max) {
|
|
26
|
+
return text.length > max ? text.slice(0, max - 1) + "\u2026" : text;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// packages/plugins/zustand/src/hub.ts
|
|
30
|
+
var REGISTRY_KEY = "__BKB_STATE_STORES_V1__";
|
|
31
|
+
var MAX_TRACKED_STORES = 64;
|
|
32
|
+
var updates = createEventHub("__BKB_STATE_STORE_UPDATES_V1__");
|
|
33
|
+
function registry() {
|
|
34
|
+
const existing = global[REGISTRY_KEY];
|
|
35
|
+
if (existing) return existing;
|
|
36
|
+
const created = { stores: /* @__PURE__ */ new Map(), apis: /* @__PURE__ */ new Map(), labelCounts: /* @__PURE__ */ new Map() };
|
|
37
|
+
global[REGISTRY_KEY] = created;
|
|
38
|
+
return created;
|
|
39
|
+
}
|
|
40
|
+
function storeLabel(variable, source) {
|
|
41
|
+
if (variable) {
|
|
42
|
+
const label = variable.replace(/^use(?=[A-Z])/, "").replace(/Store$/, "");
|
|
43
|
+
return clip(label.charAt(0).toLowerCase() + label.slice(1) || variable, 128);
|
|
44
|
+
}
|
|
45
|
+
return source ? clip(source.file, 200) + ":" + source.line : "store";
|
|
46
|
+
}
|
|
47
|
+
function registerStore(variable, source, store) {
|
|
48
|
+
var _a;
|
|
49
|
+
const state = registry();
|
|
50
|
+
const label = storeLabel(variable, source);
|
|
51
|
+
const count = ((_a = state.labelCounts.get(label)) != null ? _a : 0) + 1;
|
|
52
|
+
state.labelCounts.set(label, count);
|
|
53
|
+
const info = {
|
|
54
|
+
storeId: count === 1 ? label : label + "#" + count,
|
|
55
|
+
store: label,
|
|
56
|
+
source
|
|
57
|
+
};
|
|
58
|
+
state.stores.set(info.storeId, info);
|
|
59
|
+
state.apis.set(info.storeId, store);
|
|
60
|
+
for (const oldest of [...state.stores.keys()].slice(
|
|
61
|
+
0,
|
|
62
|
+
Math.max(0, state.stores.size - MAX_TRACKED_STORES)
|
|
63
|
+
)) {
|
|
64
|
+
state.stores.delete(oldest);
|
|
65
|
+
state.apis.delete(oldest);
|
|
66
|
+
}
|
|
67
|
+
return info;
|
|
68
|
+
}
|
|
69
|
+
function readableStore(storeId) {
|
|
70
|
+
return registry().apis.get(storeId);
|
|
71
|
+
}
|
|
72
|
+
function listStores() {
|
|
73
|
+
return [...registry().stores.values()];
|
|
74
|
+
}
|
|
75
|
+
var publishUpdate = updates.publish;
|
|
76
|
+
var onStoreUpdate = updates.subscribe;
|
|
77
|
+
var droppedUpdateCount = updates.droppedCount;
|
|
78
|
+
|
|
79
|
+
// packages/plugins/zustand/src/state-values.ts
|
|
80
|
+
var MAX_DEPTH = 4;
|
|
81
|
+
function isPlainRecord(value) {
|
|
82
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
83
|
+
const prototype = Object.getPrototypeOf(value);
|
|
84
|
+
return prototype === Object.prototype || prototype === null;
|
|
85
|
+
}
|
|
86
|
+
function withoutFunctions(value, depth = 0) {
|
|
87
|
+
if (depth >= MAX_DEPTH) return value;
|
|
88
|
+
if (Array.isArray(value)) return value.map((item) => withoutFunctions(item, depth + 1));
|
|
89
|
+
if (!isPlainRecord(value)) return value;
|
|
90
|
+
const copy = {};
|
|
91
|
+
for (const key of Object.keys(value)) {
|
|
92
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
93
|
+
if (!descriptor || !("value" in descriptor) || typeof descriptor.value === "function") continue;
|
|
94
|
+
copy[key] = withoutFunctions(descriptor.value, depth + 1);
|
|
95
|
+
}
|
|
96
|
+
return copy;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// packages/plugins/zustand/src/commands.ts
|
|
100
|
+
function field(value, key) {
|
|
101
|
+
if (typeof value !== "object" || value === null) return void 0;
|
|
102
|
+
try {
|
|
103
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
104
|
+
return descriptor && "value" in descriptor ? descriptor.value : void 0;
|
|
105
|
+
} catch (e) {
|
|
106
|
+
return void 0;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function refused(reason) {
|
|
110
|
+
return { outcome: "refused", reason };
|
|
111
|
+
}
|
|
112
|
+
function handleStateCommand(command) {
|
|
113
|
+
const operation = field(command, "operation");
|
|
114
|
+
const storeId = field(command, "storeId");
|
|
115
|
+
if (operation !== "read-state" || typeof storeId !== "string" || !storeId || storeId.length > 256)
|
|
116
|
+
return refused("The request was not understood.");
|
|
117
|
+
const store = readableStore(storeId);
|
|
118
|
+
if (!store) return refused("That store is not tracked.");
|
|
119
|
+
try {
|
|
120
|
+
const { data, truncated } = snapshotValue(withoutFunctions(store.getState()));
|
|
121
|
+
return { outcome: "data", data, truncated };
|
|
122
|
+
} catch (e) {
|
|
123
|
+
return refused("The state could not be read.");
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// packages/plugins/zustand/src/provider.ts
|
|
128
|
+
function stateStoreProvider() {
|
|
129
|
+
return {
|
|
130
|
+
capability: "stateStores",
|
|
131
|
+
instanceId: "zustand-" + Date.now().toString(36) + Math.random().toString(36).slice(2, 8),
|
|
132
|
+
subscribe: (listener) => onStoreUpdate((update) => listener(update)),
|
|
133
|
+
handleCommand: handleStateCommand,
|
|
134
|
+
droppedCount: droppedUpdateCount
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// packages/plugins/zustand/src/track.ts
|
|
139
|
+
import { trackSource } from "@bkbdev/core/plugin";
|
|
140
|
+
|
|
141
|
+
// packages/plugins/zustand/src/actions.ts
|
|
142
|
+
function isPromiseLike(value) {
|
|
143
|
+
return typeof value === "object" && value !== null && typeof value.then === "function";
|
|
144
|
+
}
|
|
145
|
+
function isPlainRecord2(value) {
|
|
146
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
147
|
+
const prototype = Object.getPrototypeOf(value);
|
|
148
|
+
return prototype === Object.prototype || prototype === null;
|
|
149
|
+
}
|
|
150
|
+
function createActionTracker() {
|
|
151
|
+
let active = null;
|
|
152
|
+
function wrap(name, action) {
|
|
153
|
+
return function wrapped(...args) {
|
|
154
|
+
const previous = active;
|
|
155
|
+
active = { name, isAsync: false };
|
|
156
|
+
let result;
|
|
157
|
+
try {
|
|
158
|
+
result = action.apply(this, args);
|
|
159
|
+
} catch (error) {
|
|
160
|
+
active = previous;
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
if (!isPromiseLike(result)) {
|
|
164
|
+
active = previous;
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
const pending = { name, isAsync: true };
|
|
168
|
+
active = pending;
|
|
169
|
+
const release = () => {
|
|
170
|
+
if (active === pending) active = previous;
|
|
171
|
+
};
|
|
172
|
+
result.then(release, release);
|
|
173
|
+
return result;
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
function patchFor(state) {
|
|
177
|
+
const patch = {};
|
|
178
|
+
for (const key of Object.keys(state)) {
|
|
179
|
+
const value = state[key];
|
|
180
|
+
if (typeof value === "function") {
|
|
181
|
+
patch[key] = wrap(key, value);
|
|
182
|
+
} else if (isPlainRecord2(value)) {
|
|
183
|
+
const nested = __spreadValues({}, value);
|
|
184
|
+
let changed = false;
|
|
185
|
+
for (const name of Object.keys(value)) {
|
|
186
|
+
const inner = value[name];
|
|
187
|
+
if (typeof inner !== "function") continue;
|
|
188
|
+
nested[name] = wrap(key + "." + name, inner);
|
|
189
|
+
changed = true;
|
|
190
|
+
}
|
|
191
|
+
if (changed) patch[key] = nested;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return patch;
|
|
195
|
+
}
|
|
196
|
+
return { current: () => active, patchFor };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// packages/plugins/zustand/src/diff.ts
|
|
200
|
+
import { previewValue } from "@bkbdev/core/plugin";
|
|
201
|
+
var MAX_DIFF_DEPTH = 3;
|
|
202
|
+
var MAX_CHANGES_PER_UPDATE = 12;
|
|
203
|
+
var MAX_ARRAY_ITEMS = 100;
|
|
204
|
+
var MAX_PATH_CHARACTERS = 256;
|
|
205
|
+
function isRecord(value) {
|
|
206
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
207
|
+
const prototype = Object.getPrototypeOf(value);
|
|
208
|
+
return prototype === Object.prototype || prototype === null;
|
|
209
|
+
}
|
|
210
|
+
function ownValue(container, key) {
|
|
211
|
+
try {
|
|
212
|
+
const descriptor = Object.getOwnPropertyDescriptor(container, key);
|
|
213
|
+
if (!descriptor || !("value" in descriptor) || typeof descriptor.value === "function")
|
|
214
|
+
return { found: false };
|
|
215
|
+
return { found: true, value: descriptor.value };
|
|
216
|
+
} catch (e) {
|
|
217
|
+
return { found: false };
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
function objectPath(parent, key) {
|
|
221
|
+
const path = /^[A-Za-z_$][\w$]*$/.test(key) ? parent ? parent + "." + key : key : parent + "[" + JSON.stringify(key) + "]";
|
|
222
|
+
return clip(path, MAX_PATH_CHARACTERS);
|
|
223
|
+
}
|
|
224
|
+
function record(out, change) {
|
|
225
|
+
if (out.changes.length < MAX_CHANGES_PER_UPDATE) out.changes.push(change);
|
|
226
|
+
else out.omittedChanges += 1;
|
|
227
|
+
}
|
|
228
|
+
function leaf(out, path, before, after) {
|
|
229
|
+
if (before.found && after.found) {
|
|
230
|
+
if (!Object.is(before.value, after.value))
|
|
231
|
+
record(out, {
|
|
232
|
+
path,
|
|
233
|
+
kind: "changed",
|
|
234
|
+
before: previewValue(before.value),
|
|
235
|
+
after: previewValue(after.value)
|
|
236
|
+
});
|
|
237
|
+
} else if (after.found)
|
|
238
|
+
record(out, { path, kind: "added", before: null, after: previewValue(after.value) });
|
|
239
|
+
else if (before.found)
|
|
240
|
+
record(out, { path, kind: "removed", before: previewValue(before.value), after: null });
|
|
241
|
+
}
|
|
242
|
+
function keysOf(before, after) {
|
|
243
|
+
return [.../* @__PURE__ */ new Set([...Object.keys(before), ...Object.keys(after)])].filter(
|
|
244
|
+
(key) => key !== "__proto__" && key !== "constructor" && key !== "prototype"
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
function walk(out, before, after, path, depth) {
|
|
248
|
+
if (before.found && after.found && Object.is(before.value, after.value)) return;
|
|
249
|
+
if (before.found && after.found && depth < MAX_DIFF_DEPTH) {
|
|
250
|
+
const b = before.value;
|
|
251
|
+
const a = after.value;
|
|
252
|
+
if (isRecord(b) && isRecord(a)) {
|
|
253
|
+
for (const name of keysOf(b, a))
|
|
254
|
+
walk(out, ownValue(b, name), ownValue(a, name), objectPath(path, name), depth + 1);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
if (Array.isArray(b) && Array.isArray(a)) {
|
|
258
|
+
const length = Math.min(Math.max(b.length, a.length), MAX_ARRAY_ITEMS);
|
|
259
|
+
for (let index = 0; index < length; index++)
|
|
260
|
+
walk(
|
|
261
|
+
out,
|
|
262
|
+
ownValue(b, String(index)),
|
|
263
|
+
ownValue(a, String(index)),
|
|
264
|
+
path + "[" + index + "]",
|
|
265
|
+
depth + 1
|
|
266
|
+
);
|
|
267
|
+
if (Math.max(b.length, a.length) > MAX_ARRAY_ITEMS) out.omittedChanges += 1;
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
leaf(out, path, before, after);
|
|
272
|
+
}
|
|
273
|
+
function diffState(before, after) {
|
|
274
|
+
const out = { changes: [], omittedChanges: 0 };
|
|
275
|
+
walk(out, { found: true, value: before }, { found: true, value: after }, "", 0);
|
|
276
|
+
return out;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// packages/plugins/zustand/src/track.ts
|
|
280
|
+
var tracked = /* @__PURE__ */ new WeakSet();
|
|
281
|
+
function isRecord2(value) {
|
|
282
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
283
|
+
}
|
|
284
|
+
function trackStore(store, meta) {
|
|
285
|
+
if (typeof __DEV__ !== "undefined" && !__DEV__) return store;
|
|
286
|
+
if (tracked.has(store)) return store;
|
|
287
|
+
tracked.add(store);
|
|
288
|
+
const source = trackSource(meta);
|
|
289
|
+
const info = registerStore(meta ? meta.variable : null, source, store);
|
|
290
|
+
const actions = createActionTracker();
|
|
291
|
+
try {
|
|
292
|
+
const state = store.getState();
|
|
293
|
+
const patch = isRecord2(state) ? actions.patchFor(state) : {};
|
|
294
|
+
if (Object.keys(patch).length) store.setState(patch);
|
|
295
|
+
} catch (e) {
|
|
296
|
+
}
|
|
297
|
+
store.subscribe((state, previous) => {
|
|
298
|
+
const diff = diffState(previous, state);
|
|
299
|
+
if (!diff.changes.length && !diff.omittedChanges) return;
|
|
300
|
+
const action = actions.current();
|
|
301
|
+
publishUpdate({
|
|
302
|
+
storeId: info.storeId,
|
|
303
|
+
store: info.store,
|
|
304
|
+
action: clip(action ? action.name : "setState", 128),
|
|
305
|
+
duringAsyncAction: action ? action.isAsync : false,
|
|
306
|
+
changes: diff.changes,
|
|
307
|
+
omittedChanges: diff.omittedChanges,
|
|
308
|
+
timestamp: Date.now(),
|
|
309
|
+
source
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
return store;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// packages/plugins/zustand/src/index.ts
|
|
316
|
+
var plugin = {
|
|
317
|
+
name: "zustand",
|
|
318
|
+
extend: () => ({ pluginProviders: [stateStoreProvider()] })
|
|
319
|
+
};
|
|
320
|
+
var src_default = plugin;
|
|
321
|
+
export {
|
|
322
|
+
src_default as default,
|
|
323
|
+
droppedUpdateCount,
|
|
324
|
+
listStores,
|
|
325
|
+
onStoreUpdate,
|
|
326
|
+
trackStore
|
|
327
|
+
};
|
|
328
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/commands.ts", "../src/hub.ts", "../src/text.ts", "../src/state-values.ts", "../src/provider.ts", "../src/track.ts", "../src/actions.ts", "../src/diff.ts", "../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["import { snapshotValue } from '@bkbdev/core/plugin';\nimport { readableStore } from './hub';\nimport { withoutFunctions } from './state-values';\n\nfunction field(value: unknown, key: string): unknown {\n if (typeof value !== 'object' || value === null) return undefined;\n try {\n const descriptor = Object.getOwnPropertyDescriptor(value, key);\n return descriptor && 'value' in descriptor ? (descriptor.value as unknown) : undefined;\n } catch {\n return undefined;\n }\n}\n\nfunction refused(reason: string): { outcome: 'refused'; reason: string } {\n return { outcome: 'refused', reason };\n}\n\n// Serves the one request BKB makes to the plugin: read the state a store holds now, without its\n// actions, bounded like every other value that leaves the app.\nexport function handleStateCommand(command: unknown): unknown {\n const operation = field(command, 'operation');\n const storeId = field(command, 'storeId');\n if (operation !== 'read-state' || typeof storeId !== 'string' || !storeId || storeId.length > 256)\n return refused('The request was not understood.');\n const store = readableStore(storeId);\n if (!store) return refused('That store is not tracked.');\n try {\n const { data, truncated } = snapshotValue(withoutFunctions(store.getState()));\n return { outcome: 'data', data, truncated };\n } catch {\n return refused('The state could not be read.');\n }\n}\n", "import { createEventHub } from '@bkbdev/core/plugin';\nimport { clip } from './text';\nimport type {\n StateSource,\n StoreInfo,\n StoreUpdate,\n} from '../../../protocol/src/state-store-provider';\n\n// The registry of tracked stores lives on the global object, not in this module: Metro can load\n// the package once for the project's own import and once for the registration, and both must\n// share one set of stores.\nconst REGISTRY_KEY = '__BKB_STATE_STORES_V1__';\n// A store made again and again (inside a component or a factory) must not pile up for the whole\n// session: past this many, the oldest is forgotten.\nexport const MAX_TRACKED_STORES = 64;\n\n// All the hub needs of a store to answer a read.\nexport interface ReadableStore {\n getState: () => unknown;\n}\n\ninterface Registry {\n stores: Map<string, StoreInfo>;\n apis: Map<string, ReadableStore>;\n labelCounts: Map<string, number>;\n}\n\ndeclare const global: { __BKB_STATE_STORES_V1__?: Registry };\n\nconst updates = createEventHub<StoreUpdate>('__BKB_STATE_STORE_UPDATES_V1__');\n\nfunction registry(): Registry {\n const existing = global[REGISTRY_KEY];\n if (existing) return existing;\n const created: Registry = { stores: new Map(), apis: new Map(), labelCounts: new Map() };\n global[REGISTRY_KEY] = created;\n return created;\n}\n\n// `useCartStore` is shown as `cart`; a store created without a variable (inside a factory,\n// as a default export) is named by the place that creates it.\nexport function storeLabel(variable: string | null, source: StateSource | null): string {\n if (variable) {\n const label = variable.replace(/^use(?=[A-Z])/, '').replace(/Store$/, '');\n return clip(label.charAt(0).toLowerCase() + label.slice(1) || variable, 128);\n }\n return source ? clip(source.file, 200) + ':' + source.line : 'store';\n}\n\nexport function registerStore(\n variable: string | null,\n source: StateSource | null,\n store: ReadableStore,\n): StoreInfo {\n const state = registry();\n const label = storeLabel(variable, source);\n const count = (state.labelCounts.get(label) ?? 0) + 1;\n state.labelCounts.set(label, count);\n const info: StoreInfo = {\n storeId: count === 1 ? label : label + '#' + count,\n store: label,\n source,\n };\n state.stores.set(info.storeId, info);\n state.apis.set(info.storeId, store);\n for (const oldest of [...state.stores.keys()].slice(\n 0,\n Math.max(0, state.stores.size - MAX_TRACKED_STORES),\n )) {\n state.stores.delete(oldest);\n state.apis.delete(oldest);\n }\n return info;\n}\n\nexport function readableStore(storeId: string): ReadableStore | undefined {\n return registry().apis.get(storeId);\n}\n\nexport function listStores(): StoreInfo[] {\n return [...registry().stores.values()];\n}\n\nexport const publishUpdate = updates.publish;\n// Listens to every store update. Updates made before the first listener are delivered first.\nexport const onStoreUpdate = updates.subscribe;\nexport const droppedUpdateCount = updates.droppedCount;\n", "// Names that reach the desktop have a length limit; longer ones are cut, never rejected.\nexport function clip(text: string, max: number): string {\n return text.length > max ? text.slice(0, max - 1) + '\u2026' : text;\n}\n", "const MAX_DEPTH = 4;\n\nfunction isPlainRecord(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;\n const prototype: unknown = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n\n// A store's state without its actions: functions are dropped at every level of plain objects\n// and arrays (to four levels), so the tree the desktop shows is the data, not the methods. Only\n// own data properties are read; a getter is skipped rather than run.\nexport function withoutFunctions(value: unknown, depth = 0): unknown {\n if (depth >= MAX_DEPTH) return value;\n if (Array.isArray(value)) return value.map((item) => withoutFunctions(item, depth + 1));\n if (!isPlainRecord(value)) return value;\n const copy: Record<string, unknown> = {};\n for (const key of Object.keys(value)) {\n const descriptor = Object.getOwnPropertyDescriptor(value, key);\n if (!descriptor || !('value' in descriptor) || typeof descriptor.value === 'function') continue;\n copy[key] = withoutFunctions(descriptor.value, depth + 1);\n }\n return copy;\n}\n", "import type { PluginProvider } from '@bkbdev/core/register';\nimport { handleStateCommand } from './commands';\nimport { droppedUpdateCount, onStoreUpdate } from './hub';\n\n// Hands the store updates to the logger: it sends each one on the `bkb.state-stores` channel\n// and reports the plugin to BKB in its hello message.\nexport function stateStoreProvider(): PluginProvider {\n return {\n capability: 'stateStores',\n instanceId: 'zustand-' + Date.now().toString(36) + Math.random().toString(36).slice(2, 8),\n subscribe: (listener) => onStoreUpdate((update) => listener(update)),\n handleCommand: handleStateCommand,\n droppedCount: droppedUpdateCount,\n };\n}\n", "import { trackSource } from '@bkbdev/core/plugin';\nimport type { TrackMeta } from '@bkbdev/core/plugin';\nimport { createActionTracker } from './actions';\nimport { clip } from './text';\nimport { diffState } from './diff';\nimport { publishUpdate, registerStore } from './hub';\n\n// The part of a Zustand store the plugin relies on. It is the same in zustand 3, 4 and 5, with\n// or without middleware, because the plugin only watches the finished store.\nexport interface TrackableStore {\n getState: () => unknown;\n // `replace?: never` accepts zustand's overloads (`replace?: false` and `replace: true`).\n setState: (partial: never, replace?: never) => void;\n subscribe: (listener: (state: unknown, previous: unknown) => void) => () => void;\n}\n\ndeclare const __DEV__: boolean | undefined;\nconst tracked = new WeakSet<object>();\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n// Reports every state change of the store to BKB, labelled with the action that caused it, and\n// returns the same store. Calling it again for a store it already tracks does nothing, and in a\n// release build it does nothing at all.\nexport function trackStore<T extends TrackableStore>(store: T, meta?: TrackMeta): T {\n if (typeof __DEV__ !== 'undefined' && !__DEV__) return store;\n if (tracked.has(store)) return store;\n tracked.add(store);\n const source = trackSource(meta);\n const info = registerStore(meta ? meta.variable : null, source, store);\n const actions = createActionTracker();\n // Wrapping happens before the subscription below, so it is not reported as an update.\n try {\n const state = store.getState();\n const patch = isRecord(state) ? actions.patchFor(state) : {};\n if (Object.keys(patch).length) store.setState(patch as never);\n } catch {\n /* Without wrappers every update is still reported, labelled setState. */\n }\n store.subscribe((state, previous) => {\n const diff = diffState(previous, state);\n if (!diff.changes.length && !diff.omittedChanges) return;\n const action = actions.current();\n publishUpdate({\n storeId: info.storeId,\n store: info.store,\n action: clip(action ? action.name : 'setState', 128),\n duringAsyncAction: action ? action.isAsync : false,\n changes: diff.changes,\n omittedChanges: diff.omittedChanges,\n timestamp: Date.now(),\n source,\n });\n });\n return store;\n}\n", "// Labels the `set` calls of a store with the action that made them: the action functions kept\n// in the state are replaced by wrappers that name themselves while they run.\n\nexport interface ActionContext {\n name: string;\n isAsync: boolean;\n}\n\nexport interface ActionTracker {\n current: () => ActionContext | null;\n // The state keys to patch so every action (and every action one object level down, like\n // `actions.reset`) runs through the tracker.\n patchFor: (state: Record<string, unknown>) => Record<string, unknown>;\n}\n\nfunction isPromiseLike(value: unknown): value is PromiseLike<unknown> {\n return (\n typeof value === 'object' &&\n value !== null &&\n typeof (value as { then?: unknown }).then === 'function'\n );\n}\n\nfunction isPlainRecord(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;\n const prototype: unknown = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n\nexport function createActionTracker(): ActionTracker {\n let active: ActionContext | null = null;\n\n function wrap(name: string, action: (...args: unknown[]) => unknown) {\n return function wrapped(this: unknown, ...args: unknown[]): unknown {\n const previous = active;\n active = { name, isAsync: false };\n let result: unknown;\n try {\n result = action.apply(this, args);\n } catch (error) {\n active = previous;\n throw error;\n }\n if (!isPromiseLike(result)) {\n active = previous;\n return result;\n }\n // Until the promise settles, updates are attributed to this action: after an `await`\n // the action is no longer on the call stack, so nothing else says where they come from.\n const pending: ActionContext = { name, isAsync: true };\n active = pending;\n const release = (): void => {\n if (active === pending) active = previous;\n };\n result.then(release, release);\n return result;\n };\n }\n\n function patchFor(state: Record<string, unknown>): Record<string, unknown> {\n const patch: Record<string, unknown> = {};\n for (const key of Object.keys(state)) {\n const value = state[key];\n if (typeof value === 'function') {\n patch[key] = wrap(key, value as (...args: unknown[]) => unknown);\n } else if (isPlainRecord(value)) {\n const nested: Record<string, unknown> = { ...value };\n let changed = false;\n for (const name of Object.keys(value)) {\n const inner = value[name];\n if (typeof inner !== 'function') continue;\n nested[name] = wrap(key + '.' + name, inner as (...args: unknown[]) => unknown);\n changed = true;\n }\n if (changed) patch[key] = nested;\n }\n }\n return patch;\n }\n\n return { current: () => active, patchFor };\n}\n", "import { previewValue } from '@bkbdev/core/plugin';\nimport { clip } from './text';\nimport type { StateChange } from '../../../protocol/src/state-store-provider';\n\nexport const MAX_DIFF_DEPTH = 3;\n// Twelve, not more: the serializer that carries an update keeps 200 nodes, and one change\n// takes about eleven of them.\nexport const MAX_CHANGES_PER_UPDATE = 12;\nconst MAX_ARRAY_ITEMS = 100;\nconst MAX_PATH_CHARACTERS = 256;\n\nexport interface StateDiff {\n changes: StateChange[];\n omittedChanges: number;\n}\n\ninterface Collector {\n changes: StateChange[];\n omittedChanges: number;\n}\n\ntype Own = { found: true; value: unknown } | { found: false };\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;\n const prototype: unknown = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n\n// A data property only: a getter could run application code or throw, and functions are the\n// store's actions, not its state.\nfunction ownValue(container: object, key: string): Own {\n try {\n const descriptor = Object.getOwnPropertyDescriptor(container, key);\n if (!descriptor || !('value' in descriptor) || typeof descriptor.value === 'function')\n return { found: false };\n return { found: true, value: descriptor.value };\n } catch {\n return { found: false };\n }\n}\n\nfunction objectPath(parent: string, key: string): string {\n const path = /^[A-Za-z_$][\\w$]*$/.test(key)\n ? parent\n ? parent + '.' + key\n : key\n : parent + '[' + JSON.stringify(key) + ']';\n return clip(path, MAX_PATH_CHARACTERS);\n}\n\nfunction record(out: Collector, change: StateChange): void {\n if (out.changes.length < MAX_CHANGES_PER_UPDATE) out.changes.push(change);\n else out.omittedChanges += 1;\n}\n\nfunction leaf(out: Collector, path: string, before: Own, after: Own): void {\n if (before.found && after.found) {\n if (!Object.is(before.value, after.value))\n record(out, {\n path,\n kind: 'changed',\n before: previewValue(before.value),\n after: previewValue(after.value),\n });\n } else if (after.found)\n record(out, { path, kind: 'added', before: null, after: previewValue(after.value) });\n else if (before.found)\n record(out, { path, kind: 'removed', before: previewValue(before.value), after: null });\n}\n\nfunction keysOf(before: object, after: object): string[] {\n return [...new Set([...Object.keys(before), ...Object.keys(after)])].filter(\n (key) => key !== '__proto__' && key !== 'constructor' && key !== 'prototype',\n );\n}\n\nfunction walk(out: Collector, before: Own, after: Own, path: string, depth: number): void {\n if (before.found && after.found && Object.is(before.value, after.value)) return;\n if (before.found && after.found && depth < MAX_DIFF_DEPTH) {\n const b = before.value;\n const a = after.value;\n if (isRecord(b) && isRecord(a)) {\n for (const name of keysOf(b, a))\n walk(out, ownValue(b, name), ownValue(a, name), objectPath(path, name), depth + 1);\n return;\n }\n if (Array.isArray(b) && Array.isArray(a)) {\n const length = Math.min(Math.max(b.length, a.length), MAX_ARRAY_ITEMS);\n for (let index = 0; index < length; index++)\n walk(\n out,\n ownValue(b, String(index)),\n ownValue(a, String(index)),\n path + '[' + index + ']',\n depth + 1,\n );\n if (Math.max(b.length, a.length) > MAX_ARRAY_ITEMS) out.omittedChanges += 1;\n return;\n }\n }\n leaf(out, path, before, after);\n}\n\n// The paths that differ between two states, to three levels deep and at most twelve of them.\n// Object keys and array indexes are compared one by one; anything deeper, or not a plain object\n// or array, is compared by identity and shown as one changed value.\nexport function diffState(before: unknown, after: unknown): StateDiff {\n const out: Collector = { changes: [], omittedChanges: 0 };\n walk(out, { found: true, value: before }, { found: true, value: after }, '', 0);\n return out;\n}\n", "import type { BkbPlugin } from '@bkbdev/core/register';\nimport { stateStoreProvider } from './provider';\n\nexport { trackStore } from './track';\nexport type { TrackableStore } from './track';\nexport type { TrackMeta } from '@bkbdev/core/plugin';\nexport { onStoreUpdate, listStores, droppedUpdateCount } from './hub';\n\n// Zustand state changes for the BKB desktop. Listing this package in the `bkbPlugins` option of\n// `@bkbdev/core/babel` makes the Babel plugin wrap the app's `create` and `createStore` calls with\n// `trackStore`; release builds never load it.\nconst plugin: BkbPlugin = {\n name: 'zustand',\n extend: () => ({ pluginProviders: [stateStoreProvider()] }),\n};\n\nexport default plugin;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA,SAAS,qBAAqB;;;ACA9B,SAAS,sBAAsB;;;ACCxB,SAAS,KAAK,MAAc,KAAqB;AACtD,SAAO,KAAK,SAAS,MAAM,KAAK,MAAM,GAAG,MAAM,CAAC,IAAI,WAAM;AAC5D;;;ADQA,IAAM,eAAe;AAGd,IAAM,qBAAqB;AAelC,IAAM,UAAU,eAA4B,gCAAgC;AAE5E,SAAS,WAAqB;AAC5B,QAAM,WAAW,OAAO,YAAY;AACpC,MAAI,SAAU,QAAO;AACrB,QAAM,UAAoB,EAAE,QAAQ,oBAAI,IAAI,GAAG,MAAM,oBAAI,IAAI,GAAG,aAAa,oBAAI,IAAI,EAAE;A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AAG,QAAO;AAC/B,UAAQ,IAAI,KAAK;AACjB,QAAM,SAAS,YAAY,IAAI;AAC/B,QAAM,OAAO,cAAc,OAAO,KAAK,WAAW,MAAM,QAAQ,KAAK;AACrE,QAAM,UAAU,oBAAoB;AAEpC,MAAI;AACF,UAAM,QAAQ,MAAM,SAAS;AAC7B,UAAM,QAAQA,UAAS,KAAK,IAAI,QAAQ,SAAS,KAAK,IAAI,CAAC;AAC3D,QAAI,OAAO,KAAK,KAAK,EAAE,OAAQ,OAAM,SAAS,KAAc;AAAA,EAC9D,SAAQ;AAAA,EAER;AACA,QAAM,UAAU,CAAC,OAAO,aAAa;AACnC,UAAM,OAAO,UAAU,UAAU,KAAK;AACtC,QAAI,CAAC,KAAK,QAAQ,UAAU,CAAC,KAAK,eAAgB;AAClD,UAAM,SAAS,QAAQ,QAAQ;AAC/B,kBAAc;AAAA,MACZ,SAAS,KAAK;AAAA,MACd,OAAO,KAAK;AAAA,MACZ,QAAQ,KAAK,SAAS,OAAO,OAAO,YAAY,GAAG;AAAA,MACnD,mBAAmB,SAAS,OAAO,UAAU;AAAA,MAC7C,SAAS,KAAK;AAAA,MACd,gBAAgB,KAAK;AAAA,MACrB,WAAW,KAAK,IAAI;AAAA,MACpB;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AACD,SAAO;AACT;;;AG9CA,IAAM,SAAoB;AAAA,EACxB,MAAM;AAAA,EACN,QAAQ,OAAO,EAAE,iBAAiB,CAAC,mBAAmB,CAAC,EAAE;AAC3D;AAEA,IAAO,cAAQ;",
|
|
6
|
+
"names": ["isPlainRecord", "isRecord"]
|
|
7
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { BkbPlugin } from '@bkbdev/core/register';
|
|
2
|
+
import type { TrackMeta, ValuePreview } from '@bkbdev/core/plugin';
|
|
3
|
+
|
|
4
|
+
export interface TrackableStore {
|
|
5
|
+
getState: () => unknown;
|
|
6
|
+
setState: (partial: never, replace?: never) => void;
|
|
7
|
+
subscribe: (listener: (state: unknown, previous: unknown) => void) => () => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type { TrackMeta } from '@bkbdev/core/plugin';
|
|
11
|
+
|
|
12
|
+
export interface StateChange {
|
|
13
|
+
path: string;
|
|
14
|
+
kind: 'changed' | 'added' | 'removed';
|
|
15
|
+
before: ValuePreview | null;
|
|
16
|
+
after: ValuePreview | null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface StoreUpdate {
|
|
20
|
+
storeId: string;
|
|
21
|
+
store: string;
|
|
22
|
+
action: string;
|
|
23
|
+
duringAsyncAction: boolean;
|
|
24
|
+
changes: StateChange[];
|
|
25
|
+
omittedChanges: number;
|
|
26
|
+
timestamp: number;
|
|
27
|
+
source: { file: string; line: number; column: number } | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface StoreInfo {
|
|
31
|
+
storeId: string;
|
|
32
|
+
store: string;
|
|
33
|
+
source: { file: string; line: number; column: number } | null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Reports the store's state changes to BKB and returns the same store. The Babel plugin adds
|
|
37
|
+
// this call around `create` and `createStore`; call it yourself only for a store it cannot see.
|
|
38
|
+
export function trackStore<T extends TrackableStore>(store: T, meta?: TrackMeta): T;
|
|
39
|
+
export function onStoreUpdate(listener: (update: StoreUpdate) => void): () => void;
|
|
40
|
+
export function listStores(): StoreInfo[];
|
|
41
|
+
export function droppedUpdateCount(): number;
|
|
42
|
+
|
|
43
|
+
declare const plugin: BkbPlugin;
|
|
44
|
+
export default plugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bkbdev/plugin-zustand",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "BKB Zustand plugin: stores, state changes and actions for @bkbdev/core.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Bobby Echo Prasetyo",
|
|
7
|
+
"homepage": "https://github.com/bobbyecho/bkb#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/bobbyecho/bkb.git",
|
|
11
|
+
"directory": "packages/plugins/zustand"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/bobbyecho/bkb/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"react-native",
|
|
18
|
+
"zustand",
|
|
19
|
+
"debugging"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"private": false,
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "./dist/index.cjs",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"react-native": "./dist/index.js",
|
|
29
|
+
"types": "./index.d.ts",
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"index.d.ts",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"types": "./index.d.ts",
|
|
39
|
+
"react-native": "./dist/index.js",
|
|
40
|
+
"import": "./dist/index.js",
|
|
41
|
+
"require": "./dist/index.cjs"
|
|
42
|
+
},
|
|
43
|
+
"./package.json": "./package.json"
|
|
44
|
+
},
|
|
45
|
+
"bkb": {
|
|
46
|
+
"wrap": [
|
|
47
|
+
{
|
|
48
|
+
"tracker": "trackStore",
|
|
49
|
+
"from": [
|
|
50
|
+
"zustand",
|
|
51
|
+
"zustand/vanilla"
|
|
52
|
+
],
|
|
53
|
+
"imports": [
|
|
54
|
+
"create",
|
|
55
|
+
"createStore",
|
|
56
|
+
"default"
|
|
57
|
+
],
|
|
58
|
+
"form": "call"
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
},
|
|
62
|
+
"peerDependencies": {
|
|
63
|
+
"@bkbdev/core": "^1.1.0"
|
|
64
|
+
}
|
|
6
65
|
}
|