Guest User

Untitled

a guest
Jul 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. /* global fin */
  2.  
  3. const phonebookUUID = 'phonebook-openfin';
  4. const phonebookManifestUrl = '';
  5. const phonebookChannel = '';
  6.  
  7. function smartSend(message) {
  8. let appExists = false;
  9. let isAppRunning;
  10. fin.desktop.System.getAllApplications(applications => {
  11. applications.forEach(app => {
  12. if (app.uuid === phonebookUUID) {
  13. appExists = true;
  14. isAppRunning = app.isRunning;
  15. }
  16. });
  17.  
  18. if (!appExists) {
  19. fin.desktop.Application.createFromManifest(phonebookManifestUrl, (app) => {
  20. app.run(() => waitThenPublishMessage(message));
  21. });
  22. } else {
  23. if (isAppRunning) {
  24. publishMessage(message);
  25. } else {
  26. const wrappedApp = fin.desktop.Application.wrap(phonebookUUID);
  27. wrappedApp.run(() => {
  28. waitThenPublishMessage(message);
  29. });
  30. }
  31. }
  32. })
  33. }
  34.  
  35. function publishMessage(message) {
  36. fin.desktop.InterApplicationBus.send(phonebookUUID, phonebookChannel, message);
  37. }
  38.  
  39. function waitThenPublishMessage(message) {
  40. function listener() {
  41. publishMessage(message);
  42. console.log(`Sent ${phonebookUUID} ${message} on channel ${phonebookUUID}`);
  43. fin.desktop.InterApplicationBus.unsubscribe(phonebookUUID, 'subscription-confirmed', listener);
  44. }
  45. fin.desktop.InterApplicationBus.subscribe(phonebookUUID, 'subscription-confirmed', listener);
  46. }
Add Comment
Please, Sign In to add comment