action-chain
Advanced tools
Comparing version 1.0.0-1532533507115 to 1.0.0-1532590229766
@@ -35,2 +35,3 @@ import { EventEmitter } from 'betsy'; | ||
result: any; | ||
args: any[]; | ||
}; | ||
@@ -55,6 +56,4 @@ 'action:start': ActionExecution; | ||
constructor(context: Context, options?: ActionChainOptions); | ||
getContext(thisExecution: Execution, executionContext: ExecutionContext): { | ||
__execution: Execution; | ||
__path: string[]; | ||
}; | ||
private createGetHandler; | ||
getContext(thisExecution: Execution, executionContext: ExecutionContext): Context & ExecutionContext; | ||
} |
import * as tslib_1 from "tslib"; | ||
import { EventEmitter } from 'betsy'; | ||
var IS_DEVELOPMENT = process.env.NODE_ENV !== 'production'; | ||
function isObject(value) { | ||
return typeof value === 'object' && !Array.isArray(value) && value !== null; | ||
} | ||
var ActionChain = /** @class */ (function (_super) { | ||
@@ -14,39 +17,51 @@ tslib_1.__extends(ActionChain, _super); | ||
} | ||
ActionChain.prototype.getContext = function (thisExecution, executionContext) { | ||
var _this = this; | ||
ActionChain.prototype.createGetHandler = function (execution, path) { | ||
var instance = this; | ||
var providers = Object.keys(this.context).reduce(function (currentContext, key) { | ||
if (IS_DEVELOPMENT && | ||
_this.options.providerExceptions.indexOf(key) === -1) { | ||
currentContext[key] = new Proxy(_this.context[key], { | ||
get: function (target, prop) { | ||
if (typeof target[prop] === 'function') { | ||
return function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var result = target[prop].apply(target, args); | ||
if (result instanceof Promise) { | ||
result.then(function (promisedResult) { | ||
instance.emit('provider', tslib_1.__assign({}, thisExecution, { name: key, method: prop, result: promisedResult })); | ||
}); | ||
} | ||
else { | ||
instance.emit('provider', tslib_1.__assign({}, thisExecution, { name: key, method: prop, result: result })); | ||
} | ||
return result; | ||
}; | ||
} | ||
return target[prop]; | ||
}, | ||
return function (target, prop) { | ||
if (typeof target[prop] === 'function') { | ||
return function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var result = target[prop].apply(target, args); | ||
if (result instanceof Promise) { | ||
result.then(function (promisedResult) { | ||
instance.emit('provider', tslib_1.__assign({}, execution, { name: path, method: prop, args: args, result: promisedResult })); | ||
}); | ||
} | ||
else { | ||
instance.emit('provider', tslib_1.__assign({}, execution, { name: path, method: prop, args: args, | ||
result: result })); | ||
} | ||
return result; | ||
}; | ||
} | ||
else if (isObject(target[prop])) { | ||
return new Proxy(target[prop], { | ||
get: instance.createGetHandler(execution, path + '.' + prop), | ||
}); | ||
} | ||
else { | ||
currentContext[key] = _this.context[key]; | ||
} | ||
return currentContext; | ||
}, {}); | ||
return tslib_1.__assign({}, providers, executionContext); | ||
return target[prop]; | ||
}; | ||
}; | ||
ActionChain.prototype.getContext = function (thisExecution, executionContext) { | ||
var _this = this; | ||
var context = this.context; | ||
if (IS_DEVELOPMENT) { | ||
context = Object.keys(this.context).reduce(function (currentContext, key) { | ||
if (_this.options.providerExceptions.indexOf(key) === -1 && | ||
isObject(_this.context[key])) { | ||
currentContext[key] = new Proxy(_this.context[key], { | ||
get: _this.createGetHandler(thisExecution, key), | ||
}); | ||
} | ||
else { | ||
currentContext[key] = _this.context[key]; | ||
} | ||
return currentContext; | ||
}, {}); | ||
} | ||
return Object.assign({}, context, executionContext); | ||
}; | ||
return ActionChain; | ||
@@ -53,0 +68,0 @@ }(EventEmitter)); |
@@ -38,2 +38,7 @@ import * as tslib_1 from "tslib"; | ||
}, | ||
nestedProvider: { | ||
bar: function () { | ||
return 'baz'; | ||
}, | ||
}, | ||
}, | ||
@@ -99,2 +104,3 @@ test: new TestProvider(), | ||
name: 'foo', | ||
args: [], | ||
result: 'baz', | ||
@@ -105,2 +111,21 @@ }); | ||
}); | ||
test('should track execution of namespaced providers', function () { | ||
expect.assertions(2); | ||
var fn = action().test(function (_a) { | ||
var foo = _a.foo; | ||
expect(foo.nestedProvider.bar()).toBe('baz'); | ||
}); | ||
actionChain.once('provider', function (task) { | ||
expect(task).toEqual({ | ||
operatorId: 0, | ||
actionId: 0, | ||
executionId: 0, | ||
method: 'bar', | ||
name: 'foo.nestedProvider', | ||
args: [], | ||
result: 'baz', | ||
}); | ||
}); | ||
fn(); | ||
}); | ||
test('should track execution of class instance providers', function () { | ||
@@ -118,2 +143,3 @@ expect.assertions(2); | ||
method: 'foo', | ||
args: [], | ||
name: 'test', | ||
@@ -120,0 +146,0 @@ result: 'bar', |
@@ -35,2 +35,3 @@ import { EventEmitter } from 'betsy'; | ||
result: any; | ||
args: any[]; | ||
}; | ||
@@ -55,6 +56,4 @@ 'action:start': ActionExecution; | ||
constructor(context: Context, options?: ActionChainOptions); | ||
getContext(thisExecution: Execution, executionContext: ExecutionContext): { | ||
__execution: Execution; | ||
__path: string[]; | ||
}; | ||
private createGetHandler; | ||
getContext(thisExecution: Execution, executionContext: ExecutionContext): Context & ExecutionContext; | ||
} |
@@ -6,2 +6,5 @@ "use strict"; | ||
var IS_DEVELOPMENT = process.env.NODE_ENV !== 'production'; | ||
function isObject(value) { | ||
return typeof value === 'object' && !Array.isArray(value) && value !== null; | ||
} | ||
var ActionChain = /** @class */ (function (_super) { | ||
@@ -17,39 +20,51 @@ tslib_1.__extends(ActionChain, _super); | ||
} | ||
ActionChain.prototype.getContext = function (thisExecution, executionContext) { | ||
var _this = this; | ||
ActionChain.prototype.createGetHandler = function (execution, path) { | ||
var instance = this; | ||
var providers = Object.keys(this.context).reduce(function (currentContext, key) { | ||
if (IS_DEVELOPMENT && | ||
_this.options.providerExceptions.indexOf(key) === -1) { | ||
currentContext[key] = new Proxy(_this.context[key], { | ||
get: function (target, prop) { | ||
if (typeof target[prop] === 'function') { | ||
return function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var result = target[prop].apply(target, args); | ||
if (result instanceof Promise) { | ||
result.then(function (promisedResult) { | ||
instance.emit('provider', tslib_1.__assign({}, thisExecution, { name: key, method: prop, result: promisedResult })); | ||
}); | ||
} | ||
else { | ||
instance.emit('provider', tslib_1.__assign({}, thisExecution, { name: key, method: prop, result: result })); | ||
} | ||
return result; | ||
}; | ||
} | ||
return target[prop]; | ||
}, | ||
return function (target, prop) { | ||
if (typeof target[prop] === 'function') { | ||
return function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var result = target[prop].apply(target, args); | ||
if (result instanceof Promise) { | ||
result.then(function (promisedResult) { | ||
instance.emit('provider', tslib_1.__assign({}, execution, { name: path, method: prop, args: args, result: promisedResult })); | ||
}); | ||
} | ||
else { | ||
instance.emit('provider', tslib_1.__assign({}, execution, { name: path, method: prop, args: args, | ||
result: result })); | ||
} | ||
return result; | ||
}; | ||
} | ||
else if (isObject(target[prop])) { | ||
return new Proxy(target[prop], { | ||
get: instance.createGetHandler(execution, path + '.' + prop), | ||
}); | ||
} | ||
else { | ||
currentContext[key] = _this.context[key]; | ||
} | ||
return currentContext; | ||
}, {}); | ||
return tslib_1.__assign({}, providers, executionContext); | ||
return target[prop]; | ||
}; | ||
}; | ||
ActionChain.prototype.getContext = function (thisExecution, executionContext) { | ||
var _this = this; | ||
var context = this.context; | ||
if (IS_DEVELOPMENT) { | ||
context = Object.keys(this.context).reduce(function (currentContext, key) { | ||
if (_this.options.providerExceptions.indexOf(key) === -1 && | ||
isObject(_this.context[key])) { | ||
currentContext[key] = new Proxy(_this.context[key], { | ||
get: _this.createGetHandler(thisExecution, key), | ||
}); | ||
} | ||
else { | ||
currentContext[key] = _this.context[key]; | ||
} | ||
return currentContext; | ||
}, {}); | ||
} | ||
return Object.assign({}, context, executionContext); | ||
}; | ||
return ActionChain; | ||
@@ -56,0 +71,0 @@ }(betsy_1.EventEmitter)); |
@@ -40,2 +40,7 @@ "use strict"; | ||
}, | ||
nestedProvider: { | ||
bar: function () { | ||
return 'baz'; | ||
}, | ||
}, | ||
}, | ||
@@ -101,2 +106,3 @@ test: new TestProvider(), | ||
name: 'foo', | ||
args: [], | ||
result: 'baz', | ||
@@ -107,2 +113,21 @@ }); | ||
}); | ||
test('should track execution of namespaced providers', function () { | ||
expect.assertions(2); | ||
var fn = action().test(function (_a) { | ||
var foo = _a.foo; | ||
expect(foo.nestedProvider.bar()).toBe('baz'); | ||
}); | ||
actionChain.once('provider', function (task) { | ||
expect(task).toEqual({ | ||
operatorId: 0, | ||
actionId: 0, | ||
executionId: 0, | ||
method: 'bar', | ||
name: 'foo.nestedProvider', | ||
args: [], | ||
result: 'baz', | ||
}); | ||
}); | ||
fn(); | ||
}); | ||
test('should track execution of class instance providers', function () { | ||
@@ -120,2 +145,3 @@ expect.assertions(2); | ||
method: 'foo', | ||
args: [], | ||
name: 'test', | ||
@@ -122,0 +148,0 @@ result: 'bar', |
{ | ||
"name": "action-chain", | ||
"version": "1.0.0-1532533507115", | ||
"version": "1.0.0-1532590229766", | ||
"description": "Functional actions", | ||
@@ -34,5 +34,5 @@ "author": "Christian Alfoni <christianalfoni@gmail.com>", | ||
"tslib": "^1.9.3", | ||
"betsy": "1.0.0-1532533507115" | ||
"betsy": "1.0.0-1532590229766" | ||
}, | ||
"devDependencies": {} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
109031
1105
+ Addedbetsy@1.0.0-1532590229766(transitive)
- Removedbetsy@1.0.0-1532533507115(transitive)
Updatedbetsy@1.0.0-1532590229766