forked from hczhcz/matt.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.ts
79 lines (64 loc) · 2.11 KB
/
context.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
import {SimpleError, ErrFunc, VoidFunc, ValFunc} from './util';
import {Context, User, Node, DirNode} from './interface';
export class PlainContext implements Context {
private _proc: DirNode; // bind in constructor
constructor(
builder: () => DirNode,
private _user: User // mutable
) {
this._proc = builder();
}
proc(callback: ValFunc<DirNode>): void {
callback(this._proc);
}
user(callback: ValFunc<User>): void {
callback(this._user);
}
_setuser(user: User, callback: VoidFunc): void {
this._user = user;
callback();
}
root(callback: ValFunc<Node>, fail: ErrFunc): void {
this._proc.open(this, 'root', callback, fail);
}
dir(callback: ValFunc<Node>, fail: ErrFunc): void {
this._proc.open(this, 'dir', callback, fail);
}
chroot(node: Node, callback: VoidFunc, fail: ErrFunc): void {
this._proc.swap(this, 'root', node, callback, fail);
}
chdir(node: Node, callback: VoidFunc, fail: ErrFunc): void {
this._proc.swap(this, 'dir', node, callback, fail);
}
};
export class ContextUser extends PlainContext implements User {
constructor(
builder: (cu: ContextUser) => DirNode,
user: User
) {
super((): DirNode => {
return builder(this);
}, user);
}
group(context: Context, callback: ValFunc<string>, fail: ErrFunc): void {
this.user((user: User): void => {
user.group(context, callback, fail);
});
}
name(context: Context, callback: ValFunc<string>, fail: ErrFunc): void {
this.user((user: User): void => {
user.name(context, callback, fail);
});
}
superuser(context: Context, callback: ValFunc<boolean>, fail: ErrFunc): void {
this.user((user: User): void => {
user.superuser(context, callback, fail);
});
}
distance(context: Context, callback: ValFunc<number>, fail: ErrFunc): void {
this.user((user: User): void => {
user.distance(context, callback, fail);
});
}
};