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

[RFC] useStatedBean with scope #27

Closed
foreleven opened this issue Aug 15, 2019 · 3 comments · Fixed by #30
Closed

[RFC] useStatedBean with scope #27

foreleven opened this issue Aug 15, 2019 · 3 comments · Fixed by #30
Labels
feature_request RFC Request for comments
Milestone

Comments

@foreleven
Copy link
Collaborator

useStatedBean(Model) will find the model from the provider in the context, and will report an error if it does not exist. This feels unreasonable. It should be possible to temporarily create an instance of the Model whose lifecycle is consistent with the current component.

useStatedBean(Model, Scope?)

proposed scopes:

  • Default: if existed in Context then Request
  • Request: temporarily create an instance whose lifecycle is consistent with the current component.
  • Context: find from the provider in the context
@issue-label-bot
Copy link

Issue-Label Bot is automatically applying the label feature_request to this issue, with a confidence of 0.58. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

@foreleven foreleven added this to the 1.0 milestone Aug 15, 2019
@JounQin
Copy link
Member

JounQin commented Aug 15, 2019

temporarily create an instance

How will you implement it, If the Model has other dependencies?

It it doesn't have any dependence, why not just useState(() => new Model())

So I think we should still resolve Model via DI, but we need find a way to register Models dynamicly.

@foreleven
Copy link
Collaborator Author

foreleven commented Aug 15, 2019

So I think we should still resolve Model via DI, but we need find a way to register Models dynamicly.

totally agree.

Currently, StatedBeanContainer caches the instance retrieved from the BeanFactory, and then useStatedBean got the cached object from the container in the context.

Now, how to create the container in useStatedBean when with the Request scope?

temporarily store the container with useState

interface UseStatedBeanOptions {
  dependentFields?: Array<string | symbol>;
  scope?: Scope
}

export function useStatedBean<T extends ClassType>(
  type: T,
  options?: UseStatedBeanOptions,
): InstanceType<T> {
  useState(() => {
    let container
    if (options.scope === Scope.Request) {
       container = new StatedBeanContainer(...);
    } else {
       // get container from context
    }
    return container;
   });
   ...  
}

@JounQin JounQin added the RFC Request for comments label Aug 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature_request RFC Request for comments
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants