3131<script >
3232import { getAPI } from ' @/api'
3333
34+ const CACHE_TTL_MS = 30_000
35+ const osTypeCache = new Map () // osId -> { ts, value?, promise? }
36+
3437export default {
3538 name: ' OsLogo' ,
3639 props: {
@@ -45,55 +48,57 @@ export default {
4548 size: {
4649 type: String ,
4750 default: ' lg'
51+ },
52+ useCache: {
53+ type: Boolean ,
54+ default: false
4855 }
4956 },
5057 data () {
51- return {
52- name: ' ' ,
53- osLogo: [' fas' , ' image' ]
54- }
55- },
56- computed: {
57- logo : function () {
58- if (! this .name ) {
59- this .fetchData ()
60- }
61- return this .osLogo
62- }
58+ return { name: ' ' , osLogo: [' fas' , ' image' ] }
6359 },
60+ computed: { logo () { return this .osLogo } },
61+ mounted () { this .fetchData () },
6462 watch: {
65- osId : function () {
66- this .fetchData ()
67- }
63+ osId () { this .fetchData () },
64+ osName () { this .fetchData () }
6865 },
6966 methods: {
67+ async fetchOsTypeName (osId , useCache = this .useCache ) {
68+ const now = Date .now ()
69+ if (useCache) {
70+ const cached = osTypeCache .get (osId)
71+ if (cached? .value && (now - cached .ts ) < CACHE_TTL_MS ) return cached .value
72+ if (cached? .promise ) return cached .promise
73+ const promise = getAPI (' listOsTypes' , { id: osId })
74+ .then (json => {
75+ const t = json? .listostypesresponse ? .ostype
76+ const name = t? .length
77+ ? (t[0 ].description || t[0 ].osdisplayname || ' Linux' )
78+ : ' Linux'
79+ osTypeCache .set (osId, { ts: Date .now (), value: name })
80+ return name
81+ })
82+ .catch (e => { osTypeCache .delete (osId); throw e })
83+ osTypeCache .set (osId, { ts: now, promise })
84+ return promise
85+ }
86+ const json = await getAPI (' listOsTypes' , { id: osId })
87+ const t = json? .listostypesresponse ? .ostype
88+ return t? .length ? (t[0 ].description || t[0 ].osdisplayname || ' Linux' ) : ' Linux'
89+ },
7090 fetchData () {
7191 if (this .osName ) {
7292 this .discoverOsLogo (this .osName )
73- } else if (this .osId ) {
74- this .findOsName (this .osId )
93+ } else if (this .osId && (' listOsTypes' in this .$store .getters .apis )) {
94+ this .fetchOsTypeName (this .osId )
95+ .then (this .discoverOsLogo )
96+ .catch (() => this .discoverOsLogo (' Linux' ))
7597 }
7698 },
77- findOsName (osId ) {
78- if (! (' listOsTypes' in this .$store .getters .apis )) {
79- return
80- }
81- this .name = ' linux'
82- getAPI (' listOsTypes' , { id: osId }).then (json => {
83- if (json && json .listostypesresponse && json .listostypesresponse .ostype && json .listostypesresponse .ostype .length > 0 ) {
84- this .discoverOsLogo (json .listostypesresponse .ostype [0 ].description )
85- } else {
86- this .discoverOsLogo (' Linux' )
87- }
88- })
89- },
90- getFontAwesomeIcon (name ) {
91- return [' fab' , name]
92- },
9399 discoverOsLogo (name ) {
94100 this .name = name
95- this .$emit (' update-osname' , this .name )
96- const osname = name .toLowerCase ()
101+ const osname = (name || ' ' ).toLowerCase ()
97102 const logos = [
98103 { name: ' centos' },
99104 { name: ' debian' },
@@ -119,6 +124,3 @@ export default {
119124 }
120125}
121126< / script>
122-
123- <style scoped>
124- </style >
0 commit comments