Skip to content

Commit

Permalink
feat: add a provider and typed hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
xlecunff-pass committed Jan 5, 2025
1 parent 65a0f2c commit 8ed2439
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import { LocationWrapper } from 'libs/location'
import { eventMonitoring } from 'libs/monitoring'
import { NetInfoWrapper } from 'libs/network/NetInfoWrapper'
import { OfflineModeContainer } from 'libs/network/OfflineModeContainer'
import { createCore } from 'libs/poc-valtio/core'
import { CoreProvider } from 'libs/poc-valtio/CoreProvider'
import { BatchMessaging, BatchPush } from 'libs/react-native-batch'
import { configureGoogleSignin } from 'libs/react-native-google-sso/configureGoogleSignin'
import { SafeAreaProvider } from 'libs/react-native-save-area-provider'
Expand Down Expand Up @@ -105,7 +107,9 @@ const App: FunctionComponent = function () {
<OnboardingWrapper>
<OfflineModeContainer>
<ScreenErrorProvider>
<AppNavigationContainer />
<CoreProvider coreInstance={createCore()}>
<AppNavigationContainer />
</CoreProvider>
</ScreenErrorProvider>
</OfflineModeContainer>
</OnboardingWrapper>
Expand Down
60 changes: 60 additions & 0 deletions src/libs/poc-valtio/CoreProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React, { Context, ReactNode, createContext, useContext, useEffect } from 'react'
import { useSnapshot } from 'valtio'

import { createCore } from 'libs/poc-valtio/core'

const AppStateContext = createContext<Record<string, unknown> | null>(null)

interface ProviderProps {
children: ReactNode
coreInstance: Record<string, unknown>
}

export const CoreProvider: React.FC<ProviderProps> = ({ children, coreInstance }) => (
<AppStateContext.Provider value={coreInstance}>{children}</AppStateContext.Provider>
)

export function useAppContext<T>(): T {
const context = useContext<T | null>(AppStateContext as Context<T | null>)
if (!context) {
throw new Error('useAppContext must be used within a CortexProvider')
}
return context
}

function generateTypedHooks<
T extends Record<string, { state: object; actions: Record<string, VoidFunction> }>,
>() {
const useStore = <U extends keyof T>(service: U): T[U]['state'] => {
const instance = useAppContext<T>()
const serviceInstance = instance[service]
if (!serviceInstance) {
throw new Error(`Service ${String(service)} not found`)
}
return useSnapshot(serviceInstance.state)
}

const useAction = <U extends keyof T>(service: U): T[U]['actions'] => {
const instance = useAppContext<T>()
const serviceInstance = instance[service]
if (!serviceInstance) {
throw new Error(`Service ${String(service)} not found`)
}
return serviceInstance.actions
}

return { useStore, useAction }
}

const { useStore, useAction } = generateTypedHooks<ReturnType<typeof createCore>>()

export const useActivationDate = () => {
const creditStore = useStore('credit')
const { incrementCredits } = useAction('credit')

useEffect(() => {
incrementCredits()
}, [incrementCredits])

return creditStore.activationDate
}
16 changes: 5 additions & 11 deletions src/libs/poc-valtio/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@ import { services } from './services/_services'

type ServicesMap = typeof services
type ServiceName = keyof ServicesMap
type Service<T extends ServiceName> = ReturnType<ServicesMap[T]>
type ServicesType = { [K in ServiceName]: Service<K> }
type ServicesType = { [K in ServiceName]: ReturnType<ServicesMap[K]> }

export function createCore(): ServicesType {
export function createCore() {
Object.entries(services).forEach(([name, factory]) => {
if (!serviceRegistry.hasFactory(name as ServiceName)) {
serviceRegistry.register(name as ServiceName, factory)
}
})

const allServices = serviceRegistry.getAllServices()

const core = {} as ServicesType
const core = {} as { [K in ServiceName]: unknown }

for (const [name, service] of allServices.entries()) {
// @ts-ignore cannot type this for the moment
core[name] = service as Service<typeof name>
core[name as ServiceName] = service
}

return core
return core as ServicesType
}

// faire les hooks
// utiliser zustand
4 changes: 2 additions & 2 deletions src/libs/poc-valtio/services/_services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { creditService } from './credit.service'
import { userService } from './user.service'
import { creditService } from 'libs/poc-valtio/services/credit.service'
import { userService } from 'libs/poc-valtio/services/user.service'

export const services = {
credit: creditService,
Expand Down

0 comments on commit 8ed2439

Please sign in to comment.