feat: add simple key-value store to the builders #480
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
With new gadgets, we may have some state we want to store over different calls. Instead of using singletons (essentially global variables), we instead add a
map[any]any
to the builders which allow to store builder-local variables.To prevent overuse of the map, we dont publish the methods publicly in the
frontend.API
, but instead embed an instance in the builders themselves. If a gadget wants to use the storage, then they first have to type-assert the frontend.API instance tokvstore.Store
and then on the new variable callSetKeyValue
andGetKeyValue
.Another great aspect of having
any
keys is that we can use any comparable types (comparable because map implementation requires so). This allows us to avoid any collisions by enforcing that the keys are of different type. See the example below:In the previous example, we could also have for example
type gadgetKey int
and then have different values for the keys if the gadget needs to store different values.Initially I thought about just using
context.Context
as it provides similar features, but it doesn't seem to fit in our use cases. See comments in #472.