Advertisement
Guest User

Untitled

a guest
May 26th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. const getAllPropertyNames = require('./get_all_property_names'),
  2. Mutex = require('./mutex')
  3.  
  4. function actor(object) {
  5. let methods = getAllPropertyNames(object),
  6. mutex = new Mutex(),
  7. wrapper = {}
  8.  
  9. for (let method of methods) {
  10. if (typeof object[method] !== 'function') continue
  11. wrapper[method] = actorMethodDescriptor(mutex, object, method)
  12. }
  13.  
  14. return Object.create(object, wrapper)
  15. }
  16.  
  17. function actorMethodDescriptor(mutex, object, method) {
  18. return {
  19. value: (...args) => {
  20. return mutex.synchronize(() => object[method](...args))
  21. }
  22. }
  23. }
  24.  
  25. module.exports = actor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement