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

Improve async cache #1328

Merged
merged 10 commits into from
Jun 8, 2021
Merged

Improve async cache #1328

merged 10 commits into from
Jun 8, 2021

Conversation

notaphplover
Copy link
Member

@notaphplover notaphplover commented May 2, 2021

Description

This PR updates the resolver to update the cache of an async result to its resolved value once the promise is fullfilled

Related Issue

#1320

Motivation and Context

See #1320 for more details

How Has This Been Tested?

Code compiles and tests passses successfully

Types of changes

  • Updated docs / Refactor code / Added a tests case (non-breaking change)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the changelog.

@notaphplover notaphplover self-assigned this May 2, 2021
@notaphplover
Copy link
Member Author

@tonyhallett This is my proposal for improving the cache system on singleton scope :), I'll add tests and update docs later

@tonyhallett
Copy link
Contributor

Looks good. If we go with my scope abstraction then the only change would need to be to the SingletonScope

class SingletonScope<T> implements interfaces.Scope<T>{
  get(binding:interfaces.Binding<T>,_: interfaces.Request): Promise<T> | T | null {
    if(binding.activated) {
      return binding.cache;
    }
    return null;
  }
  set(binding:interfaces.Binding<T>,_:interfaces.Request,resolved:T|Promise<T>):T | Promise<T> {
      binding.cache = resolved;
      binding.activated = true;

      if (isPromise(resolved)) {
          resolved = resolved.catch((ex) => {
              // allow binding to retry in future
              binding.cache = null;
              binding.activated = false;

              throw ex;
          });
      }
      return resolved;
  }

}

@notaphplover notaphplover marked this pull request as ready for review May 6, 2021 20:55
@notaphplover notaphplover requested a review from a team May 7, 2021 16:54
Copy link
Member

@PodaruDragos PodaruDragos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM !

@dcavanagh dcavanagh merged commit f663023 into master Jun 8, 2021
@dcavanagh dcavanagh deleted the improve-async-cache branch June 8, 2021 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants