Skip to content
This repository was archived by the owner on May 17, 2019. It is now read-only.

Commit 612a16d

Browse files
committed
Remove browser-specific memoization
1 parent 4b0b7b8 commit 612a16d

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

src/__tests__/memoize.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,15 @@ test('memoize', t => {
3030
t.equal(memoizedB(mockCtx), 1, 'memoizes correctly');
3131
t.equal(memoized(mockCtx), 1, 'calls function when it has no value');
3232
t.equal(memoized(mockCtx), 1, 'memoizes correctly');
33+
34+
// New context object should cause new calculation
35+
const mockCtx2: Context = ({}: any);
36+
t.equal(memoized(mockCtx2), 2, 'calls function when it has no value');
37+
t.equal(memoized(mockCtx2), 2, 'memoizes correctly');
38+
t.equal(memoizedB(mockCtx2), 2, 'calls function when it has no value');
39+
t.equal(memoizedB(mockCtx2), 2, 'memoizes correctly');
40+
t.equal(memoized(mockCtx2), 2, 'calls function when it has no value');
41+
t.equal(memoized(mockCtx2), 2, 'memoizes correctly');
42+
3343
t.end();
3444
});

src/memoize.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,13 @@ import type {Context} from './types.js';
1111
type MemoizeFn<A> = (ctx: Context) => A;
1212

1313
export function memoize<A>(fn: MemoizeFn<A>): MemoizeFn<A> {
14-
if (__BROWSER__) {
15-
return browserMemoize(fn);
16-
}
17-
1814
const wm = new WeakMap();
1915
return ctx => {
2016
if (wm.has(ctx)) {
21-
return ((wm.get(ctx): any): A); // Refinement doesn't seem to work
22-
} else {
23-
const result = fn(ctx);
24-
wm.set(ctx, result);
25-
return result;
26-
}
27-
};
28-
}
29-
30-
/**
31-
* There is only ever a single ctx object in the browser.
32-
* Therefore we can use a simple memoization function.
33-
*/
34-
function browserMemoize<A>(fn: MemoizeFn<A>): MemoizeFn<A> {
35-
let memoized;
36-
let called = false;
37-
return ctx => {
38-
if (!called) {
39-
memoized = fn(ctx);
40-
called = true;
17+
return ((wm.get(ctx): any): A); // Refinement with `has` doesn't seem to work
4118
}
42-
return memoized;
19+
const result = fn(ctx);
20+
wm.set(ctx, result);
21+
return result;
4322
};
4423
}

0 commit comments

Comments
 (0)