observer-pattern

php observer pattern to log user out when session times out

前提是你 提交于 2019-11-30 05:05:19
问题 I'm trying to log users out when the user's session timeout happens. Logging users out - in my case - requires modifying the user's "online" status in a database. I was thinking that I might be able to use the observer pattern to make something that would monitor the state of the user session and trigger a callback when the session expires - which would preserve the user's name so we can update the db. I'm not exactly sure where to begin on the session side. Can I tie a callback to the

Scala Listener/Observer

梦想的初衷 提交于 2019-11-30 03:31:53
Typically, in Java, when I've got an object who's providing some sort of notification to other objects, I'll employ the Listener/Observer pattern. Is there a more Scala-like way to do this? Should I be using this pattern in Scala, or is there something else baked into the language I should be taking advantage of? Alex Cruise You can still accumulate a list of callbacks, but you can just make them functions instead of having to come up with yet another single method interface. e.g. case class MyEvent(...) object Foo { var listeners: List[MyEvent => ()] = Nil def listen(listener: MyEvent => ())

Reactive Extensions for .NET (Rx) in WPF - MVVM

馋奶兔 提交于 2019-11-30 02:09:46
I am using Reactive extensions for NET (Rx) with Caliburn.Micro in my WPF app. I'm trying to port my WPF app to use an MVVM architecture and I need to monitor changes in the Text property of a TextBox control. If the last change of the Text property was more than 3 seconds ago I need to call the LoadUser method of a service. Porting the logic from my old solution to the new solution with MVVM architecture. OLD XAML: <TextBox Name="Nick" Grid.Row="0" FontSize="14" Margin="2,2,2,2" HorizontalAlignment="Stretch" TextChanged="Nick_TextChanged" /> In code behind I have this: ... Observable

How would you test observers with rSpec in a Ruby on Rails application?

和自甴很熟 提交于 2019-11-29 20:35:57
Suppose you have an ActiveRecord::Observer in one of your Ruby on Rails applications - how do you test this observer with rSpec? Pete You are on the right track, but I have run into a number of frustrating unexpected message errors when using rSpec, observers, and mock objects. When I am spec testing my model, I don't want to have to handle observer behavior in my message expectations. In your example, there isn't a really good way to spec "set_status" on the model without knowledge of what the observer is going to do to it. Therefore, I like to use the "No Peeping Toms" plugin. Given your

C#: events or an observer interface? Pros/cons?

对着背影说爱祢 提交于 2019-11-29 20:25:16
I've got the following (simplified): interface IFindFilesObserver { void OnFoundFile(FileInfo fileInfo); void OnFoundDirectory(DirectoryInfo directoryInfo); } class FindFiles { IFindFilesObserver _observer; // ... } ...and I'm conflicted. This is basically what I would have written in C++, but C# has events. Should I change the code to use events, or should I leave it alone? What are the advantages or disadvantages of events over a traditional observer interface? Consider an event to be a callback interface where the interface has only one method. Only hook events you need With events you only

Rx for .NET - What happened to Scheduler.Dispatcher?

雨燕双飞 提交于 2019-11-29 16:06:27
问题 I'm trying to work through Dan Sullivan's Rx Extensions training course on PluralSight. It's excellent stuff but unfortunately Rx seems to have already been changed, even though the course was only published a month ago. Most of the changes are trivial to work out (change from three dlls to a single dll, change in namespaces used etc) but I'm struggling to understand what I should use in place of Scheduler.Dispatcher in Dan's example. I can't see anything obvious in the properties that are

Simple, clean way to sync observables from different view models

 ̄綄美尐妖づ 提交于 2019-11-29 15:32:43
问题 Say I have two view models that each have an observable property that represents different, but similar data. function site1Model(username) { this.username = ko.observable(username); .... } function site2Model(username) = { this.username = ko.observable(username); .... } These view models are independent and not necessarily linked to each other, but in some cases, a third view model creates a link between them. function site3Model(username) = { this.site1 = new site1Model(username); this

vector of pointer to object - how to avoid memory leak?

◇◆丶佛笑我妖孽 提交于 2019-11-29 12:54:42
How do we ususaly deal with a vector whose elements are pointers to object? My specific question is the comment at the end of the code supplied below. Thanks. class A { public: virtual int play() = 0 ; }; class B : public A { public: int play() {cout << "play in B " << endl;}; }; class C : public A { public: int play() {cout << "play in C " << endl;}; }; int main() { vector<A *> l; l.push_back(new B()); l.push_back(new C()); for(int i = 0 ; i < l.size();i++) { l[i]->play(); } //Do i have to do this to avoid memory leak? It is akward. Any better way to do this? for(int i = 0 ; i < l.size();i++)

Android custom listener for an event

旧城冷巷雨未停 提交于 2019-11-29 12:37:04
I'm trying to fire an event when an integer value is updated, but it's failing. Here's the code I'm using: Declaring The Custom Listener public class fieldactivity extends AppCompatActivity implements View.OnClickListener { OnModeUpdate modeupdate; //Create custom listener for mode update int mode = 1; Mode Update Code protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fieldsignals); Button button = (Button) findViewById(R.id.mode_rotate_button); button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v

MVC Java: How does a Controller set listeners to the children classes of a View

大城市里の小女人 提交于 2019-11-29 03:10:31
问题 I have a Controller, and a View with many children views with children with children. Example: JPanels within JPanels that have buttons and fields for a controller to pass to the model. The current way I'm doing it is instantiating 'Controllers' in the view that have action listeners and access my models which are singletons. This works- But it's definitely not MVC. So the question is- how do I do it? Is the only way to daisy chain from the controller: mainview.getSubView().getSubView()