It works, but I was thinking about creating something more generic.
When trying to write a simple event service, I figured out that a
solution with event listeners could lead to leaks of event listeners
and its scopes. Controllers come and go, any controller could register
a listener in the service. Once the controller is gone - the event
service would still hold the reference, so each controller would have
to de-register its listener - which would be hard to achieve.
I was thinking about how does scope.$watch work - it registers a
listener, but that listener lives no longer than the scope itself -
that was very clever idea and I was thinking about similar solution
for the event bus service.
Do you have any suggestions?
Thanks,
Witold Szczerba
I had a simular issue implementing polling in one of our pages. The
page kept polling even after the user navigated to another partial.
Solved that by looking at the $location between each poll, which is
kind of a hack. Thinking about creating a general polling/scheduling
service but haven't come around to it yet... It would be useful to
have a hook when the scope dies...
/mårten
2011/6/23 rur <flui...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "angular" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to angular+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/angular?hl=en.
>
>
+1 from me as well :-)
I had a simular issue implementing polling in one of our pages. The
page kept polling even after the user navigated to another partial.
Solved that by looking at the $location between each poll, which is
kind of a hack. Thinking about creating a general polling/scheduling
service but haven't come around to it yet... It would be useful to
have a hook when the scope dies...
/mårten
Have you found the solution to the problem?
Regards,
Witold Szczerba
P.S.
Is there something like java.lang.ref.WeakReference in JavaScript? If
so, would that be enough to get rid of the problem? What if unused
scope has not been garbage collected yet, but it still consumes
events, eating processing resources like a candies?
On 23 June 2011 16:44, Igor Minar <iim...@gmail.com> wrote:
> We have many other use-cases for some sort of an event bus that would work
> in cooperation with scopes.
> And are thinking how could we go about implementing it. It will likely be
> some kind of a scope api, so you can do:
> scope.on('someEvent', function() { doSomething() });
> and this listener would be active only as long as the scope on which it was
> defined is still active.
> There are many details that we haven't sorted out yet, event propagation
> through scopes for example, so don't expect this in the next release just
> yet :)
> /i
>
>
>
> On Thu, Jun 23, 2011 at 7:35 AM, Mårten Dolk <marte...@gmail.com> wrote:
>>
>> +1 from me as well :-)
>>
>> I had a simular issue implementing polling in one of our pages. The
>> page kept polling even after the user navigated to another partial.
>> Solved that by looking at the $location between each poll, which is
>> kind of a hack. Thinking about creating a general polling/scheduling
>> service but haven't come around to it yet... It would be useful to
>> have a hook when the scope dies...
>>
>> /mårten
>>
>> 2011/6/23 rur <flui...@gmail.com>:
Implementing the event subscription as part of scopes' built-in
feature seems to be good approach - it resolves the problem of leaking
listeners, because they go away together with theirs scope, so there
is no problem with leaks. However, on the other side, there are the
publishers, firing events - how is the publishing mechanism know which
scopes are to be notified? Once we register them - the leaking
scopes/listeners problem is back :/
Have you found the solution to the problem?
Regards,
Witold Szczerba
P.S.
Is there something like java.lang.ref.WeakReference in JavaScript? If
Sorry yes , 'up to' :)
In the mean time, a event bus could be written as a service and
therefore be both a singleton and injected where needed.
As I see this would require either one of two things: services/
controllers subscribing to the event bus would need to have a
distructor method in which they handle unsubscribing themselves, or,
even better, if the the service somehow got notified when a controller
was removed it could unsubscribe it automatically!
Is any of this possible within reason?
Thanks,
rur
On 26 June 2011 01:36, Igor Minar <iim...@gmail.com> wrote:
> There is no way for a controller know that it's about to be killed, so there
> is no way to manually or automatically unsubscribe from the event bus
> service.
> that's why I think that the event bus has to be tightly integrated with
> scopes and can't be just a simple service (unless you want to use it only
> from other services).
> /i
>
>
First one is just a counter which gets incremented every time event
fires, so all the scopes can watch this property. During fire
evaluation cycle, the payload and event name are temporarily stored in
root scope. They do not need to be exposed, might be stored in some
enclosed local vars as well.
- scope.$fire(eventName, payload); looks like this:
//pseudo code
this.$fire = function(eventName, payload) {
var root = ...//root scope
try {
root.$eventPayload = payload; //no need to store it here
root/$eventName = eventName; //same as above
root.$eventTrigger += 1;
root.$eval();
} finally {
root.$eventPayload = null;
}
};
- scope.$on('evtName', function() {callback...;}); looks this:
//pseudo code:
this.$on = function(eventName, callback) {
var payload = self.$root.eventPayload; //or from somewhere else
self.$watch('$eventTrigger', function() {
if (eventName === self.$root.eventPayload) {
callback(self.$root.$eventPyload);
}
}
}
So, the user would just call scope.$on(...) to register an event
listener (in other words to subscribe) and scope.$fire(...) to fire
event (or in other words to publish an event).
What naming convention do you prefer? Register listener and fire an
event or publish-subscribe?
Do you think it might be better OK to add an option to use regexp to
match event name as an addition to simple string checking?
I would also make the event name optional, in that case it would be
the responsibility of callback to decide if it is interested or not.
Callback parameter would have to include the eventName as well. I
think it might be OK for en event callback function to receive exactly
the same parameters used in $fire instead of single payload object,
hm?
What do you think? If this looks OK to you, I could try to follow the:
http://docs.angularjs.org/#!/misc/contribute
guide to submit a pull request.
Thanks,
Witold Szczerba
So, what do you think about this:
- root scope gets extra properties, e.g.:
$eventTrigger = 0; $eventPayload = null; $eventName = null;
First one is just a counter which gets incremented every time event
fires, so all the scopes can watch this property. During fire
evaluation cycle, the payload and event name are temporarily stored in
root scope. They do not need to be exposed, might be stored in some
enclosed local vars as well.
- scope.$fire(eventName, payload); looks like this:
//pseudo code
this.$fire = function(eventName, payload) {
var root = ...//root scope
try {
root.$eventPayload = payload; //no need to store it here
root/$eventName = eventName; //same as above
root.$eventTrigger += 1;
root.$eval();
} finally {
root.$eventPayload = null;
}
};
- scope.$on('evtName', function() {callback...;}); looks this:
//pseudo code:
this.$on = function(eventName, callback) {
var payload = self.$root.eventPayload; //or from somewhere else
self.$watch('$eventTrigger', function() {
if (eventName === self.$root.eventPayload) {
callback(self.$root.$eventPyload);
}
}
}
So, the user would just call scope.$on(...) to register an event
listener (in other words to subscribe) and scope.$fire(...) to fire
event (or in other words to publish an event).
What naming convention do you prefer? Register listener and fire an
event or publish-subscribe?
Do you think it might be better OK to add an option to use regexp to
match event name as an addition to simple string checking?
I would also make the event name optional, in that case it would be
the responsibility of callback to decide if it is interested or not.
Callback parameter would have to include the eventName as well. I
think it might be OK for en event callback function to receive exactly
the same parameters used in $fire instead of single payload object,
hm?
What do you think? If this looks OK to you, I could try to follow the:
http://docs.angularjs.org/#!/misc/contribute
guide to submit a pull request.
Thanks,
Witold Szczerba