-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathLinks.js
44 lines (39 loc) · 1.47 KB
/
Links.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
import AgentSet from './AgentSet.js'
// Links are a collection of all the Link objects between turtles.
/**
* Links are a collection of all the {@link Link} objects between turtles.
*
*/
class Links extends AgentSet {
// Use AgentSeet ctor: constructor (model, AgentClass, name)
constructor(model, AgentClass, name, baseSet = null) {
super(model, AgentClass, name, baseSet)
}
// Factories:
// Add 1 or more links from the from turtle to the to turtle(s) which
// can be a single turtle or an array of turtles. The optional init
// proc is called on the new link after inserting in the agentSet.
// Return a single link
createOne(from, to, initFcn = link => {}) {
const link = this.addAgent()
link.init(from, to)
initFcn(link)
return link
}
// Return an array of links.
// To can be an array or a single turtle (returning an array of 1 link)
create(from, to, initFcn = link => {}) {
// if (!Array.isArray(to)) return this.createOne(from, to, initFcn)
if (!Array.isArray(to)) to = [to]
// Return array of new links. REMIND: should be agentarray?
return to.map(t => {
// REMIND: skip dups
// const link = this.addAgent()
// link.init(from, t)
// initFcn(link)
// return link
return this.createOne(from, t, initFcn)
}) // REMIND: return single link if to not an array?
}
}
export default Links