E.g. Is it possible for there to be something like
login({credentials}, function(err, api) {
...
});
logout();
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
Most helpful comment
How would it support concurrent connections ?
logoutrequires a context to know who should be logged out.logindoesn't need any context, it creates the context (apiis 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:
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.