Skip to content

Commit a376fd3

Browse files
committed
refactor(ghoulscript): improve code spliting
1 parent 7ed5075 commit a376fd3

File tree

8 files changed

+137
-25
lines changed

8 files changed

+137
-25
lines changed

packages/ghoulscript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@privyid/ghoulscript",
33
"packageManager": "yarn@4.2.2",
4-
"version": "0.1.0-alpha.2",
4+
"version": "0.1.0-alpha.4",
55
"type": "module",
66
"main": "./dist/index.cjs",
77
"module": "./dist/index.mjs",

packages/ghoulscript/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface Config {
88
useWorker: boolean,
99
}
1010

11-
const config: Config = { useWorker: typeof window !== 'undefined' }
11+
const config: Config = { useWorker: typeof window !== 'undefined' && !!window.Worker }
1212

1313
export function useConfig () {
1414
return config

packages/ghoulscript/src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
/* eslint-disable unicorn/no-await-expression-member */
12
import { useConfig } from './config'
23
import type {
34
CommandArgs,
45
CommandResult,
56
Commands,
6-
} from './rpc'
7+
} from './rpc/call'
78
import {
8-
callRPC,
99
callWorkerRPC,
1010
} from './rpc'
11+
import { callLocalRPC } from './rpc/worker.local'
1112
import type * as core from './core'
1213

1314
export {
@@ -21,7 +22,7 @@ export {
2122
async function call <C extends Commands> (name: C, args: CommandArgs<C>): Promise<CommandResult<C>> {
2223
return useConfig().useWorker
2324
? callWorkerRPC(name, args)
24-
: callRPC(name, args)
25+
: callLocalRPC(name, args)
2526
}
2627

2728
export const optimizePDF: typeof core.optimizePDF = async (...args: CommandArgs<'optimizePDF'>): CommandResult<'optimizePDF'> => {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as core from '../core'
2+
3+
export type Core = typeof core
4+
5+
export type Commands = keyof Core
6+
7+
export type CommandArgs<C extends Commands> = Parameters<Core[C]>
8+
9+
export type CommandResult<C extends Commands> = ReturnType<Core[C]>
10+
11+
export async function callRPC<C extends Commands> (name: C, args: CommandArgs<C>): Promise<CommandResult<C>> {
12+
return await (core[name] as (...args: any[]) => Promise<any>)(...args)
13+
}

packages/ghoulscript/src/rpc.ts renamed to packages/ghoulscript/src/rpc/index.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import * as core from './core'
2-
import RpcWorker from './rpc.worker?worker&inline'
31

4-
export type Core = typeof core
5-
6-
export type Commands = keyof Core
7-
8-
export type CommandArgs<C extends Commands> = Parameters<Core[C]>
9-
10-
export type CommandResult<C extends Commands> = ReturnType<Core[C]>
2+
import type {
3+
CommandArgs,
4+
CommandResult,
5+
Commands,
6+
} from './call'
7+
import { useRef } from '../utils/use-ref'
118

129
export interface RPC <C extends Commands = any> {
1310
id: number,
@@ -21,17 +18,30 @@ export interface RPCResult<C extends Commands = any> {
2118
error?: undefined,
2219
}
2320

24-
let worker: Worker
21+
const worker = useRef<Worker>()
22+
const isLoading = useRef(false)
2523

2624
export async function useWorkerRPC () {
27-
if (!worker)
28-
worker = new RpcWorker({ name: 'rpc-worker' })
25+
if (isLoading.value)
26+
await isLoading.toBe(false)
27+
28+
if (!worker.value) {
29+
isLoading.value = true
30+
31+
try {
32+
const { default: RpcWorker } = await import('./worker?worker&inline')
2933

30-
return worker
34+
worker.value = new RpcWorker({ name: 'rpc-worker' })
35+
} finally {
36+
isLoading.value = false
37+
}
38+
}
39+
40+
return worker.value
3141
}
3242

3343
export function setWorkerRPC (worker_: Worker) {
34-
worker = worker_
44+
worker.value = worker_
3545
}
3646

3747
export async function callWorkerRPC<C extends Commands, A extends CommandArgs<C>> (name: C, args: A) {
@@ -70,7 +80,3 @@ export async function callWorkerRPC<C extends Commands, A extends CommandArgs<C>
7080
})
7181
})
7282
}
73-
74-
export async function callRPC<C extends Commands> (name: C, args: CommandArgs<C>): Promise<CommandResult<C>> {
75-
return await (core[name] as (...args: any[]) => Promise<any>)(...args)
76-
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { CommandArgs, Commands } from './call'
2+
3+
export async function callLocalRPC<C extends Commands, A extends CommandArgs<C>> (name: C, args: A) {
4+
// eslint-disable-next-line unicorn/no-await-expression-member
5+
return (await import('./call')).callRPC(name, args)
6+
}

packages/ghoulscript/src/rpc.worker.ts renamed to packages/ghoulscript/src/rpc/worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable @typescript-eslint/triple-slash-reference */
22
/* eslint-env serviceworker */
33

4-
import type { RPC } from './rpc'
5-
import { callRPC } from './rpc'
4+
import type { RPC } from '.'
5+
import { callRPC } from './call'
66

77
self.addEventListener('message', (event: MessageEvent<RPC>) => {
88
const rpc = event.data
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
type OnChange<T> = (value: T) => void
2+
3+
type OffChange = () => void
4+
5+
interface Ref <T> {
6+
value: T,
7+
change: (fn: OnChange<T>) => OffChange,
8+
toBe: (expect: T) => Promise<void>,
9+
}
10+
11+
/**
12+
* Reactive variable
13+
*
14+
* @param initialValue initial value
15+
* @example
16+
*
17+
* const isBusy = useRef(false)
18+
*
19+
* function onClick () {
20+
* // Wait other tobe done
21+
* if (isBusy.value)
22+
* await isBusy.toBe(false)
23+
*
24+
* isBusy.value = true
25+
*
26+
* // Heavy async function
27+
* setTimeout(() => {
28+
* isBusy.value = false
29+
* },5000)
30+
* }
31+
*/
32+
export function useRef<T = any> (): Ref<T | undefined>
33+
export function useRef<T = any> (initialValue: T): Ref<T>
34+
export function useRef<T> (initialValue?: T): Ref<T | undefined> {
35+
let value: T | undefined = initialValue
36+
37+
const watcher = new Set<OnChange<T>>()
38+
39+
return {
40+
/**
41+
* Get value
42+
*/
43+
get value (): T | undefined {
44+
return value
45+
},
46+
47+
/**
48+
* Set value
49+
*/
50+
set value (newValue: T) {
51+
value = newValue
52+
53+
// emit on-change
54+
for (const emit of watcher)
55+
emit(newValue)
56+
},
57+
58+
/**
59+
* Add on change listener
60+
* @param fn on change handler
61+
*/
62+
change (fn: OnChange<T>): OffChange {
63+
watcher.add(fn)
64+
65+
return () => {
66+
watcher.delete(fn)
67+
}
68+
},
69+
70+
/**
71+
* Wait until
72+
* @param expect expected value
73+
*/
74+
// eslint-disable-next-line @typescript-eslint/promise-function-async
75+
toBe (expect: T | undefined) {
76+
return new Promise<void>((resolve) => {
77+
const stop = this.change((value) => {
78+
if (value === expect) {
79+
stop()
80+
resolve()
81+
}
82+
})
83+
})
84+
},
85+
}
86+
}

0 commit comments

Comments
 (0)