Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jniles opened this issue Dec 27, 2023 · 0 comments · May be fixed by Third-Culture-Software/bhima#6
Open

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

jniles opened this issue Dec 27, 2023 · 0 comments · May be fixed by Third-Culture-Software/bhima#6

Comments

@jniles
Copy link
Contributor

jniles commented Dec 27, 2023

The first lines of the BHIMA Store starts with:

/**
* @class Store
*
* @description
* This class implements an object data store, similar to Dojo's Memory Store. It could
* forseeably be replaced by a WeakMap() in the future.
*/

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 added a commit to Third-Culture-Software/bhima that referenced this issue May 18, 2024
This commit removes the previous `Store.js` internals and replaces them
with a simple JavaScript `Map()` while retaining the same API.  It is
likely more efficient than the previous version.

Closes IMA-WorldHealth#7387.
jniles added a commit to Third-Culture-Software/bhima that referenced this issue May 23, 2024
This commit removes the previous `Store.js` internals and replaces them
with a simple JavaScript `Map()` while retaining the same API.  It is
likely more efficient than the previous version.

Closes IMA-WorldHealth#7387.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant