Skip to content

Commit

Permalink
Merge pull request #33 from npetillon/master
Browse files Browse the repository at this point in the history
Bug fix: `optionalParams` not being destructured properly in logLevel
  • Loading branch information
npetillon authored Jun 12, 2020
2 parents 04e8870 + f2784a5 commit 56bfef0
Show file tree
Hide file tree
Showing 7 changed files with 960 additions and 1,788 deletions.
2,645 changes: 874 additions & 1,771 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-toolkit",
"version": "9.0.1",
"version": "9.0.4",
"license": "MIT",
"scripts": {
"ng": "ng",
Expand Down
8 changes: 8 additions & 0 deletions projects/logger/src/lib/console-logger.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ describe('ConsoleLoggerService', () => {
service.log('log');
expect(toSpy.log).not.toHaveBeenCalledWith('log');
});

it('check console logger logLevel', () => {
service = new ConsoleLoggerService(Level.LOG);

const args: any = {data: {foo: 'bar'}};
service.logLevel(Level.INFO, 'test', args);
expect(toSpy.log).toHaveBeenCalledWith('test', args);
});
});
2 changes: 1 addition & 1 deletion projects/logger/src/lib/logger.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ describe('LoggerDecorator', () => {

const test: Test = new Test();
expect(test.toto(1)).toBe(2);
expect(LOGGER_DECORATOR_DATA.loggerService.debug).toHaveBeenCalledWith('increment', [[1], 2]);
expect(LOGGER_DECORATOR_DATA.loggerService.debug).toHaveBeenCalledWith('increment', [1], 2);
});
});
10 changes: 5 additions & 5 deletions projects/logger/src/lib/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ export class LoggerService {
logLevel(level: Level, message?: any, ...optionalParams: any[]) {
switch (level) {
case Level.ERROR :
this.error(message, optionalParams);
this.error(message, ...optionalParams);
break;
case Level.WARN :
this.warn(message, optionalParams);
this.warn(message, ...optionalParams);
break;
case Level.INFO :
this.info(message, optionalParams);
this.info(message, ...optionalParams);
break;
case Level.DEBUG :
this.debug(message, optionalParams);
this.debug(message, ...optionalParams);
break;
case Level.LOG :
this.log(message, optionalParams);
this.log(message, ...optionalParams);
break;
}
}
Expand Down
15 changes: 7 additions & 8 deletions projects/utils/src/lib/wait.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,25 @@ class ClassTest {
}
}

function testIt(classTest: ClassTest, fn: Function, done: DoneFn, firstPassValue: number = 3, secondPassValue: number = 4) {
function testIt(classTest: ClassTest, fn: Function, done: DoneFn, firstPassValue: number = 2, secondPassValue: number = 3) {
fn.apply(classTest);

timer(WAIT_TIME / 2).subscribe(() => {
timer(WAIT_TIME * 1.1).subscribe(() => {
fn.apply(classTest);
fn.apply(classTest);
});

timer(WAIT_TIME * 2.1).pipe(
timer(WAIT_TIME * 2.2).pipe(
tap(() => {
expect(classTest.incr).toEqual(firstPassValue);

fn.apply(classTest);
}),
delay(WAIT_TIME * 1.1)
)
.subscribe(() => {
expect(classTest.incr).toEqual(secondPassValue);
done();
});
).subscribe(() => {
expect(classTest.incr).toEqual(secondPassValue);
done();
});
}

describe('WaitDecorator', () => {
Expand Down
66 changes: 64 additions & 2 deletions projects/utils/src/lib/wait.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,69 @@
import {Queue} from './queue.decorator';
import {Subscription} from 'rxjs';
import {Deferred, isPromise} from './functions';

export interface WaitData {
wait?: Function;
promise: Promise<any>;
}

/**
* Wait for a Promise / Subscription before to be re-executed
* /!\ the method result is modified => Return a Promise
*/
export const Wait = (name?: string) => Queue.bind(this, 1, name);
export function Wait(name?: string): MethodDecorator {
return function(
target: Object,
propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<any>
): TypedPropertyDescriptor<any> | void {
// Init
name = name || propertyKey.toString();
const data: WaitData = (target[`_ngxWait_${name}`] = target[`_ngxWait_${name}`] || {
promise: Promise.resolve()
});
const originalMethod = descriptor.value;

// Change method
descriptor.value = function(...args: any[]) {
// Push Future call
const deferred: Deferred<any> = new Deferred<any>();

// Ignore next call
if (!data.wait) {
data.wait = () => {
try {
const result: any = originalMethod.apply(this, args);

if (isPromise(result)) {
result.then(r => deferred.resolve(r), e => deferred.reject(e));
} else if (result instanceof Subscription) {
result.add(() => deferred.resolve());
} else {
deferred.resolve(result);
}
} catch (e) {
deferred.reject(e);
}

return deferred.promise;
};

// Execute
data.promise = data.promise

// Call
.then(() => data.wait.apply(this))
// Pop
.then(() => data.wait = null, () => data.wait = null);
} else {
deferred.reject('Wait: Function is running');
}

// Return promise
return deferred.promise;
};

return descriptor;
};
}

0 comments on commit 56bfef0

Please sign in to comment.