0

I have the following Comet server:

object UserServer extends LiftActor with ListenerManager {

    private var users: List[UserItem] = Nil

    def createUpdate = users

    override def lowPriority = {

        case UserItem(user, room, active, stamp) => {

            users :+= UserItem(user, room, active, stamp);
            updateListeners()

        }

    }

}

Currently each time the form is submitted a new UserItem is added to the users list. What I'm trying to do is each time the server receives a new user, instead of concatenating to the list it should overwrite an existing item with the same user and room handle.

So if the list contains the following:

UserItem("jam_2323", "demo-room", "James", "1320073365")
UserItem("jim_4533", "demo-room", "Jim", "1320073365")

The next time these users submit the form the above two items in the list will be replaced with the new stamp value:

UserItem("jam_2323", "demo-room", "James", "1320073435")
UserItem("jim_4533", "demo-room", "Jim", "1320073435")

Thanks in advance for any help, much appreciated :)

1
  • As a side note, instead of extracting and rebuilding the UserItem, you can simply match on it as an object. You should also either prepend to the List, or use a Vector; appending to a List performs very poorly. case u: UserItem => users ::= u. Commented Oct 31, 2011 at 15:51

2 Answers 2

2

This sounds like a classic case where you need a map, rather than a list. I don't know about the details of Lift / Comet, but I guess you want something like

    case class User(id: String)
    case class Activity(room: String, active: String, stamp: String)

    var lastUserActivity = Map[User, Activity]()
...
    case UserItem(id, room, active, stamp) => {
        lastUserActivity += User(id) -> Activity(room, active, stamp)
    }
Sign up to request clarification or add additional context in comments.

Comments

1

If you adjust UserItem from being a straight case class (I assume) to being one where you have overriden equals to ignore the stamp field, then you could make users into a Set.

Alternatively, you could filter the List to remove the old matching values before appending.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.