Skip to content
This repository was archived by the owner on Aug 14, 2024. It is now read-only.

In-memory/memoize payload API #14

Closed
fraxken opened this issue Aug 29, 2022 · 3 comments
Closed

In-memory/memoize payload API #14

fraxken opened this issue Aug 29, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@fraxken
Copy link
Member

fraxken commented Aug 29, 2022

I think it might be nice to have an in-memory API for RC. This would simplify implementations on some projects that are spread over several packages.

Example of two new API:

  • .memoize()
  • .memoized()
const configurationPayload = (
    await RC.read(void 0, { createMode: "ci" })
).unwrap();

/**
 * OVERWRITE [TRUE]: Overwrite the previous memoized payload
 * OVERWRITE [FALSE]: Merge with the previous memoized payload
 */
RC.memoize(configurationPayload, { overwrite: false });

const result = RC.memoized({
    default: {}
}).unwrap();

Automatically memoize using .read() API:

const configurationPayload = (
    await RC.read(void 0, { createMode: "ci", memoize: true })
).unwrap();

const result = RC.memoized({
    default: {}
}).unwrap();

Note: The default options allow to set a default value if there is no memoized value.

@fraxken fraxken added enhancement New feature or request good first issue Good for newcomers labels Aug 29, 2022
@fraxken fraxken mentioned this issue Jan 12, 2023
@fabnguess
Copy link
Contributor

Hello @fraxken, I would like to discuss this issue that interests me. Could you explain to me how the "memoize" method will work? Will we have an existing object that we can work on or will we have to create a new one in memory for this purpose?

@fraxken
Copy link
Member Author

fraxken commented Feb 12, 2023

Hello @fabnguess what do you mean by how the "memoize" will work?

The idea is just to keep a runtime configuration in memory (with a new memoize API) and then be able to get it with memoized API (look at my example).

As my example state we can then add options to:

  • Automatically memoize when using read API.
  • Overwrite (merging or not) using memoize. Look at lodash.merge.
  • Getting a default value when using memoized API (if there is no config in memory).

Overall you need to be familiar with those concepts and with the package itself.

@fraxken
Copy link
Member Author

fraxken commented Feb 12, 2023

Fairly simple example

let memoizedValue = null;

export function memoize(obj) {
  // TODO: validating obj is a valid rc?

  memoizedValue = obj;
}

export function memoized(options = {}) {
  const { defaultValue } = options;

  return memoizedValue === null ? defaultValue : memoizedValue;
}

@fabnguess fabnguess self-assigned this Feb 13, 2023
@fraxken fraxken closed this as completed Mar 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants