-
Notifications
You must be signed in to change notification settings - Fork 535
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
Expose .is() typeguard on SharedObjectKind #21850
Conversation
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.
I like this. Its discoverable, and an easy change given that all instanceof uses already have the shared object type. curious how @CraigMacomber feels about having an implementation leak through the type erasure like this, and if he foresees any problems from that
⯅ @fluid-example/bundle-size-tests: +708 Bytes
Baseline commit: 957b194 |
I don't think this is leaking any implementation info: nothing in the exposed API says how it works (well the remarks comment does a bit, but I think thats ok as its documenting historical things not usage contracts) One of the reasons I created SharedObjectKind was to be able to add things like this to all of them in a consistent way easily. Since the interface is sealed, we are allowed to add to it like this as a non breaking change. I don't see any problem supporting this going forward. either, so I think its a good change. |
Co-authored-by: Craig Macomber (Microsoft) <[email protected]>
## Description With the removal of concrete DDS classes, one pattern that's come up repeatedly is customer code which previously checked `instanceof MySharedObject` (usually when the code supported multiple shared object types for whatever reason), which no longer works. This change adds a drop-in replacement to the public API surface. ## Breaking Changes As `SharedObjectKind` was marked sealed, this is non-breaking. ## Alternatives Considered We could expose free functions in each package easily e.g. by using a helper like this: ```typescript export function createSharedObjectTypeguard<TSharedObject>( kind: ISharedObjectKind<TSharedObject>, ): (loadable: IFluidLoadable) => loadable is IFluidLoadable & TSharedObject { const factoryType = kind.getFactory().type; return (loadable: IFluidLoadable): loadable is IFluidLoadable & TSharedObject => { return isChannel(loadable) && loadable.attributes.type === factoryType; }; } ``` Ultimately this will be more code though and arguably less discoverable. We could also add back support for `instanceof` using `Symbol.hasInstance` (and the same implementation as `.is`), but due to microsoft/TypeScript#56536, this won't work for customers using TS below 5.5, so we'll need something else anyway at least for now. --------- Co-authored-by: Abram Sanderson <[email protected]> Co-authored-by: Craig Macomber (Microsoft) <[email protected]>
Description
With the removal of concrete DDS classes, one pattern that's come up repeatedly is customer code which previously checked
instanceof MySharedObject
(usually when the code supported multiple shared object types for whatever reason), which no longer works. This change adds a drop-in replacement to the public API surface.Breaking Changes
As
SharedObjectKind
was marked sealed, this is non-breaking.Alternatives Considered
We could expose free functions in each package easily e.g. by using a helper like this:
Ultimately this will be more code though and arguably less discoverable.
We could also add back support for
instanceof
usingSymbol.hasInstance
(and the same implementation as.is
), but due to microsoft/TypeScript#56536, this won't work for customers using TS below 5.5, so we'll need something else anyway at least for now.