Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) Issue #416. Use typeof rather than instanceof Function. #421

Merged
merged 1 commit into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions common/core/src/promise_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export type HttpResponseCallback<TResult> = TripleValueCallback<TResult, any>;
*/
export function callbackToPromise<TResult>(callBackOperation: (callback: Callback<TResult>) => void, userCallback?: Callback<TResult>): Promise<TResult> | void {
if (userCallback) {
if (!(userCallback instanceof Function)) {
if (!((typeof userCallback) === 'function')) {
throw new TypeError('Callback has to be a Function');
}

Expand Down Expand Up @@ -144,7 +144,7 @@ export function errorCallbackToPromise(callBackOperation: (callback: ErrorCallba
*/
export function noErrorCallbackToPromise<TResult>(callBackOperation: (callback: NoErrorCallback<TResult>) => void, userCallback?: NoErrorCallback<TResult>): Promise<TResult> | void {
if (userCallback) {
if (!(userCallback instanceof Function)) {
if (!((typeof userCallback) === 'function')) {
throw new TypeError('Callback has to be a Function');
}

Expand Down Expand Up @@ -193,7 +193,7 @@ export function doubleValueCallbackToPromise<TResult1, TResult2, TPromiseResult>
packResults: (result1: TResult1, result2: TResult2) => TPromiseResult,
userCallback?: DoubleValueCallback<TResult1, TResult2>): Promise<TPromiseResult> | void {
if (userCallback) {
if (!(userCallback instanceof Function)) {
if (!((typeof userCallback) === 'function')) {
throw new TypeError('Callback has to be a Function');
}

Expand Down Expand Up @@ -254,7 +254,7 @@ export function tripleValueCallbackToPromise<TResult1, TResult2, TPromiseResult>
packResults: (result1: TResult1, result2: TResult2) => TPromiseResult,
userCallback?: TripleValueCallback<TResult1, TResult2>): Promise<TPromiseResult> | void {
if (userCallback) {
if (!(userCallback instanceof Function)) {
if (!((typeof userCallback) === 'function')) {
throw new TypeError('Callback has to be a Function');
}

Expand Down
2 changes: 1 addition & 1 deletion device/core/src/module_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class ModuleClient extends InternalClient {
invokeMethod(deviceId: string, moduleIdOrMethodParams: string | MethodParams, methodParamsOrCallback: MethodParams | Callback<MethodResult>, callback?: Callback<MethodResult>): Promise<MethodResult> | void {
if (callback) {
return this._invokeMethod(deviceId, moduleIdOrMethodParams as string, methodParamsOrCallback as MethodParams, callback);
} else if (methodParamsOrCallback instanceof Function) {
} else if ((typeof methodParamsOrCallback) === 'function') {
return this._invokeMethod(deviceId, moduleIdOrMethodParams as MethodParams, methodParamsOrCallback as Callback<MethodResult>);
}

Expand Down
20 changes: 10 additions & 10 deletions provisioning/service/src/provisioningserviceclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ export class ProvisioningServiceClient {
* @returns {Promise<void> | void} Promise if no callback function was passed, void otherwise.
*/
public deleteIndividualEnrollment(enrollmentOrId: string | IndividualEnrollment, etagOrCallback?: string | ErrorCallback, deleteCallback?: ErrorCallback): Promise<void> | void {
if (deleteCallback && !(deleteCallback instanceof Function)) {
if (deleteCallback && !((typeof deleteCallback) === 'function')) {
throw new ArgumentError('Callback has to be a Function');
}

if (!deleteCallback && etagOrCallback instanceof Function) {
deleteCallback = etagOrCallback;
if (!deleteCallback && ((typeof etagOrCallback) === 'function')) {
deleteCallback = etagOrCallback as ErrorCallback;
etagOrCallback = undefined;
}

Expand Down Expand Up @@ -133,12 +133,12 @@ export class ProvisioningServiceClient {
* @returns {Promise<void> | void} Promise if no callback function was passed, void otherwise.
*/
public deleteEnrollmentGroup(enrollmentGroupOrId: string | EnrollmentGroup, etagOrCallback?: string | ErrorCallback, deleteCallback?: ErrorCallback): Promise<void> | void {
if (deleteCallback && !(deleteCallback instanceof Function)) {
if (deleteCallback && !((typeof deleteCallback) === 'function')) {
throw new ArgumentError('Callback has to be a Function');
}

if (etagOrCallback instanceof Function) {
deleteCallback = etagOrCallback;
if ((typeof etagOrCallback) === 'function') {
deleteCallback = etagOrCallback as ErrorCallback;
etagOrCallback = undefined;
}

Expand Down Expand Up @@ -233,12 +233,12 @@ export class ProvisioningServiceClient {
* @returns {Promise<void> | void} Promise if no callback function was passed, void otherwise.
*/
public deleteDeviceRegistrationState(idOrRegistrationState: string | DeviceRegistrationState, etagOrCallback?: string | ErrorCallback, deleteCallback?: ErrorCallback): Promise<void> | void {
if (deleteCallback && !(deleteCallback instanceof Function)) {
if (deleteCallback && !((typeof deleteCallback) === 'function')) {
throw new ArgumentError('Callback has to be a Function');
}

if (etagOrCallback instanceof Function) {
deleteCallback = etagOrCallback;
if ((typeof etagOrCallback) === 'function') {
deleteCallback = etagOrCallback as ErrorCallback;
etagOrCallback = undefined;
}

Expand Down Expand Up @@ -334,7 +334,7 @@ export class ProvisioningServiceClient {
let suppliedCallback: ErrorCallback;
let id: string;

suppliedCallback = deleteCallback || (etagOrCallback instanceof Function ? etagOrCallback : undefined);
suppliedCallback = deleteCallback || (((typeof etagOrCallback) === 'function') ? etagOrCallback as ErrorCallback : undefined);
if (!suppliedCallback) {
throw new ArgumentError('No callback was passed.');
}
Expand Down
4 changes: 2 additions & 2 deletions provisioning/service/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class Query {
* @returns {Promise<ResultWithHttpResponse<QueryResult>> | void} Promise if no callback function was passed, void otherwise.
*/
next(continuationTokenOrCallback: string | QueryCallback, done?: QueryCallback): Promise<ResultWithHttpResponse<QueryResult>> | void {
const callback = done || (continuationTokenOrCallback instanceof Function ? continuationTokenOrCallback : undefined);
const callback = done || (((typeof continuationTokenOrCallback) === 'function') ? continuationTokenOrCallback : undefined);

return httpCallbackToPromise((_callback) => {
let actualContinuationToken = this.continuationToken;
Expand All @@ -76,6 +76,6 @@ export class Query {
actualCallback(null, result, response);
}
});
}, callback);
}, callback as QueryCallback);
}
}
2 changes: 1 addition & 1 deletion service/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class Client extends EventEmitter {
invokeDeviceMethod(deviceId: string, methodParams: DeviceMethodParams, done?: IncomingMessageCallback<any>): void;
invokeDeviceMethod(deviceId: string, moduleId: string, methodParams: DeviceMethodParams, done?: IncomingMessageCallback<any>): void;
invokeDeviceMethod(deviceId: string, moduleIdOrMethodParams: string | DeviceMethodParams, methodParamsOrDone?: DeviceMethodParams | IncomingMessageCallback<any>, done?: IncomingMessageCallback<any>): Promise<ResultWithIncomingMessage<any>> | void {
const callback = done || (methodParamsOrDone instanceof Function ? methodParamsOrDone : undefined);
const callback = done || (((typeof methodParamsOrDone) === 'function') ? methodParamsOrDone as IncomingMessageCallback<any> : undefined);
if (callback) {
return this._invokeDeviceMethod(deviceId, moduleIdOrMethodParams, methodParamsOrDone, done);
}
Expand Down
4 changes: 2 additions & 2 deletions service/src/job_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export class JobClient {
* @throws {TypeError} If the callback is not the last parameter
*/
scheduleDeviceMethod(jobId: string | number, queryCondition: string, methodParams: DeviceMethodParams, jobStartTime?: Date | TripleValueCallback<any, any>, maxExecutionTimeInSeconds?: number | TripleValueCallback<any, any>, done?: TripleValueCallback<any, any>): Promise<JobStatusResponse> | void {
const callback = jobStartTime instanceof Function ? jobStartTime : (maxExecutionTimeInSeconds instanceof Function ? maxExecutionTimeInSeconds : done);
const callback = ((typeof jobStartTime) === 'function') ? jobStartTime as TripleValueCallback<any, any> : (((typeof maxExecutionTimeInSeconds) === 'function') ? maxExecutionTimeInSeconds as TripleValueCallback<any, any> : done);

return tripleValueCallbackToPromise((_callback) => {
/*Codes_SRS_NODE_JOB_CLIENT_16_013: [The `scheduleDeviceMethod` method shall throw a `ReferenceError` if `jobId` is `null`, `undefined` or an empty string.]*/
Expand Down Expand Up @@ -254,7 +254,7 @@ export class JobClient {
* @throws {TypeError} If the callback is not the last parameter
*/
scheduleTwinUpdate(jobId: string | number, queryCondition: string, patch: any, jobStartTime?: Date | TripleValueCallback<any, any>, maxExecutionTimeInSeconds?: number | TripleValueCallback<any, any>, done?: TripleValueCallback<any, any>): Promise<JobStatusResponse> | void {
const callback = jobStartTime instanceof Function ? jobStartTime : (maxExecutionTimeInSeconds instanceof Function ? maxExecutionTimeInSeconds : done);
const callback = ((typeof jobStartTime) === 'function') ? jobStartTime as TripleValueCallback<any, any> : (((typeof maxExecutionTimeInSeconds) === 'function') ? maxExecutionTimeInSeconds as TripleValueCallback<any, any> : done);

return tripleValueCallbackToPromise((_callback) => {
/*Codes_SRS_NODE_JOB_CLIENT_16_021: [The `scheduleTwinUpdate` method shall throw a `ReferenceError` if `jobId` is `null`, `undefined` or an empty string.]*/
Expand Down
8 changes: 4 additions & 4 deletions service/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ export class Registry {
updateConfiguration(configuration: Configuration, done: HttpResponseCallback<any>): void;
updateConfiguration(configuration: Configuration, forceUpdate: boolean, done?: HttpResponseCallback<any>): void;
updateConfiguration(configuration: Configuration, forceUpdateOrDone: boolean | HttpResponseCallback<any>, done?: HttpResponseCallback<any>): Promise<ResultWithHttpResponse<any>> | void {
const callback = done || (forceUpdateOrDone instanceof Function ? forceUpdateOrDone : undefined);
const callback = done || (((typeof forceUpdateOrDone) === 'function') ? forceUpdateOrDone : undefined);

if (callback) {
return this._updateConfiguration(configuration, forceUpdateOrDone, done);
Expand Down Expand Up @@ -1121,7 +1121,7 @@ export class Registry {
updateModule(module: Module, done: TripleValueCallback<any, any>): void;
updateModule(module: Module, forceUpdate: boolean, done: HttpResponseCallback<any>): void;
updateModule(module: Module, forceUpdateOrDone: boolean | HttpResponseCallback<any>, done?: HttpResponseCallback<any>): Promise<ResultWithHttpResponse<any>> | void {
const callback = done || (forceUpdateOrDone instanceof Function ? forceUpdateOrDone : undefined);
const callback = done || (((typeof forceUpdateOrDone) === 'function') ? forceUpdateOrDone : undefined);

if (callback) {
return this._updateModule(module, forceUpdateOrDone, done);
Expand Down Expand Up @@ -1198,10 +1198,10 @@ export class Registry {
removeModule(module: Module, done: TripleValueCallback<any, any>): void;
removeModule(deviceId: string, moduleId: string, done: TripleValueCallback<any, any>): void;
removeModule(moduleOrDeviceId: Module | string, doneOrModuleId: HttpResponseCallback<any> | string, done?: HttpResponseCallback<any>): Promise<ResultWithHttpResponse<any>> | void {
const callback = done || (doneOrModuleId instanceof Function ? doneOrModuleId : undefined);
const callback = done || (((typeof doneOrModuleId) === 'function') ? doneOrModuleId : undefined);

if (callback) {
return this._removeModule(moduleOrDeviceId, doneOrModuleId, callback);
return this._removeModule(moduleOrDeviceId, doneOrModuleId, callback as HttpResponseCallback<any>);
}

return httpCallbackToPromise((_callback) => {
Expand Down