Repository navigation
Conversation
|
This is just... so great. I could remove so much boilerplate with this. |
c6ac92e to
282045b
Compare
|
It would be great to have an |
b5fbdaa to
77cece8
Compare
|
I'm tackling view_compiler's behaviors. Currently, a component which has neither I think it's needed that a metadata to enable |
const enum LifecycleEvent {
OnInit,
OnChanges ,
...
}
class ComponentLifecycle {
private subject = new Subject<LifecycleEvent>();
events = this.subject.asObservable();
private getLifecycleEvent(eventType: LifecycleEvent){
return this.subject.filter(v => v === eventType).mapTo(true);
}
// utility observable, probably better to make them lazy so we don't build them if not needed
// a getter should do
onInit = getLifecycleEvent(LifecycleEvent.OnInit);
onChanges = getLifecycleEvent(LifecycleEvent.OnChanges);
}and then once we get forInput(inputName: string) {
return this.onChanges.pluck(inputName).filter(v => v !== undefined);
}edit: We'll need something more complex than just number for the subject since we need to emit the changes for onChanges, but that's the idea. |
|
@tbosch I'd like to hear how do you think this approach. Could I do this, avoiding to touch compiler codes? |
77cece8 to
96c4946
Compare
|
let me think about this... |
Why would it? I think the proposal should mirror existing lifecycle hooks where you get a method per hook rather than a single method that takes an argument corresponding to destroy/init etc. You can always easily combine observables with various operators and ideally each observable will have a single typed parameter (change lifecycle needs to be passed the changes, the rest are all void). |
That comment what kinda off topic, I did it after viewing how Especially considering that for this exact feature I really think we should change the internals. Basically if we have stream events, adding a lifecycle function on a component would just be sugar for 'subscribe to that event and use that function as callback'. function subscribeToEvents(component, lifecycle) {
for (let event of ['OnInit', 'OnChanges', etc...]) {
const fnName = 'ng' + event;
const eventCallback = component[fnName];
if (typeof eventCallback === 'function') {
const streamName = event +'$';
lifecycle[streamName].subscribe( eventCallback.bind(component) );
}
}
}
|
|
@ohjames Well we already had this debate on a different issue but in short :
|
|
|
Could a single observable be used though with a simpler API that uses a consistent payload as follows:
Then you could |
|
Hm I really think that taking advantage of the things that observables provide to produce a lifecycle API would be much better than taking the plain old life cycle methods and converting them to the world of observables. For example the I really think that the I want this feature so badly but I really think this implementation gets too much inspiration from the existing lifecycle events without taking advantage of what rxjs offers. |
|
so I am for something like this, but I don't want to do this as one off just for |
|
it seems that this feature just part of that the rxjs deep integrated with angular. remind me this have wants to see any genius idea, or real case requirements? can't modify template free of hand, just data definition. I don't know using high level abstract downgrade to template file, or just data transform and binding data with stream. |
|
@LinboLen This wont be usable anytime soon |
The author changed the code to provide all events rather than just init. I don't think this is suitable though:
So I'd really like to see a single changes stream, no init stream and no destroy stream, but I'm not sure about how to handle the other lifecycle events, maybe those really do warrant extra observables. |
|
Hi @lacolaco! This PR has merge conflicts due to recent upstream merges. |
|
This seems like a very cool idea. Here is an optimization question:
|
|
@ThomasBurleson or just provide the changes observable, take the first stream element ( |
|
@lacolaco Is the PR is relevant? |
|
Good news is that we most definitely want this feature! The bad news is that we don't want to add it to ViewEngine and than have to reimplement in Ivy because the API will be different in Ivy. So for now I am going to close this PR and we will add this feature to Ivy. |
|
Thank you. I'm looking forward to that! |
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Close #10185
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
This feature is inspired by #10185, "Provide Component Lifecycle Hooks as Observables".
New
ComponentLifecyclecan bring many benefits. For example...initializedflag without local boolean property (more async friendly).takeUntiloperator (to unsubscribe automatically).This feature doesn't affect any current behaviors. It's just a new API.
Does this PR introduce a breaking change?
Other information