Facebook-chat-api: Would it be possible to make logout act outside of the login callback?

Created on 29 Apr 2016  路  2Comments  路  Source: Schmavery/facebook-chat-api

E.g. Is it possible for there to be something like

login({credentials}, function(err, api) {
 ...
});
logout();

Most helpful comment

How would it support concurrent connections ?

logout requires a context to know who should be logged out.
login doesn't need any context, it creates the context (api is built by aggregating numerous closures, each one injecting the IO functions, the cookieJar, etc.)

A similar behaviour to what you want could be achieved by a simple wrapper like:

class FacebookAccount {
    credentials;
    api;

    constructor(credentials) {
        this.credentials = credentials;
        this.api = null;
    }

    login(cb) {
        if (this.api) {
            cb(null, this.api);
        } else {
            login(this.credentials, (err, api) => {
                 this.api = api;
                 cb(err, api);
            });
        }
   }

    logout(cb) {
        if (this.api) {
            this.api.logout((err) => {this.api = null; cb(err)});
        } else {
            cb(null);
        }
    }
}

It's not perfect (you should use a state variable to prevent simultaneous connections of the same account, errors should be handled) but you get the idea.

All 2 comments

How would it support concurrent connections ?

logout requires a context to know who should be logged out.
login doesn't need any context, it creates the context (api is built by aggregating numerous closures, each one injecting the IO functions, the cookieJar, etc.)

A similar behaviour to what you want could be achieved by a simple wrapper like:

class FacebookAccount {
    credentials;
    api;

    constructor(credentials) {
        this.credentials = credentials;
        this.api = null;
    }

    login(cb) {
        if (this.api) {
            cb(null, this.api);
        } else {
            login(this.credentials, (err, api) => {
                 this.api = api;
                 cb(err, api);
            });
        }
   }

    logout(cb) {
        if (this.api) {
            this.api.logout((err) => {this.api = null; cb(err)});
        } else {
            cb(null);
        }
    }
}

It's not perfect (you should use a state variable to prevent simultaneous connections of the same account, errors should be handled) but you get the idea.

Thanks for explaining @Demurgos

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Dinamots picture Dinamots  路  8Comments

TKasperczyk picture TKasperczyk  路  5Comments

DanH42 picture DanH42  路  3Comments

GEOFBOT picture GEOFBOT  路  4Comments

Hraph picture Hraph  路  6Comments