-
Notifications
You must be signed in to change notification settings - Fork 2k
/
HandlerRegistryImpl.ts
174 lines (151 loc) · 4.59 KB
/
HandlerRegistryImpl.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
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
171
172
173
174
import { Store } from 'redux'
import invariant from 'invariant'
import isArray from 'lodash/isArray'
import {
addSource,
addTarget,
removeSource,
removeTarget,
} from './actions/registry'
import getNextUniqueId from './utils/getNextUniqueId'
import { State } from './reducers'
import {
DragSource,
DropTarget,
SourceType,
TargetType,
Identifier,
HandlerRole,
HandlerRegistry,
} from './interfaces'
import {
validateSourceContract,
validateTargetContract,
validateType,
} from './contracts'
function getNextHandlerId(role: HandlerRole): string {
const id = getNextUniqueId().toString()
switch (role) {
case HandlerRole.SOURCE:
return `S${id}`
case HandlerRole.TARGET:
return `T${id}`
default:
throw new Error(`Unknown Handler Role: ${role}`)
}
}
function parseRoleFromHandlerId(handlerId: string) {
switch (handlerId[0]) {
case 'S':
return HandlerRole.SOURCE
case 'T':
return HandlerRole.TARGET
default:
invariant(false, `Cannot parse handler ID: ${handlerId}`)
}
}
function mapContainsValue<T>(map: Map<string, T>, searchValue: T) {
const entries = map.entries()
let isDone = false
do {
const { done, value: [key, value] } = entries.next()
if (value === searchValue) {
return true
}
isDone = done
} while (!isDone)
return false
}
export default class HandlerRegistryImpl implements HandlerRegistry {
private types: Map<string, SourceType | TargetType> = new Map()
private dragSources: Map<string, DragSource> = new Map()
private dropTargets: Map<string, DropTarget> = new Map()
private pinnedSourceId: string | null = null
private pinnedSource: any = null
constructor(private store: Store<State>) {}
public addSource(type: SourceType, source: DragSource) {
validateType(type)
validateSourceContract(source)
const sourceId = this.addHandler(HandlerRole.SOURCE, type, source)
this.store.dispatch(addSource(sourceId))
return sourceId
}
public addTarget(type: TargetType, target: DropTarget) {
validateType(type, true)
validateTargetContract(target)
const targetId = this.addHandler(HandlerRole.TARGET, type, target)
this.store.dispatch(addTarget(targetId))
return targetId
}
public containsHandler(handler: DragSource | DropTarget) {
return (
mapContainsValue(this.dragSources, handler) ||
mapContainsValue(this.dropTargets, handler)
)
}
public getSource(sourceId: string, includePinned = false): DragSource {
invariant(this.isSourceId(sourceId), 'Expected a valid source ID.')
const isPinned = includePinned && sourceId === this.pinnedSourceId
const source = isPinned ? this.pinnedSource : this.dragSources.get(sourceId)
return source
}
public getTarget(targetId: string): DropTarget {
invariant(this.isTargetId(targetId), 'Expected a valid target ID.')
return this.dropTargets.get(targetId) as DropTarget
}
public getSourceType(sourceId: string) {
invariant(this.isSourceId(sourceId), 'Expected a valid source ID.')
return this.types.get(sourceId) as Identifier
}
public getTargetType(targetId: string): Identifier | Identifier[] {
invariant(this.isTargetId(targetId), 'Expected a valid target ID.')
return this.types.get(targetId) as Identifier | Identifier[]
}
public isSourceId(handlerId: string) {
const role = parseRoleFromHandlerId(handlerId)
return role === HandlerRole.SOURCE
}
public isTargetId(handlerId: string) {
const role = parseRoleFromHandlerId(handlerId)
return role === HandlerRole.TARGET
}
public removeSource(sourceId: string) {
invariant(this.getSource(sourceId), 'Expected an existing source.')
this.store.dispatch(removeSource(sourceId))
setImmediate(() => {
this.dragSources.delete(sourceId)
this.types.delete(sourceId)
})
}
public removeTarget(targetId: string) {
invariant(this.getTarget(targetId), 'Expected an existing target.')
this.store.dispatch(removeTarget(targetId))
this.dropTargets.delete(targetId)
this.types.delete(targetId)
}
public pinSource(sourceId: string) {
const source = this.getSource(sourceId)
invariant(source, 'Expected an existing source.')
this.pinnedSourceId = sourceId
this.pinnedSource = source
}
public unpinSource() {
invariant(this.pinnedSource, 'No source is pinned at the time.')
this.pinnedSourceId = null
this.pinnedSource = null
}
private addHandler(
role: HandlerRole,
type: SourceType | TargetType,
handler: DragSource | DropTarget,
): string {
const id = getNextHandlerId(role)
this.types.set(id, type)
if (role === HandlerRole.SOURCE) {
this.dragSources.set(id, handler as DragSource)
} else if (role === HandlerRole.TARGET) {
this.dropTargets.set(id, handler as DropTarget)
}
return id
}
}