Skip to content

Commit 7e079b5

Browse files
committed
feat: add destroy method to OhbugClient
1 parent 05b6e8d commit 7e079b5

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

packages/ohbug-browser/src/client.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import type { OhbugClient, OhbugConfig, OhbugSDK } from '@ohbug/types'
22
import { Client } from '@ohbug/core'
3-
43
import { getGlobal } from '@ohbug/utils'
54

65
import { device } from './device'
76
import { version } from './version'
87
import { extension } from './extension'
98
import { notifier } from './notifier'
9+
import { destroy } from './destroy'
1010

1111
interface OhbugBrowserClient {
1212
__client: OhbugClient | null
1313
setup: (config: OhbugConfig) => OhbugClient
1414
}
1515

16-
function createClient(config: OhbugConfig) {
16+
function createClient(config: OhbugConfig, handleDestroy: () => void) {
1717
const global = getGlobal<Window>()
1818

1919
const sdk: OhbugSDK = {
2020
platform: 'ohbug-browser',
2121
version,
2222
}
23-
const client = new Client({ sdk, config, device, notifier })
23+
const client = new Client({ sdk, config, device, notifier, destroy: handleDestroy })
24+
2425
global.__OHBUG__ = { client }
2526
client.use(extension)
2627
client.__logger.info(
@@ -35,12 +36,21 @@ function createClient(config: OhbugConfig) {
3536
export const BrowserClient: OhbugBrowserClient = {
3637
__client: null,
3738
setup(config: OhbugConfig) {
39+
function destroyClient() {
40+
const global = getGlobal<Window>()
41+
42+
destroy()
43+
BrowserClient.__client = null
44+
// @ts-expect-error noop
45+
global.__OHBUG__ = undefined
46+
}
47+
3848
if (BrowserClient.__client) {
39-
BrowserClient.__client.__logger?.warn('init() has been called. Ignored.')
49+
BrowserClient.__client.__logger?.warn('setup() has been called. Ignored.')
4050
return BrowserClient.__client
4151
}
4252

43-
BrowserClient.__client = createClient(config)
53+
BrowserClient.__client = createClient(config, destroyClient)
4454
return BrowserClient.__client
4555
},
4656
}

packages/ohbug-browser/src/destroy.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ import {
99

1010
const global = getGlobal<Window>()
1111

12+
export function destroy() {
13+
removeReplaceAddEventListener()
14+
removeCaptureScript()
15+
removeCaptureNetwork()
16+
removeCaptureAction()
17+
removeCaptureConsole()
18+
}
19+
1220
export function handleDestroy() {
1321
global?.addEventListener?.(
1422
'unload',
1523
() => {
16-
removeReplaceAddEventListener()
17-
removeCaptureScript()
18-
removeCaptureNetwork()
19-
removeCaptureAction()
20-
removeCaptureConsole()
24+
destroy()
2125
},
2226
true,
2327
)

packages/ohbug-core/src/client.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
OhbugClientConstructorValues,
66
OhbugConfig,
77
OhbugCreateEvent,
8+
OhbugDestroy,
89
OhbugEventWithMethods,
910
OhbugExtension,
1011
OhbugGetDevice,
@@ -36,6 +37,8 @@ implements OhbugClient {
3637

3738
readonly __notifier: OhbugNotifier
3839

40+
readonly __destroy?: OhbugDestroy
41+
3942
readonly __extensions: OhbugExtension[]
4043

4144
readonly __actions: OhbugAction[]
@@ -50,6 +53,7 @@ implements OhbugClient {
5053
schema = baseSchema,
5154
device,
5255
notifier,
56+
destroy,
5357
}: OhbugClientConstructorValues) {
5458
const { config, errors } = verifyConfig(baseConfig, schema)
5559

@@ -59,6 +63,7 @@ implements OhbugClient {
5963
this.__logger = config.logger
6064
this.__device = device
6165
this.__notifier = notifier
66+
this.__destroy = destroy
6267

6368
this.__extensions = []
6469

@@ -88,6 +93,18 @@ implements OhbugClient {
8893
return this
8994
}
9095

96+
destroy(): void {
97+
if (this.__destroy) {
98+
this.__logger.info(
99+
'%c @ohbug/core %c has been destroyed %c',
100+
'background:#333; padding: 2px 1px; color: #FFF',
101+
'background:#FF6F61; padding: 2px 1px; color: #FFF',
102+
'background:transparent',
103+
)
104+
return this.__destroy?.()
105+
}
106+
}
107+
91108
/**
92109
* Create an event, you will get a data body containing device actions and other information
93110
* 创建事件,将会得到一个含有 device actions 等信息的数据体

packages/ohbug-types/src/client.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import type { OhbugAction } from './action'
77
import type { OhbugUser } from './user'
88
import type { OhbugMetadata } from './metadata'
99

10+
export type OhbugDestroy = () => void
11+
1012
export interface OhbugClientConstructorValues {
1113
sdk: OhbugSDK
1214
config: OhbugConfig
1315
schema?: OhbugSchema
1416
device: OhbugGetDevice
1517
notifier: OhbugNotifier
18+
destroy?: OhbugDestroy
1619
}
1720
export interface OhbugClientConstructor {
1821
new (values: OhbugClientConstructorValues): OhbugClient
@@ -24,6 +27,7 @@ export interface OhbugClient {
2427
readonly __logger: OhbugLoggerConfig
2528
readonly __device: OhbugGetDevice
2629
readonly __notifier: OhbugNotifier
30+
readonly __destroy?: OhbugDestroy
2731

2832
readonly __extensions: OhbugExtension[]
2933

@@ -32,6 +36,7 @@ export interface OhbugClient {
3236
readonly __metadata: OhbugMetadata
3337

3438
use: (extension: OhbugExtension) => OhbugClient
39+
destroy: () => void
3540
createEvent: <D = any>(
3641
value: OhbugCreateEvent<D>
3742
) => OhbugEventWithMethods<D> | null

playground/src/App.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ function Trigger() {
4747
const onClickRenderError = useCallback(() => {
4848
setRenderError(true)
4949
}, [])
50+
const onDestroy = useCallback(() => {
51+
client.destroy()
52+
}, [])
5053

5154
if (renderError) {
5255
throw new Error('is Render error')
@@ -62,6 +65,8 @@ function Trigger() {
6265
<button onClick={onClickTriggerCodeError}>代码错误</button>
6366
<button onClick={onClickCustomLog}>自定义log</button>
6467
<button onClick={onClickRenderError}>render错误</button>
68+
69+
<button onClick={onDestroy}>destroy</button>
6570
</div>
6671
)
6772
}

0 commit comments

Comments
 (0)