UNPKG

2.98 kBJavaScriptView Raw
1/*
2<!-- LICENSEFILE/ -->
3
4<h1>License</h1>
5
6Unless stated otherwise all works are:
7
8<ul><li>Copyright &copy; 2013+ <a href="http://bevry.me">Bevry Pty Ltd</a></li></ul>
9
10and licensed under:
11
12<ul><li><a href="http://spdx.org/licenses/MIT.html">MIT License</a></li></ul>
13
14<h2>MIT License</h2>
15
16<pre>
17Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
18
19The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
20
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22</pre>
23
24<!-- /LICENSEFILE -->
25*/
26/*
27modified by Calvin Metcalf to adhere to how the node one works a little better
28*/
29import {EventEmitter} from 'events';
30import {inherits} from 'util';
31inherits(Domain, EventEmitter);
32function createEmitError(d) {
33 return emitError;
34 function emitError(e) {
35 d.emit('error', e)
36 }
37}
38
39export function Domain() {
40 EventEmitter.call(this);
41 this.__emitError = createEmitError(this);
42}
43Domain.prototype.add = function (emitter) {
44 emitter.on('error', this.__emitError);
45}
46Domain.prototype.remove = function(emitter) {
47 emitter.removeListener('error', this.__emitError)
48}
49Domain.prototype.bind = function(fn) {
50 var emitError = this.__emitError;
51 return function() {
52 var args = Array.prototype.slice.call(arguments)
53 try {
54 fn.apply(null, args)
55 } catch (err) {
56 emitError(err)
57 }
58 }
59}
60Domain.prototype.intercept = function(fn) {
61 var emitError = this.__emitError;
62 return function(err) {
63 if (err) {
64 emitError(err)
65 } else {
66 var args = Array.prototype.slice.call(arguments, 1)
67 try {
68 fn.apply(null, args)
69 } catch (err) {
70 emitError(err)
71 }
72 }
73 }
74}
75Domain.prototype.run = function(fn) {
76 var emitError = this.__emitError;
77 try {
78 fn()
79 } catch (err) {
80 emitError(err)
81 }
82 return this
83}
84Domain.prototype.dispose = function() {
85 this.removeAllListeners()
86 return this
87}
88Domain.prototype.enter = Domain.prototype.exit = function() {
89 return this
90}
91export function createDomain() {
92 return new Domain();
93}
94export var create = createDomain;
95
96export default {
97 Domain: Domain,
98 createDomain: createDomain,
99 create: create
100}