2023-02-28 00:57:59 +01:00
|
|
|
import React from "react";
|
2023-02-16 21:49:46 +01:00
|
|
|
import { createDecoder, createEncoder } from "@waku/core";
|
|
|
|
|
import type { Decoder, Encoder } from "@waku/core/dist/lib/message/version_0";
|
|
|
|
|
|
2023-02-28 00:57:59 +01:00
|
|
|
import type { ContentPair } from "./types";
|
2023-02-16 21:49:46 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates Encoder / Decoder pair for a given contentTopic.
|
|
|
|
|
* @param {string} contentTopic - topic to orient to
|
2023-02-28 00:57:59 +01:00
|
|
|
* @param {boolean} ephemeral - makes messages ephemeral, default to false
|
2023-02-16 21:49:46 +01:00
|
|
|
* @returns {Object} Encoder / Decoder pair
|
|
|
|
|
*/
|
2023-02-28 00:57:59 +01:00
|
|
|
export const useCreateContentPair = (
|
2023-02-16 21:49:46 +01:00
|
|
|
contentTopic: string,
|
2023-02-28 00:57:59 +01:00
|
|
|
ephemeral = false,
|
2023-02-16 21:49:46 +01:00
|
|
|
): ContentPair => {
|
2023-02-28 00:57:59 +01:00
|
|
|
const [encoder, setEncoder] = React.useState<Encoder>(
|
|
|
|
|
createEncoder(contentTopic, ephemeral),
|
|
|
|
|
);
|
|
|
|
|
const [decoder, setDecoder] = React.useState<Decoder>(
|
|
|
|
|
createDecoder(contentTopic),
|
|
|
|
|
);
|
2023-02-16 21:49:46 +01:00
|
|
|
|
2023-02-28 00:57:59 +01:00
|
|
|
React.useEffect(() => {
|
2023-02-16 21:49:46 +01:00
|
|
|
setEncoder(createEncoder(contentTopic, ephemeral));
|
|
|
|
|
setDecoder(createDecoder(contentTopic));
|
|
|
|
|
}, [contentTopic, ephemeral]);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
encoder,
|
|
|
|
|
decoder,
|
|
|
|
|
};
|
|
|
|
|
};
|