UNPKG

949 BJavaScriptView Raw
1var Set = require('es6-set')
2
3module.exports = Binding
4
5function 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
13Binding.prototype.add = function (node) {
14 this.references.add(node)
15 return this
16}
17
18Binding.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
26Binding.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
35Binding.prototype.getReferences = function () {
36 var arr = []
37 this.each(function (ref) { arr.push(ref) })
38 return arr
39}
40
41Binding.prototype.each = function (cb) {
42 this.references.forEach(function (ref) { cb(ref) })
43 return this
44}