-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (50 loc) · 1.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { createClient } from "redis";
import { initialize } from "unleash-client";
const SEGMENT_NAME = "someSegmentOfUsers"; // We'll be using "someSegmentOfUsers" but the name is arbitrary
const TOKEN = "*:development:some-secret"; // Prepopulated in the docker compose
const USERS_TO_QUERY = [7, 8, 9];
class RedisLayer {
constructor() {
this.client = createClient({
url: "redis://redis",
});
}
async connect() {
await this.client.connect();
this.client.on("error", (err) => console.log("Redis Client Error", err));
}
async addToSegment(userId) {
await this.client.sAdd(SEGMENT_NAME, userId.toString());
}
async isInSegment(userId) {
return this.client.sIsMember(SEGMENT_NAME, userId.toString());
}
async setup() {
await this.addToSegment(7);
await this.addToSegment(8);
}
}
let redisLayer = new RedisLayer();
await redisLayer.connect();
await redisLayer.setup(); // Don't do this in prod, this is to populate our users in Redis, practically, this would be handled by another system
const unleash = initialize({
url: "http://unleash:4242/api",
appName: "some-app-name",
customHeaders: { Authorization: TOKEN },
});
// End setup code, below is how this would be used
async function pollUnleash() {
for (const userId of USERS_TO_QUERY) {
// Ask our Redis client if this userId is in the segment
const isInSegment = await redisLayer.isInSegment(userId);
const isEnabled = unleash.isEnabled("test-toggle", {
// Pass the "true"/"false" context to Unleash
isInSegment: isInSegment.toString(),
});
console.log(
`Toggle test-toggle is enabled? ${isEnabled} for userId ${userId} ${isInSegment}`
);
}
await new Promise((_resolve) => setTimeout(pollUnleash, 1000));
}
await pollUnleash();