Skip to content

Commit 4581ad1

Browse files
committed
Router: caching (#247)
* frint-router: caching. * frint-router: coverage. * frint-router: caching docs. * examples: require packages from local node_modules.
1 parent 84f9cf3 commit 4581ad1

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

examples/router/vendors/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ window.Rx = require('rxjs');
44
window.Frint = require('frint');
55
window.FrintStore = require('frint-store');
66
window.FrintReact = require('frint-react');
7-
8-
window.FrintRouter = require('../../../packages/frint-router');
9-
window.FrintRouterReact = require('../../../packages/frint-router-react');
7+
window.FrintRouter = require('frint-router');
8+
window.FrintRouterReact = require('frint-router-react');

packages/frint-router/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ All of them implement the same set of methods.
108108

109109
## BrowserRouterService
110110

111+
### constructor
112+
113+
#### Arguments
114+
115+
1. `options` (`Object`)
116+
* `options.enableCache` (`Boolean`): Enables caching, set to `true` by default.
117+
* `options.cacheLimit` (`Number`): Maximum limit of entries to cache, set to `10000` by default.
118+
111119
### getHistory$
112120

113121
> getHistory$()
@@ -139,7 +147,6 @@ Keeps matching pattern against history as it keeps changing over time.
139147
1. `pattern` (`String`): Pattern to match against
140148
1. `options` (`Object`)
141149
* `options.exact` (`Boolean`): Matches pattern exactly, defaults to `false`.
142-
* `options.cache` (`Boolean`): Cache matches (TODO)
143150

144151
#### Returns
145152

packages/frint-router/src/makeRouterService.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import matchFromHistory from './matchFromHistory';
55
export default function makeRouterService(createHistory) {
66
class RouterService {
77
constructor(options = {}) {
8-
this.options = options;
8+
const defaultOptions = {
9+
enableCache: true,
10+
cacheLimit: 10000,
11+
};
12+
this.options = {
13+
...defaultOptions,
14+
...options,
15+
};
916
this._history = createHistory(options);
1017

1118
this._history$ = new BehaviorSubject({
@@ -21,6 +28,9 @@ export default function makeRouterService(createHistory) {
2128
action,
2229
});
2330
});
31+
32+
this._cache = {};
33+
this._cacheCount = 0;
2434
}
2535

2636
getHistory$() {
@@ -50,12 +60,27 @@ export default function makeRouterService(createHistory) {
5060
getMatch(pattern, history, opts = {}) { // eslint-disable-line
5161
const options = {
5262
exact: false,
53-
cache: true, // @TODO: implement later
5463
...opts,
5564
};
65+
const cacheKey = `${pattern}|${history.location.pathname}|${options.exact}`;
66+
67+
if (
68+
this.options.enableCache &&
69+
this._cache[cacheKey]
70+
) {
71+
return this._cache[cacheKey];
72+
}
5673

5774
const matched = matchFromHistory(pattern, history, options);
5875

76+
if (
77+
this.options.enableCache &&
78+
this._cacheCount < this.options.cacheLimit
79+
) {
80+
this._cache[cacheKey] = matched;
81+
this._cacheCount += 1;
82+
}
83+
5984
return matched;
6085
}
6186

packages/frint-router/src/makeRouterService.spec.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('frint-router › makeRouterService', function () {
1414
const MemoryRouterService = makeRouterService(createMemoryHistory);
1515

1616
const router = new MemoryRouterService({ foo: 'foo' });
17-
expect(router.options).to.deep.equal({ foo: 'foo' });
17+
expect(router.options).to.contain({ foo: 'foo' });
1818
});
1919

2020
describe('MemoryRouter', function () {
@@ -93,7 +93,7 @@ describe('frint-router › makeRouterService', function () {
9393
const router = new MemoryRouterService();
9494

9595
router.getMatch$('/about')
96-
.take(3)
96+
.take(4)
9797
.scan(
9898
(matches, currentMatch) => matches.concat([currentMatch]),
9999
[]
@@ -113,13 +113,21 @@ describe('frint-router › makeRouterService', function () {
113113

114114
// second push
115115
null,
116+
117+
// third push
118+
{
119+
url: '/about',
120+
isExact: true,
121+
params: {},
122+
},
116123
]);
117124

118125
done();
119126
});
120127

121128
router.push('/about');
122129
router.push('/');
130+
router.push('/about');
123131
});
124132

125133
it('destroys listener', function (done) {

0 commit comments

Comments
 (0)