File tree Expand file tree Collapse file tree 4 files changed +34
-9
lines changed Expand file tree Collapse file tree 4 files changed +34
-9
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import * as React from 'react';
44import { IConfig , UnleashClient } from 'unleash-proxy-client' ;
55import FlagContext , { IFlagContextValue } from './FlagContext' ;
66
7- interface IFlagProvider {
7+ export interface IFlagProvider {
88 config ?: IConfig ;
99 unleashClient ?: UnleashClient ;
1010 startClient ?: boolean ;
Original file line number Diff line number Diff line change @@ -23,6 +23,8 @@ import useVariant from './useVariant';
2323import useUnleashContext from './useUnleashContext' ;
2424import useUnleashClient from './useUnleashClient' ;
2525
26+ import { IFlagProvider } from './FlagProvider' ;
27+
2628export {
2729 FlagContext ,
2830 FlagProvider ,
@@ -34,4 +36,6 @@ export {
3436 useUnleashClient ,
3537} ;
3638
39+ export type { IFlagProvider } ;
40+
3741export default FlagProvider ;
Original file line number Diff line number Diff line change @@ -9,18 +9,28 @@ const useFlag = (name: string) => {
99
1010 useEffect ( ( ) => {
1111 if ( ! client ) return ;
12- client . on ( 'update' , ( ) => {
12+
13+ const updateHandler = ( ) => {
1314 const enabled = isEnabled ( name ) ;
1415 if ( enabled !== flagRef . current ) {
1516 flagRef . current = enabled ;
1617 setFlag ( ! ! enabled ) ;
1718 }
18- } ) ;
19+ } ;
1920
20- client . on ( 'ready' , ( ) => {
21+ const readyHandler = ( ) => {
2122 const enabled = isEnabled ( name ) ;
23+ flagRef . current = enabled ;
2224 setFlag ( enabled ) ;
23- } ) ;
25+ } ;
26+
27+ client . on ( 'update' , updateHandler ) ;
28+ client . on ( 'ready' , readyHandler ) ;
29+
30+ ( ) => {
31+ client . off ( 'update' , updateHandler ) ;
32+ client . off ( 'ready' , readyHandler ) ;
33+ } ;
2434 } , [ client ] ) ;
2535
2636 return flag ;
Original file line number Diff line number Diff line change @@ -11,7 +11,8 @@ const useVariant = (name: string): Partial<IVariant> => {
1111
1212 useEffect ( ( ) => {
1313 if ( ! client ) return ;
14- client . on ( 'update' , ( ) => {
14+
15+ const updateHandler = ( ) => {
1516 const newVariant = getVariant ( name ) ;
1617 if (
1718 variantRef . current . name !== newVariant . name ||
@@ -20,12 +21,22 @@ const useVariant = (name: string): Partial<IVariant> => {
2021 setVariant ( newVariant ) ;
2122 variantRef . current = newVariant ;
2223 }
23- } ) ;
24+ } ;
2425
25- client . on ( 'ready' , ( ) => {
26+ const readyHandler = ( ) => {
2627 const variant = getVariant ( name ) ;
28+ variantRef . current . name = variant . name ;
29+ variantRef . current . enabled = variant . enabled ;
2730 setVariant ( variant ) ;
28- } ) ;
31+ } ;
32+
33+ client . on ( 'update' , updateHandler ) ;
34+ client . on ( 'ready' , readyHandler ) ;
35+
36+ ( ) => {
37+ client . off ( 'update' , updateHandler ) ;
38+ client . off ( 'ready' , readyHandler ) ;
39+ } ;
2940 } , [ client ] ) ;
3041
3142 return variant || { } ;
You can’t perform that action at this time.
0 commit comments