-
Notifications
You must be signed in to change notification settings - Fork 857
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples.libavoid): use web worker to run libavoid router
- Loading branch information
1 parent
12b0c32
commit 5cdbedb
Showing
4 changed files
with
256 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { AvoidRouter } from './avoid-router'; | ||
import { dia, shapes, util } from '@joint/core'; | ||
|
||
const routerLoaded = AvoidRouter.load(); | ||
|
||
onmessage = async(e) => { | ||
|
||
await routerLoaded; | ||
|
||
const [{ command, ...data }] = e.data; | ||
switch (command) { | ||
case 'reset': { | ||
const { json } = data; | ||
graph.resetCells(data.cells, { fromBrowser: true }); | ||
router.routeAll(); | ||
break; | ||
} | ||
case 'change': { | ||
const { cell } = data; | ||
const model = graph.getCell(cell.id); | ||
if (!model) { | ||
console.error(`Cell with id ${cell.id} not found.`); | ||
return; | ||
} | ||
if (model.isElement()) { | ||
// A node was changed | ||
model.set({ | ||
position: cell.position, | ||
size: cell.size, | ||
}, { | ||
fromBrowser: true | ||
}); | ||
} else { | ||
// An edge was changed. | ||
model.set({ | ||
source: cell.source, | ||
target: cell.target | ||
}, { | ||
fromBrowser: true | ||
}); | ||
} | ||
break; | ||
} | ||
case 'remove': { | ||
const { id } = data; | ||
const model = graph.getCell(id); | ||
if (!model) break; | ||
model.remove({ fromBrowser: true }); | ||
break; | ||
} | ||
case 'add': { | ||
const { cell } = data; | ||
graph.addCell(cell, { fromBrowser: true }); | ||
break; | ||
} | ||
default: | ||
console.log('Unknown command', command); | ||
break; | ||
} | ||
}; | ||
|
||
await routerLoaded; | ||
|
||
const graph = new dia.Graph({}, { | ||
cellNamespace: { | ||
...shapes, | ||
Node: shapes.standard.Rectangle, | ||
Edge: shapes.standard.Link, | ||
} | ||
}); | ||
|
||
const router = new AvoidRouter(graph, { | ||
shapeBufferDistance: 20, | ||
idealNudgingDistance: 10, | ||
portOverflow: 8, | ||
}); | ||
|
||
let changed = {}; | ||
|
||
const debouncedProcessTransaction = util.debounce(() => { | ||
router.avoidRouter.processTransaction(); | ||
setTimeout(() => { | ||
if (debouncedProcessTransaction.pending()) return; | ||
postMessage({ | ||
command: 'routed', | ||
cells: Object.values(changed), | ||
}); | ||
changed = {}; | ||
}, 0); | ||
}, 100); | ||
|
||
router.addGraphListeners(); | ||
|
||
graph.on('change', (cell, opt) => { | ||
if (opt.fromBrowser) { | ||
debouncedProcessTransaction(); | ||
return; | ||
} | ||
changed[cell.id] = cell.toJSON(); | ||
}); | ||
|
||
graph.on('reset', (collection, opt) => { | ||
if (!opt.fromBrowser) return; | ||
debouncedProcessTransaction(); | ||
}); | ||
|
||
graph.on('add', (cell, opt) => { | ||
if (!opt.fromBrowser) return; | ||
debouncedProcessTransaction(); | ||
}); | ||
|
||
graph.on('remove', (cell, opt) => { | ||
delete changed[cell.id]; | ||
if (!opt.fromBrowser) return; | ||
debouncedProcessTransaction(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters