-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test-d.ts
65 lines (51 loc) · 1.78 KB
/
index.test-d.ts
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
63
64
65
import { expectType } from 'tsd';
import scaly, { ScalyResult } from '.';
const db = {
maxUsers: 10,
cursor: 0,
users: new Map<number, { username: string; messages: string[] }>(),
async *registerUser(username: string): ScalyResult<number> {
if (this.users.size >= this.maxUsers) {
yield 'tooManyUsers';
}
const id = this.cursor++;
this.users.set(id, { username, messages: [] });
return id;
},
async *getUsername(id: number): ScalyResult<string> {
const user = this.users.get(id);
return user ? user.username : yield 'noSuchUser';
},
async *getMessages(id: number): ScalyResult<string[]> {
const user = this.users.get(id);
return user ? user.messages.slice() : yield 'noSuchUser';
},
async *sendMessage(id: number, message: string): ScalyResult<null> {
const user = this.users.get(id);
return user ? (user.messages.push(message), null) : yield 'noSuchUser';
},
async *checkDBStatus(): ScalyResult<null> {
return this.users.size < this.maxUsers ? null : yield 'tooManyUsers';
}
};
const cache = {
usernames: new Map<number, string>(),
async *getUsername(id: number): ScalyResult<string> {
const username = this.usernames.get(id);
if (username) {
return username;
}
this.usernames.set(id, yield);
},
async *checkCacheStatus(): ScalyResult<null> {
return null;
}
};
const api = scaly(db, cache);
type ScalyAPI<Res, Error_ = string> = Promise<[true, Res] | [false, Error_]>;
expectType<(username: string) => ScalyAPI<number>>(api.registerUser);
expectType<(id: number) => ScalyAPI<string>>(api.getUsername);
expectType<(id: number) => ScalyAPI<string[]>>(api.getMessages);
expectType<(id: number, message: string) => ScalyAPI<null>>(api.sendMessage);
expectType<() => ScalyAPI<null>>(api.checkDBStatus);
expectType<() => ScalyAPI<null>>(api.checkCacheStatus);