Skip to content

Commit

Permalink
feat: added ability to warm up cache on app startup (#1198)
Browse files Browse the repository at this point in the history
Signed-off-by: wadeking98 <[email protected]>
  • Loading branch information
wadeking98 authored Jul 18, 2024
1 parent 1406202 commit da6ebe3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/legacy/core/App/container-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export const OBJECT_TOKENS = {
OBJECT_ONBOARDING_CONFIG: 'object.onboarding-config',
} as const

export const CACHE_TOKENS = {
CACHE_CRED_DEFS: 'cache.cred-defs',
CACHE_SCHEMAS: 'cache.schemas',
} as const

export const UTILITY_TOKENS = {
UTIL_LOGGER: 'utility.logger',
UTIL_OCA_RESOLVER: 'utility.oca-resolver',
Expand All @@ -74,6 +79,7 @@ export const TOKENS = {
...COMP_TOKENS,
...LOAD_STATE_TOKENS,
...OBJECT_TOKENS,
...CACHE_TOKENS,
...UTILITY_TOKENS,
} as const

Expand All @@ -93,6 +99,8 @@ export type TokenMapping = {
[TOKENS.LOAD_STATE]: LoadStateFn
[TOKENS.COMP_BUTTON]: Button
[TOKENS.OBJECT_ONBOARDING_CONFIG]: ScreenOptionsType
[TOKENS.CACHE_CRED_DEFS]: { did: string; id: string }[]
[TOKENS.CACHE_SCHEMAS]: { did: string; id: string }[]
[TOKENS.UTIL_LOGGER]: BaseLogger
[TOKENS.UTIL_OCA_RESOLVER]: OCABundleResolverType
[TOKENS.UTIL_LEDGERS]: IndyVdrPoolConfig[]
Expand Down
2 changes: 2 additions & 0 deletions packages/legacy/core/App/container-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export class MainContainer implements Container {
this._container.registerInstance(TOKENS.UTIL_OCA_RESOLVER, new DefaultOCABundleResolver(bundle))
this._container.registerInstance(TOKENS.UTIL_LEDGERS, defaultIndyLedgers)
this._container.registerInstance(TOKENS.UTIL_PROOF_TEMPLATE, useProofRequestTemplates)
this._container.registerInstance(TOKENS.CACHE_CRED_DEFS, [])
this._container.registerInstance(TOKENS.CACHE_SCHEMAS, [])
this._container.registerInstance(
TOKENS.FN_ONBOARDING_DONE,
(dispatch: React.Dispatch<ReducerAction<unknown>>, navigation: StackNavigationProp<AuthenticateStackParams>) => {
Expand Down
24 changes: 24 additions & 0 deletions packages/legacy/core/App/screens/Splash.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Agent, HttpOutboundTransport, WsOutboundTransport } from '@credo-ts/core'
import { IndyVdrPoolService } from '@credo-ts/indy-vdr/build/pool'
import { useAgent } from '@credo-ts/react-hooks'
import { agentDependencies } from '@credo-ts/react-native'
import { RemoteOCABundleResolver } from '@hyperledger/aries-oca/build/legacy'
import { GetCredentialDefinitionRequest, GetSchemaRequest } from '@hyperledger/indy-vdr-shared'
import { useNavigation } from '@react-navigation/core'
import { CommonActions } from '@react-navigation/native'
import React, { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { DeviceEventEmitter, StyleSheet } from 'react-native'
import { Config } from 'react-native-config'
import { CachesDirectoryPath } from 'react-native-fs'
import { SafeAreaView } from 'react-native-safe-area-context'

import { EventTypes } from '../constants'
Expand Down Expand Up @@ -232,6 +235,11 @@ const Splash: React.FC = () => {
modules: getAgentModules({
indyNetworks: indyLedgers,
mediatorInvitationUrl: Config.MEDIATOR_URL,
txnCache: {
capacity: 1000,
expiryOffsetMs: 1000 * 60 * 60 * 24 * 7,
path: CachesDirectoryPath + '/txn-cache',
},
}),
})
const wsTransport = new WsOutboundTransport()
Expand All @@ -257,6 +265,22 @@ const Splash: React.FC = () => {

await createLinkSecretIfRequired(newAgent)

const credDefs = container.resolve(TOKENS.CACHE_CRED_DEFS)
const schemas = container.resolve(TOKENS.CACHE_SCHEMAS)

const poolService = newAgent.dependencyManager.resolve(IndyVdrPoolService)
credDefs.forEach(async ({ did, id }) => {
const pool = await poolService.getPoolForDid(newAgent.context, did)
const credDefRequest = new GetCredentialDefinitionRequest({ credentialDefinitionId: id })
await pool.pool.submitRequest(credDefRequest)
})

schemas.forEach(async ({ did, id }) => {
const pool = await poolService.getPoolForDid(newAgent.context, did)
const schemaRequest = new GetSchemaRequest({ schemaId: id })
await pool.pool.submitRequest(schemaRequest)
})

setAgent(newAgent)
navigation.dispatch(
CommonActions.reset({
Expand Down
2 changes: 2 additions & 0 deletions packages/legacy/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@hyperledger/aries-bifold-verifier": "^1.0.0",
"@hyperledger/aries-oca": "^1.0.0",
"@hyperledger/indy-vdr-react-native": "0.2.2",
"@hyperledger/indy-vdr-shared": "0.2.2",
"@react-native-async-storage/async-storage": "1.15.11",
"@react-native-community/eslint-config": "^3.2.0",
"@react-native-community/eslint-plugin": "^1.3.0",
Expand Down Expand Up @@ -165,6 +166,7 @@
"@hyperledger/aries-bifold-verifier": "^1.0.0",
"@hyperledger/aries-oca": "^1.0.0",
"@hyperledger/indy-vdr-react-native": "0.2.2",
"@hyperledger/indy-vdr-shared": "0.2.2",
"@react-native-async-storage/async-storage": "1.15.11",
"@react-native-community/masked-view": "0.1.11",
"@react-native-community/netinfo": "^9.3.0",
Expand Down
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4451,6 +4451,7 @@ __metadata:
"@hyperledger/aries-bifold-verifier": ^1.0.0
"@hyperledger/aries-oca": ^1.0.0
"@hyperledger/indy-vdr-react-native": 0.2.2
"@hyperledger/indy-vdr-shared": 0.2.2
"@react-native-async-storage/async-storage": 1.15.11
"@react-native-community/eslint-config": ^3.2.0
"@react-native-community/eslint-plugin": ^1.3.0
Expand Down Expand Up @@ -4554,6 +4555,7 @@ __metadata:
"@hyperledger/aries-bifold-verifier": ^1.0.0
"@hyperledger/aries-oca": ^1.0.0
"@hyperledger/indy-vdr-react-native": 0.2.2
"@hyperledger/indy-vdr-shared": 0.2.2
"@react-native-async-storage/async-storage": 1.15.11
"@react-native-community/masked-view": 0.1.11
"@react-native-community/netinfo": ^9.3.0
Expand Down

0 comments on commit da6ebe3

Please sign in to comment.