123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.Client = void 0;
- var _logger = _interopRequireDefault(require("../logger"));
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
- const dafaultFields = ['className', 'objectId', 'updatedAt', 'createdAt', 'ACL'];
- class Client {
- constructor(id, parseWebSocket, hasMasterKey = false, sessionToken, installationId) {
- this.id = id;
- this.parseWebSocket = parseWebSocket;
- this.hasMasterKey = hasMasterKey;
- this.sessionToken = sessionToken;
- this.installationId = installationId;
- this.roles = [];
- this.subscriptionInfos = new Map();
- this.pushConnect = this._pushEvent('connected');
- this.pushSubscribe = this._pushEvent('subscribed');
- this.pushUnsubscribe = this._pushEvent('unsubscribed');
- this.pushCreate = this._pushEvent('create');
- this.pushEnter = this._pushEvent('enter');
- this.pushUpdate = this._pushEvent('update');
- this.pushDelete = this._pushEvent('delete');
- this.pushLeave = this._pushEvent('leave');
- }
- static pushResponse(parseWebSocket, message) {
- _logger.default.verbose('Push Response : %j', message);
- parseWebSocket.send(message);
- }
- static pushError(parseWebSocket, code, error, reconnect = true, requestId = null) {
- Client.pushResponse(parseWebSocket, JSON.stringify({
- op: 'error',
- error,
- code,
- reconnect,
- requestId
- }));
- }
- addSubscriptionInfo(requestId, subscriptionInfo) {
- this.subscriptionInfos.set(requestId, subscriptionInfo);
- }
- getSubscriptionInfo(requestId) {
- return this.subscriptionInfos.get(requestId);
- }
- deleteSubscriptionInfo(requestId) {
- return this.subscriptionInfos.delete(requestId);
- }
- _pushEvent(type) {
- return function (subscriptionId, parseObjectJSON, parseOriginalObjectJSON) {
- const response = {
- op: type,
- clientId: this.id,
- installationId: this.installationId
- };
- if (typeof subscriptionId !== 'undefined') {
- response['requestId'] = subscriptionId;
- }
- if (typeof parseObjectJSON !== 'undefined') {
- let keys;
- if (this.subscriptionInfos.has(subscriptionId)) {
- keys = this.subscriptionInfos.get(subscriptionId).keys;
- }
- response['object'] = this._toJSONWithFields(parseObjectJSON, keys);
- if (parseOriginalObjectJSON) {
- response['original'] = this._toJSONWithFields(parseOriginalObjectJSON, keys);
- }
- }
- Client.pushResponse(this.parseWebSocket, JSON.stringify(response));
- };
- }
- _toJSONWithFields(parseObjectJSON, fields) {
- if (!fields) {
- return parseObjectJSON;
- }
- const limitedParseObject = {};
- for (const field of dafaultFields) {
- limitedParseObject[field] = parseObjectJSON[field];
- }
- for (const field of fields) {
- if (field in parseObjectJSON) {
- limitedParseObject[field] = parseObjectJSON[field];
- }
- }
- return limitedParseObject;
- }
- }
- exports.Client = Client;
|