Skip to content

Commit dfe9459

Browse files
authored
Merge pull request #27 from retejs/fix-async-selector-handlers
fix: support async operations in selector
2 parents 5b06907 + d3cf313 commit dfe9459

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/extensions/selectable.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function accumulateOnCtrl() {
3131
}
3232
}
3333

34-
export type SelectorEntity = { label: string, id: string, unselect(): void, translate(dx: number, dy: number): void }
34+
export type SelectorEntity = { label: string, id: string, unselect(): void | Promise<void>, translate(dx: number, dy: number): void }
3535

3636
/**
3737
* Selector class. Used to collect selected entities (nodes, connections, etc.) and synchronize them (select, unselect, translate, etc.).
@@ -45,23 +45,23 @@ export class Selector<E extends SelectorEntity> {
4545
return this.entities.has(`${entity.label}_${entity.id}`)
4646
}
4747

48-
add(entity: E, accumulate: boolean) {
49-
if (!accumulate) this.unselectAll()
48+
async add(entity: E, accumulate: boolean) {
49+
if (!accumulate) await this.unselectAll()
5050
this.entities.set(`${entity.label}_${entity.id}`, entity)
5151
}
5252

53-
remove(entity: Pick<E, 'label' | 'id'>) {
53+
async remove(entity: Pick<E, 'label' | 'id'>) {
5454
const id = `${entity.label}_${entity.id}`
5555
const item = this.entities.get(id)
5656

5757
if (item) {
5858
this.entities.delete(id)
59-
item.unselect()
59+
await item.unselect()
6060
}
6161
}
6262

63-
unselectAll() {
64-
[...Array.from(this.entities.values())].forEach(item => this.remove(item))
63+
async unselectAll() {
64+
await Promise.all([...Array.from(this.entities.values())].map(item => this.remove(item)))
6565
}
6666

6767
translate(dx: number, dy: number) {
@@ -134,12 +134,12 @@ export function selectableNodes<T>(base: BaseAreaPlugin<Schemes, T>, core: Selec
134134
* @param nodeId Node id
135135
* @param accumulate Whether to accumulate nodes on selection
136136
*/
137-
function add(nodeId: NodeId, accumulate: boolean) {
137+
async function add(nodeId: NodeId, accumulate: boolean) {
138138
const node = getEditor().getNode(nodeId)
139139

140140
if (!node) return
141141

142-
core.add({
142+
await core.add({
143143
label: 'node',
144144
id: node.id,
145145
translate(dx, dy) {
@@ -160,12 +160,12 @@ export function selectableNodes<T>(base: BaseAreaPlugin<Schemes, T>, core: Selec
160160
* Unselect node programmatically
161161
* @param nodeId Node id
162162
*/
163-
function remove(nodeId: NodeId) {
164-
core.remove({ id: nodeId, label: 'node' })
163+
async function remove(nodeId: NodeId) {
164+
await core.remove({ id: nodeId, label: 'node' })
165165
}
166166

167167
// eslint-disable-next-line max-statements, complexity
168-
area.addPipe(context => {
168+
area.addPipe(async context => {
169169
if (!context || typeof context !== 'object' || !('type' in context)) return context
170170

171171
if (context.type === 'nodepicked') {
@@ -174,7 +174,7 @@ export function selectableNodes<T>(base: BaseAreaPlugin<Schemes, T>, core: Selec
174174

175175
core.pick({ id: pickedId, label: 'node' })
176176
twitch = null
177-
add(pickedId, accumulate)
177+
await add(pickedId, accumulate)
178178
} else if (context.type === 'nodetranslated') {
179179
const { id, position, previous } = context.data
180180
const dx = position.x - previous.x
@@ -187,7 +187,7 @@ export function selectableNodes<T>(base: BaseAreaPlugin<Schemes, T>, core: Selec
187187
if (twitch !== null) twitch++
188188
} else if (context.type === 'pointerup') {
189189
if (twitch !== null && twitch < 4) {
190-
core.unselectAll()
190+
await core.unselectAll()
191191
}
192192
twitch = null
193193
}

0 commit comments

Comments
 (0)