/* * @Author: huyang huyang@myun.info * @Date: 2022-09-21 15:45:46 * @LastEditors: zcxb chen13157379235@outlook.com * @LastEditTime: 2022-10-13 14:44:06 * @FilePath: \easy-front-core-sdk\packages\jos\src\security\TdeClientFactory.ts * @Description: * * Copyright (c) 2022 by huyang huyang@myun.info, All Rights Reserved. */ import { JosCore } from '../JosCore' import { TdeClient } from './TdeClient' export class TdeClientFactory { static KM_EPOCH = 28800 * 1000 private static CLIENT_MAP: Map = new Map() static async instance(josCore: JosCore, access_token: string): Promise { const client_info = this.CLIENT_MAP.get(access_token) if (client_info) { const [client, time] = client_info if (new Date().getTime() - time >= this.KM_EPOCH) { // 过期 this.CLIENT_MAP.delete(access_token) return this.instance(josCore, access_token) } else { return client } } else { const [client] = await this.__get_instance(josCore, access_token) return client } } private static async __get_instance(josCore: JosCore, access_token: string): Promise<[TdeClient, number]> { const client = new TdeClient(josCore, access_token) await client.init_client() if (client.isReady) { const ts = new Date().getTime() this.CLIENT_MAP.set(access_token, [client, ts]) return [client, ts] } } }