Open MichaelKentBurns opened 3 months ago
See Notes/Strange execution timing.txt (https://github.com/MichaelKentBurns/BibleModel/blob/main/Notes/Strange%20execution%20timing.txt)
I have studied JavaScript's strange event loop quite a lot.
I kind of understand why it is that way but it is pretty strange compared to most programming language execution models.
https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick https://dev.to/emmanuelayinde/understanding-asynchronous-programming-in-javascript-synchronous-asynchronous-microtasks-macrotasks-and-the-event-loop-h5e https://dev.to/ssd/javascript-promises-in-depth-with-v8-engine-internals-1jlb https://dev.to/bhataasim/event-loop-in-2-minutes-5895 https://www.freecodecamp.org/news/javascript-engine-and-runtime-explained/
In some of my reading, I concluded that this is best coded as a state machine. Part of the complexity is that Bible.js is the top level controlling class. It coordinates actions that get executed in other classes such as Book.js, Xref.js etc. So, Model.js is now a state machine initialized at the top with a list of the states as numerics and an array of strings that describe each state.
The complexity comes in when Bible.js wants to call Book.js to load the descriptions of all of the books (either from a json file, or from the SQL database). Both of those require some I/o which runs asychnronous. JavaScript asynchronous tasks execute outside the simple event loop in Bible.js and use a callback to signal completion. When this happens, the Bible.js event loop keeps running. If nothing stops it, then it will run to completion and the program finishes. At that point, it has lost control and can't process the results of the query. So, my current plan is to wrap a loop around the state handling code and put a short timeout at the end of the loop. When the timeout relinquishes control, the asynch processing proceeds. When the timeout completes the callback would have been called and the state machine will be moved on to the next state. So, the loop will continue until the state machine reaches the termination state. Expect my next major commit and push to see at least some of that coding.
I have updated Bible.js to reflect the implementation of the state machine. I have also documented that state machine in ObjectModel/StateMachine.md (and corresponding graphic files of the same base filename).
I probably still have work to do on this, but the latest runs demonstrate the state machine working. See notes/runs/Bible.js.trace.run.2024-11-01.run
One thing I still see is wrong with the state machine is that when the books are all loaded and it tries to advance to the saveBooks state, it seems to just exit the program. I think it just needs to get restarted into the state machine one more time.
The update labeled 'Many changes to state machine, which is update ID bac7f17160fd64c8ba4e5aa77cafcd639fea8c24 Got enough of the state machine to work. Part of the timing problem is having the Bible.js traceBible turned on. That does MANY console.log output which disrupts the timing. While it helps with understaning the state machine, it sometimes keeps it from working. I have ideas for fixing that, but in the mean time its OK.
Don't know why. Must read and try tutorials I guess.