mirror of
https://github.com/logos-messaging/waku-react.git
synced 2026-07-24 00:33:15 +00:00
remove export of WakuContext, create ContetnPair provider
This commit is contained in:
parent
8213ee2cb5
commit
157a28d3e7
47
src/ContentPairProvider.tsx
Normal file
47
src/ContentPairProvider.tsx
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import React from "react";
|
||||||
|
import type { ContentPair, ReactChildrenProps } from "./types";
|
||||||
|
import { useCreateContentPair } from "./useCreatContentPair";
|
||||||
|
|
||||||
|
type ContentPairContextType = Partial<ContentPair>;
|
||||||
|
|
||||||
|
const ContentPairContext = React.createContext<ContentPairContextType>({
|
||||||
|
decoder: undefined,
|
||||||
|
encoder: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook to retrieve Encoder/Decoder pair from Context.
|
||||||
|
* @example
|
||||||
|
* const { encoder, decoder } = useContentPair();
|
||||||
|
* @returns {Object} { encoder, decoder }
|
||||||
|
*/
|
||||||
|
export const useContentPair = (): ContentPairContextType => React.useContext(ContentPairContext);
|
||||||
|
|
||||||
|
type ContentPairProviderProps = ReactChildrenProps & {
|
||||||
|
contentTopic: string;
|
||||||
|
ephemeral?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provider for creating Encoder/Decoder pair based on contentTopic
|
||||||
|
* @example
|
||||||
|
* const App = (props) => (
|
||||||
|
* <ContentPairProvider contentTopic="/toy-chat/2/huilong/proto">
|
||||||
|
* <Component />
|
||||||
|
* </ContentPairProvider>
|
||||||
|
* );
|
||||||
|
* const Component = (props) => {
|
||||||
|
* const { encoder, decoder } = useContentPair();
|
||||||
|
* ...
|
||||||
|
* };
|
||||||
|
* @param {string} contentTopic - content topic for configuring the pair
|
||||||
|
* @param {boolean} ephemeral - flag to set messages ephemeral according to RFC https://rfc.vac.dev/spec/14/
|
||||||
|
* @returns React ContentPair Provider component
|
||||||
|
*/
|
||||||
|
export const ContentPairProvider: React.FunctionComponent<ContentPairProviderProps> = (props) => {
|
||||||
|
const result = useCreateContentPair(props.contentPair, props.ephemeral);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ContentPairContext.Provider value={result}>{props.children}</ContentPairContext.Provider>
|
||||||
|
)
|
||||||
|
};
|
||||||
@ -16,7 +16,7 @@ import {
|
|||||||
|
|
||||||
type WakuContextType<T extends Waku> = CrateNodeResult<T>;
|
type WakuContextType<T extends Waku> = CrateNodeResult<T>;
|
||||||
|
|
||||||
export const WakuContext = React.createContext<WakuContextType<Waku>>({
|
const WakuContext = React.createContext<WakuContextType<Waku>>({
|
||||||
node: undefined,
|
node: undefined,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
|
|||||||
@ -12,5 +12,8 @@ export {
|
|||||||
LightNodeProvider,
|
LightNodeProvider,
|
||||||
RelayNodeProvider,
|
RelayNodeProvider,
|
||||||
useWaku,
|
useWaku,
|
||||||
WakuContext,
|
|
||||||
} from "./WakuProvider";
|
} from "./WakuProvider";
|
||||||
|
export {
|
||||||
|
useContentPair,
|
||||||
|
ContentPairProvider,
|
||||||
|
} from "./ContentPairProvider";
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { RelayCreateOptions, WakuOptions } from "@waku/core";
|
import { RelayCreateOptions, WakuOptions } from "@waku/core";
|
||||||
|
import type { Decoder, Encoder } from "@waku/core/dist/lib/message/version_0";
|
||||||
import type { CreateOptions } from "@waku/create";
|
import type { CreateOptions } from "@waku/create";
|
||||||
import type { Protocols, Waku } from "@waku/interfaces";
|
import type { Protocols, Waku } from "@waku/interfaces";
|
||||||
import type { Decoder, Encoder } from "@waku/core/dist/lib/message/version_0";
|
|
||||||
|
|
||||||
export type HookState = {
|
export type HookState = {
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
@ -29,3 +29,7 @@ export type ContentPair = {
|
|||||||
encoder: Encoder;
|
encoder: Encoder;
|
||||||
decoder: Decoder;
|
decoder: Decoder;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type ReactChildrenProps = {
|
||||||
|
children?: React.ReactNode;
|
||||||
|
};
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { createDecoder, createEncoder } from "@waku/core";
|
import { createDecoder, createEncoder } from "@waku/core";
|
||||||
import type { Decoder, Encoder } from "@waku/core/dist/lib/message/version_0";
|
import type { Decoder, Encoder } from "@waku/core/dist/lib/message/version_0";
|
||||||
|
|
||||||
import type { ContentPair } from "./types";
|
import type { ContentPair } from "./types";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user