michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "unstable" michael@0: }; michael@0: michael@0: const { Class } = require('../core/heritage'); michael@0: const { MatchPattern } = require('./match-pattern'); michael@0: const { on, off, emit } = require('../event/core'); michael@0: const { method } = require('../lang/functional'); michael@0: const objectUtil = require('./object'); michael@0: const { EventTarget } = require('../event/target'); michael@0: const { List, addListItem, removeListItem } = require('./list'); michael@0: michael@0: // Should deprecate usage of EventEmitter/compose michael@0: const Rules = Class({ michael@0: implements: [ michael@0: EventTarget, michael@0: List michael@0: ], michael@0: add: function(...rules) [].concat(rules).forEach(function onAdd(rule) { michael@0: addListItem(this, rule); michael@0: emit(this, 'add', rule); michael@0: }, this), michael@0: remove: function(...rules) [].concat(rules).forEach(function onRemove(rule) { michael@0: removeListItem(this, rule); michael@0: emit(this, 'remove', rule); michael@0: }, this), michael@0: get: function(rule) { michael@0: let found = false; michael@0: for (let i in this) if (this[i] === rule) found = true; michael@0: return found; michael@0: }, michael@0: // Returns true if uri matches atleast one stored rule michael@0: matchesAny: function(uri) !!filterMatches(this, uri).length, michael@0: toString: function() '[object Rules]' michael@0: }); michael@0: exports.Rules = Rules; michael@0: michael@0: function filterMatches(instance, uri) { michael@0: let matches = []; michael@0: for (let i in instance) { michael@0: if (new MatchPattern(instance[i]).test(uri)) matches.push(instance[i]); michael@0: } michael@0: return matches; michael@0: }