applesauce-react 1.0.0 → 2.0.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/README.md CHANGED
@@ -10,7 +10,7 @@ npm install applesauce-react
10
10
 
11
11
  ## Example
12
12
 
13
- ```jsx
13
+ ```tsx
14
14
  import { EventStore, QueryStore, Queries } from "applesauce-core";
15
15
  import { QueryStoreProvider } from "applesauce-react/providers";
16
16
  import { useStoreQuery } from "applesauce-react/hooks";
@@ -4,9 +4,9 @@ export * from "./use-action-hub.js";
4
4
  export * from "./use-action.js";
5
5
  export * from "./use-active-account.js";
6
6
  export * from "./use-event-factory.js";
7
+ export * from "./use-event-model.js";
7
8
  export * from "./use-event-store.js";
9
+ export * from "./use-observable-memo.js";
8
10
  export * from "./use-observable.js";
9
- export * from "./use-query-store.js";
10
11
  export * from "./use-render-nast.js";
11
12
  export * from "./use-rendered-content.js";
12
- export * from "./use-store-query.js";
@@ -4,9 +4,9 @@ export * from "./use-action-hub.js";
4
4
  export * from "./use-action.js";
5
5
  export * from "./use-active-account.js";
6
6
  export * from "./use-event-factory.js";
7
+ export * from "./use-event-model.js";
7
8
  export * from "./use-event-store.js";
9
+ export * from "./use-observable-memo.js";
8
10
  export * from "./use-observable.js";
9
- export * from "./use-query-store.js";
10
11
  export * from "./use-render-nast.js";
11
12
  export * from "./use-rendered-content.js";
12
- export * from "./use-store-query.js";
@@ -0,0 +1,3 @@
1
+ import { ModelConstructor } from "applesauce-core";
2
+ /** Runs and subscribes to a model on the event store */
3
+ export declare function useEventModel<T extends unknown, Args extends Array<any>>(factory: ModelConstructor<T, Args>, args?: Args | null): T | undefined;
@@ -0,0 +1,16 @@
1
+ import { withImmediateValueOrDefault } from "applesauce-core";
2
+ import { useObservableEagerState } from "observable-hooks";
3
+ import { useMemo } from "react";
4
+ import { of } from "rxjs";
5
+ import { useEventStore } from "./use-event-store.js";
6
+ /** Runs and subscribes to a model on the event store */
7
+ export function useEventModel(factory, args) {
8
+ const store = useEventStore();
9
+ const observable$ = useMemo(() => {
10
+ if (args)
11
+ return store.model(factory, ...args).pipe(withImmediateValueOrDefault(undefined));
12
+ else
13
+ return of(undefined);
14
+ }, [args, store]);
15
+ return useObservableEagerState(observable$);
16
+ }
@@ -0,0 +1,7 @@
1
+ import { BehaviorSubject, Observable } from "rxjs";
2
+ /** A hook that recreates an observable when the dependencies change */
3
+ export declare function useObservableMemo<T>(factory: () => BehaviorSubject<T>, deps: any[]): T;
4
+ export declare function useObservableMemo<T>(factory: () => Observable<T> | undefined, deps: any[]): T | undefined;
5
+ /** A hook that recreates a synchronous observable when the dependencies change */
6
+ export declare function useObservableEagerMemo<T>(factory: () => Observable<T>, deps: any[]): T;
7
+ export declare function useObservableEagerMemo<T>(factory: () => Observable<T> | undefined, deps: any[]): T | undefined;
@@ -0,0 +1,9 @@
1
+ import { useObservableEagerState, useObservableState } from "observable-hooks";
2
+ import { useMemo } from "react";
3
+ import { EMPTY } from "rxjs";
4
+ export function useObservableMemo(factory, deps) {
5
+ return useObservableState(useMemo(() => factory() || EMPTY, deps));
6
+ }
7
+ export function useObservableEagerMemo(factory, deps) {
8
+ return useObservableEagerState(useMemo(() => factory() || EMPTY, deps));
9
+ }
@@ -1,4 +1 @@
1
- import { type BehaviorSubject, type Observable } from "rxjs";
2
- /** A thing wrapper around useObservableState that allows undefined */
3
- export declare function useObservable<T extends unknown>(observable?: BehaviorSubject<T>): T;
4
- export declare function useObservable<T extends unknown>(observable?: Observable<T>): T | undefined;
1
+ export * from "observable-hooks";
@@ -1,5 +1,2 @@
1
- import { useObservableState } from "observable-hooks";
2
- import { EMPTY } from "rxjs";
3
- export function useObservable(observable) {
4
- return useObservableState(observable ?? EMPTY);
5
- }
1
+ // NOTE: re-export hooks from observable-hooks to avoid confusion
2
+ export * from "observable-hooks";
@@ -1,12 +1,7 @@
1
1
  import { PropsWithChildren } from "react";
