Removing Id from Component Creation
Removing Id from Component Creation
Goal
- Restrict the ability to create a Fluid Component with a given id.
- Components should be holding Component Handles and not unique ids to components.
Overview
Currently Component Developers are free to choose the id of component they are creating. This has been historically valuable because you would then use these ids to request the component from the container with a relative path.
The pattern looks something like ...
Create Component
const clickerRuntime = await this.context.createComponent("unique-component-id", "clicker");
clickerRuntime.attach();
Get Component
this.context.hostRuntime.request({ url:"unique-component-id" });
The need for direct referencing based on ids has since changed with the introduction of Component Handles. Components can expose themselves as a handle that is serializable into a Distributed Data Structure (DDS). This means that you can create a new component object and store it on a DDS, such as a SharedMap. Component Handles work in the same way as DDS Handles. When you store the Component Handle on the DDS, the component will attach when the DDS is attached. If the DDS is already attached the component will attach right away.
In aqueduct the pattern looks something like …
Create Component
const clicker = this.createAndAttachComponent("clicker");
this.root.set("my-clicker", clicker.handle);
Get Component
const clicker1 = await this.root.get<IComponentHandle>("my-clicker").get<IComponent>();
Id’s will not completely go away because there are still some scenarios where we need them. The primary example for this is creating the default component. Because we don’t have state in the instantiateRuntime call we don’t have a way to store the id of the component being generated. This should, however, be the exception not the rule.
Why do we want to remove Id’s
We want to do this as a step towards garbage collection of components.
Id based routing has been pivotal to the success of the framework so far. The pattern of every id can be crafted into a relative path has allowed for easy component composition. The problem with this pattern is that if every component can be reached by anyone who knows of the id, you have to assume everyone has a reference to the component. This means that the runtime does not have the context to confidently remove the component. This is why ComponentRuntime objects cannot be cleaned up.
If we make the statement that every component will hold only handles to other components we can walk the tree of DDSs to see if there are any loose components that no one is referencing. This will enable garbage collection.
What won’t change
- Components will still have ids but they will be generated by the runtime. These ids will still be exposed and accessible on IComponentLoadable.
- Component routing via request will still be functional
Bohemia Fallout
Changing to handle based component resolution from requesting the component breaks one of the flows bohemia is currently depending on. Bohemia as a Container wants the ability to know when components have been created/loaded in the Container. They want to know this so they can execute some container level actions. The current examples is that they want to log when the component is created/loaded and they want to register a component with registro when the component is loaded.
To resolve this we'll add a componentCreated and componentLoaded event to the ContainerRuntime that the Container developer can listen on.
Work Items
- Remove id from ComponentContext create
- Remove id from SharedComponent createAndAttachComponent call
- Update ContainerRuntime and IHostRuntime create calls to be distinct.
- Something like:
createComponentandcreateComponentWithIdDiscouraged
- Something like:
- Update examples to store handles and not ids.
- Update bohemia components to use handles instead of ids.
- path/url in
IComponentLoadableshould be readonly - Expose events on the ContainerRuntime to notify when a component has been created/loaded.
These ids will still be exposed and accessible on IComponentLoadable.
I assume in a read-only way? Or is that how they behave today? They're set at creation time then become read-only?
Id’s will not completely go away because there are still some scenarios where we need them. The primary example for this is creating the default component. Because we don’t have state in the instantiateRuntime call we don’t have a way to store the id of the component being generated. This should, however, be the exception not the rule.
Are you saying it'll still be possible for people to "shoot themselves in the foot," so to speak?
@tylerbutler This is a progression towards removing component ids as a user concept. They will still always exist as a underlying fluid runtime concept because we use them for routing. We are trying to push towards a world where no one cares about them.
IComponentLoadable should only expose a readonly url (id). Apparently looking at the interface that is not true so I've added it as a line item above. The reason I called this out is because if for some reason you scenario still needs you to have the id of the component you've created you can do something like below. We are highly discouraging this but also realize that there might be holes we need to fill.
const loadable = component.IComponentLoadable
if(loadable) {
const componentId = loadable.url;
}
For the second part I'm not trying to say that we are offering people a way to shoot themselves but I'm saying that there are select scenarios currently that require us to know the id of the thing we are getting. The primary example for this is the default component (and in bohemia case singletons). This is because the instantiateRuntime(...) call which creates the default component doesn't have a place to save state/handles so it has no way (currently) to store the default.
There a different ways to solve the second scenario but that is a future problem and my goal in calling it out is to provide comfort to people who will quickly jump to these scenarios that we don't have a good solution to.
This is a progression towards removing component ids as a user concept.
That's what I thought; this is great and makes a lot of sense. I got confused about the long term direction vs. this step towards it. I read it as, "you shouldn't need to think about component IDs ever, except oh but really you will." But with your clarifications I get it.
We are highly discouraging this but also realize that there might be holes we need to fill.
Got it. This is one "escape hatch" built into the design. Any ideas for how we discourage it?
There a different ways to solve the second scenario but that is a future problem and my goal in calling it out is to provide comfort to people who will quickly jump to these scenarios that we don't have a good solution to.
Makes sense.
Expose events on the ContainerRuntime to notify when a component has been created/loaded. This will allow Container developers to plug into events and perform actions.
How does this relate to removing ids?
Expose events on the ContainerRuntime to notify when a component has been created/loaded. This will allow Container developers to plug into events and perform actions.
How does this relate to removing ids?
That is a great question. Bohemia currently uses the requestHandler flow to intercept component gets and do work at the container level. Mainly they register components with registro and they log that the component has been opened.
Having handles breaks this flow because they can't directly intercept component calls. Alternatively I don't think we want them doing that. Exposing the ability for the Container dev to know when load/creates happen so they can act seems like a reasonable ask.
I think there is an interesting pattern here where a container can have context about what it provides and can automatically subscribe components without the components having to do checks.
We are highly discouraging this but also realize that there might be holes we need to fill.
Got it. This is one "escape hatch" built into the design. Any ideas for how we discourage it?
My original thoughts are to have two calls on the ContainerRuntime. One that is the createComponent and one that is createComponentWithIdDiscouraged. To make it very intentional that if you are calling this you should know what you're doing.
My original thoughts are to have two calls on the ContainerRuntime. One that is the
createComponentand one that iscreateComponentWithIdDiscouraged. To make it very intentional that if you are calling this you should know what you're doing.
I like this approach. I suggest being more explicit with the method name, taking a cue from React: how about UNSAFE_createComponentWithId?
Closing this issue as all the work items from here are now being tracked in this project (https://github.com/microsoft/FluidFramework/projects/23)