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

seCacheReplay #1

Merged
merged 5 commits into from
Jul 4, 2024
Merged
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
7 changes: 6 additions & 1 deletion dist/kaysh.cjs.development.js

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

2 changes: 1 addition & 1 deletion dist/kaysh.cjs.development.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kaysh.cjs.production.min.js

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

2 changes: 1 addition & 1 deletion dist/kaysh.cjs.production.min.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions dist/kaysh.esm.js

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

2 changes: 1 addition & 1 deletion dist/kaysh.esm.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/lib/simple-cache-store.d.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ export interface DefaultConfigCacheModel {
localStorage?: boolean;
maxTime?: number;
maxItems?: number;
useCacheReplay?: boolean;
}
declare function setCacheValue(key: string, value: any, argsToHash?: any, config?: DefaultConfigCacheModel): void;
declare function getCacheValue(key: string, argsToHash?: any): any;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@karacas/kaysh",
"description": "In-memory caching with support for RxJs observables, native promises, functions & methods. Optional localStorage.",
"version": "0.1.89",
"version": "0.2.02",
"author": "karacas",
"module": "dist/kaysh.esm.js",
"repository": {
3 changes: 3 additions & 0 deletions src/lib/rxjs-cache-store.ts
Original file line number Diff line number Diff line change
@@ -26,6 +26,9 @@ const setRxjsObservableCacheValue = (
if ($data != null) __simpleCacheStore.setCacheValue(key, $data, argsToHash, config);
};

let useCacheReplay = config?.useCacheReplay !== false;
if (useCacheReplay === false) return stream.pipe(tap((_data: any) => setCache(_data)));

let cacheReplay = stream.pipe(shareReplay());

setCache(cacheReplay);
2 changes: 2 additions & 0 deletions src/lib/simple-cache-store.ts
Original file line number Diff line number Diff line change
@@ -42,12 +42,14 @@ export interface DefaultConfigCacheModel {
localStorage?: boolean;
maxTime?: number;
maxItems?: number;
useCacheReplay?: boolean;
}

const defaultConfig: DefaultConfigCacheModel = {
localStorage: false,
maxTime: 0,
maxItems: 10,
useCacheReplay: true,
};

let store_state: StoredValueModelObjRoot = {};
35 changes: 35 additions & 0 deletions test/rxjs-cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { of } from 'rxjs/internal/observable/of';
import { delay, map, tap } from 'rxjs/operators';
import { kayshOperator, rxjsKaysh, simpleKaysh } from '../src/';
import { Subject } from 'rxjs/internal/Subject';

const _log = console.log;
const _timeout = ms => new Promise(res => setTimeout(res, ms));
@@ -401,3 +402,37 @@ test('Test customOperator LS', async function() {
let cache5 = await testCustomPipe().toPromise();
expect(cache5.val).toStrictEqual(cache4.val);
});

const fakeHttpVal = () => {
const __subject = new Subject<any>();

setTimeout(() => __subject.next(Math.random()), 10);

return __subject.pipe(kayshOperator('__subjectCache', null, { useCacheReplay: false }));
};

const fakeSubscription = () => {
let subscription: any = null;

let ___promise = new Promise((resolve, reject) => {
subscription = fakeHttpVal().subscribe(data => resolve(data));
});

return { promise: ___promise, subscription };
};

test('Test get cache useCacheReplay', async function() {
let _fakeSubscription = fakeSubscription();
let _fakeSubscription2 = fakeSubscription();

let cache1 = await _fakeSubscription.promise;
let cache2 = await _fakeSubscription2.promise;

expect(cache1).not.toStrictEqual(cache2);

let _fakeSubscription3 = fakeSubscription();
let cache3 = await _fakeSubscription3.promise;
if (false) console.log(cache1, cache2, cache3);

expect(cache2).toStrictEqual(cache3);
});