2
- import { IEventStore, QueryStore } from "applesauce-core";
3
- export declare const QueryStoreContext: import("react").Context<QueryStore | null>;
2
+ import { IEventStore } from "applesauce-core";
4
3
  export declare const EventStoreContext: import("react").Context<IEventStore | null>;
5
4
  /** Provides a EventStore to the component tree */
6
5
  export declare function EventStoreProvider({ eventStore, children }: PropsWithChildren<{
7
6
  eventStore: IEventStore;
8
7
  }>): import("react/jsx-runtime").JSX.Element;
9
- /** Provides a QueryStore and EventStore to the component tree */
10
- export declare function QueryStoreProvider({ queryStore, children }: PropsWithChildren<{
11
- queryStore: QueryStore;
12
- }>): import("react/jsx-runtime").JSX.Element;
@@ -1,12 +1,7 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { createContext } from "react";
3
- export const QueryStoreContext = createContext(null);
4
3
  export const EventStoreContext = createContext(null);
5
4
  /** Provides a EventStore to the component tree */
6
5
  export function EventStoreProvider({ eventStore, children }) {
7
6
  return _jsx(EventStoreContext.Provider, { value: eventStore, children: children });
8
7
  }
9
- /** Provides a QueryStore and EventStore to the component tree */
10
- export function QueryStoreProvider({ queryStore, children }) {
11
- return (_jsx(EventStoreProvider, { eventStore: queryStore.store, children: _jsx(QueryStoreContext.Provider, { value: queryStore, children: children }) }));
12
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-react",
3
- "version": "1.0.0",
3
+ "version": "2.0.0",
4
4
  "description": "React hooks for applesauce",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -49,12 +49,12 @@
49
49
  }
50
50
  },
51
51
  "dependencies": {
52
- "applesauce-accounts": "^1.0.0",
53
- "applesauce-actions": "^1.0.0",
54
- "applesauce-content": "^1.0.0",
55
- "applesauce-core": "^1.0.0",
56
- "applesauce-factory": "^1.0.0",
57
- "nostr-tools": "^2.10.4",
52
+ "applesauce-accounts": "^2.0.0",
53
+ "applesauce-actions": "^2.0.0",
54
+ "applesauce-content": "^2.0.0",
55
+ "applesauce-core": "^2.0.0",
56
+ "applesauce-factory": "^2.0.0",
57
+ "nostr-tools": "^2.13",
58
58
  "observable-hooks": "^4.2.4",
59
59
  "react": "^18.3.1",
60
60
  "rxjs": "^7.8.1"
@@ -62,7 +62,7 @@
62
62
  "devDependencies": {
63
63
  "@types/react": "^18.3.18",
64
64
  "typescript": "^5.8.3",
65
- "vitest": "^3.1.1"
65
+ "vitest": "^3.2.3"
66
66
  },
67
67
  "funding": {
68
68
  "type": "lightning",
@@ -1,5 +0,0 @@
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;
@@ -1,12 +0,0 @@
1
- import { useContext } from "react";
2
- import { QueryStoreContext } from "../providers/store-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,7 +0,0 @@
1
- import { QueryConstructor } from "applesauce-core";
2
- /**
3
- * Runs and subscribes to a query in the query store
4
- * @example
5
- * const events = useStoreQuery(TimelineQuery, [{kinds: [1]}])
6
- */
7
- export declare function useStoreQuery<T extends unknown, Args extends Array<any>>(queryConstructor: QueryConstructor<T, Args>, args?: Args | null): T | undefined;
@@ -1,19 +0,0 @@
1
- import { useMemo } from "react";
2
- import { of } from "rxjs";
3
- import { useObservableEagerState } from "observable-hooks";
4
- import { useQueryStore } from "./use-query-store.js";
5
- /**
6
- * Runs and subscribes to a query in the query store
7
- * @example
8
- * const events = useStoreQuery(TimelineQuery, [{kinds: [1]}])
9
- */
10
- export function useStoreQuery(queryConstructor, args) {
11
- const store = useQueryStore();
12
- const observable = useMemo(() => {
13
- if (args)
14
- return store.createQuery(queryConstructor, ...args);
15
- else
16
- return of(undefined);
17
- }, [args, store]);
18
- return useObservableEagerState(observable);
19
- }