Files
boris/node_modules/observable-hooks/src/use-observable.ts
Gigi 5d53a827e0 feat: initialize markr nostr bookmark client
- Add project structure with TypeScript, React, and Vite
- Implement nostr authentication using browser extension (NIP-07)
- Add NIP-51 compliant bookmark fetching and display
- Create minimal UI with login and bookmark components
- Integrate applesauce-core and applesauce-react libraries
- Add responsive styling with dark/light mode support
- Include comprehensive README with setup instructions

This is a minimal MVP for a nostr bookmark client that allows users to
view their bookmarks according to NIP-51 specification.
2025-10-02 07:17:07 +02:00

58 lines
2.2 KiB
TypeScript

import { Observable } from "rxjs";
import { useObservableInternal } from "./internal/use-observable-internal";
import { useEffect } from "react";
/**
* Accepts a function that returns an Observable.
* Optionally accepts an array of dependencies which
* will be turned into Observable and be passed to the
* `init` function.
*
* React functional components are called many times during their lifecycle.
* Create or transform Observables in `init` function so that the operations
* won't be repeatedly performed.
*
* ⚠ **Note:** `useObservable` will call `init` once and always return
* the same Observable. It is not safe to access closure (except other Observables)
* directly inside `init`.
* You should use ref or pass them as dependencies through the second argument.
*
* ⚠ **Note:** Due to rules of hooks you can either offer or omit the
* dependencies array but do not change to one another during Component's life cycle.
* The length of the dependencies array must also be fixed.
*
* @template TOutput Output value in Observable
*
* @param init A pure function that, when applied to an Observable,
* returns an Observable.
*/
export function useObservable<
TOutput,
TObservable extends Observable<TOutput> = Observable<TOutput>
>(init: () => TObservable): TObservable;
/**
* @template TOutput Output value within Observable.
* @template TInputs A readonly tuple of all dependencies.
*
* @param init A pure function that, when applied to an Observable,
* returns an Observable.
* @param inputs An dependency array with fixed length. When one of the dependencies
* changes the Observable in `init` will emit an array of all the dependencies.
*/
export function useObservable<
TOutput,
TInputs extends Readonly<any[]>,
TObservable extends Observable<TOutput> = Observable<TOutput>
>(
init: (inputs$: Observable<[...TInputs]>) => TObservable,
inputs: [...TInputs]
): TObservable;
export function useObservable<TOutput, TInputs extends Readonly<any[]>>(
init:
| (() => Observable<TOutput>)
| ((inputs$: Observable<[...TInputs]>) => Observable<TOutput>),
inputs?: [...TInputs]
): Observable<TOutput> {
return useObservableInternal(useEffect, init, inputs);
}