zhengyiming
2025-03-28 8bb9173a95a87e3ceab4f48b5b34041af38b7c70
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
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.listener.addListener({
    //   update: (state) => {
    //     updateState(_blLifeRecharge, state);
    //   },
    // });
 
    app.provide(clientKey, {
      blLifeRecharge: _blLifeRecharge,
    });
  },
};
 
export function useLifeRechargeContext() {
  return inject(clientKey);
}