Skip to content

Commit

Permalink
fix(operators): cleanup & optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanGerbeth committed Nov 26, 2024
1 parent 1fb04e5 commit 200b476
Show file tree
Hide file tree
Showing 34 changed files with 245 additions and 272 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file removed packages/observables/index.js
Empty file.
2 changes: 1 addition & 1 deletion packages/observables/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"type": "module",
"main": "index.js",
"main": "src/index.js",
"files": [
"./src/*"
],
Expand Down
1 change: 1 addition & 0 deletions packages/observables/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { connectionObservable } from './dom/window';
12 changes: 0 additions & 12 deletions packages/operators/fixtures/paginatedFetchFixture.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/operators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"type": "module",
"main": "index.js",
"main": "src/index.js",
"files": [
"./src/*"
],
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TestScheduler } from 'rxjs/testing';
import { beforeEach, describe, expect, test } from 'vitest';

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

describe('cache', () => {
let testScheduler;
Expand All @@ -22,7 +23,9 @@ describe('cache', () => {
testScheduler.run(({ cold, expectObservable }) => {
const stream = cold('a', { a: () => triggerVal.shift() }).pipe(
map(fn => fn()),
cache({ ttl: 2 })
log('operators:cache:default:input'),
cache({ ttl: 2 }),
log('operators:cache:default:output')
);

const unsubA = '-^!';
Expand Down
2 changes: 1 addition & 1 deletion packages/operators/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export {
resolveBlob,
distinctUntilResponseChanged
} from './request/response';
export { retryWhenError } from './request/retry';
export { retryWhenRequestError } from './retry';
35 changes: 0 additions & 35 deletions packages/operators/src/json.js

This file was deleted.

10 changes: 0 additions & 10 deletions packages/operators/src/json/replacer.js

This file was deleted.

26 changes: 0 additions & 26 deletions packages/operators/src/json/reviver.js

This file was deleted.

30 changes: 0 additions & 30 deletions packages/operators/src/json/reworker/default.js

This file was deleted.

43 changes: 0 additions & 43 deletions packages/operators/src/json/reworker/token.js

This file was deleted.

75 changes: 51 additions & 24 deletions packages/operators/src/log.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
import { bgGreen } from 'ansi-colors';
import debug from 'debug';
import { connectable, finalize, Observable, Subject } from 'rxjs';
import { connectable, finalize, Subject, tap } from 'rxjs';

import { pipeWhen } from './when';

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

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

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();
}
});
});
};
} else {
return source => source;
}
return source =>
source.pipe(
pipeWhen(
() => debug.enabled(tag),
source =>
source.pipe(
tap({
subscribe: () => logger('subscribed'),
unsubscribe: () => logger('unsubscribed'),
finalize: () => logger('finalize'),
next: val => logger(val),
error: err => error(err),
complete: () => logger(bgGreen.bold('complete!'))
})
)
)
);
};

export const logResult = (tag, observable) => {
Expand All @@ -46,3 +43,33 @@ 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;
// };
11 changes: 11 additions & 0 deletions packages/operators/src/log.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, test } from 'vitest';

describe('log', () => {
test('default', () => {
//TODO: add test
});

test('logResult', () => {
//TODO: add test
});
});
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { concatMap, from, throwError } from 'rxjs';

import { cache } from './cache';
import { resolveBlob, resolveJSON, resolveText } from './response';
import { retryWhenError } from './retry';
import { retryWhenRequestError } from './retry';

export const request = ({ retry, cache: cacheOptions } = {}) => {
return source =>
Expand All @@ -14,7 +14,7 @@ export const request = ({ retry, cache: cacheOptions } = {}) => {
return throwError(() => new Error('Failed to fetch: resource not valid'));
}
}),
retryWhenError(retry),
retryWhenRequestError(retry),
cache(cacheOptions)
);
};
Expand Down
Loading

0 comments on commit 200b476

Please sign in to comment.