Skip to content

Commit

Permalink
remove frameLength
Browse files Browse the repository at this point in the history
  • Loading branch information
yisar committed Oct 10, 2019
1 parent 86d6b15 commit 4205751
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ render(<Counter />, document.getElementById('root'))

#### useEffect

`useEffect` takes two parameters, the first is a effect callback and the second is an array, usually props
`useEffect` takes two parameters, the first is a effect callback and the second is an array

if the array changed, the effect callback will execute after commitWork, such as `pureComponentDidUpdate`
if the array changed, the callback will execute after commitWork, such as `pureComponentDidUpdate`

if the array is empty, it means execute once, such as `componentDidMount`

Expand Down
8 changes: 4 additions & 4 deletions demo/useEffect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { h, render, useState, useEffect } from '../src'
// import { render } from "react-dom"
// import { createElement as h, useState, useEffect,useRef } from "react"
// import { h, render, useState, useEffect } from '../src'
import { render } from "react-dom"
import { createElement as h, useState, useEffect,useRef } from "react"

function Counter ({ id, remove }) {
const [count, setCount] = useState(0)
Expand All @@ -13,7 +13,7 @@ function Counter ({ id, remove }) {
console.log(`Counter #${id} removed`);
}
},
[count]
[]
)

return (
Expand Down
10 changes: 7 additions & 3 deletions src/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { push, pop, peek } from './heapify'
let taskQueue = []
let currentTask = null
let currentCallback = null
let inMC = false
let frameDeadline = 0

export function scheduleCallback (callback) {
Expand All @@ -21,7 +22,7 @@ export function scheduleCallback (callback) {

currentCallback = flushWork

planWork()
if (!inMC) planWork() && (inMC = true)

This comment has been minimized.

Copy link
@mindplay-dk

mindplay-dk Oct 16, 2019

Contributor

Possible bug here: the inMC = true statement will never execute.

It might work under test, where planWork() returns a truthy timer handle - but in a browser it's returning the return-value from postMessage which is a falsy void, so this assignment can't happen.

planWork doesn't have a defined return-value, so you probably want something like:

if (!inMC) { planWork(); inMC = true }

(on the other hand, if you meant for inMC to be set true only if there's any work left to plan, that's going to be harder, since planWork only schedules performWork to be executed later - it doesn't know at that time if there's work left or not...)


return newTask
}
Expand Down Expand Up @@ -61,23 +62,26 @@ function workLoop (iniTime) {
function performWork () {
if (currentCallback) {
let currentTime = getTime()
frameDeadline = currentTime + 5
frameDeadline = currentTime
let moreWork = currentCallback(currentTime)
if (!moreWork) {
inMC = false
currentCallback = null
} else {
planWork()
}
}
} else inMC = false
}

const planWork = (() => {
if (typeof MessageChannel !== 'undefined') {
const channel = new MessageChannel()
const port = channel.port2
channel.port1.onmessage = performWork

return () => port.postMessage(null)
}

return () => setTimeout(performWork, 0)
})()

Expand Down

0 comments on commit 4205751

Please sign in to comment.