MichaelKentBurns / BibleModel

Data model / database containing complete text of the Bible, Cross References, Notes and other references
3 stars 0 forks source link

Asynch database queries not completing as expected. #1

Open MichaelKentBurns opened 3 months ago

MichaelKentBurns commented 3 months ago

Don't know why. Must read and try tutorials I guess.

MichaelKentBurns commented 1 month 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/

MichaelKentBurns commented 1 month ago

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.

MichaelKentBurns commented 2 days ago

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.