-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(idea/frontend): local node metadata storage for codes (#1593)
- Loading branch information
1 parent
2abe4bd
commit b2218d6
Showing
45 changed files
with
468 additions
and
373 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { useWasmFileHandler } from './use-wasm-file-handler'; | ||
import { useWasmFile } from './use-wasm-file'; | ||
import { useCode } from './use-code'; | ||
|
||
export { useWasmFileHandler, useWasmFile }; | ||
export { useWasmFileHandler, useWasmFile, useCode }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { HexString } from '@gear-js/api'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { getCode } from '@/api'; | ||
import { useChain } from '@/hooks'; | ||
|
||
function useCode(id: HexString) { | ||
const { isDevChain } = useChain(); | ||
|
||
return useQuery({ | ||
queryKey: ['code', id], | ||
queryFn: async () => (await getCode(id)).result, | ||
enabled: !isDevChain, | ||
}); | ||
} | ||
|
||
export { useCode }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { CodeTable } from './ui'; | ||
import { useWasmFileHandler, useWasmFile } from './hooks'; | ||
import { useWasmFileHandler, useWasmFile, useCode } from './hooks'; | ||
|
||
export { CodeTable, useWasmFileHandler, useWasmFile }; | ||
export { CodeTable, useWasmFileHandler, useWasmFile, useCode }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { HexString, ProgramMetadata } from '@gear-js/api'; | ||
import localForage from 'localforage'; | ||
|
||
import { IMeta } from '@/entities/metadata'; | ||
import { isState } from '@/features/metadata'; | ||
|
||
import { METADATA_LOCAL_FORAGE, PROGRAMS_LOCAL_FORAGE } from './consts'; | ||
import { DBProgram } from './types'; | ||
|
||
const getLocalEntity = | ||
<T>(db: typeof localForage, type: string) => | ||
async (id: string) => { | ||
const result = await db.getItem<T>(id); | ||
|
||
if (!result) throw new Error(`${type} not found`); | ||
|
||
return { result }; | ||
}; | ||
|
||
const getLocalProgram = getLocalEntity<DBProgram>(PROGRAMS_LOCAL_FORAGE, 'Program'); | ||
const getLocalMetadata = getLocalEntity<IMeta>(METADATA_LOCAL_FORAGE, 'Metadata'); | ||
|
||
const addLocalProgram = (program: DBProgram) => PROGRAMS_LOCAL_FORAGE.setItem(program.id, program); | ||
|
||
const addLocalProgramName = async (id: HexString, name: string) => { | ||
const { result } = await getLocalProgram(id); | ||
|
||
return PROGRAMS_LOCAL_FORAGE.setItem(id, { ...result, name }); | ||
}; | ||
|
||
const changeProgramStateStatus = async (metahash: HexString, metaHex: HexString) => { | ||
let program: DBProgram | undefined; | ||
|
||
// not an efficient way, probably would be better to get program by index. | ||
// however, it'd require different library like idb.js | ||
await PROGRAMS_LOCAL_FORAGE.iterate<DBProgram, unknown>((value) => { | ||
if (value.metahash !== metahash) return; | ||
|
||
program = value; | ||
}); | ||
|
||
if (!program) return; | ||
|
||
const metadata = ProgramMetadata.from(metaHex); | ||
const hasState = isState(metadata); | ||
|
||
return PROGRAMS_LOCAL_FORAGE.setItem(program.id, { ...program, hasState }); | ||
}; | ||
|
||
const addLocalMetadata = async (hash: HexString, hex: HexString) => | ||
Promise.all([METADATA_LOCAL_FORAGE.setItem(hash, { hex }), changeProgramStateStatus(hash, hex)]); | ||
|
||
export { getLocalProgram, getLocalMetadata, addLocalProgram, addLocalProgramName, addLocalMetadata }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import localForage from 'localforage'; | ||
|
||
const PROGRAMS_LOCAL_FORAGE = localForage.createInstance({ name: 'programs' }); | ||
const METADATA_LOCAL_FORAGE = localForage.createInstance({ name: 'metadata' }); | ||
|
||
export { PROGRAMS_LOCAL_FORAGE, METADATA_LOCAL_FORAGE }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { useLocalProgram } from './use-local-program'; | ||
import { useLocalPrograms } from './use-local-programs'; | ||
import { useLocalCode } from './use-local-code'; | ||
|
||
export { useLocalProgram, useLocalPrograms }; | ||
export { useLocalCode, useLocalProgram, useLocalPrograms }; |
39 changes: 39 additions & 0 deletions
39
idea/frontend/src/features/local-indexer/hooks/use-local-code.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { HexString } from '@gear-js/api'; | ||
import { useApi } from '@gear-js/react-hooks'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { useChain } from '@/hooks'; | ||
|
||
function useLocalCode(id: HexString) { | ||
const { isDevChain } = useChain(); | ||
const { api, isApiReady } = useApi(); | ||
|
||
// TODO: useMetadataHash hook or util? | ||
const getMetadataHash = async () => { | ||
if (!isApiReady) throw new Error('API is not initialized'); | ||
|
||
try { | ||
return await api.code.metaHash(id); | ||
} catch (error) { | ||
return null; | ||
} | ||
}; | ||
|
||
const getCode = async () => { | ||
if (!isApiReady) throw new Error('API is not initialized'); | ||
if (!isDevChain) throw new Error('For indexed nodes use appropriate storage request'); | ||
|
||
const name = id; | ||
const metahash = await getMetadataHash(); | ||
|
||
return { id, name, metahash }; | ||
}; | ||
|
||
return useQuery({ | ||
queryKey: ['local-code', id], | ||
queryFn: getCode, | ||
enabled: isApiReady && isDevChain, | ||
}); | ||
} | ||
|
||
export { useLocalCode }; |
68 changes: 37 additions & 31 deletions
68
idea/frontend/src/features/local-indexer/hooks/use-local-program.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
import { useLocalProgram, useLocalPrograms } from './hooks'; | ||
import { LocalProgram } from './types'; | ||
import { getLocalMetadata, addLocalProgram, addLocalProgramName, addLocalMetadata } from './api'; | ||
import { useLocalProgram, useLocalPrograms, useLocalCode } from './hooks'; | ||
import { LocalProgram, LocalCode } from './types'; | ||
|
||
export { useLocalProgram, useLocalPrograms }; | ||
export type { LocalProgram }; | ||
export { | ||
getLocalMetadata, | ||
addLocalProgram, | ||
addLocalProgramName, | ||
addLocalMetadata, | ||
useLocalProgram, | ||
useLocalPrograms, | ||
useLocalCode, | ||
}; | ||
|
||
export type { LocalProgram, LocalCode }; |
Oops, something went wrong.