@rn-perf-kit/adapters 0.0.0-stage → 0.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/package.json +31 -4
- package/src/fetch.ts +53 -0
- package/src/index.ts +6 -0
- package/src/query.ts +38 -0
- package/src/redux.ts +24 -0
- package/src/zustand.ts +40 -0
- package/tsconfig.json +5 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rn-perf-kit/adapters",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Store adapters for rn-perf-kit: Redux, Zustand, TanStack Query, fetch",
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
|
+
"types": "./src/index.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"typecheck": "tsc --noEmit",
|
|
9
|
+
"lint": "eslint src/"
|
|
10
|
+
},
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"@reduxjs/toolkit": ">=1.9.0",
|
|
13
|
+
"@tanstack/react-query": ">=5.0.0",
|
|
14
|
+
"zustand": ">=4.0.0"
|
|
15
|
+
},
|
|
16
|
+
"peerDependenciesMeta": {
|
|
17
|
+
"@reduxjs/toolkit": {
|
|
18
|
+
"optional": true
|
|
19
|
+
},
|
|
20
|
+
"@tanstack/react-query": {
|
|
21
|
+
"optional": true
|
|
22
|
+
},
|
|
23
|
+
"zustand": {
|
|
24
|
+
"optional": true
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"typescript": "^5.3.0"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@rn-perf-kit/core": "^0.1.0"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/fetch.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fetch interceptor for rn-perf-kit.
|
|
3
|
+
* Records network events on the timeline.
|
|
4
|
+
*/
|
|
5
|
+
import { timeline } from '@rn-perf-kit/core';
|
|
6
|
+
|
|
7
|
+
const originalFetch = global.fetch;
|
|
8
|
+
let installed = false;
|
|
9
|
+
|
|
10
|
+
export function installFetchInterceptor(): void {
|
|
11
|
+
if (installed || !originalFetch) return;
|
|
12
|
+
installed = true;
|
|
13
|
+
global.fetch = async function patchedFetch(
|
|
14
|
+
input: RequestInfo | URL,
|
|
15
|
+
init?: RequestInit
|
|
16
|
+
): Promise<Response> {
|
|
17
|
+
const t = timeline.now();
|
|
18
|
+
const url =
|
|
19
|
+
typeof input === 'string'
|
|
20
|
+
? input
|
|
21
|
+
: input instanceof URL
|
|
22
|
+
? input.href
|
|
23
|
+
: (input as Request).url;
|
|
24
|
+
const redacted = redactUrl(url);
|
|
25
|
+
let response: Response;
|
|
26
|
+
try {
|
|
27
|
+
response = await originalFetch(input, init);
|
|
28
|
+
} catch (err) {
|
|
29
|
+
timeline.add({ type: 'net', t, dur: timeline.now() - t, url: redacted, bytes: 0, status: 0 });
|
|
30
|
+
throw err;
|
|
31
|
+
}
|
|
32
|
+
const dur = timeline.now() - t;
|
|
33
|
+
const bytes = Number(response.headers.get('content-length') ?? 0);
|
|
34
|
+
timeline.add({ type: 'net', t, dur, url: redacted, bytes, status: response.status });
|
|
35
|
+
return response;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function uninstallFetchInterceptor(): void {
|
|
40
|
+
if (!installed) return;
|
|
41
|
+
global.fetch = originalFetch;
|
|
42
|
+
installed = false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function redactUrl(url: string): string {
|
|
46
|
+
try {
|
|
47
|
+
const u = new URL(url);
|
|
48
|
+
u.search = u.search ? '?[redacted]' : '';
|
|
49
|
+
return u.toString();
|
|
50
|
+
} catch {
|
|
51
|
+
return url.split('?')[0];
|
|
52
|
+
}
|
|
53
|
+
}
|
package/src/index.ts
ADDED
package/src/query.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TanStack Query adapter for rn-perf-kit.
|
|
3
|
+
* Subscribes to QueryCache and flags identical refetches.
|
|
4
|
+
*
|
|
5
|
+
* Usage: queryPerf(queryClient);
|
|
6
|
+
*/
|
|
7
|
+
import { timeline } from '@rn-perf-kit/core';
|
|
8
|
+
|
|
9
|
+
interface QueryClient { getQueryCache: () => QueryCache; }
|
|
10
|
+
interface QueryCache { subscribe: (listener: (event: QueryCacheEvent) => void) => () => void; }
|
|
11
|
+
interface QueryCacheEvent {
|
|
12
|
+
type: 'added' | 'removed' | 'updated' | 'observerAdded' | 'observerRemoved' | 'observerResultsUpdated';
|
|
13
|
+
query: { queryKey: unknown[]; state: { data: unknown; status: string } };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const previousData = new Map<string, unknown>();
|
|
17
|
+
|
|
18
|
+
export function queryPerf(queryClient: QueryClient): () => void {
|
|
19
|
+
const cache = queryClient.getQueryCache();
|
|
20
|
+
return cache.subscribe((event: QueryCacheEvent) => {
|
|
21
|
+
if (event.type !== 'updated') return;
|
|
22
|
+
const key = JSON.stringify(event.query.queryKey);
|
|
23
|
+
const prevData = previousData.get(key);
|
|
24
|
+
const nextData = event.query.state.data;
|
|
25
|
+
const isIdenticalRefetch =
|
|
26
|
+
prevData !== undefined &&
|
|
27
|
+
prevData !== nextData &&
|
|
28
|
+
JSON.stringify(prevData) === JSON.stringify(nextData);
|
|
29
|
+
previousData.set(key, nextData);
|
|
30
|
+
timeline.add({
|
|
31
|
+
type: 'store',
|
|
32
|
+
t: timeline.now(),
|
|
33
|
+
source: 'query',
|
|
34
|
+
key,
|
|
35
|
+
storeName: isIdenticalRefetch ? `${key} [identical-refetch]` : key,
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
package/src/redux.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redux middleware for rn-perf-kit.
|
|
3
|
+
* Records every dispatched action as a 'store' event on the timeline.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* const store = configureStore({
|
|
7
|
+
* reducer,
|
|
8
|
+
* middleware: (getDefault) => getDefault().concat(reduxPerfMiddleware),
|
|
9
|
+
* });
|
|
10
|
+
*/
|
|
11
|
+
import { timeline } from '@rn-perf-kit/core';
|
|
12
|
+
|
|
13
|
+
type Action = { type: string; [key: string]: unknown };
|
|
14
|
+
type Dispatch = (action: Action) => Action;
|
|
15
|
+
type MiddlewareAPI = { getState: () => unknown; dispatch: Dispatch };
|
|
16
|
+
type Middleware = (api: MiddlewareAPI) => (next: Dispatch) => (action: Action) => Action;
|
|
17
|
+
|
|
18
|
+
export const reduxPerfMiddleware: Middleware =
|
|
19
|
+
(_api) => (next) => (action) => {
|
|
20
|
+
const t = timeline.now();
|
|
21
|
+
const result = next(action);
|
|
22
|
+
timeline.add({ type: 'store', t, source: 'redux', key: action.type ?? 'UNKNOWN' });
|
|
23
|
+
return result;
|
|
24
|
+
};
|
package/src/zustand.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zustand middleware for rn-perf-kit.
|
|
3
|
+
* Records state changes with their changed keys.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* const useCart = create(zustandPerf('cart', (set) => ({ ... })));
|
|
7
|
+
*/
|
|
8
|
+
import { timeline } from '@rn-perf-kit/core';
|
|
9
|
+
|
|
10
|
+
type StateCreator<T> = (
|
|
11
|
+
set: (partial: Partial<T> | ((state: T) => Partial<T>)) => void,
|
|
12
|
+
get: () => T,
|
|
13
|
+
api: unknown
|
|
14
|
+
) => T;
|
|
15
|
+
|
|
16
|
+
export function zustandPerf<T extends object>(
|
|
17
|
+
storeName: string,
|
|
18
|
+
creator: StateCreator<T>
|
|
19
|
+
): StateCreator<T> {
|
|
20
|
+
return (set, get, api) => {
|
|
21
|
+
const patchedSet = (partial: Partial<T> | ((state: T) => Partial<T>)): void => {
|
|
22
|
+
const prevState = get();
|
|
23
|
+
set(partial);
|
|
24
|
+
const nextState = get();
|
|
25
|
+
const changedKeys = (Object.keys(nextState) as Array<keyof T>)
|
|
26
|
+
.filter((k) => prevState[k] !== nextState[k])
|
|
27
|
+
.map(String);
|
|
28
|
+
if (changedKeys.length > 0) {
|
|
29
|
+
timeline.add({
|
|
30
|
+
type: 'store',
|
|
31
|
+
t: timeline.now(),
|
|
32
|
+
source: 'zustand',
|
|
33
|
+
key: changedKeys.join(','),
|
|
34
|
+
storeName,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
return creator(patchedSet, get, api);
|
|
39
|
+
};
|
|
40
|
+
}
|
package/tsconfig.json
ADDED
package/README.md
DELETED