@@ -14,7 +14,7 @@ export namespace SubscriberEvents {
1414export type Subscriber < T > = ( set : SubscriberEvents . Set < T > , onError : SubscriberEvents . OnError , onCompletion : SubscriberEvents . OnCompletion ) => PotentialPromise < Unsubscribe | void | undefined > ;
1515
1616export type SharedSubscriptionValue < T > = {
17- data ? : T ;
17+ data : T ;
1818 isLoading : boolean ;
1919 error ?: unknown ;
2020 subscribed ?: boolean
@@ -24,11 +24,12 @@ interface SharedSubscription<T> extends SharedSubscriptionValue<T> {
2424 unsubscribe ?: Unsubscribe | void ;
2525}
2626
27- const sharedSubscriptionsManager = new SharedValuesManager < SharedSubscription < any > > ( ( ) => defaultValue ) ;
27+ const sharedSubscriptionsManager = new SharedValuesManager < SharedSubscription < any > > ( ) ;
2828export const sharedSubscriptionsApi = new SharedValuesApi < SharedSubscription < any > > ( sharedSubscriptionsManager ) ;
2929
3030interface SharedSubscriptionCreated < T > extends SharedCreated {
31- subscriber : Subscriber < T >
31+ subscriber : Subscriber < T > ;
32+ triggerImmediately ?: boolean ;
3233}
3334
3435const defaultValue : SharedSubscription < any > = {
@@ -39,8 +40,11 @@ const defaultValue: SharedSubscription<any> = {
3940 unsubscribe : undefined ,
4041} ;
4142
42- export const createSharedSubscription = < T > ( subscriber : Subscriber < T > , scopeName ?: Prefix ) : SharedSubscriptionCreated < T > => {
43- return sharedSubscriptionsManager . createStatic < SharedSubscriptionCreated < T > > ( { subscriber} , defaultValue , scopeName ) ;
43+ export const createSharedSubscription = < T > ( subscriber : Subscriber < T > , options ?: {
44+ initialValue ?: T ,
45+ triggerImmediately ?: boolean ,
46+ } , scopeName ?: Prefix ) : SharedSubscriptionCreated < T > => {
47+ return sharedSubscriptionsManager . createStatic < SharedSubscriptionCreated < T > > ( { subscriber} , { ...defaultValue , data : options ?. initialValue as T } , scopeName ) ;
4448}
4549
4650export type SharedSubscriptionStateReturn < T > = {
@@ -69,12 +73,14 @@ export function useSharedSubscription<T, S extends string = string>(
6973 let keyStr : string ;
7074 let subscriberVal ! : Subscriber < T > ;
7175 let scope : Prefix | undefined = scopeName ;
76+ let triggerImmediately = false ;
7277
7378 if ( typeof key !== "string" ) {
74- const { key : key2 , subscriber : sub , prefix : prefix2 } = key ;
79+ const { key : key2 , subscriber : sub , prefix : prefix2 , triggerImmediately : _triggerImmediately } = key ;
7580 keyStr = key2 ;
7681 subscriberVal = sub ;
7782 scope = prefix2 ;
83+ triggerImmediately = _triggerImmediately ?? false ;
7884 } else {
7985 keyStr = ensureNonEmptyString ( key ) ;
8086 subscriberVal = subscriber as Subscriber < T > ;
@@ -147,6 +153,12 @@ export function useSharedSubscription<T, S extends string = string>(
147153
148154 sharedSubscriptionsManager . useEffect ( keyStr , prefix ) ;
149155
156+ useEffect ( ( ) => {
157+ if ( triggerImmediately ) {
158+ void trigger ( false ) ;
159+ }
160+ } , [ ] ) ;
161+
150162 return {
151163 state,
152164 trigger : ( ) => {
0 commit comments