Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Last active August 29, 2015 14:17
Show Gist options
  • Save ishiduca/b366a14cceff8335254a to your computer and use it in GitHub Desktop.
Save ishiduca/b366a14cceff8335254a to your computer and use it in GitHub Desktop.
apiObjectを作る時のutility
'use strict'
var handle = require('./handle')
var events = require('events')
var assign = require('object-assign')
var list = []
var api = assign({}, events.EventEmitter.prototype)
// API
// handle.call(
// targetObject
// , methods
// , sourceObject
// )
handle.call(api, {
put: 'push'
, get: 'shift'
}, list)
// handle.call(...) is
// api.put = function (value) {
// return list.push(value)
// }
// api.get = function () {
// return list.shift()
// }
api.on('change', function () {
console.log(api.get())
})
api.put({hello: 'hello world'})
api.emit('change')
'use strict'
module.exports = function handle (pair, she) {
var me = this
Object.keys(pair).forEach(function (myMethod) {
var herMethod = pair[myMethod]
if ('function' === typeof she[herMethod]) {
me[myMethod] = function () {
return she[herMethod].apply(she, arguments)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment