IMA-WorldHealth / bhima

A hospital information management application for rural Congolese hospitals
GNU General Public License v2.0
215 stars 103 forks source link

Replace the Store() with a Javascript Map() #7387

Open jniles opened 8 months ago

jniles commented 8 months ago

The first lines of the BHIMA Store starts with:

https://github.com/IMA-WorldHealth/bhima/blob/9e66f548538674242f0043f5a74ef7676e9a3e97/client/src/js/services/Store.js#L4-L10

The Store was lifted almost in its entirety from the DojoToolkit's MemoryStore implemented back in 2010. Javascript has evolved since then, and built its own native version of a Store - the Map/WeakMap. Indeed, the MDN documentation for WeakMap states:

A map API could be implemented in JavaScript with two arrays (one for keys, one for values) shared by the four API methods. Setting elements on this map would involve pushing a key and value onto the end of each of those arrays simultaneously.

This is almost exactly what the BHIMA store does, except we use an object for our index, not an array. Because of this, and the fact that we need to be able to iterate over the contents of a store, we should use a Map() instead of a WeakMap().

I propose we keep all the methods of Store, and keep the Store service, but transparently replace the implementation with Map(). The test should still pass, and no other code outside of the Store service will need to be changed. It will be a silent upgrade to one of the oldest modules in the BHIMA library.

jniles commented 1 month ago

Note that this fix is currently stalled because some modules are $watch-ing the store.data array. This array doesn't exist in the when using a native Map(). We'll have to refactor the APIs that watch these items to make sure we can appropriately handle these cases.