waku-react/src/useLightPush.ts
Sasha e4d0106499
feat!: add new hooks and improvements (#3)
* use React global var

* use undefined instead on null

* use react as peer dependency

* make ephemeral default to false

* set default ephemeral to false, use node instead of waku naming

* implement useStoreMessages

* fix types

* export useStoreMessages

* make content pair set initially

* remove deps for useEffect for createWaku to prevent re rendering

* prevent setting of empty messages

* accept undefined node, handle empty message case

* accept undefined node, handle empty message case

* add TODOs

* rename to useCreateContentPair

* remove export of WakuContext, create ContetnPair provider

* remove export of WakuContext, create ContetnPair provider

* fix lint

* fix typo

* remove export

* fix prettier

* make decoded optional

* add jsdocs

* add useLightPush hook

* update types, add usePeers, add prettierignore

* remove full node hook, provider

* remove export

* remove FullNode stuff
2023-02-28 00:57:59 +01:00

55 lines
1.1 KiB
TypeScript

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 {};
}
return {
push,
};
};