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

feat(kernel): add last resolver #1976

Merged
merged 5 commits into from
May 18, 2024
Merged

feat(kernel): add last resolver #1976

merged 5 commits into from
May 18, 2024

Conversation

Vheissu
Copy link
Member

@Vheissu Vheissu commented May 18, 2024

Pull Request

📖 Description

This pull request introduces support for retrieving the last registered value from the Aurelia DI (Dependency Injection) container. In scenarios where multiple values are registered under the same key, the new last resolver ensures that the most recently registered value is always returned.

In the Aurelia DI system, it's possible to register multiple values under the same key. This feature can be useful for various purposes, such as overriding previously registered values without losing them. However, when retrieving these values, the container typically returns all instances associated with the key. There are cases where it's desirable to get only the most recent value, such as when the latest configuration or instance should take precedence.

The implementation of the last resolver involves creating a new resolver that:

  • Checks for the presence of multiple values under a specified key.
  • Retrieves and returns the last (most recently registered) value among them.

This approach leverages the existing functionality of the getAll method to fetch all instances associated with a key and then accesses the last element in the array.

  • Single Call to getAll: The last resolver optimises performance by making a single call to the getAll method and storing the result.

  • Direct Element Access: It accesses the last element directly using array indexing, ensuring no modifications are made to the array, which preserves immutability.

  • Graceful Handling of Empty Arrays: The resolver includes checks to handle cases where no values are registered under the key, returning undefined in such situations.

Usage

The following example demonstrates what happens when you register three different values under the same key.

container.register(Registration.instance('key', 'value1'));
container.register(Registration.instance('key', 'value2'));
container.register(Registration.instance('key', 'value3'));

const myValue = container.get(last('key'));
console.log(myValue); // Outputs: value3

Adds in support for getting last value from the container in instances where are are multiple registered values under the same key.
@Vheissu Vheissu requested a review from bigopon May 18, 2024 00:34
@bigopon bigopon changed the title feat(kernel): add last decorator feat(kernel): add last resolver May 18, 2024
Copy link
Member

@bigopon bigopon left a comment

Choose a reason for hiding this comment

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

Nice work, thanks @Vheissu

packages/__tests__/src/1-kernel/di.last.spec.ts Outdated Show resolved Hide resolved
packages/__tests__/src/1-kernel/di.last.spec.ts Outdated Show resolved Hide resolved
Vheissu and others added 2 commits May 18, 2024 18:38
Copy link

codecov bot commented May 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.21%. Comparing base (f1a73d6) to head (e506473).
Report is 7 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1976      +/-   ##
==========================================
- Coverage   88.59%   84.21%   -4.38%     
==========================================
  Files         273      270       -3     
  Lines       22945    22388     -557     
  Branches     5305     5220      -85     
==========================================
- Hits        20328    18854    -1474     
- Misses       2617     3534     +917     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bigopon bigopon merged commit c47817c into master May 18, 2024
28 of 29 checks passed
@bigopon bigopon deleted the feat/last-resolver branch May 18, 2024 09:21
AureliaEffect pushed a commit that referenced this pull request May 23, 2024
2.0.0-beta.18 (2024-05-23)

**BREAKING CHANGES:**

* **dom-queue:** merge dom read and write queues (#1970) ([3a63cde](3a63cde))

**Features:**

* **dialog:** ability to specify dialog per open call (#1978) ([7d44ed1](7d44ed1))
* **dialog:** add event manager for custom event handling extension ([7d44ed1](7d44ed1))
* **kernel:** add last resolver (#1976) ([c47817c](c47817c))
* **router-lite:** current route (#1966) ([d966e15](d966e15))

**Bug Fixes:**

* **di:** use official metadata instead of weakmap (#1977) ([9aeeffa](9aeeffa))
* **router-lite:** current route subscription disposal (#1969) ([ace4c65](ace4c65))
* **convention:** typing: use array for bindables isntead of object (#1967) ([f1a73d6](f1a73d6))

**Refactorings:**

* **collection:** define map & set overrides on the instance instead of prototype (#1975) ([253e92a](253e92a))
* **runtime:** reoganise utils import ([253e92a](253e92a))
* **fetch-client:** extract error codes and cleanup (#1974) ([63ffdc9](63ffdc9))
* **i18n-validation:** replace errors with error codes (#1972) ([f91f31c](f91f31c))
* **dev:** turbo package input glob + ts dev script ([253e92a](253e92a))

**Docs:**

* **doc:** updated ISignaler documentation example (#1973) ([e0481d6](e0481d6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants