applesauce-react 0.6.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 +29 -0
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.js +2 -0
- package/dist/hooks/use-observable.d.ts +3 -0
- package/dist/hooks/use-observable.js +17 -0
- package/dist/hooks/use-store-query.d.ts +3 -0
- package/dist/hooks/use-store-query.js +14 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/provider.d.ts +6 -0
- package/dist/provider.js +12 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 hzrd149
|
|
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
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# applesauce-react
|
|
2
|
+
|
|
3
|
+
React hooks for applesauce
|
|
4
|
+
|
|
5
|
+
## Example
|
|
6
|
+
|
|
7
|
+
```jsx
|
|
8
|
+
import { EventStore, QueryStore, Queries } from "applesauce-core";
|
|
9
|
+
import { QueryStoreProvider, useStoreQuery } from "applesauce-react";
|
|
10
|
+
|
|
11
|
+
const events = new EventStore();
|
|
12
|
+
const store = new QueryStore(events);
|
|
13
|
+
|
|
14
|
+
function UserName({ pubkey }) {
|
|
15
|
+
const profile = useStoreQuery(Queries.ProfileQuery, [pubkey]);
|
|
16
|
+
|
|
17
|
+
return <span>{profile.name || "loading..."}</span>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function App() {
|
|
21
|
+
return (
|
|
22
|
+
<QueryStoreProvider store={store}>
|
|
23
|
+
<h1>App</h1>
|
|
24
|
+
|
|
25
|
+
<UserName pubkey="82341f882b6eabcd2ba7f1ef90aad961cf074af15b9ef44a09f9d2a8fbfbe6a2" />
|
|
26
|
+
</QueryStoreProvider>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
```
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useState, useEffect } from "react";
|
|
2
|
+
import { isStateful } from "applesauce-core/observable";
|
|
3
|
+
/** Subscribe to the value of an observable */
|
|
4
|
+
export function useObservable(observable) {
|
|
5
|
+
const [_, forceUpdate] = useState(0);
|
|
6
|
+
const [value, update] = useState(observable && isStateful(observable) ? observable.value : undefined);
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
if (!observable)
|
|
9
|
+
return;
|
|
10
|
+
const s = observable.subscribe((v) => {
|
|
11
|
+
update(v);
|
|
12
|
+
forceUpdate(Math.random());
|
|
13
|
+
});
|
|
14
|
+
return () => s.unsubscribe();
|
|
15
|
+
}, [observable]);
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import { useObservable } from "./use-observable.js";
|
|
3
|
+
import { useQueryStore } from "../provider.js";
|
|
4
|
+
/** Runs and subscribes to a query in the query store */
|
|
5
|
+
export function useStoreQuery(queryConstructor, args) {
|
|
6
|
+
const store = useQueryStore();
|
|
7
|
+
const observable = useMemo(() => {
|
|
8
|
+
if (args)
|
|
9
|
+
return store.runQuery(queryConstructor)(...args);
|
|
10
|
+
else
|
|
11
|
+
return undefined;
|
|
12
|
+
}, [args, store]);
|
|
13
|
+
return useObservable(observable);
|
|
14
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { QueryStore } from "applesauce-core";
|
|
2
|
+
import { PropsWithChildren } from "react";
|
|
3
|
+
export declare function useQueryStore(): QueryStore;
|
|
4
|
+
export declare function QueryStoreProvider({ store, children }: PropsWithChildren<{
|
|
5
|
+
store: QueryStore;
|
|
6
|
+
}>): import("react/jsx-runtime").JSX.Element;
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
const QueryStoreContext = createContext(null);
|
|
4
|
+
export function useQueryStore() {
|
|
5
|
+
const store = useContext(QueryStoreContext);
|
|
6
|
+
if (!store)
|
|
7
|
+
throw new Error("Missing QueryStoreProvider");
|
|
8
|
+
return store;
|
|
9
|
+
}
|
|
10
|
+
export function QueryStoreProvider({ store, children }) {
|
|
11
|
+
return _jsx(QueryStoreContext.Provider, { value: store, children: children });
|
|
12
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "applesauce-react",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "React hooks for applesauce",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"nostr",
|
|
10
|
+
"react"
|
|
11
|
+
],
|
|
12
|
+
"author": "hzrd149",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts"
|
|
21
|
+
},
|
|
22
|
+
"./hooks": {
|
|
23
|
+
"import": "./dist/hooks/index.js",
|
|
24
|
+
"types": "./dist/hooks/index.d.ts"
|
|
25
|
+
},
|
|
26
|
+
"./hooks/*": {
|
|
27
|
+
"import": "./dist/hooks/*.js",
|
|
28
|
+
"types": "./dist/hooks/*.d.ts"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"applesauce-core": "^0.6.0",
|
|
33
|
+
"nostr-tools": "^2.7.2",
|
|
34
|
+
"react": "^18.3.1",
|
|
35
|
+
"zen-observable": "^0.10.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@jest/globals": "^29.7.0",
|
|
39
|
+
"@types/jest": "^29.5.13",
|
|
40
|
+
"@types/react": "^18.3.11",
|
|
41
|
+
"@types/zen-observable": "^0.8.7",
|
|
42
|
+
"jest": "^29.7.0",
|
|
43
|
+
"jest-extended": "^4.0.2"
|
|
44
|
+
},
|
|
45
|
+
"jest": {
|
|
46
|
+
"roots": [
|
|
47
|
+
"dist"
|
|
48
|
+
],
|
|
49
|
+
"setupFilesAfterEnv": [
|
|
50
|
+
"jest-extended/all"
|
|
51
|
+
]
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsc",
|
|
55
|
+
"watch:build": "tsc --watch > /dev/null",
|
|
56
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --passWithNoTests",
|
|
57
|
+
"watch:test": "(trap 'kill 0' SIGINT; pnpm run build -w > /dev/null & pnpm run test --watch)"
|
|
58
|
+
}
|
|
59
|
+
}
|