Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | module.metadata = { |
michael@0 | 7 | "stability": "unstable" |
michael@0 | 8 | }; |
michael@0 | 9 | |
michael@0 | 10 | const { EventEmitter } = require('../deprecated/events'); |
michael@0 | 11 | const unload = require('../system/unload'); |
michael@0 | 12 | |
michael@0 | 13 | const Registry = EventEmitter.compose({ |
michael@0 | 14 | _registry: null, |
michael@0 | 15 | _constructor: null, |
michael@0 | 16 | constructor: function Registry(constructor) { |
michael@0 | 17 | this._registry = []; |
michael@0 | 18 | this._constructor = constructor; |
michael@0 | 19 | this.on('error', this._onError = this._onError.bind(this)); |
michael@0 | 20 | unload.ensure(this, "_destructor"); |
michael@0 | 21 | }, |
michael@0 | 22 | _destructor: function _destructor() { |
michael@0 | 23 | let _registry = this._registry.slice(0); |
michael@0 | 24 | for each (let instance in _registry) |
michael@0 | 25 | this._emit('remove', instance); |
michael@0 | 26 | this._registry.splice(0); |
michael@0 | 27 | }, |
michael@0 | 28 | _onError: function _onError(e) { |
michael@0 | 29 | if (!this._listeners('error').length) |
michael@0 | 30 | console.error(e); |
michael@0 | 31 | }, |
michael@0 | 32 | has: function has(instance) { |
michael@0 | 33 | let _registry = this._registry; |
michael@0 | 34 | return ( |
michael@0 | 35 | (0 <= _registry.indexOf(instance)) || |
michael@0 | 36 | (instance && instance._public && 0 <= _registry.indexOf(instance._public)) |
michael@0 | 37 | ); |
michael@0 | 38 | }, |
michael@0 | 39 | add: function add(instance) { |
michael@0 | 40 | let { _constructor, _registry } = this; |
michael@0 | 41 | if (!(instance instanceof _constructor)) |
michael@0 | 42 | instance = new _constructor(instance); |
michael@0 | 43 | if (0 > _registry.indexOf(instance)) { |
michael@0 | 44 | _registry.push(instance); |
michael@0 | 45 | this._emit('add', instance); |
michael@0 | 46 | } |
michael@0 | 47 | return instance; |
michael@0 | 48 | }, |
michael@0 | 49 | remove: function remove(instance) { |
michael@0 | 50 | let _registry = this._registry; |
michael@0 | 51 | let index = _registry.indexOf(instance) |
michael@0 | 52 | if (0 <= index) { |
michael@0 | 53 | this._emit('remove', instance); |
michael@0 | 54 | _registry.splice(index, 1); |
michael@0 | 55 | } |
michael@0 | 56 | } |
michael@0 | 57 | }); |
michael@0 | 58 | exports.Registry = Registry; |
michael@0 | 59 |