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

Injections: callbacks should have option to be memoized #60

Open
joshjung opened this issue Feb 9, 2017 · 0 comments
Open

Injections: callbacks should have option to be memoized #60

joshjung opened this issue Feb 9, 2017 · 0 comments

Comments

@joshjung
Copy link
Collaborator

joshjung commented Feb 9, 2017

Example code:

export default class FormBaseController extends Controller {
  constructor() {
    super();

    this.injections.formModel = () => {
      this.modelWatcher.find(FormBaseModel);
    };
  }
}

Note that in this case every single time an injection runs, the injector will recompute (find) the FormBaseModel - which might be costly. In some cases this might be required, but I think that we should have a rule that if the injection callback returns a value, then that value that is returned is memoized (cached) for next time the injection is evaluated rather than calling the callback all over again.

Target Code:

export default class FormBaseController extends Controller {
  constructor() {
    super();

    // Our callback will only be called once, and then after that it will reuse the returned value.
    this.injections.formModel = () => this.modelWatcher.find(FormBaseModel);
  }
}

However, if the injection property (formModel in this case) is ever overwritten with a new function or a new value, then the memoized value needs to be discarded so we will have to watch for changes to the injection value (in this case formModel)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant