-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Make it possible to export container dependant variables #562
Conversation
See: #517 |
c720ad4
to
da55bfc
Compare
I don't think we should generalize it, a case with the root logger is special. It would be nice to keep the module interface simple. Also the first thing to access from the root container must be an application, otherwise the root container is not considered complete, meaning that other extensions could override some services require for logging after loading the loader module. Is there a reason not to do in |
I agree with Anton, I think it should just be a side-effect in container's load. |
Ha yes indeed onActivation is what I need, thanks! |
da55bfc
to
d214726
Compare
The latest change is WIP , I don't understand how to make this global var work, ideas ? |
|
@svenefftinge but if I do that the application will need to depend on the logger it works for core but it would not work for another package. Also independenly of that do you have an idea for the gobal export ? |
Note we will want do that with the localize package also... |
I think you would need to rebind the FrontendApplication within the logger container module and add an onActivation hook. But I'm not so happy with that, too. Could you outline why we need that for the localization? Maybe with two cases, it is worth to introduce a callback as you did in the first PR. :) |
In the case of the localization we want to make it easy to localize a string anywhere in theia. So you could call localize("mystring"); directly from anywhere without injecting the MessageTranslation class (localize is exported from that class). But we need the MessageTranslation class in the inversify context so that it can access the user preferences to select the proper language. So we need to bridge the gap between the inversify context and static context same as with the logger. |
Actually thinking about this now localize can't depend on preferences since preferences depend on too much stuff... I'm not sure how to structure this properly yet it seems :( |
Actually for localization I think we could have a mock preferences class until we get the real preferences and then rebind those. So it would work properly once you get the whole application but otherwise use a failover. |
I thought we were doing this not for convenience, but because we need to log in a static context? |
adding injection for logger and localize should be the default. For localize we should try to avoid doing this global scope hack. |
For localize it would mean basically every class would have a localize injected given that you would want to localize log messages too possibly. That's what I'm trying to avoid. |
Note the logger is pretty much as oblivious |
I think we should only localize user-facing messages. logs are for developers. |
Maybe that restrains the places where it's used enought... |
Why should it be global, is not possible to use the module import? |
BTW what about an idea to rebind console functions instead of exposing our logger? |
For localize it has to be gobal to do like this: See:
|
For the logger there is less reason to be global at the moment since I don't have a static context to call it from like I had before. But I wanted to do the logger as an example for localize to follow and provide a general way to do this when need be for anything. |
Ok, do we need localize in the container, then? |
I guess you want to use server-side preferences for the locale? |
Not sure what you mean ?
Yes that's the plan and this is another issue since core has strings to localize but if localize depends on preferences and preferences depend on core/filesystem/workspace it's a problem... but let's tackle that later. For now I'm trying to figureout out how an extension can contribute a global to be able to call something in an inversify context from static context. |
Why should it be global? Globals are generally discouraged to avoid namespace conflicts. Why not use the module import as you already do for import { logger } from '@theia/core/common/logger';
logger.log('lalala'); |
or |
Actually the problem was that doing import { logger } from '@theia/core/lib/common'; Gives: var common_1 = require("@theia/core/lib/common");
common_1.logger.info("Starting terminal process: " + options.command + "," And here logger is undefined However doing: import { logger } from '@theia/core/lib/common/logger'; Gives: var logger_1 = require("@theia/core/lib/common/logger");
logger_1.logger.info("Starting terminal process: " + options.command + "," And this is OK. So the reexporting in the index is the issue, not sure why ... ideas? |
because of it again? basically, reexport creates a new module and reassigns properties from reexported modules, it happens only once |
What if you just reexport functions, like import * as logger from '@theia/core/lib/common/logger';
logger.info('blabla'); or import { info } from '@theia/core';
info('blabla'); What with the idea to rebind |
So looking at index.js: function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./types"));
` So yes this makes an new export object so here it's taken by value and can't be changed. So I'm just going to directly import it for this case and use: import { logger } from '@theia/core/lib/common/logger';
logger.info('foo'); |
Are you ok with adding this, given the issue with the localization I exposed ? |
It somehow feels like we are going down the wrong route here, but I don't see alternatives :-| |
But if you want to move on with the logger, I'm fine with adding the rebind for now. |
I agree, however at lest the initialze thing will be quite self contained and generic.
Yes I tought about that it's just that, I wonder if preferences could have a local storage backend ? so we could use the same API if the filesystem is not present?
Thanks I'll repost the original PR |
d214726
to
c720ad4
Compare
@svenefftinge back to to originial proposal... is it OK ? |
It is ok, although if we go with the generic callback variant I would prefer using DI and our |
That sounds like a good idea, I'll give it a try. |
This patch introduces the initialize method in Frontend/BackendApplicationContribution this is called as the first thing the application does before onStart or configure. This enables the logger module to export a logger object fetched via container.get<ILogger>(ILogger) such that this new logger object can be exported to code that does not use injection. Signed-off-by: Antoine Tremblay <antoine.tremblay@ericsson.com>
c720ad4
to
ee96767
Compare
@svenefftinge reworked that with bindContributionProvider... looks better ? |
@svenefftinge I'd like to merge this to enable localization to go forward and use this too.. any objections? |
Could we reuse |
@akosyakov no since we can't be certain of the order of the contribution... if another contribution is loaded before and expects things to be initialzed it will fail :( Unless there's a way to put some priority there.. but I'd like to avoid depending on the order the modules are loaded. |
In which cases? Do you mean other contributions want to use logger? I thought they should inject an instance of Or add |
Yes but if we give the option that they don't have to inject this, it has to work in all cases. True I could add initialize to the I'll refactor as such. |
c102fbc
to
dacdd68
Compare
@akosyakov OK with recent changes ? |
|
||
for (const contribution of this.contributionsProvider.getContributions()) { | ||
if (contribution.initialize) { | ||
contribution.initialize(); |
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 be done separately, but we should also wrap calls in try-catch in the backend
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'm adding this to this PR after all.. since it's quite trivial
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.
LGTM
Signed-off-by: Antoine Tremblay <antoine.tremblay@ericsson.com>
This patch introduces the initialize function inside a module, this
function is called with the root container of the application at
application startup.
This enables the logger module to export a logger object fetched via
container.get(ILogger) such that this new logger object can be
exported to code that does not use injection.
Signed-off-by: Antoine Tremblay antoine.tremblay@ericsson.com