Skip to content

Commit

Permalink
fix(operators): refactored log
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanGerbeth committed Nov 27, 2024
1 parent 39aa30c commit 345487e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 57 deletions.
19 changes: 14 additions & 5 deletions packages/operators/src/cache.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { mockResponse } from '#mocks/response.js';
import { map } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { beforeEach, describe, expect, test } from 'vitest';
import { afterAll, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';

import { cache } from './cache';
import { log } from './log';

describe('cache', () => {
let testScheduler;

beforeAll(async () => {
global.Response = mockResponse();
});

beforeEach(() => {
testScheduler = new TestScheduler((actual, expected) => expect(actual).deep.equal(expected));
});

afterAll(() => {
vi.restoreAllMocks();
});

test('default', () => {
const expectedVal = {
a: new Response('initial', { status: 200 }),
b: new Response('updated', { status: 200 })
a: new Response('initial'),
b: new Response('updated')
};

const triggerVal = [expectedVal.a, expectedVal.b];
Expand All @@ -24,8 +33,8 @@ describe('cache', () => {
const stream = cold('a', { a: () => triggerVal.shift() }).pipe(
map(fn => fn()),
log('operators:cache:default:input'),
cache({ ttl: 2 })
// log('operators:cache:default:output')
cache({ ttl: 2 }),
log('operators:cache:default:output')
);

const unsubA = '-^!';
Expand Down
74 changes: 24 additions & 50 deletions packages/operators/src/log.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import { bgGreen } from 'ansi-colors';
import debug from 'debug';
import { connectable, finalize, Subject, tap } from 'rxjs';
import { connectable, finalize, Observable, Subject } from 'rxjs';

import { pipeWhen } from './when';

export const enableLog = tag => {
debug.enable(tag);
};
export const enableLog = tag => debug.enable(tag);

export const log = tag => {
const logger = debug(tag);
logger.log = global.console.log.bind(console);
const error = debug(`${tag}:error`);

return source =>
source.pipe(
pipeWhen(
() => debug.enabled(tag),
tap({
subscribe: () => logger('subscribed'),
unsubscribe: () => logger('unsubscribed'),
finalize: () => logger('finalize'),
next: val => logger(val),
error: err => error(err),
complete: () => logger(bgGreen.bold('complete!'))
})
)
);
if (debug.enabled(tag)) {
return source => {
return new Observable(observer => {
return source.subscribe({
next: val => {
logger(val);
observer.next(val);
},
error: err => {
error(err);
observer.error(err);
},
complete: () => {
logger(bgGreen.bold('complete!'));
observer.complete();
}
});
});
};
}

return source => source;
};

export const logResult = (tag, observable) => {
Expand All @@ -40,33 +44,3 @@ export const logResult = (tag, observable) => {
).connect();
});
};

// export const log = tag => {
// var logger = debug(tag);
// logger.log = global.console.log.bind(console);
// var error = debug(`${tag}:error`);

// if (debug.enabled(tag)) {
// return source =>
// new Observable(observer => {
// source.subscribe({
// subscribe: () => logger('subscribed'),
// unsubscribe: () => logger('unsubscribed'),
// finalize: () => logger('finalize'),
// next: val => {
// logger(val);
// observer.next(val);
// },
// error: err => {
// error(err);
// observer.error(err);
// },
// complete: () => {
// logger(bgGreen.bold('complete!'));
// observer.complete();
// }
// });
// });
// }
// return source => source;
// };
6 changes: 5 additions & 1 deletion packages/operators/src/retry.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { map } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { beforeEach, describe, expect, test } from 'vitest';
import { afterAll, beforeEach, describe, expect, test, vi } from 'vitest';

import { log } from './log';
import { retryWhenRequestError } from './retry';
Expand All @@ -12,6 +12,10 @@ describe('request retry', () => {
testScheduler = new TestScheduler((actual, expected) => expect(actual).deep.equal(expected));
});

afterAll(() => {
vi.restoreAllMocks();
});

test('2x error -> 1x success', () => {
const expectedVal = {
a: new Response('', { status: 500 }),
Expand Down
6 changes: 5 additions & 1 deletion packages/operators/src/when.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { map } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { beforeEach, describe, expect, test } from 'vitest';
import { afterAll, beforeEach, describe, expect, test, vi } from 'vitest';

import { log } from './log';
import { pipeWhen } from './when';
Expand All @@ -12,6 +12,10 @@ describe('when', () => {
testScheduler = new TestScheduler((actual, expected) => expect(actual).deep.equal(expected));
});

afterAll(() => {
vi.restoreAllMocks();
});

test('default', () => {
const triggerVal = {
a: 1,
Expand Down

0 comments on commit 345487e

Please sign in to comment.