-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TASK: Add package
@neos-project/framework-observable-react
This new package contains react bindings for the other recently introduced package `@neos-project/framework-observable`.
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@neos-project/framework-observable-react", | ||
"version": "", | ||
"description": "React bindings for @neos-project/framework-observable", | ||
"private": true, | ||
"main": "./src/index.ts", | ||
"dependencies": { | ||
"@neos-project/framework-observable": "workspace:*", | ||
"react": "^16.12.0" | ||
}, | ||
"license": "GNU GPLv3" | ||
} |
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,11 @@ | ||
/* | ||
* This file is part of the Neos.Neos.Ui package. | ||
* | ||
* (c) Contributors of the Neos Project - www.neos.io | ||
* | ||
* This package is Open Source Software. For the full copyright and license | ||
* information, please view the LICENSE file which was distributed with this | ||
* source code. | ||
*/ | ||
export {useLatestState} from './useLatestState'; | ||
export {useLatestValueFrom} from './useLatestValueFrom'; |
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,30 @@ | ||
/* | ||
* This file is part of the Neos.Neos.Ui package. | ||
* | ||
* (c) Contributors of the Neos Project - www.neos.io | ||
* | ||
* This package is Open Source Software. For the full copyright and license | ||
* information, please view the LICENSE file which was distributed with this | ||
* source code. | ||
*/ | ||
import React from 'react'; | ||
|
||
import type {State} from '@neos-project/framework-observable'; | ||
|
||
export function useLatestState<V>(state$: State<V>) { | ||
const [value, setValue] = React.useState<V>(state$.current); | ||
|
||
React.useEffect(() => { | ||
const subscription = state$.subscribe({ | ||
next: (incomingValue) => { | ||
if (incomingValue !== value) { | ||
setValue(incomingValue); | ||
} | ||
} | ||
}); | ||
|
||
return () => subscription.unsubscribe(); | ||
}, [state$]); | ||
|
||
return value; | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/framework-observable-react/src/useLatestValueFrom.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,34 @@ | ||
/* | ||
* This file is part of the Neos.Neos.Ui package. | ||
* | ||
* (c) Contributors of the Neos Project - www.neos.io | ||
* | ||
* This package is Open Source Software. For the full copyright and license | ||
* information, please view the LICENSE file which was distributed with this | ||
* source code. | ||
*/ | ||
import React from 'react'; | ||
|
||
import type {Observable} from '@neos-project/framework-observable'; | ||
|
||
export function useLatestValueFrom<V>( | ||
observableFactory: () => Observable<V>, | ||
deps: any[] = [] | ||
) { | ||
const observable$ = React.useMemo(observableFactory, deps); | ||
const [value, setValue] = React.useState<null | V>(null); | ||
|
||
React.useEffect(() => { | ||
const subscription = observable$.subscribe({ | ||
next: (incomingValue) => { | ||
if (incomingValue !== value) { | ||
setValue(incomingValue); | ||
} | ||
} | ||
}); | ||
|
||
return () => subscription.unsubscribe(); | ||
}, [observable$]); | ||
|
||
return value; | ||
} |
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