BVB Source Codes

parse-server Show PushWorker.js Source code

Return Download parse-server: download PushWorker.js Source code - Download parse-server Source code - Type:.js
  1. // @flow
  2. import deepcopy               from 'deepcopy';
  3. import AdaptableController    from '../Controllers/AdaptableController';
  4. import { master }             from '../Auth';
  5. import Config                 from '../Config';
  6. import { PushAdapter }        from '../Adapters/Push/PushAdapter';
  7. import rest                   from '../rest';
  8. import { pushStatusHandler }  from '../StatusHandler';
  9. import { isPushIncrementing } from './utils';
  10. import { ParseMessageQueue }  from '../ParseMessageQueue';
  11. import { PushQueue }          from './PushQueue';
  12.  
  13. const UNSUPPORTED_BADGE_KEY = "unsupported";
  14.  
  15. function groupByBadge(installations) {
  16.   return installations.reduce((map, installation) => {
  17.     let badge = installation.badge + '';
  18.     if (installation.deviceType != "ios") {
  19.       badge = UNSUPPORTED_BADGE_KEY;
  20.     }
  21.     map[badge] = map[badge] || [];
  22.     map[badge].push(installation);
  23.     return map;
  24.   }, {});
  25. }
  26.  
  27. export class PushWorker {
  28.   subscriber: ?any;
  29.   adapter: any;
  30.   channel: string;
  31.  
  32.   constructor(pushAdapter: PushAdapter, subscriberConfig: any = {}) {
  33.     AdaptableController.validateAdapter(pushAdapter, this, PushAdapter);
  34.     this.adapter = pushAdapter;
  35.  
  36.     this.channel = subscriberConfig.channel || PushQueue.defaultPushChannel();
  37.     this.subscriber = ParseMessageQueue.createSubscriber(subscriberConfig);
  38.     if (this.subscriber) {
  39.       const subscriber = this.subscriber;
  40.       subscriber.subscribe(this.channel);
  41.       subscriber.on('message', (channel, messageStr) => {
  42.         const workItem = JSON.parse(messageStr);
  43.         this.run(workItem);
  44.       });
  45.     }
  46.   }
  47.  
  48.   unsubscribe(): void {
  49.     if (this.subscriber) {
  50.       this.subscriber.unsubscribe(this.channel);
  51.     }
  52.   }
  53.  
  54.   run({ body, query, pushStatus, applicationId }: any): Promise<*> {
  55.     const config = new Config(applicationId);
  56.     const auth = master(config);
  57.     const where  = query.where;
  58.     delete query.where;
  59.     return rest.find(config, auth, '_Installation', where, query).then(({results}) => {
  60.       if (results.length == 0) {
  61.         return;
  62.       }
  63.       return this.sendToAdapter(body, results, pushStatus, config);
  64.     }, err => {
  65.       throw err;
  66.     });
  67.   }
  68.  
  69.   sendToAdapter(body: any, installations: any[], pushStatus: any, config: Config): Promise<*> {
  70.     pushStatus = pushStatusHandler(config, pushStatus.objectId);
  71.     if (!isPushIncrementing(body)) {
  72.       return this.adapter.send(body, installations, pushStatus.objectId).then((results) => {
  73.         return pushStatus.trackSent(results);
  74.       });
  75.     }
  76.  
  77.     // Collect the badges to reduce the # of calls
  78.     const badgeInstallationsMap = groupByBadge(installations);
  79.  
  80.     // Map the on the badges count and return the send result
  81.     const promises = Object.keys(badgeInstallationsMap).map((badge) => {
  82.       const payload = deepcopy(body);
  83.       if (badge == UNSUPPORTED_BADGE_KEY) {
  84.         delete payload.data.badge;
  85.       } else {
  86.         payload.data.badge = parseInt(badge);
  87.       }
  88.       const installations = badgeInstallationsMap[badge];
  89.       return this.sendToAdapter(payload, installations, pushStatus, config);
  90.     });
  91.     return Promise.all(promises);
  92.   }
  93. }
  94.  
  95. export default PushWorker;
  96.  
downloadPushWorker.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top