-
-
Notifications
You must be signed in to change notification settings - Fork 67
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
[NON-BREAKING] ES2015 Proxy approach (requires Node.js 6) #32
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
|
||
const processFn = (fn, opts) => function (...args) { | ||
const P = opts.promiseModule; | ||
|
||
return new P((resolve, reject) => { | ||
args.push((err, result, ...results) => { | ||
if (err) { | ||
reject(err); | ||
} else if (opts.multiArgs) { | ||
resolve([result, ...results]); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
|
||
fn.apply(this, args); | ||
}); | ||
}; | ||
|
||
module.exports = (obj, opts) => { | ||
opts = Object.assign({ | ||
exclude: [/.+Sync$/], | ||
promiseModule: Promise | ||
}, opts); | ||
|
||
const filter = key => { | ||
const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key); | ||
return opts.include ? opts.include.some(match) : !opts.exclude.some(match); | ||
}; | ||
|
||
const cache = new Map(); | ||
|
||
const handler = { | ||
get: (target, key) => { | ||
let cached = cache.get(key); | ||
|
||
if (!cached) { | ||
const x = target[key]; | ||
|
||
cached = typeof x === 'function' && filter(key) ? processFn(x, opts) : x; | ||
cache.set(key, cached); | ||
} | ||
|
||
return cached; | ||
} | ||
}; | ||
|
||
if (!opts.excludeMain) { | ||
const main = Symbol('main'); | ||
|
||
handler.apply = (target, thisArg, argumentsList) => { | ||
let cached = cache.get(main); | ||
|
||
if (!cached) { | ||
cached = processFn(target, opts); | ||
cache.set(main, cached); | ||
} | ||
|
||
return cached.apply(thisArg, argumentsList); | ||
}; | ||
} | ||
|
||
return new Proxy(obj, handler); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import fs from 'fs'; | ||
import test from 'ava'; | ||
import pinkiePromise from 'pinkie-promise'; | ||
import fn from './proxy'; | ||
|
||
const fixture = cb => setImmediate(() => cb(null, 'unicorn')); | ||
const fixture2 = (x, cb) => setImmediate(() => cb(null, x)); | ||
const fixture3 = cb => setImmediate(() => cb(null, 'unicorn', 'rainbow')); | ||
const fixture4 = cb => setImmediate(() => { | ||
cb(null, 'unicorn'); | ||
return 'rainbow'; | ||
}); | ||
|
||
fixture4.meow = cb => { | ||
setImmediate(() => { | ||
cb(null, 'unicorn'); | ||
}); | ||
}; | ||
|
||
const fixture5 = () => 'rainbow'; | ||
|
||
const fixtureModule = { | ||
method1: fixture, | ||
method2: fixture, | ||
method3: fixture5 | ||
}; | ||
|
||
test('main', async t => { | ||
t.is(typeof fn(fixture)().then, 'function'); | ||
t.is(await fn(fixture)(), 'unicorn'); | ||
}); | ||
|
||
test('pass argument', async t => { | ||
t.is(await fn(fixture2)('rainbow'), 'rainbow'); | ||
}); | ||
|
||
test('custom Promise module', async t => { | ||
t.is(await fn(fixture, {promiseModule: pinkiePromise})(), 'unicorn'); | ||
}); | ||
|
||
test('multiArgs option', async t => { | ||
t.deepEqual(await fn(fixture3, {multiArgs: true})(), ['unicorn', 'rainbow']); | ||
}); | ||
|
||
test('wrap core method', async t => { | ||
t.is(JSON.parse(await fn(fs.readFile)('package.json')).name, 'pify'); | ||
}); | ||
|
||
test('module support', async t => { | ||
t.is(JSON.parse(await fn(fs).readFile('package.json')).name, 'pify'); | ||
}); | ||
|
||
test('module support - doesn\'t transform *Sync methods by default', t => { | ||
t.is(JSON.parse(fn(fs).readFileSync('package.json')).name, 'pify'); | ||
}); | ||
|
||
test('module support - preserves non-function members', t => { | ||
const module = { | ||
method: () => {}, | ||
nonMethod: 3 | ||
}; | ||
|
||
t.deepEqual(Object.keys(module), Object.keys(fn(module))); | ||
}); | ||
|
||
test('module support - transforms only members in options.include', t => { | ||
const pModule = fn(fixtureModule, { | ||
include: ['method1', 'method2'] | ||
}); | ||
|
||
t.is(typeof pModule.method1().then, 'function'); | ||
t.is(typeof pModule.method2().then, 'function'); | ||
t.not(typeof pModule.method3().then, 'function'); | ||
}); | ||
|
||
test('module support - doesn\'t transform members in options.exclude', t => { | ||
const pModule = fn(fixtureModule, { | ||
exclude: ['method3'] | ||
}); | ||
|
||
t.is(typeof pModule.method1().then, 'function'); | ||
t.is(typeof pModule.method2().then, 'function'); | ||
t.not(typeof pModule.method3().then, 'function'); | ||
}); | ||
|
||
test('module support - options.include over options.exclude', t => { | ||
const pModule = fn(fixtureModule, { | ||
include: ['method1', 'method2'], | ||
exclude: ['method2', 'method3'] | ||
}); | ||
|
||
t.is(typeof pModule.method1().then, 'function'); | ||
t.is(typeof pModule.method2().then, 'function'); | ||
t.not(typeof pModule.method3().then, 'function'); | ||
}); | ||
|
||
test('module support — function modules', t => { | ||
const pModule = fn(fixture4); | ||
|
||
t.is(typeof pModule().then, 'function'); | ||
t.is(typeof pModule.meow().then, 'function'); | ||
}); | ||
|
||
test('module support — function modules exclusion', t => { | ||
const pModule = fn(fixture4, { | ||
excludeMain: true | ||
}); | ||
|
||
t.is(typeof pModule.meow().then, 'function'); | ||
t.not(typeof pModule(() => {}).then, 'function'); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test-proxy.js
is (almost) an one-to-one duplicate oftest.js
😒There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's ok for now. We'll either replace proxy with the current approach or adopt both in the same API once we can target Node.js 6.