Instances and lifecycle of a widget


first of all congratulations to this great tooling and thanks to Alex from Typefox for the good training he gave us.

I have a question regarding the lifecycle and instances of widgets.
I have implemented a new custom widget (inspired by the getting-started widget) and contribute it like that:

bindViewContribution(bind, EventViewContribution);
bind(WidgetFactory).toDynamicValue(context => ({
    id: EventViewWidget.ID,
    createWidget: () => context.container.get<EventViewWidget>(EventViewWidget)

So far, so good. It is coming up and shown after startup as expected.
But when I close the widget and re-open it, obviously a NEW instance is created.

Since the binding was set to “inSingletonScope()” I would have expected, that there is only instance over the complete lifecycle.

Did I misunderstood the concept ?


[original thread by surrender70]

[Lucas Koehler]

While you registered the widget factory in singleton scope, you didn’t do that with the widget itself. Consequently, every call of createWidget creates a new instance of EventViewWidget.

To fix this, you need to register the widget itself as a singleton like this: bind(EventViewWidget).toSelf().inSingletonScope();

How do you create new instance?

If you only use APIs of AbstractViewContribution then it should not be possible to get different instances regardless of inSingletonScope, since WidgetManager ensures that there is only one instance.

I assume you create a widget around your contribution somehow and get another instance. Please don’t do it, otherwise WidgetManager won’t know about it and it won’t play nicely with the shell, for instance it won’t be restored properly on restart.

He is a presentation on widget lifecycle and contracts:


Thanks for the link to the presentation. That is very helpful.