-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac6213e
commit 186912c
Showing
8 changed files
with
129 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,25 @@ | ||
import { } from "rval" | ||
import { Observable, sub, isVal, isDrv } from "rval" | ||
import { useState, useEffect } from "react" | ||
|
||
// TODO: how to use correct context? | ||
|
||
// TODO: explicit subscription is `useVal` arg 1 is a Drv / Val | ||
export function useVal() { | ||
|
||
export function useVal<T>(observable: Observable<T>): T { | ||
if (!isVal(observable) && !isDrv(observable)) throw new Error("useval - expected val or drv") | ||
const [val, updater] = useState(observable) | ||
useEffect(() => { | ||
const disposer = sub(observable, updater) | ||
// observable has changed before effect was run first time, so trigger additional update | ||
if (observable() !== val) | ||
updater(observable()) | ||
return disposer | ||
}, [observable]) | ||
return val | ||
} | ||
|
||
export function useDrv() { | ||
|
||
} | ||
|
||
// TODO: how to use correct context? | ||
export function render() { | ||
|
||
} |
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,21 @@ | ||
import { val } from 'rval' | ||
import { useVal } from 'rval/react' | ||
import * as React from 'react' | ||
import { render, waitForElement } from 'react-testing-library' | ||
|
||
test('useVal - 1 ', async () => { | ||
const counter = val(0) | ||
const Comp = () => { | ||
const c = useVal(counter) | ||
return <h1>{c}</h1> | ||
} | ||
|
||
const re = render(<Comp />) | ||
expect(re.container.innerHTML).toEqual('<h1>0</h1>') | ||
|
||
counter(counter() + 1) | ||
await waitForElement(() => re.container.innerHTML === '<h1>1</h1>') | ||
|
||
counter(counter() + 1) | ||
await waitForElement(() => re.container.innerHTML === '<h1>2</h1>') | ||
}) |
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