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

Introduce clearNavigationCache() to clear children and titleResolver cache #2383

Merged
merged 3 commits into from
Nov 18, 2021
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
27 changes: 27 additions & 0 deletions core/src/core-api/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AsyncHelpers, EventListenerHelpers, GenericHelpers, StateHelpers } from '../utilities/helpers';
import { LuigiAuth, LuigiElements } from '.';
import { AuthLayerSvc, LifecycleHooks } from '../services';
import { NodeDataManagementStorage } from '../services/node-data-management.js';
/**
* @name Configuration
*/
Expand Down Expand Up @@ -279,6 +280,32 @@ class LuigiConfig {
this.unload();
this.setConfig(cfg);
}

/**
* Clear navigation node related caches.
* @memberof Configuration
* @example
* Luigi.clearNavigationCache();
* @since NEXT_RELEASE
*/
clearNavigationCache() {
NodeDataManagementStorage.deleteCache();

const clearTitleResolverCache = nodes => {
if (nodes && nodes.forEach) {
nodes.forEach(node => {
if (node.titleResolver && node.titleResolver._cache) {
node.titleResolver._cache = undefined;
}
if (node.children) {
clearTitleResolverCache(node.children);
}
});
}
};

clearTitleResolverCache(this.getConfig().navigation.nodes);
}
}
Comment on lines +291 to 309
Copy link
Contributor

@wdoberschuetz wdoberschuetz Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(q) We clear the cache of the nested navigation nodes - shouldn't we return a warning of some kind of any of the nodes do not meet the criteria (i.e. titleResolver doesn't exist in the node) within the ifs? I presume it makes no sense because we can only clean caches if the titleResolver already exists... but my questions stands - should we inform somehow anything/anybody that it's been cleaned or (not) cleaned? :)


export const config = new LuigiConfig();
66 changes: 33 additions & 33 deletions core/test/services/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ describe('Config', () => {

// then
assert.equal(LuigiConfig.getConfigBooleanValue('whatever'), false);
assert.equal(
LuigiConfig.getConfigBooleanValue('whateverparent.whateverchild'),
false
);
assert.equal(LuigiConfig.getConfigBooleanValue('whateverparent.whateverchild'), false);
});
});

Expand All @@ -51,42 +48,28 @@ describe('Config', () => {
//given
LuigiConfig.config = {
truthyFn: (param1, param2) => 'value' + param1 + param2,
truthyFnAsync: (param1, param2) =>
Promise.resolve('value' + param1 + param2)
truthyFnAsync: (param1, param2) => Promise.resolve('value' + param1 + param2)
};

const resultTruthyFn = await LuigiConfig.executeConfigFnAsync(
'truthyFn',
false,
'foo',
'bar'
);
const resultTruthyFn = await LuigiConfig.executeConfigFnAsync('truthyFn', false, 'foo', 'bar');
assert.equal(resultTruthyFn, 'valuefoobar');

const resultTruthyFnAsync = await LuigiConfig.executeConfigFnAsync(
'truthyFnAsync',
false,
'foo',
'bar'
);
const resultTruthyFnAsync = await LuigiConfig.executeConfigFnAsync('truthyFnAsync', false, 'foo', 'bar');
assert.equal(resultTruthyFnAsync, 'valuefoobar');
});

it('returns undefined on non-existing fn', async () => {
//given
LuigiConfig.config = {};

const resultUndefined = await LuigiConfig.executeConfigFnAsync(
'not.existing.value'
);
const resultUndefined = await LuigiConfig.executeConfigFnAsync('not.existing.value');
assert.isUndefined(resultUndefined, 'async fn result is not undefined');
});

it('returns undefined on error/rejected promise', async () => {
//given
LuigiConfig.config = {
rejectFnAsync: (param1, param2) =>
Promise.reject(new Error('rejected')),
rejectFnAsync: (param1, param2) => Promise.reject(new Error('rejected')),
errFnAsync: (param1, param2) => {
throw new Error({
name: 'someError',
Expand All @@ -95,24 +78,18 @@ describe('Config', () => {
}
};

const resultRejectFnAsync = await LuigiConfig.executeConfigFnAsync(
'rejectFnAsync'
);
const resultRejectFnAsync = await LuigiConfig.executeConfigFnAsync('rejectFnAsync');
assert.isUndefined(resultRejectFnAsync, 'rejection did not return');

const resultErrFnAsync = await LuigiConfig.executeConfigFnAsync(
'errFnAsync'
);
const resultErrFnAsync = await LuigiConfig.executeConfigFnAsync('errFnAsync');
assert.isUndefined(resultErrFnAsync, 'error did not return');
});

it('throws an error if throwError flag is set', async () => {
sinon
.stub(AsyncHelpers, 'applyFunctionPromisified')
.returns(Promise.reject(new Error('rejected')));
sinon.stub(AsyncHelpers, 'applyFunctionPromisified').returns(Promise.reject(new Error('rejected')));
//given
LuigiConfig.config = {
rejectFnAsync: (param1, param2) => { }
rejectFnAsync: (param1, param2) => {}
};

// second parameter throws an error and we want it to throw an error on failure
Expand Down Expand Up @@ -179,4 +156,27 @@ describe('Config', () => {
sinon.assert.calledOnce(console.log);
});
});

describe('clearNavigationCache', () => {
it('should clean navigation titleResolver cache', () => {
LuigiConfig.config = {
navigation: {
nodes: {
titleResolver: {
url: 'http://localhost',
_cache: {
key: 'cacheKey',
value: 'cacheValue'
}
}
}
}
};
LuigiConfig.clearNavigationCache();

const actual = LuigiConfig.getConfigValue('navigation.titleResolver._cache');
const expected = undefined;
assert.equal(actual, expected);
});
});
});
3 changes: 1 addition & 2 deletions core/test/utilities/helpers/navigation-helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const chai = require('chai');
const assert = chai.assert;
const sinon = require('sinon');
import { AuthHelpers, NavigationHelpers, GenericHelpers, RoutingHelpers } from '../../../src/utilities/helpers';
import { AuthHelpers, NavigationHelpers, RoutingHelpers } from '../../../src/utilities/helpers';
import { LuigiAuth, LuigiConfig } from '../../../src/core-api';
import { Routing } from '../../../src/services/routing';
import { Navigation } from '../../../src/navigation/services/navigation';
Expand Down Expand Up @@ -490,7 +490,6 @@ describe('Navigation-helpers', () => {
it('group nodes by category label', () => {
nodes[1].category.collapsible = true;
const result = NavigationHelpers.groupNodesBy(nodes, 'category', true);
console.log('result ', result.test['metaInfo']);
assert.deepEqual(Object.keys(result), ['1', 'test', 'luigi']);
assert.deepEqual(result.test['metaInfo'], { categoryUid: 'test', label: 'test', collapsible: true, order: 1 });
});
Expand Down
4 changes: 2 additions & 2 deletions docs/luigi-client-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,11 @@ LuigiClient.linkManager().newTab().navigate('/projects/xy/foobar');

#### preserveQueryParams

Keeps url's query parameters for a navigation request.
Keeps the URL's query parameters for a navigation request.

##### Parameters

- `preserve` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** By default, it is set to `false`. If it is set to `true` the url's query parameters will be kept after navigation. (optional, default `false`)
- `preserve` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** By default, it is set to `false`. If it is set to `true`, the URL's query parameters will be kept after navigation. (optional, default `false`)

##### Examples

Expand Down
14 changes: 14 additions & 0 deletions docs/luigi-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,20 @@ Luigi.reset();

- **since**: 1.14.0

#### clearNavigationCache

Clear navigation node related caches.

##### Examples

```javascript
Luigi.clearNavigationCache();
```

**Meta**

- **since**: NEXT_RELEASE

## Luigi.elements()

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
Expand Down