@streamlayer/react-polyfills 0.1.46 → 0.1.48
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/lib/index.d.ts +4 -4
- package/lib/index.js +3 -4
- package/lib/legacy.d.ts +3 -1
- package/lib/legacy.js +20 -4
- package/package.json +1 -2
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as
|
|
3
|
-
export declare const useStore: typeof
|
|
4
|
-
export declare const useTransition:
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import * as legacy from './legacy';
|
|
3
|
+
export declare const useStore: typeof legacy.useStore;
|
|
4
|
+
export declare const useTransition: typeof React.useTransition;
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as modernReact from 'react';
|
|
1
|
+
import React from 'react';
|
|
3
2
|
import * as legacy from './legacy';
|
|
4
|
-
export const useStore =
|
|
3
|
+
export const useStore = legacy.useStore;
|
|
5
4
|
const legacyUseTransition = () => {
|
|
6
5
|
return [
|
|
7
6
|
false,
|
|
@@ -10,4 +9,4 @@ const legacyUseTransition = () => {
|
|
|
10
9
|
},
|
|
11
10
|
];
|
|
12
11
|
};
|
|
13
|
-
export const useTransition =
|
|
12
|
+
export const useTransition = React.useTransition || legacyUseTransition;
|
package/lib/legacy.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { type Store, type StoreValue } from 'nanostores';
|
|
2
|
+
import React from 'react';
|
|
2
3
|
type StoreKeys<T> = T extends {
|
|
3
4
|
setKey: (k: infer K, v: any) => unknown;
|
|
4
5
|
} ? K : never;
|
|
5
6
|
export interface UseStoreOptions<SomeStore> {
|
|
7
|
+
deps?: React.DependencyList;
|
|
6
8
|
/**
|
|
7
9
|
* Will re-render components only on specific key changes.
|
|
8
10
|
*/
|
|
9
11
|
keys?: StoreKeys<SomeStore>[];
|
|
10
12
|
}
|
|
11
|
-
export declare function useStore<SomeStore extends Store>(store:
|
|
13
|
+
export declare function useStore<SomeStore extends Store>(store: SomeStore, { keys, deps }?: UseStoreOptions<SomeStore>): StoreValue<SomeStore>;
|
|
12
14
|
export {};
|
package/lib/legacy.js
CHANGED
|
@@ -1,10 +1,26 @@
|
|
|
1
1
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2
2
|
// @ts-nocheck
|
|
3
3
|
import { listenKeys } from 'nanostores';
|
|
4
|
-
import
|
|
4
|
+
import React from 'react';
|
|
5
5
|
import { useSyncExternalStore } from 'use-sync-external-store/shim';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const emit = (snapshotRef, onChange) => (value) => {
|
|
7
|
+
if (snapshotRef.current === value)
|
|
8
|
+
return;
|
|
9
|
+
snapshotRef.current = value;
|
|
10
|
+
onChange();
|
|
11
|
+
};
|
|
12
|
+
export function useStore(store, { keys, deps = [store, keys] } = {}) {
|
|
13
|
+
const snapshotRef = React.useRef();
|
|
14
|
+
snapshotRef.current = store.get();
|
|
15
|
+
const subscribe = React.useCallback((onChange) => {
|
|
16
|
+
emit(snapshotRef, onChange)(store.value);
|
|
17
|
+
return keys?.length > 0
|
|
18
|
+
? listenKeys(store, keys, emit(snapshotRef, onChange))
|
|
19
|
+
: store.listen(emit(snapshotRef, onChange));
|
|
20
|
+
}, deps);
|
|
21
|
+
const get = () => snapshotRef.current;
|
|
22
|
+
if (React.useSyncExternalStore) {
|
|
23
|
+
return React.useSyncExternalStore(subscribe, get, get);
|
|
24
|
+
}
|
|
9
25
|
return useSyncExternalStore(subscribe, get, get);
|
|
10
26
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@streamlayer/react-polyfills",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.48",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"typings": "./lib/index.d.ts",
|
|
@@ -21,7 +21,6 @@
|
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"@nanostores/react": "^0.7.2",
|
|
25
24
|
"nanostores": "^0.11.4",
|
|
26
25
|
"react": "18.3.1",
|
|
27
26
|
"use-sync-external-store": "^1.4.0"
|