@orbcharts/core 3.0.0-alpha.44 → 3.0.0-alpha.46

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- import { takeUntil, map, shareReplay, startWith, Subject, Observable } from 'rxjs'
1
+ import { of, takeUntil, map, switchMap, shareReplay, startWith, Subject, BehaviorSubject, Observable } from 'rxjs'
2
2
  import type { ChartType, CreateBasePlugin, PluginInitFn, PluginContext } from '../types'
3
3
  import { mergeOptionsWithDefault } from '../utils'
4
4
 
@@ -10,14 +10,23 @@ function createPlugin <T extends ChartType, PluginName, PluginParams>({ name, de
10
10
  }) {
11
11
 
12
12
  const destroy$ = new Subject()
13
- const params$: Subject<Partial<typeof defaultParams>> = new Subject()
14
- const StoreMap = new WeakMap() // 避免memory leak
13
+ const EntityWeakMap = new WeakMap() // <selection, pluginEntity> 避免只移除selection而沒回收pluginEntity的memory leak
15
14
  let pluginDestroyFn = () => {}
16
15
  let pluginContext: PluginContext<T, PluginName, PluginParams> | undefined
17
- let mergedDefaultParams: PluginParams = defaultParams
16
+ const mergedDefaultParams$ = new BehaviorSubject(defaultParams)
17
+ const params$: Subject<Partial<typeof defaultParams>> = new BehaviorSubject({})
18
+ const fullParams$ = mergedDefaultParams$.pipe(
19
+ switchMap(mergedDefaultParams => {
20
+ return params$.pipe(
21
+ takeUntil(destroy$),
22
+ map(d => mergeOptionsWithDefault(d, mergedDefaultParams)),
23
+ )
24
+ }),
25
+ shareReplay(1)
26
+ )
18
27
 
19
28
  // 建立plugin實例
20
- return {
29
+ const pluginEntity = {
21
30
  params$,
22
31
  name,
23
32
  defaultParams,
@@ -27,7 +36,7 @@ function createPlugin <T extends ChartType, PluginName, PluginParams>({ name, de
27
36
  }
28
37
  // 執行
29
38
  pluginDestroyFn = (initFn(pluginContext) ?? (() => {})) // plugin執行會回傳destroy函式
30
- StoreMap.set(pluginContext.selection, pluginContext)
39
+ EntityWeakMap.set(pluginContext.selection, pluginContext)
31
40
  },
32
41
  destroy () {
33
42
  pluginDestroyFn()
@@ -38,19 +47,16 @@ function createPlugin <T extends ChartType, PluginName, PluginParams>({ name, de
38
47
  destroy$.next(undefined)
39
48
  },
40
49
  setPresetParams: (presetParams: Partial<PluginParams>) => {
41
- mergedDefaultParams = mergeOptionsWithDefault(presetParams, defaultParams)
50
+ mergedDefaultParams$.next(mergeOptionsWithDefault(presetParams, defaultParams))
51
+
42
52
  },
43
53
  setContext: (_pluginContext: PluginContext<T, PluginName, PluginParams>) => {
44
54
  pluginContext = _pluginContext
45
- pluginContext.observer.fullParams$ = params$
46
- .pipe(
47
- takeUntil(destroy$),
48
- startWith({}),
49
- map(d => mergeOptionsWithDefault(d, mergedDefaultParams)),
50
- shareReplay(1),
51
- )
55
+ pluginContext.observer.fullParams$ = fullParams$
52
56
  }
53
57
  }
58
+
59
+ return pluginEntity
54
60
  }
55
61
 
56
62
  // 建立plugin類別