zhengyiming
2025-03-28 381c97332e567a1b95a9a5220275461d0ae3f74e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { BlLifeRecharge, BlLifeRechargeOptions } from '@life-payment/core';
import { inject, reactive, provide, ref } from 'vue';
import type { InjectionKey, UnwrapNestedRefs, Ref } from 'vue';
 
export type LifeRechargeContext = {
  blLifeRecharge: UnwrapNestedRefs<BlLifeRecharge>;
};
 
const clientKey: InjectionKey<LifeRechargeContext> = Symbol('blLifeRecharge');
 
export type VueLifeRechargePluginOptions = {
  blLifeRecharge: BlLifeRecharge;
};
 
function updateState(state: Record<string, unknown>, update: Record<string, any>): void {
  Object.keys(state).forEach((key) => {
    state[key] = update[key];
  });
}
 
export const VueLifeRechargePlugin = {
  install: (app: any, { blLifeRecharge }: VueLifeRechargePluginOptions) => {
    const _blLifeRecharge = reactive(blLifeRecharge);
    _blLifeRecharge.init();
 
    // blLifeRecharge.listener.addListener({
    //   update: (state) => {
    //     updateState(_blLifeRecharge, state);
    //   },
    // });
 
    app.provide(clientKey, {
      blLifeRecharge: _blLifeRecharge,
    });
  },
};
 
export function useLifeRechargeContext() {
  return inject(clientKey);
}