123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- const typeChecker = require('typechecker')
- module.exports = function ambi (method, ...args) {
-
- let fireMethod, introspectMethod
-
-
-
- if ( typeChecker.isArray(method) ) {
- [fireMethod, introspectMethod] = method
- }
- else {
- fireMethod = introspectMethod = method
- }
-
- const simpleArguments = args.slice(0, -1)
- const completionCallback = args.slice(-1)[0]
-
- if ( !typeChecker.isFunction(completionCallback) ) {
- throw new Error('ambi was called without a completion callback')
- }
-
- const givenArgumentsLength = args.length
- const acceptedArgumentsLength = introspectMethod.length
- let argumentsDifferenceLength = null
- let executeAsynchronously = null
-
-
-
- if ( givenArgumentsLength === acceptedArgumentsLength ) {
- executeAsynchronously = true
- }
-
-
-
-
- else if ( givenArgumentsLength < acceptedArgumentsLength ) {
- executeAsynchronously = true
- argumentsDifferenceLength = acceptedArgumentsLength - givenArgumentsLength
- args = simpleArguments.slice().concat(new Array(argumentsDifferenceLength)).concat([completionCallback])
- }
-
-
-
-
-
-
- else {
- executeAsynchronously = false
- args = simpleArguments.slice()
- }
-
- if ( executeAsynchronously ) {
-
- fireMethod(...args)
- }
-
-
- else {
-
- const result = fireMethod(...args)
-
- if ( typeChecker.isError(result) ) {
-
- const err = result
- completionCallback(err)
- }
- else {
-
- completionCallback(null, result)
- }
- }
-
-
-
-
- return null
- }
|