Skip to content

Commit

Permalink
feat: getWoker
Browse files Browse the repository at this point in the history
  • Loading branch information
seiry committed Nov 11, 2023
1 parent e24b77c commit 63c1d6b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/getWoker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { wrap } from 'comlink';
import { Obj } from './webWorker';

const worker = new Worker(new URL('./webWorker.ts', import.meta.url));
export const workerObj = wrap<Obj>(worker);
2 changes: 1 addition & 1 deletion src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useState } from 'react';
import * as mmdb from 'mmdb.js';
import { fetchFileAsBuffer } from './utils';

const _mmdbUrl =
export const _mmdbUrl =
'https://raw.githubusercontent.com/Max-Sum/17mon-mmdb/release/Country.mmdb';

export const useIp = (mmdbUrl?: string) => {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './hook';
export * from './webWorker';
export * from './getWoker';
22 changes: 7 additions & 15 deletions src/ipBox.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useEffect } from 'react';
import { FC, useEffect, useState } from 'react';
import { useIp } from './hook';
import { prettyPrintJson } from 'pretty-print-json';
import './box.css';
Expand Down Expand Up @@ -28,23 +28,15 @@ const obj = wrap<Obj>(worker);

export const IpBoxWebworker: FC<IpBoxProps> = (props) => {
const { ip } = props;
const reader = useIp();
const data = reader?.get(ip);
const html = prettyPrintJson.toHtml(data);

const [html, setHtml] = useState('');
useEffect(() => {
const func = async () => {};
func();
}, []);
obj.getIPInfo(ip).then((res) => {
setHtml(prettyPrintJson.toHtml(res));
});
}, [ip]);

return (
<div
className="box "
onClick={async () => {
obj.inc();
alert(`Counter: ${await obj.counter}`);
}}
>
<div className="box ">
ip address: <pre>{ip}</pre>
<pre dangerouslySetInnerHTML={{ __html: html }}></pre>
</div>
Expand Down
21 changes: 18 additions & 3 deletions src/webWorker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import { expose } from 'comlink';
import { fetchFileAsBuffer } from './utils';
import * as mmdb from 'mmdb.js';
import { _mmdbUrl } from './hook';

let cache: mmdb.Reader<mmdb.CityResponse> | null = null;

const getReader = async (url: string) => {
if (cache) return cache;
const res = await fetchFileAsBuffer(url);
if (!res) return;
const reader = new mmdb.Reader<mmdb.CityResponse>(res);
cache = reader;
return reader;
};

const obj = {
counter: 12,
inc() {
this.counter++;
getIPInfo: async (ip: string, url: string = _mmdbUrl) => {
const reader = await getReader(url);
return reader?.get(ip);
},
};

expose(obj);

export type Obj = typeof obj;
export type Reader = typeof cache;

0 comments on commit 63c1d6b

Please sign in to comment.