Implement Client And Server

μžμ‹ μœˆλ„μš°(μ›Ήλ·°)μ—μ„œ ν΄λΌμ΄μ–ΈνŠΈμ™€ μ„œλ²„ κ΅¬ν˜„ν•˜κΈ°

μžμ‹ μœˆλ„μš°μ—μ„œλŠ” @pbkit/wrp-jotai/parentκ°€ μ œκ³΅ν•˜λŠ” useChannel, useClientImpl만 μ‚¬μš©ν•˜λ©΄ WRP μžμ›μ„ 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.

ν΄λΌμ΄μ–ΈνŠΈ κ΅¬ν˜„

pbkit을 톡해 μƒμ„±ν•œ μ„œλΉ„μŠ€ μ½”λ“œ createServiceClient에 clientImpl을 μ œκ³΅ν•˜λ©΄ λ©λ‹ˆλ‹€.

import { useClientImpl } from "@pbkit/wrp-jotai/parent";
import { createServiceClient } from "../generated/services/..."

const Client = () => {
  const clientImpl = useClientImpl();
  const serviceClient = useMemo(() => {
    if (!clientImpl) return;
    return createServiceClient(clientImpl);
  }, [clientImpl]);
  const onClick = async () => {
    await serviceClient?.getTextValue({}))?.text;
  };

μ„œλ²„ κ΅¬ν˜„

pbkit을 톡해 μƒμ„±ν•œ μ„œλΉ„μŠ€ μ½”λ“œ methodDescriptors와 channel을 useWrpServer에 μ œκ³΅ν•˜λ©΄ λ©λ‹ˆλ‹€.

import { useWrpServer } from "@pbkit/wrp-react";
import { useChannel } from "@pbkit/wrp-jotai/parent";
import { methodDescriptors } from "../generated/services/...";

const Server = () => {
  const channel = useChannel();
  useWrpServer(channel, {}, [
    [
      methodDescriptors.getTextValue,
      ({ res }) => {
        res.header({});
        res.send({ text: "hello" });
        res.end({});
      },
    ],
  ]);

λΆ€λͺ¨ μœˆλ„μš°(μ•±)μ—μ„œ ν΄λΌμ΄μ–ΈνŠΈμ™€ μ„œλ²„ κ΅¬ν˜„ν•˜κΈ°

λΆ€λͺ¨ μœˆλ„μš°μ—μ„œλŠ” @pbkit/wrp-jotai/pwasa와 @pbkit/wrp-jotai/iframe을 μ΄μš©ν•΄ WRP μžμ›λ“€μ„ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€. 각각 createPrimitiveWrpAtomSetAtomκ³Ό useIframeWrpAtomSetUpdateEffectλ₯Ό λΆˆλŸ¬μ™€μ„œ μ•„λž˜μ™€ 같이 λ§Œλ“€μ–΄μ£Όμ„Έμš”.

import { useIframeWrpAtomSetUpdateEffect } from "@pbkit/wrp-jotai/iframe";
import { createPrimitiveWrpAtomSetAtom } from "@pbkit/wrp-jotai/pwasa";

const pwasa = createPrimitiveWrpAtomSetAtom();

const Component = () => {
  const { iframeRef } = useIframeWrpAtomSetUpdateEffect(pwasa);
  return <iframe ref={iframeRef} />;
};

μœ„μ™€ 같이 pwasaλ₯Ό μ—…λ°μ΄νŠΈν•œ λ’€, @pbkit/wrp-jotai/pwasaμ—μ„œ useChannel, useClientImpl을 μ΄μš©ν•΄ channelκ³Ό clientImpl을 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.

ν΄λΌμ΄μ–ΈνŠΈ κ΅¬ν˜„

pbkit을 톡해 μƒμ„±ν•œ μ„œλΉ„μŠ€ μ½”λ“œ createServiceClient에 clientImpl을 μ œκ³΅ν•˜λ©΄ λ©λ‹ˆλ‹€.

import { useIframeWrpAtomSetUpdateEffect, useClientImpl } from "@pbkit/wrp-jotai/iframe";
import { createPrimitiveWrpAtomSetAtom } from "@pbkit/wrp-jotai/pwasa";

const pwasa = createPrimitiveWrpAtomSetAtom();

const Client = () => {
  const { iframeRef } = useIframeWrpAtomSetUpdateEffect(pwasa);
  const clientImpl = useClientImpl(pwasa);
  const serviceClient = useMemo(() => {
    if (!clientImpl) return;
    return createServiceClient(clientImpl);
  }, [clientImpl]);
  const onClick = async () => {
    await serviceClient?.getTextValue({}))?.text;
  };
  return <iframe ref={iframeRef} />;

μ„œλ²„ κ΅¬ν˜„

pbkit을 톡해 μƒμ„±ν•œ μ„œλΉ„μŠ€ μ½”λ“œ methodDescriptors와 channel을 useWrpServer에 μ œκ³΅ν•˜λ©΄ λ©λ‹ˆλ‹€.

import { useIframeWrpAtomSetUpdateEffect, useChannel } from "@pbkit/wrp-jotai/iframe";
import { createPrimitiveWrpAtomSetAtom } from "@pbkit/wrp-jotai/pwasa";
import { methodDescriptors } from "../generated/services/...";

const pwasa = createPrimitiveWrpAtomSetAtom();

const Server = () => {
  const { iframeRef } = useIframeWrpAtomSetUpdateEffect(pwasa);
  const channel = useChannel(pwasa);
  useWrpServer(channel, {}, [
    [
      methodDescriptors.getTextValue,
      ({ res }) => {
        res.header({});
        res.send({ text: "hello" });
        res.end({});
      },
    ],
  ]);
  return <iframe ref={iframeRef} />;