This repository has been archived by the owner on Jun 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
HandlerRegistry.js
170 lines (137 loc) · 4.46 KB
/
HandlerRegistry.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import invariant from 'invariant';
import isArray from 'lodash/isArray';
import asap from 'asap';
import { addSource, addTarget, removeSource, removeTarget } from './actions/registry';
import getNextUniqueId from './utils/getNextUniqueId';
const HandlerRoles = {
SOURCE: 'SOURCE',
TARGET: 'TARGET',
};
function validateSourceContract(source) {
invariant(typeof source.canDrag === 'function', 'Expected canDrag to be a function.');
invariant(typeof source.beginDrag === 'function', 'Expected beginDrag to be a function.');
invariant(typeof source.endDrag === 'function', 'Expected endDrag to be a function.');
}
function validateTargetContract(target) {
invariant(typeof target.canDrop === 'function', 'Expected canDrop to be a function.');
invariant(typeof target.hover === 'function', 'Expected hover to be a function.');
invariant(typeof target.drop === 'function', 'Expected beginDrag to be a function.');
}
function validateType(type, allowArray) {
if (allowArray && isArray(type)) {
type.forEach(t => validateType(t, false));
return;
}
invariant(
typeof type === 'string' || typeof type === 'symbol',
allowArray ?
'Type can only be a string, a symbol, or an array of either.' :
'Type can only be a string or a symbol.',
);
}
function getNextHandlerId(role) {
const id = getNextUniqueId().toString();
switch (role) {
case HandlerRoles.SOURCE:
return `S${id}`;
case HandlerRoles.TARGET:
return `T${id}`;
default:
invariant(false, `Unknown role: ${role}`);
}
}
function parseRoleFromHandlerId(handlerId) {
switch (handlerId[0]) {
case 'S':
return HandlerRoles.SOURCE;
case 'T':
return HandlerRoles.TARGET;
default:
invariant(false, `Cannot parse handler ID: ${handlerId}`);
}
}
export default class HandlerRegistry {
constructor(store) {
this.store = store;
this.types = {};
this.handlers = {};
this.pinnedSourceId = null;
this.pinnedSource = null;
}
addSource(type, source) {
validateType(type);
validateSourceContract(source);
const sourceId = this.addHandler(HandlerRoles.SOURCE, type, source);
this.store.dispatch(addSource(sourceId));
return sourceId;
}
addTarget(type, target) {
validateType(type, true);
validateTargetContract(target);
const targetId = this.addHandler(HandlerRoles.TARGET, type, target);
this.store.dispatch(addTarget(targetId));
return targetId;
}
addHandler(role, type, handler) {
const id = getNextHandlerId(role);
this.types[id] = type;
this.handlers[id] = handler;
return id;
}
containsHandler(handler) {
return Object.keys(this.handlers).some(key => this.handlers[key] === handler);
}
getSource(sourceId, includePinned) {
invariant(this.isSourceId(sourceId), 'Expected a valid source ID.');
const isPinned = includePinned && sourceId === this.pinnedSourceId;
const source = isPinned ? this.pinnedSource : this.handlers[sourceId];
return source;
}
getTarget(targetId) {
invariant(this.isTargetId(targetId), 'Expected a valid target ID.');
return this.handlers[targetId];
}
getSourceType(sourceId) {
invariant(this.isSourceId(sourceId), 'Expected a valid source ID.');
return this.types[sourceId];
}
getTargetType(targetId) {
invariant(this.isTargetId(targetId), 'Expected a valid target ID.');
return this.types[targetId];
}
isSourceId(handlerId) {
const role = parseRoleFromHandlerId(handlerId);
return role === HandlerRoles.SOURCE;
}
isTargetId(handlerId) {
const role = parseRoleFromHandlerId(handlerId);
return role === HandlerRoles.TARGET;
}
removeSource(sourceId) {
invariant(this.getSource(sourceId), 'Expected an existing source.');
this.store.dispatch(removeSource(sourceId));
asap(() => {
delete this.handlers[sourceId];
delete this.types[sourceId];
});
}
removeTarget(targetId) {
invariant(this.getTarget(targetId), 'Expected an existing target.');
this.store.dispatch(removeTarget(targetId));
asap(() => {
delete this.handlers[targetId];
delete this.types[targetId];
});
}
pinSource(sourceId) {
const source = this.getSource(sourceId);
invariant(source, 'Expected an existing source.');
this.pinnedSourceId = sourceId;
this.pinnedSource = source;
}
unpinSource() {
invariant(this.pinnedSource, 'No source is pinned at the time.');
this.pinnedSourceId = null;
this.pinnedSource = null;
}
}