forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from nreese/publishing_subject
isolate publishing subject logic and rename methods
- Loading branch information
Showing
15 changed files
with
95 additions
and
85 deletions.
There are no files selected for viewing
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
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
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
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
60 changes: 60 additions & 0 deletions
60
packages/presentation/presentation_publishing/publishing_subject.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,60 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { useEffect, useMemo, useState } from 'react'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
|
||
/** | ||
* A publishing subject is a RxJS subject that can be used to listen to value changes, but does not allow pushing values via the Next method. | ||
*/ | ||
export type PublishingSubject<T extends unknown = unknown> = Omit<BehaviorSubject<T>, 'next'>; | ||
|
||
/** | ||
* A utility type that makes a type optional if another passed in type is optional. | ||
*/ | ||
type OptionalIfOptional<TestType, Type> = undefined extends TestType | ||
? Type | undefined | ||
: Type; | ||
|
||
/** | ||
* Declares a publishing subject, allowing external code to subscribe to react state changes. | ||
* Changes to state fire subject.next | ||
* @param state React state from useState hook. | ||
*/ | ||
export const usePublishingSubject = <T extends unknown = unknown>( | ||
state: T | ||
): PublishingSubject<T> => { | ||
const subject = useMemo<BehaviorSubject<T>>( | ||
() => new BehaviorSubject<T>(state), | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[] | ||
); | ||
useEffect(() => subject.next(state), [subject, state]); | ||
return subject; | ||
}; | ||
|
||
/** | ||
* Declares a state variable that is synced with a publishing subject value. | ||
* @param subject Publishing subject. | ||
*/ | ||
export const useStateFromPublishingSubject = < | ||
ValueType extends unknown = unknown, | ||
SubjectType extends PublishingSubject<ValueType> | undefined = | ||
| PublishingSubject<ValueType> | ||
| undefined | ||
>( | ||
subject?: SubjectType | ||
): OptionalIfOptional<SubjectType, ValueType> => { | ||
const [value, setValue] = useState<ValueType | undefined>(subject?.getValue()); | ||
useEffect(() => { | ||
if (!subject) return; | ||
const subscription = subject.subscribe((newValue) => setValue(newValue)); | ||
return () => subscription.unsubscribe(); | ||
}, [subject]); | ||
return value as OptionalIfOptional<SubjectType, ValueType>; | ||
}; |
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