Files
boris/node_modules/observable-hooks/src/use-observable-ref.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

49 lines
1.6 KiB
TypeScript

import type { MutableRefObject, RefObject } from "react";
import { BehaviorSubject } from "rxjs";
import { useState } from "react";
/**
* Returns a mutable ref object and a BehaviorSubject.
*
* Whenever ref.current is changed, the BehaviorSubject will emit the new value.
*
* @param initialValue The initial value of the BehaviorSubject.
*/
export function useObservableRef<TValue>(
initialValue: TValue
): [MutableRefObject<TValue>, BehaviorSubject<TValue>];
/**
* Returns a ref object and a BehaviorSubject.
*
* Whenever ref.current is changed, the BehaviorSubject will emit the new value.
*
* @param initialValue The initial value of the BehaviorSubject.
*/
export function useObservableRef<TValue>(
initialValue: TValue | null
): [RefObject<TValue>, BehaviorSubject<TValue>];
/**
* Returns a mutable ref object and a BehaviorSubject.
*
* Whenever ref.current is changed, the BehaviorSubject will emit the new value.
*
* @param initialValue A optional initial value of the BehaviorSubject.
*/
export function useObservableRef<TValue = undefined>(
initialValue?: TValue
): [MutableRefObject<TValue | undefined>, BehaviorSubject<TValue | undefined>];
export function useObservableRef<TValue>(
initialValue?: TValue
): [MutableRefObject<TValue | undefined>, BehaviorSubject<TValue | undefined>] {
const [value$] = useState(() => new BehaviorSubject(initialValue));
const [ref] = useState<MutableRefObject<TValue | undefined>>(() => ({
get current(): TValue | undefined {
return value$.value;
},
set current(value: TValue | undefined) {
value$.next(value);
},
}));
return [ref, value$];
}