Skip to content

Commit

Permalink
fix: split worker box
Browse files Browse the repository at this point in the history
  • Loading branch information
seiry committed Nov 11, 2023
1 parent 788d626 commit 53c492d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
26 changes: 2 additions & 24 deletions src/ipBox.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { FC, useEffect, useState } from 'react';
import { FC } from 'react';
import { useIp } from './hook';
import { prettyPrintJson } from 'pretty-print-json';
import './box.css';
import { wrap } from 'comlink';
import { Obj } from './webWorker';

interface IpBoxProps {
export interface IpBoxProps {
ip: string;
}

Expand All @@ -22,23 +20,3 @@ export const IpBox: FC<IpBoxProps> = (props) => {
</div>
);
};

const worker = new Worker(new URL('./webWorker.ts', import.meta.url));
const obj = wrap<Obj>(worker);

export const IpBoxWebworker: FC<IpBoxProps> = (props) => {
const { ip } = props;
const [html, setHtml] = useState('');
useEffect(() => {
obj.getIPInfo(ip).then((res) => {
setHtml(prettyPrintJson.toHtml(res));
});
}, [ip]);

return (
<div className="box ">
ip address: <pre>{ip}</pre>
<pre dangerouslySetInnerHTML={{ __html: html }}></pre>
</div>
);
};
25 changes: 25 additions & 0 deletions src/workerBox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { wrap } from 'comlink';
import { Obj } from './webWorker';
import { IpBoxProps } from './ipBox';
import { prettyPrintJson } from 'pretty-print-json';
import { FC, useState, useEffect } from 'react';

const worker = new Worker(new URL('./webWorker.ts', import.meta.url));
const obj = wrap<Obj>(worker);

export const IpBoxWebworker: FC<IpBoxProps> = (props) => {
const { ip } = props;
const [html, setHtml] = useState('');
useEffect(() => {
obj.getIPInfo(ip).then((res) => {
setHtml(prettyPrintJson.toHtml(res));
});
}, [ip]);

return (
<div className="box ">
ip address: <pre>{ip}</pre>
<pre dangerouslySetInnerHTML={{ __html: html }}></pre>
</div>
);
};

0 comments on commit 53c492d

Please sign in to comment.