canjs / can-connect-feathers

The FeathersJS client library for DoneJS and can-connect
https://canjs.com/doc/can-connect-feathers.html
MIT License
10 stars 4 forks source link

Implement Session.current #44

Closed marshallswain closed 7 years ago

marshallswain commented 7 years ago
get session(){
    return Session.current;
}

new Session().save();

Session.current.destroy()

var data;
var write = function(prop, value){
  if(typeof Zone === "undefined") { 
    data[prop] = value;
  } else {
    Zone.current.data[prop] = value;
  }
}
var read = function(prop){
  if(typeof Zone === "undefined") { 
    return data[prop]
  } else {
    return Zone.current.data[prop]
  }
}

Object.defineProperty(Session, "current", {
    get: function(){
        Observation.add(Session, "current");
        if( !read("can-connect-feathers-session") ) {
            Session.get().then(function(session){
                write("can-connect-feathers-session", session)
                Session.dispatch("current", [session]);
            })
        }
        return read("can-connect-feathers-session");
    }
});

Session.on("created", function(ev, session){
    write("can-connect-feathers-session", session)
    Session.dispatch("current", [session]);
});
Session.on("destroyed", function(ev){
    write("can-connect-feathers-session", undefined)
    Session.dispatch("current", []);
});
matthewp commented 7 years ago
Object.defineProperty(Session, "current", {
    get: function(){
        Observation.add(Session, "current");
        if(Zone.current.data.session === null) {
            Session.get().then(function(session){
                Zone.current.data.session = session;
                Session.dispatch("current", [session]);
            })
        }
        return Zone.current.data.session;
    }
});
marshallswain commented 7 years ago

Implemented in https://github.com/canjs/can-connect-feathers/commit/b6dba879cf9149d1f80b40093c20ae1705b6deff