-
Notifications
You must be signed in to change notification settings - Fork 3
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
Replace Lazy with LazyRef #141
base: develop
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: c8889a9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few notes to self...
@@ -1274,7 +1274,7 @@ export class ContainerInstance implements Disposable { | |||
* One important note is that the dependency *itself* is never resolved in the container. | |||
*/ | |||
const parameters = serviceMetadata.dependencies.map(resolvable => ({ | |||
id: this.resolveTypeWrapper(resolvable.typeWrapper), | |||
id: resolvable.typeWrapper.eagerType, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The now-removed method featured additional error-checking, e.g. null checks. This should either be reimplemented or added to changeset, as this is a breaking change.
@@ -1350,14 +1350,13 @@ export class ContainerInstance implements Disposable { | |||
*/ | |||
private resolveResolvable(resolvable: Resolvable, guardBuiltIns: boolean): unknown { | |||
const { typeWrapper } = resolvable; | |||
|
|||
const identifier = this.resolveTypeWrapper(resolvable.typeWrapper); | |||
const identifier = resolvable.typeWrapper.eagerType; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing here...
@@ -12,15 +12,6 @@ import { isTypeWrapper } from './is-type-wrapper.util.mjs'; | |||
* @param target the class definition of the target of the decorator | |||
*/ | |||
export function resolveToTypeWrapper(typeOrIdentifier: AnyInjectIdentifier): TypeWrapper { | |||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps these comments should be re-added (but actually relevant in the context of the API changes)? For context, these haven't changed since typestack -- they would probably just confuse readers more than help them.
@@ -1396,6 +1395,8 @@ export class ContainerInstance implements Disposable { | |||
*/ | |||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |||
return typeWrapper.extract!(this, constraints ?? ResolutionConstraintFlag.None); | |||
} else if (!identifier) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably simpler just to hoist this to the top, which would still save the potentially unnecessary built-ins check.
@@ -1494,35 +1495,6 @@ export class ContainerInstance implements Disposable { | |||
} | |||
} | |||
|
|||
private resolveTypeWrapper(wrapper: TypeWrapper): ServiceIdentifier { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The removal of this, even with it being private, should be in the changeset somewhere...
|
||
protected readonly container: ContainerInstance; | ||
protected readonly constraints: number; | ||
protected readonly getID: LazyRefFactory<TIdentifier>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to have some documentation for getID
.
|
||
export type LazyRefFactory<TIdentifier extends ServiceIdentifier> = () => TIdentifier; | ||
|
||
export class LazyRefHost< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a sprinkling of TSDoc.
} | ||
|
||
public createOrNull(): TInstance | null { | ||
return this.resolve(ResolutionConstraintFlag.Optional); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could do with the comment in create
, but inverted.
|
||
**This is a breaking change for anyone constructing `TypeWrapper` objects.** | ||
|
||
Internal `TypeWrapper` objects have been refactored. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changesets are a right mess.
|
||
Now, the `eagerType` property is optional, and the `lazyType` property has been removed. This has been changed because the `lazyType` property was part of broader lazy functionality (alongside the `Lazy` function) which was mainly a remnant from the upstream TypeDI project. | ||
|
||
Lazy reference functionality has been moved into a contributory package, appropriately named *lazy-ref*. This package contains a new `LazyRef` function, which allows for holding weak, or "lazy" references to services. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This deserves its own changeset.
'@freshgum/typedi': patch | ||
--- | ||
|
||
Two new type-wrapper helper interfaces have been added: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some examples of these might be nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few more notes to self.
* ? We want to error out as soon as possible when looking up services to inject, however | ||
* ? we cannot determine the type at decorator execution when cyclic dependencies are involved | ||
* ? because calling the received `() => MyType` function right away would cause a JS error: | ||
* ? "Cannot access 'MyType' before initialization", so we need to execute the function in the handler, | ||
* ? when the classes are already created. To overcome this, we use a wrapper: | ||
* ? - the lazyType is executed in the handler so we never have a JS error | ||
* ? - the eagerType is checked when decorator is running and an error is raised if an unknown type is encountered | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this documentation important? Should it be replaced with something more relevant, which details how type wrappers currently work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few more ideas
7a216b8
to
68c1a15
Compare
Goodbye to a pretty useless function. It was handy for testing extractable type-wrappers, but not for anything else. It has also been removed from the index.
Welcome the newest member to the contrib family: LazyRefHost! This is basically the Lazy function, but... it's actually lazy. It performs very similarly to TransientRefHost, though it operates on lazy references instead of transient ones.
Make the ID property of the DependencyDescriptor interface optional, to support cases wherein the eagerType of a TypeWrapper is not present.
...as opposed to playing double-duty for both TypeWrapper changes and Lazy.
68c1a15
to
c8889a9
Compare
Replace the useless
Lazy
function with a newLazyRef
function in contrib.Unlike
Lazy
, theLazyRef
function is actually...well, lazy -- it creates a reference host, much like
TransientRefHost
, which allows for creatingan instance of the given reference at any time.