mirror of
https://github.com/logos-messaging/waku-react.git
synced 2026-01-07 00:23:07 +00:00
add useLightPush hook
This commit is contained in:
parent
599ac5cbe4
commit
0d34db28b7
@ -7,6 +7,7 @@ export {
|
|||||||
useCreateRelayNode,
|
useCreateRelayNode,
|
||||||
} from "./useCreateWaku";
|
} from "./useCreateWaku";
|
||||||
export { useFilterMessages } from "./useFilterMessages";
|
export { useFilterMessages } from "./useFilterMessages";
|
||||||
|
export { useLightPush } from "./useLightPush";
|
||||||
export { useStoreMessages } from "./useStoreMessages";
|
export { useStoreMessages } from "./useStoreMessages";
|
||||||
export {
|
export {
|
||||||
FullNodeProvider,
|
FullNodeProvider,
|
||||||
|
|||||||
56
src/useLightPush.ts
Normal file
56
src/useLightPush.ts
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import React from "react";
|
||||||
|
import type {
|
||||||
|
IEncoder,
|
||||||
|
ILightPush,
|
||||||
|
IMessage,
|
||||||
|
ProtocolOptions,
|
||||||
|
SendResult,
|
||||||
|
Waku,
|
||||||
|
} from "@waku/interfaces";
|
||||||
|
|
||||||
|
type AbstractLightPushNode = Waku & {
|
||||||
|
lightPush: ILightPush;
|
||||||
|
};
|
||||||
|
|
||||||
|
type UseLightPushParams = {
|
||||||
|
encoder: undefined | IEncoder;
|
||||||
|
node: undefined | AbstractLightPushNode;
|
||||||
|
};
|
||||||
|
|
||||||
|
type PushFn = (
|
||||||
|
message: IMessage,
|
||||||
|
opts?: ProtocolOptions,
|
||||||
|
) => Promise<SendResult>;
|
||||||
|
|
||||||
|
type UseLightPushResult = {
|
||||||
|
push: undefined | PushFn;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns light push methods bound to node and encoder
|
||||||
|
* @param {Object} params.node - node that implements ILightPush, hook does nothing if empty
|
||||||
|
* @param {Object} params.encoder - encoder for processing messages, hook does nothing if empty
|
||||||
|
* @returns {Object} methods of ILightPush such as push
|
||||||
|
*/
|
||||||
|
export const useLightPush = (
|
||||||
|
params: UseLightPushParams,
|
||||||
|
): UseLightPushResult => {
|
||||||
|
const { node, encoder } = params;
|
||||||
|
|
||||||
|
const push = React.useCallback<PushFn>(
|
||||||
|
(message, opts = undefined) => {
|
||||||
|
return node!.lightPush.push(encoder as IEncoder, message, opts);
|
||||||
|
},
|
||||||
|
[node, encoder],
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!node && !encoder) {
|
||||||
|
return {
|
||||||
|
push: undefined,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
push,
|
||||||
|
};
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user