| 1 |
var Set = require('es6-set') |
| 2 |
|
| 3 |
module.exports = Binding |
| 4 |
|
| 5 |
function Binding (name, definition) { |
| 6 |
this.name = name |
| 7 |
this.definition = definition |
| 8 |
this.references = new Set() |
| 9 |
|
| 10 |
if (definition) this.add(definition) |
| 11 |
} |
| 12 |
|
| 13 |
Binding.prototype.add = function (node) { |
| 14 |
this.references.add(node) |
| 15 |
return this |
| 16 |
} |
| 17 |
|
| 18 |
Binding.prototype.remove = function (node) { |
| 19 |
if (!this.references.has(node)) { |
| 20 |
throw new Error('Tried removing nonexistent reference') |
| 21 |
} |
| 22 |
this.references.delete(node) |
| 23 |
return this |
| 24 |
} |
| 25 |
|
| 26 |
Binding.prototype.isReferenced = function () { |
| 27 |
var definition = this.definition |
| 28 |
var isReferenced = false |
| 29 |
this.each(function (ref) { |
| 30 |
if (ref !== definition) isReferenced = true |
| 31 |
}) |
| 32 |
return isReferenced |
| 33 |
} |
| 34 |
|
| 35 |
Binding.prototype.getReferences = function () { |
| 36 |
var arr = [] |
| 37 |
this.each(function (ref) { arr.push(ref) }) |
| 38 |
return arr |
| 39 |
} |
| 40 |
|
| 41 |
Binding.prototype.each = function (cb) { |
| 42 |
this.references.forEach(function (ref) { cb(ref) }) |
| 43 |
return this |
| 44 |
} |