Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Using prototype functions in napa #238

Open
ciko92 opened this issue May 22, 2018 · 3 comments
Open

Using prototype functions in napa #238

ciko92 opened this issue May 22, 2018 · 3 comments

Comments

@ciko92
Copy link

ciko92 commented May 22, 2018

I'm trying to use napajs with prototype functions. How can I bind 'this' inside the broadcast api?
Moreover, I'm trying to require momentjs and toolbox inside broadcast, but it's not working.

@fs-eire
Copy link
Contributor

fs-eire commented May 24, 2018

How can I bind 'this' inside the broadcast api?

I don't get what's the question... could you share an example to help me understand?

I've tried the moment module. It works in this way -

const napa = require('napajs');
let z1 = napa.zone.create('z1', { workers: 1 });
z1.broadcastSync(()=>{
  let moment = require('moment');
  console.log(moment().format("MMM Do YY"));
});

This does not work:

z1.broadcastSync(()=>{
  let moment = require('moment');
});
z1.execute(()=>{
  console.log(moment().format("MMM Do YY")); //ReferenceError: moment is not defined
});

By using global it does work:

z1.broadcastSync(()=>{
  global.moment = require('moment');
});
z1.execute(()=>{
  console.log(global.moment().format("MMM Do YY"));
});

@ciko92
Copy link
Author

ciko92 commented May 25, 2018

Thank you fs-eire! Global works perfectly for me.

I have a class Greedy and some prototype functions that I need to broadcast such as Greedy.prototype.optimize = function(orders, moovers){ ... return solution}. I can't call z1.broadcastSync(this.optimize.toString()) inside others prototype functions. How can I do it?

@fs-eire
Copy link
Contributor

fs-eire commented Jun 11, 2018

In my previous attempt I always put definitions of customized class into a separated file. Say I have a file greedy.js with the definition of optimize() inside so I broadcast a call to require() to handle this kind of requirement.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants