|
1 | 1 | /** @format */ |
2 | 2 |
|
3 | | -import React, { type FC, type PropsWithChildren, useEffect, useMemo, useState } from 'react'; |
4 | | -import { type IConfig, UnleashClient } from 'unleash-proxy-client'; |
| 3 | +import React, { type FC, type PropsWithChildren, useCallback, useEffect, useMemo, useState } from 'react'; |
| 4 | +import { type IConfig, IMutableContext, UnleashClient } from 'unleash-proxy-client'; |
5 | 5 | import FlagContext, { type IFlagContextValue } from './FlagContext'; |
6 | 6 |
|
7 | 7 | export interface IFlagProvider { |
@@ -106,20 +106,40 @@ const FlagProvider: FC<PropsWithChildren<IFlagProvider>> = ({ |
106 | 106 | }; |
107 | 107 | }, []); |
108 | 108 |
|
| 109 | + const on = useCallback<IFlagContextValue['on']>(client.current.on, []); |
| 110 | + |
| 111 | + const off = useCallback<IFlagContextValue['off']>(client.current.off, []); |
| 112 | + |
| 113 | + const isEnabled = useCallback<IFlagContextValue['isEnabled']>( |
| 114 | + (toggleName: string) => client.current.isEnabled(toggleName), |
| 115 | + [] |
| 116 | + ) |
| 117 | + |
| 118 | + const updateContext = useCallback<IFlagContextValue['updateContext']>( |
| 119 | + async (context: IMutableContext) => |
| 120 | + await client.current.updateContext(context), |
| 121 | + [] |
| 122 | + ) |
| 123 | + |
| 124 | + const getVariant = useCallback<IFlagContextValue['getVariant']>( |
| 125 | + (toggleName: string) => client.current.getVariant(toggleName), |
| 126 | + [] |
| 127 | + ) |
| 128 | + |
109 | 129 | const context = useMemo<IFlagContextValue>( |
110 | 130 | () => ({ |
111 | | - on: ((event, callback, ctx) => client.current.on(event, callback, ctx)) as IFlagContextValue['on'], |
112 | | - off: ((event, callback) => client.current.off(event, callback)) as IFlagContextValue['off'], |
113 | | - updateContext: async (context) => await client.current.updateContext(context), |
114 | | - isEnabled: (toggleName) => client.current.isEnabled(toggleName), |
115 | | - getVariant: (toggleName) => client.current.getVariant(toggleName), |
| 131 | + on, |
| 132 | + off, |
| 133 | + updateContext, |
| 134 | + isEnabled, |
| 135 | + getVariant, |
116 | 136 | client: client.current, |
117 | 137 | flagsReady, |
118 | 138 | flagsError, |
119 | 139 | setFlagsReady, |
120 | 140 | setFlagsError, |
121 | 141 | }), |
122 | | - [flagsReady, flagsError] |
| 142 | + [flagsReady, flagsError, on, off, updateContext, isEnabled, getVariant] |
123 | 143 | ); |
124 | 144 |
|
125 | 145 | return ( |
|
0 commit comments