Skip to content

Instantly share code, notes, and snippets.

@iamfunsho iamfunsho/block3.js Secret
Created Oct 24, 2017

Embed
What would you like to do?
"use strict";
import { client } from "../lib/redis";
export let fetchMessages = () => {
return new Promise((resolve, reject) => {
client().then(
res => {
res.lrangeAsync("messages", 0, -1).then(
messages => {
resolve(messages);
},
err => {
reject(err);
}
);
},
err => {
reject("Redis connection failed: " + err);
}
);
});
};
export let addMessage = message => {
return new Promise((resolve, reject) => {
client().then(
res => {
res
.multi()
.rpush("messages", message)
.execAsync()
.then(
res => {
resolve(res);
},
err => {
reject(err);
}
);
},
err => {
reject("Redis connection failed: " + err);
}
);
});
};
export let fetchActiveUsers = () => {
return new Promise((resolve, reject) => {
client().then(
res => {
res.smembersAsync("users").then(
users => {
resolve(users);
},
err => {
reject(err);
}
);
},
err => {
reject("Redis connection failed: " + err);
}
);
});
};
export let addActiveUser = user => {
return new Promise((resolve, reject) => {
client().then(
res => {
res
.multi()
.sadd("users", user)
.execAsync()
.then(
res => {
if (res[0] === 1) {
resolve("User added");
}
reject("User already in list");
},
err => {
reject(err);
}
);
},
err => {
reject("Redis connection failed: " + err);
}
);
});
};
export let removeActiveUser = user => {
return new Promise((resolve, reject) => {
client().then(
res => {
res
.multi()
.srem("users", user)
.execAsync()
.then(
res => {
if (res === 1) {
resolve("User removed");
}
reject("User is not in list");
},
err => {
reject(err);
}
);
},
err => {
reject("Redis connection failed: " + err);
}
);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.