-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
10 changed files
with
190 additions
and
45 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useRef, useState } from "react"; | ||
|
||
interface DataProvider { | ||
selected: number; | ||
} | ||
|
||
export const useDataProvider = (initVal: DataProvider) => { | ||
const stateRef = useRef(initVal); | ||
const [state, setState] = useState(initVal); | ||
|
||
// wrong impl to mimic react state update side effect | ||
stateRef.current = state; | ||
|
||
return { | ||
getData: () => stateRef.current, | ||
setData: setState, | ||
}; | ||
}; |
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,27 @@ | ||
import { useState } from "react"; | ||
|
||
export function RadioButtonGroup({ | ||
options, | ||
onChange, | ||
}: { | ||
options: string[]; | ||
onChange: (option: string) => void; | ||
}) { | ||
const [selected, setSelected] = useState(options[0]); | ||
return options.map((option) => ( | ||
<label key={option}> | ||
<input | ||
type="radio" | ||
value={option} | ||
checked={selected === option} | ||
onChange={(ev) => { | ||
setSelected(ev.target.value); | ||
onChange(ev.target.value); | ||
}} | ||
/> | ||
{option} | ||
</label> | ||
)); | ||
} | ||
|
||
export default RadioButtonGroup; |
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,36 @@ | ||
import { useState } from "react"; | ||
import { useDataProvider } from "./DataProvider"; | ||
|
||
|
||
|
||
export function SampleV0() { | ||
const { getData, setData } = useDataProvider({ selected: 6 }); | ||
const [selected, setSelected] = useState(getData().selected); | ||
|
||
return ( | ||
<div> | ||
<h3>"Scheduler" Sample V0</h3> | ||
<div>Current selection (by state): {getData().selected}</div> | ||
<div>Current selection (by action): {selected}</div> | ||
<button | ||
onClick={() => { | ||
// r17 | ||
// - all setState will be executed immediately | ||
// r18 compatible mode | ||
// - setState in sync mode will be batched | ||
// - setState in async mode will be executed immediately | ||
// r18 concurrent mode | ||
// - all setState will be batched | ||
Promise.resolve().then(() => { | ||
setData({ selected: 0 }); | ||
setSelected(getData().selected); | ||
}); | ||
}} | ||
> | ||
Select None | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export default SampleV0; |
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,45 @@ | ||
import { useState } from "react"; | ||
import { useDataProvider } from "./DataProvider"; | ||
import { useProcessJobQueue, useScheduler } from "./libs/v1"; | ||
|
||
export function SampleV1() { | ||
const { getData, setData } = useDataProvider({ selected: 6 }); | ||
const [selected, setSelected] = useState(getData().selected); | ||
const { schedulePostDispatchEvents, queue } = useScheduler("test"); | ||
|
||
useProcessJobQueue(queue, "test"); | ||
|
||
return ( | ||
<div> | ||
<h3>"Scheduler" Sample V0</h3> | ||
<div>Current selection (by state): {getData().selected}</div> | ||
<div>Current selection (by action): {selected}</div> | ||
<button | ||
onClick={() => { | ||
Promise.resolve().then(() => { | ||
setData({ selected: 0 }); | ||
schedulePostDispatchEvents({ | ||
execute: () => { | ||
setSelected(getData().selected); | ||
}, | ||
}); | ||
}); | ||
// issue for v1: this schedule will not be executed | ||
setTimeout( | ||
() => | ||
schedulePostDispatchEvents({ | ||
execute: () => { | ||
console.log("Job2 (schedule in use effect) executed"); | ||
}, | ||
}), | ||
2000 | ||
); | ||
}} | ||
> | ||
Select None | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export default SampleV1; |
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,43 @@ | ||
import { useState } from "react"; | ||
import { useDataProvider } from "./DataProvider"; | ||
import { useDeferJobQueue } from "./libs/v2"; | ||
|
||
export function SampleV2() { | ||
const { getData, setData } = useDataProvider({ selected: 6 }); | ||
const [selected, setSelected] = useState(getData().selected); | ||
const { defer } = useDeferJobQueue(); | ||
|
||
return ( | ||
<div> | ||
<h3>"Scheduler" Sample V0</h3> | ||
<div>Current selection (by state): {getData().selected}</div> | ||
<div>Current selection (by action): {selected}</div> | ||
<button | ||
onClick={() => { | ||
Promise.resolve().then(() => { | ||
setData({ selected: 0 }); | ||
defer({ | ||
execute: () => { | ||
setSelected(getData().selected); | ||
}, | ||
}); | ||
}); | ||
// issue for v1: this schedule will not be executed | ||
setTimeout( | ||
() => | ||
defer({ | ||
execute: () => { | ||
console.log("Job2 (schedule in use effect) executed"); | ||
}, | ||
}), | ||
2000 | ||
); | ||
}} | ||
> | ||
Select None | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export default SampleV2; |
File renamed without changes.
File renamed without changes.
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