let workInpreogressHook = null
let isMount = true
const fiber = {
stateNode: App,
memoizedState: null
}
function useState(initialState) {
let hook
if (isMount) {
hook = {
memoizedState: initialState,
next: null,
queue: {
pending: null
}
}
if (!fiber.memoizedState) {
fiber.memoizedState = hook
} else {
workInpreogressHook.next = hook
}
workInpreogressHook = hook
} else {
hook = workInpreogressHook
workInpreogressHook = workInpreogressHook.next
}
console.log('luov---workInpreogressHook', workInpreogressHook);
let baseState = hook.memoizedState;
if (hook.queue.pending) {
let firstUpdate = hook.queue.pending.next
do {
const action = firstUpdate.action
if (action instanceof Function) {
baseState = action(baseState)
} else {
baseState = action
}
firstUpdate = firstUpdate.next
} while (firstUpdate !== hook.queue.pending.next)
hook.queue.pending = null
}
hook.memoizedState = baseState
return [baseState, dispatchAction.bind(null, hook.queue)]
}
function dispatchAction(queue, action) {
const update = {
action,
next: null
}
if (queue.pending === null) {
update.next = update
} else {
update.next = queue.pending.next
queue.pending.next = update
}
queue.pending = update
console.log('luov--queue', queue);
schedule()
}
export function schedule() {
workInpreogressHook = fiber.memoizedState
const app = fiber.stateNode()
isMount = false
return app;
}
function App() {
const [num, updateNum] = useState(0)
console.log('luov-isMount:', isMount);
console.log('luov-num:', num);
return {
onclick() {
updateNum(num => num + 1)
updateNum(num => num * 10)
}
}
}
schedule()