applesauce-react 0.7.0 → 0.8.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.
@@ -1,4 +1,5 @@
1
1
  export * from "./use-observable.js";
2
2
  export * from "./use-store-query.js";
3
3
  export * from "./use-render-nast.js";
4
+ export * from "./use-query-store.js";
4
5
  export * from "./use-rendered-text-content.js";
@@ -1,4 +1,5 @@
1
1
  export * from "./use-observable.js";
2
2
  export * from "./use-store-query.js";
3
3
  export * from "./use-render-nast.js";
4
+ export * from "./use-query-store.js";
4
5
  export * from "./use-rendered-text-content.js";
@@ -1,3 +1,4 @@
1
- import Observable from "zen-observable";
1
+ import { type BehaviorSubject, type Observable } from "rxjs";
2
2
  /** Subscribe to the value of an observable */
3
+ export declare function useObservable<T extends unknown>(observable?: BehaviorSubject<T>): T;
3
4
  export declare function useObservable<T extends unknown>(observable?: Observable<T>): T | undefined;
@@ -1,32 +1,7 @@
1
1
  import { useState, useEffect } from "react";
2
- import { isStateful } from "applesauce-core/observable";
3
- /** Subscribe to the value of an observable */
4
2
  export function useObservable(observable) {
5
3
  const [_count, update] = useState(0);
6
- // const init = useRef(true);
7
- // const value = useRef<T | undefined>(observable && isStateful(observable) ? observable.value : undefined);
8
- // const prev = useRef(observable);
9
- // const sub = useRef<ZenObservable.Subscription>();
10
- // // This intentionally does not use useEffect
11
- // // because we want the value to be returned on the first render
12
- // if (!sub.current || prev.current !== observable) {
13
- // prev.current = observable;
14
- // if (sub.current) sub.current.unsubscribe();
15
- // sub.current = observable?.subscribe((v) => {
16
- // value.current = v;
17
- // // only explicitly update if its not the first render
18
- // if (!init.current) update(count + 1);
19
- // });
20
- // init.current = false;
21
- // }
22
- // // unsubscribe when unmount
23
- // useEffect(() => {
24
- // return () => {
25
- // if (sub.current) sub.current.unsubscribe();
26
- // };
27
- // }, []);
28
- // return value.current;
29
- const [value, setValue] = useState(observable && isStateful(observable) ? observable.value : undefined);
4
+ const [value, setValue] = useState(observable && Reflect.has(observable, "value") ? Reflect.get(observable, "value") : undefined);
30
5
  useEffect(() => {
31
6
  const sub = observable?.subscribe((v) => {
32
7
  setValue(v);
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Gets the QueryStore from a parent {@link QueryStoreProvider} component
3
+ * If there is none it throws an error
4
+ */
5
+ export declare function useQueryStore(): import("applesauce-core").QueryStore;
@@ -0,0 +1,12 @@
1
+ import { useContext } from "react";
2
+ import { QueryStoreContext } from "../provider.js";
3
+ /**
4
+ * Gets the QueryStore from a parent {@link QueryStoreProvider} component
5
+ * If there is none it throws an error
6
+ */
7
+ export function useQueryStore() {
8
+ const store = useContext(QueryStoreContext);
9
+ if (!store)
10
+ throw new Error("Missing QueryStoreProvider");
11
+ return store;
12
+ }
@@ -1,3 +1,7 @@
1
1
  import { QueryConstructor } from "applesauce-core";
2
- /** Runs and subscribes to a query in the query store */
2
+ /**
3
+ * Runs and subscribes to a query in the query store
4
+ * @example
5
+ * const events = useStoreQuery(TimelineQuery, [{kinds: [1]}])
6
+ */
3
7
  export declare function useStoreQuery<T extends unknown, Args extends Array<any>>(queryConstructor: QueryConstructor<T, Args>, args?: Args | null): T | undefined;
@@ -1,7 +1,11 @@
1
1
  import { useMemo } from "react";
2
2
  import { useObservable } from "./use-observable.js";
3
- import { useQueryStore } from "../provider.js";
4
- /** Runs and subscribes to a query in the query store */
3
+ import { useQueryStore } from "./use-query-store.js";
4
+ /**
5
+ * Runs and subscribes to a query in the query store
6
+ * @example
7
+ * const events = useStoreQuery(TimelineQuery, [{kinds: [1]}])
8
+ */
5
9
  export function useStoreQuery(queryConstructor, args) {
6
10
  const store = useQueryStore();
7
11
  const observable = useMemo(() => {
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./provider.js";
2
- export * from "./hooks/index.js";
2
+ export * as Helpers from "./helpers/index.js";
3
+ export * as Hooks from "./hooks/index.js";
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./provider.js";
2
- export * from "./hooks/index.js";
2
+ export * as Helpers from "./helpers/index.js";
3
+ export * as Hooks from "./hooks/index.js";
@@ -1,6 +1,7 @@
1
1
  import { QueryStore } from "applesauce-core";
2
2
  import { PropsWithChildren } from "react";
3
- export declare function useQueryStore(): QueryStore;
3
+ export declare const QueryStoreContext: import("react").Context<QueryStore | null>;
4
+ /** Provides a QueryStore to the component tree */
4
5
  export declare function QueryStoreProvider({ store, children }: PropsWithChildren<{
5
6
  store: QueryStore;
6
7
  }>): import("react/jsx-runtime").JSX.Element;
package/dist/provider.js CHANGED
@@ -1,12 +1,7 @@
1
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
- }
2
+ import { createContext } from "react";
3
+ export const QueryStoreContext = createContext(null);
4
+ /** Provides a QueryStore to the component tree */
10
5
  export function QueryStoreProvider({ store, children }) {
11
6
  return _jsx(QueryStoreContext.Provider, { value: store, children: children });
12
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-react",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "React hooks for applesauce",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -33,17 +33,16 @@
33
33
  }
34
34
  },
35
35
  "dependencies": {
36
- "applesauce-content": "^0.7.0",
37
- "applesauce-core": "^0.7.0",
36
+ "applesauce-content": "^0.8.0",
37
+ "applesauce-core": "^0.8.0",
38
38
  "nostr-tools": "^2.7.2",
39
39
  "react": "^18.3.1",
40
- "zen-observable": "^0.10.0"
40
+ "rxjs": "^7.8.1"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@jest/globals": "^29.7.0",
44
44
  "@types/jest": "^29.5.13",
45
45
  "@types/react": "^18.3.11",
46
- "@types/zen-observable": "^0.8.7",
47
46
  "jest": "^29.7.0",
48
47
  "jest-extended": "^4.0.2"
49
48
  },