1
|
// This file should be ES5 compatible
|
2
|
/* eslint prefer-spread:0, no-var:0, prefer-reflect:0, no-magic-numbers:0 */
|
3
|
'use strict'
|
4
|
|
5
|
module.exports = (function () {
|
6
|
// Import Events
|
7
|
var events = require('events')
|
8
|
|
9
|
// Export Domain
|
10
|
var domain = {}
|
11
|
domain.createDomain = domain.create = function () {
|
12
|
var d = new events.EventEmitter()
|
13
|
|
14
|
function emitError (e) {
|
15
|
d.emit('error', e)
|
16
|
}
|
17
|
|
18
|
d.add = function (emitter) {
|
19
|
emitter.on('error', emitError)
|
20
|
}
|
21
|
d.remove = function (emitter) {
|
22
|
emitter.removeListener('error', emitError)
|
23
|
}
|
24
|
d.bind = function (fn) {
|
25
|
return function () {
|
26
|
var args = Array.prototype.slice.call(arguments)
|
27
|
try {
|
28
|
fn.apply(null, args)
|
29
|
}
|
30
|
catch (err) {
|
31
|
emitError(err)
|
32
|
}
|
33
|
}
|
34
|
}
|
35
|
d.intercept = function (fn) {
|
36
|
return function (err) {
|
37
|
if ( err ) {
|
38
|
emitError(err)
|
39
|
}
|
40
|
else {
|
41
|
var args = Array.prototype.slice.call(arguments, 1)
|
42
|
try {
|
43
|
fn.apply(null, args)
|
44
|
}
|
45
|
catch (err) {
|
46
|
emitError(err)
|
47
|
}
|
48
|
}
|
49
|
}
|
50
|
}
|
51
|
d.run = function (fn) {
|
52
|
try {
|
53
|
fn()
|
54
|
}
|
55
|
catch (err) {
|
56
|
emitError(err)
|
57
|
}
|
58
|
return this
|
59
|
}
|
60
|
d.dispose = function () {
|
61
|
this.removeAllListeners()
|
62
|
return this
|
63
|
}
|
64
|
d.enter = d.exit = function () {
|
65
|
return this
|
66
|
}
|
67
|
return d
|
68
|
}
|
69
|
return domain
|
70
|
}).call(this)
|