Skip to content
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

[Proposal] Resolve injections in properties #2

Open
makamekm opened this issue Apr 2, 2019 · 1 comment
Open

[Proposal] Resolve injections in properties #2

makamekm opened this issue Apr 2, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@makamekm
Copy link

makamekm commented Apr 2, 2019

Hi. I have a proposal. There is an example:

export class Config {
    public url = "https://google.com";
}

export class SubService {
    @inject public configModel: ConfigModel;
}

export class MainService {
    public prop = new SubService();
    public props = [new SubService(), new SubService()];
}

@provider(
    Config,
    MainService,
)
export class Html extends React.Component {
    @inject private service: MainService;
}
@gnaeus
Copy link
Owner

gnaeus commented Jul 29, 2019

Your example already works with inject() function syntax (without decorators):

class Config {
  url = "https://google.com";
}

class SubService {
  configModel = inject(this, Config)
}

class MainService {
  prop = new SubService();
  props = [new SubService(), new SubService()];
}

class Html extends React.Component {
  static contextType = InjectorContext;

  service = inject(this, MainService);

  render() {
    html = this;
    return <div />;
  }
}
let html: Html;

const App = provider(Config, MainService)(Html);

render(<App />, document.createElement("div"));
expect(html.service.prop.configModel.url).toBe("https://google.com");
expect(html.service.props[0].configModel).toBe(html.service.prop.configModel);
expect(html.service.props[1].configModel).toBe(html.service.prop.configModel);

But with decorators it can't work. Because @inject doesn't immediately write dependency to decorated property, but rather it define a lazy property getter that resolves dependency on demand. And it is conceptual limitation of TypeScript decorators.

@gnaeus gnaeus added the enhancement New feature or request label Jul 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants