Skip to content
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
3 changes: 2 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"noDynamicNamespaceImportAccess": "off"
},
"suspicious": {
"noAssignInExpressions": "off"
"noAssignInExpressions": "off",
"useIterableCallbackReturn": "off"
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions lib/caching/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,21 @@ export function getStrategy(caching, object) {
}
return caching.evaluate(object);
}

function getValues(caching, values = []) {
if (typeof caching === 'string') {
values.push(caching);
return values;
}
if (Array.isArray(caching)) {
caching.forEach(item => getValues(item, values));
}
return values;
}

export function isCacheable(caching) {
if (!caching) {
return;
}
return getValues(caching).some(key => key.startsWith('cache-'));
}
12 changes: 1 addition & 11 deletions lib/map-style.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isCacheable } from './caching/index.js';
import loadGlyphs from './glyphs.js';
import { init as initLayers, reset as resetLayers } from './layers.js';
import loader from './loader.js';
Expand Down Expand Up @@ -114,14 +115,3 @@ function cacheableOnly(style) {

return cached;
}

function isCacheable(caching) {
if (!caching) {
return;
}
const keys = Object.keys(caching);
if (caching.default) {
keys.push(caching.default);
}
return keys.some(key => key.startsWith('cache-'));
}
28 changes: 27 additions & 1 deletion test/caching.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { getStrategy, initCaching } from '../lib/caching/index.js';
import { getStrategy, initCaching, isCacheable } from '../lib/caching/index.js';

test('caching default', () => {
const caching = initCaching('do-nothing');
Expand Down Expand Up @@ -72,3 +72,29 @@ test('caching multiple fields', () => {
strategy = getStrategy(caching, {});
assert.equal(strategy, 'do-nothing');
});

test('isCacheable', () => {
assert.equal(isCacheable('do-nothing'), false);
assert.equal(
isCacheable([
'case',
['all', ['to-boolean', ['global-state', 'field1']], ['to-boolean', ['global-state', 'field2']]],
'network-only',
'do-nothing'
]),
false
);

assert.equal(isCacheable('cache-only'), true);
assert.equal(
isCacheable([
'case',
['to-boolean', ['global-state', 'field1']],
'network-only',
['to-boolean', ['global-state', 'field2']],
'cache-only',
'do-nothing'
]),
true
);
});