Skip to content

Commit 1294544

Browse files
authored
Merge pull request #1 from r24y/feat/no-proxy
New: Add `no-proxy` version for usage in Node 4
2 parents 86375e4 + a1982a6 commit 1294544

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

src/__tests__/no-proxy.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const anticipatedCall = require('../no-proxy');
2+
3+
describe('no-proxy', () => {
4+
describe('nextCall', () => {
5+
test('should delay resolution until first call', () => {
6+
const spy = jest.fn();
7+
const myFn = anticipatedCall(spy);
8+
9+
const promise = myFn.anticipated.nextCall
10+
.then(() => {
11+
expect(spy).toHaveBeenCalledTimes(1);
12+
});
13+
expect(spy).not.toHaveBeenCalled();
14+
myFn();
15+
expect(spy).toHaveBeenCalledTimes(1);
16+
});
17+
test('should return a new Promise', () => {
18+
const myFn = anticipatedCall();
19+
const promise1 = myFn.anticipated.nextCall;
20+
const promise2 = myFn.anticipated.nextCall;
21+
expect(promise1).not.toBe(promise2);
22+
});
23+
test('should handle subsequent calls as expected', () => {
24+
const spy = jest.fn();
25+
const myFn = anticipatedCall(spy);
26+
27+
const promise1 = myFn.anticipated.nextCall;
28+
29+
myFn();
30+
31+
return promise1.then(() => {
32+
expect(spy).toHaveBeenCalledTimes(1);
33+
34+
const promise2 = myFn.anticipated.nextCall;
35+
myFn();
36+
return promise2;
37+
}).then(() => {
38+
expect(spy).toHaveBeenCalledTimes(2);
39+
})
40+
})
41+
})
42+
})

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ function anticipateCall(fn = noop) {
3838
return new Proxy(fn, new Anticipated());
3939
}
4040

41-
module.exports = anticipateCall;
41+
module.exports = anticipateCall;
42+
module.exports.Anticipated = Anticipated;
43+
module.exports.noop = noop;

src/no-proxy.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const {Anticipated, noop} = require('./index');
2+
3+
function anticipateCallWithoutProxy(fn) {
4+
const anticipated = new Anticipated();
5+
function proxiedCall(...args) {
6+
return anticipated.apply(fn, this, args);
7+
}
8+
proxiedCall.anticipated = anticipated;
9+
return proxiedCall;
10+
}
11+
12+
module.exports = anticipateCallWithoutProxy;

0 commit comments

Comments
 (0)