This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
action.ts
87 lines (75 loc) · 2.42 KB
/
action.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
80
81
82
83
84
85
86
87
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { ActionState } from '.';
import { User } from '..';
/**
* The action handler function type.
*/
export type ActionHandler = (user: User) => void;
interface ActionHandlers {
'started'?: ActionHandler;
'stopped'?: ActionHandler;
}
/**
* Class that represents a discrete action that can be in one of two states,
* started or stopped for each user. @see ActionState
*/
export class DiscreteAction {
private handlers: ActionHandlers = {};
private activeUserIds: string[] = [];
/**
* Add a handler for the given action state for when it is triggered.
* @param actionState The action state that the handle should be assigned to.
* @param handler The handler to call when the action state is triggered.
*/
public on(actionState: ActionState, handler: ActionHandler): this {
this.handlers[actionState] = handler;
return this;
}
/**
* Gets the current state of the action for the user with the given id.
* @param user The user to get the action state for.
* @returns The current state of the action for the user.
*/
public getState(user: User): ActionState {
return this.activeUserIds.find(id => id === user.id) ?
'started' : 'stopped';
}
/**
* Get whether the action is active for the user with the given id.
* @param user - The user to get whether the action is active for, or null
* if active for any user is desired..
* @returns - True if the action is active for the user, false if it is not. In the case
* that no user is given, the value is true if the action is active for any user, and false
* if not.
*/
public isActive(user?: User): boolean {
if (user) {
return !!this.activeUserIds.find(id => id === user.id);
} else {
return this.activeUserIds.length > 0;
}
}
/**
* INTERNAL METHODS
*/
/** @hidden */
public _setState(user: User, actionState: ActionState): boolean {
const currentState = this.activeUserIds.find(id => id === user.id) || 'stopped';
if (currentState !== actionState) {
if (actionState === 'started') {
this.activeUserIds.push(user.id);
} else {
this.activeUserIds = this.activeUserIds.filter(id => id === user.id);
}
const handler = this.handlers[actionState];
if (handler) {
handler(user);
}
return true;
}
return false;
}
}