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

implement withMemory() function #1

Open
mkloubert opened this issue Apr 29, 2022 · 0 comments
Open

implement withMemory() function #1

mkloubert opened this issue Apr 29, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@mkloubert
Copy link
Owner

the function should wrap a function as async version of it

if it is called the first time, it should be able to remember the result and return this result, without calling the action again

it should also be able to reset its state

import assert from "assert"
const { setTimeout } = require('timers/promises')
import { withMemory } from "@marcelkloubert/promises"

const myAsyncFunc = async (value: number) => {
  return {
    time: new Date(),
    value2x: value * 2
  }
}

const myWrappedAction = withMemory(myAsyncFunc)

// new, because not invoked with these argument yet
const result1a = await myWrappedAction(1)
await setTimeout(1000)
// cached
const result1b = await myWrappedAction(1)

// also new, because not invoked with these argument yet
const result2a = await myWrappedAction(2)
await setTimeout(1000)
// cached
const result2b = await myWrappedAction(2)

// wrapped function whould have this method
// to reset all states
myWrappedAction.reset()

// now all actions are invoked again
const result1c = await myWrappedAction(1)
const result2c = await myWrappedAction(2)

// reset one cached state
myWrappedAction.resetOne(2)
// cached
const result1d = await myWrappedAction(1)
// not cached
const result2d = await myWrappedAction(2)
@mkloubert mkloubert added the enhancement New feature or request label Apr 29, 2022
@mkloubert mkloubert self-assigned this Apr 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant