File tree Expand file tree Collapse file tree 3 files changed +40
-3
lines changed Expand file tree Collapse file tree 3 files changed +40
-3
lines changed Original file line number Diff line number Diff line change @@ -40,7 +40,7 @@ Please refer to [CONTRIBUTING.md](CONTRIBUTING.md) and [HACKING.md](HACKING.md)
4040
4141### Sponsoring
4242
43- <a href =" https://gno.land/?from= goplay-tools " target =" _blank " >
43+ <a href =" https://gno.land/?utm_source=sponsor&utm_medium= goplay&utm_campaign=DevAcquisition+&utm_content=Devtool " target =" _blank " >
4444 <picture>
4545 <source media="(prefers-color-scheme: dark)" srcset="./docs/img/sponsors/gnoland-dark.svg" width="480">
4646 <img alt="Gno.land logo" width="480" src="./docs/img/sponsors/gnoland-light.svg">
Original file line number Diff line number Diff line change 11import React from 'react'
2+ import { addDays } from 'date-fns'
23import { CommandBar , type ICommandBarItemProps , Stack } from '@fluentui/react'
34
45import type { Snippet } from '~/services/examples'
@@ -11,6 +12,7 @@ import { ExamplesModal } from '~/components/features/examples/ExamplesModal'
1112import { RunTargetSelector } from '~/components/elements/inputs/RunTargetSelector'
1213import { SharePopup } from '~/components/utils/SharePopup'
1314
15+ import { keyValue } from '~/services/storage'
1416import { dispatchTerminalSettingsChange } from '~/store/terminal'
1517import {
1618 dispatchFormatFile ,
@@ -35,6 +37,12 @@ import './Header.css'
3537 */
3638const BTN_SHARE_CLASSNAME = 'Header__btn--share'
3739
40+ const goVersionsCacheEntry = {
41+ key : 'api.go.versions' ,
42+ ttl : ( ) => addDays ( new Date ( ) , 7 ) ,
43+ getInitialValue : async ( ) => await apiClient . getBackendVersions ( ) ,
44+ }
45+
3846interface HeaderState {
3947 showSettings ?: boolean
4048 showAbout ?: boolean
@@ -67,8 +75,8 @@ class HeaderContainer extends ThemeableComponent<Props, HeaderState> {
6775 }
6876
6977 componentDidMount ( ) : void {
70- apiClient
71- . getBackendVersions ( )
78+ keyValue
79+ . getOrInsert ( goVersionsCacheEntry )
7280 . then ( ( rsp ) => {
7381 this . setState ( {
7482 goVersions : rsp ,
Original file line number Diff line number Diff line change @@ -4,6 +4,13 @@ import type { CacheEntry, CacheStorage } from './types'
44
55type RecordValidator < T > = ( entry : CacheEntry < T > ) => boolean
66
7+ export interface CachedValueDescriptor < T > {
8+ key : string
9+ ttl : ( ) => Date
10+ validate ?: ( entry : CacheEntry < T > ) => boolean
11+ getInitialValue : ( ) => Promise < T >
12+ }
13+
714export class KeyValueStore implements CacheStorage {
815 constructor ( private readonly db : DatabaseStorage ) { }
916
@@ -22,6 +29,28 @@ export class KeyValueStore implements CacheStorage {
2229 return entry ?. value as T | undefined
2330 }
2431
32+ async getOrInsert < T > ( { ttl, key, validate, getInitialValue } : CachedValueDescriptor < any > ) {
33+ let cachedVal : T | undefined
34+ try {
35+ cachedVal = await this . getItem < T > ( key , validate )
36+ } catch ( err ) {
37+ console . error ( `Failed to get cached record "${ key } ": ${ err } , falling back to default value.` )
38+ }
39+
40+ if ( typeof cachedVal !== 'undefined' ) {
41+ return cachedVal
42+ }
43+
44+ const initVal = await getInitialValue ( )
45+ try {
46+ await this . setItem ( key , initVal , ttl ( ) )
47+ } catch ( err ) {
48+ console . error ( `Failed to save cached record "${ key } ": ${ err } ` )
49+ }
50+
51+ return initVal
52+ }
53+
2554 async deleteItem ( key : string ) {
2655 const n = await this . db . keyValue . where ( { key } ) . delete ( )
2756 return n > 0
You can’t perform that action at this time.
0 commit comments