BVB Source Codes

parse-server Show Client.js Source code

Return Download parse-server: download Client.js Source code - Download parse-server Source code - Type:.js
  1. import logger from '../logger';
  2.  
  3. import type { FlattenedObjectData } from './Subscription';
  4. export type Message = { [attr: string]: any };
  5.  
  6. const dafaultFields = ['className', 'objectId', 'updatedAt', 'createdAt', 'ACL'];
  7.  
  8. class Client {
  9.   id: number;
  10.   parseWebSocket: any;
  11.   userId: string;
  12.   roles: Array<string>;
  13.   subscriptionInfos: Object;
  14.   pushConnect: Function;
  15.   pushSubscribe: Function;
  16.   pushUnsubscribe: Function;
  17.   pushCreate: Function;
  18.   pushEnter: Function;
  19.   pushUpdate: Function;
  20.   pushDelete: Function;
  21.   pushLeave: Function;
  22.  
  23.   constructor(id: number, parseWebSocket: any) {
  24.     this.id = id;
  25.     this.parseWebSocket = parseWebSocket;
  26.     this.roles = [];
  27.     this.subscriptionInfos = new Map();
  28.     this.pushConnect = this._pushEvent('connected');
  29.     this.pushSubscribe = this._pushEvent('subscribed');
  30.     this.pushUnsubscribe = this._pushEvent('unsubscribed');
  31.     this.pushCreate = this._pushEvent('create');
  32.     this.pushEnter = this._pushEvent('enter');
  33.     this.pushUpdate = this._pushEvent('update');
  34.     this.pushDelete = this._pushEvent('delete');
  35.     this.pushLeave = this._pushEvent('leave');
  36.   }
  37.  
  38.   static pushResponse(parseWebSocket: any, message: Message): void {
  39.     logger.verbose('Push Response : %j', message);
  40.     parseWebSocket.send(message);
  41.   }
  42.  
  43.   static pushError(parseWebSocket: any, code: number, error: string, reconnect: boolean = true): void {
  44.     Client.pushResponse(parseWebSocket, JSON.stringify({
  45.       'op': 'error',
  46.       'error': error,
  47.       'code': code,
  48.       'reconnect': reconnect
  49.     }));
  50.   }
  51.  
  52.   addSubscriptionInfo(requestId: number, subscriptionInfo: any): void {
  53.     this.subscriptionInfos.set(requestId, subscriptionInfo);
  54.   }
  55.  
  56.   getSubscriptionInfo(requestId: number): any {
  57.     return this.subscriptionInfos.get(requestId);
  58.   }
  59.  
  60.   deleteSubscriptionInfo(requestId: number): void {
  61.     return this.subscriptionInfos.delete(requestId);
  62.   }
  63.  
  64.   _pushEvent(type: string): Function {
  65.     return function(subscriptionId: number, parseObjectJSON: any): void {
  66.       const response: Message = {
  67.         'op' : type,
  68.         'clientId' : this.id
  69.       };
  70.       if (typeof subscriptionId !== 'undefined') {
  71.         response['requestId'] = subscriptionId;
  72.       }
  73.       if (typeof parseObjectJSON !== 'undefined') {
  74.         let fields;
  75.         if (this.subscriptionInfos.has(subscriptionId)) {
  76.           fields = this.subscriptionInfos.get(subscriptionId).fields;
  77.         }
  78.         response['object'] = this._toJSONWithFields(parseObjectJSON, fields);
  79.       }
  80.       Client.pushResponse(this.parseWebSocket, JSON.stringify(response));
  81.     }
  82.   }
  83.  
  84.   _toJSONWithFields(parseObjectJSON: any, fields: any): FlattenedObjectData {
  85.     if (!fields) {
  86.       return parseObjectJSON;
  87.     }
  88.     const limitedParseObject = {};
  89.     for (const field of dafaultFields) {
  90.       limitedParseObject[field] = parseObjectJSON[field];
  91.     }
  92.     for (const field of fields) {
  93.       if (field in parseObjectJSON) {
  94.         limitedParseObject[field] = parseObjectJSON[field];
  95.       }
  96.     }
  97.     return limitedParseObject;
  98.   }
  99. }
  100.  
  101. export {
  102.   Client
  103. }
  104.  
downloadClient.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