leancloud-realtime
Version:
LeanCloud Realtime Message JavaScript SDK
2,260 lines (2,128 loc) • 315 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var platformAdaptersNode = require('@leancloud/platform-adapters-node');
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty'));
var protobufLight = _interopDefault(require('protobufjs/dist/protobuf-light'));
var EventEmitter = _interopDefault(require('eventemitter3'));
var _regeneratorRuntime = _interopDefault(require('@babel/runtime/regenerator'));
var _asyncToGenerator = _interopDefault(require('@babel/runtime/helpers/asyncToGenerator'));
var _toConsumableArray = _interopDefault(require('@babel/runtime/helpers/toConsumableArray'));
var _objectWithoutProperties = _interopDefault(require('@babel/runtime/helpers/objectWithoutProperties'));
var _assertThisInitialized = _interopDefault(require('@babel/runtime/helpers/assertThisInitialized'));
var _inheritsLoose = _interopDefault(require('@babel/runtime/helpers/inheritsLoose'));
var d = _interopDefault(require('debug'));
var shuffle = _interopDefault(require('lodash/shuffle'));
var values = _interopDefault(require('lodash/values'));
var _toArray = _interopDefault(require('@babel/runtime/helpers/toArray'));
var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass'));
var _applyDecoratedDescriptor = _interopDefault(require('@babel/runtime/helpers/applyDecoratedDescriptor'));
var StateMachine = _interopDefault(require('javascript-state-machine'));
var _typeof = _interopDefault(require('@babel/runtime/helpers/typeof'));
var isPlainObject = _interopDefault(require('lodash/isPlainObject'));
var promiseTimeout = require('promise-timeout');
var uuid = _interopDefault(require('uuid/v4'));
var _slicedToArray = _interopDefault(require('@babel/runtime/helpers/slicedToArray'));
var base64Arraybuffer = require('base64-arraybuffer');
var remove = _interopDefault(require('lodash/remove'));
var isEmpty = _interopDefault(require('lodash/isEmpty'));
var cloneDeep = _interopDefault(require('lodash/cloneDeep'));
var find = _interopDefault(require('lodash/find'));
var _get = _interopDefault(require('lodash/get'));
var messageCompiled = protobufLight.newBuilder({})['import']({
"package": 'push_server.messages2',
syntax: 'proto2',
options: {
objc_class_prefix: 'AVIM'
},
messages: [{
name: 'JsonObjectMessage',
syntax: 'proto2',
fields: [{
rule: 'required',
type: 'string',
name: 'data',
id: 1
}]
}, {
name: 'UnreadTuple',
syntax: 'proto2',
fields: [{
rule: 'required',
type: 'string',
name: 'cid',
id: 1
}, {
rule: 'required',
type: 'int32',
name: 'unread',
id: 2
}, {
rule: 'optional',
type: 'string',
name: 'mid',
id: 3
}, {
rule: 'optional',
type: 'int64',
name: 'timestamp',
id: 4
}, {
rule: 'optional',
type: 'string',
name: 'from',
id: 5
}, {
rule: 'optional',
type: 'string',
name: 'data',
id: 6
}, {
rule: 'optional',
type: 'int64',
name: 'patchTimestamp',
id: 7
}, {
rule: 'optional',
type: 'bool',
name: 'mentioned',
id: 8
}, {
rule: 'optional',
type: 'bytes',
name: 'binaryMsg',
id: 9
}, {
rule: 'optional',
type: 'int32',
name: 'convType',
id: 10
}]
}, {
name: 'LogItem',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'string',
name: 'from',
id: 1
}, {
rule: 'optional',
type: 'string',
name: 'data',
id: 2
}, {
rule: 'optional',
type: 'int64',
name: 'timestamp',
id: 3
}, {
rule: 'optional',
type: 'string',
name: 'msgId',
id: 4
}, {
rule: 'optional',
type: 'int64',
name: 'ackAt',
id: 5
}, {
rule: 'optional',
type: 'int64',
name: 'readAt',
id: 6
}, {
rule: 'optional',
type: 'int64',
name: 'patchTimestamp',
id: 7
}, {
rule: 'optional',
type: 'bool',
name: 'mentionAll',
id: 8
}, {
rule: 'repeated',
type: 'string',
name: 'mentionPids',
id: 9
}, {
rule: 'optional',
type: 'bool',
name: 'bin',
id: 10
}, {
rule: 'optional',
type: 'int32',
name: 'convType',
id: 11
}]
}, {
name: 'ConvMemberInfo',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'string',
name: 'pid',
id: 1
}, {
rule: 'optional',
type: 'string',
name: 'role',
id: 2
}, {
rule: 'optional',
type: 'string',
name: 'infoId',
id: 3
}]
}, {
name: 'DataCommand',
syntax: 'proto2',
fields: [{
rule: 'repeated',
type: 'string',
name: 'ids',
id: 1
}, {
rule: 'repeated',
type: 'JsonObjectMessage',
name: 'msg',
id: 2
}, {
rule: 'optional',
type: 'bool',
name: 'offline',
id: 3
}]
}, {
name: 'SessionCommand',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'int64',
name: 't',
id: 1
}, {
rule: 'optional',
type: 'string',
name: 'n',
id: 2
}, {
rule: 'optional',
type: 'string',
name: 's',
id: 3
}, {
rule: 'optional',
type: 'string',
name: 'ua',
id: 4
}, {
rule: 'optional',
type: 'bool',
name: 'r',
id: 5
}, {
rule: 'optional',
type: 'string',
name: 'tag',
id: 6
}, {
rule: 'optional',
type: 'string',
name: 'deviceId',
id: 7
}, {
rule: 'repeated',
type: 'string',
name: 'sessionPeerIds',
id: 8
}, {
rule: 'repeated',
type: 'string',
name: 'onlineSessionPeerIds',
id: 9
}, {
rule: 'optional',
type: 'string',
name: 'st',
id: 10
}, {
rule: 'optional',
type: 'int32',
name: 'stTtl',
id: 11
}, {
rule: 'optional',
type: 'int32',
name: 'code',
id: 12
}, {
rule: 'optional',
type: 'string',
name: 'reason',
id: 13
}, {
rule: 'optional',
type: 'string',
name: 'deviceToken',
id: 14
}, {
rule: 'optional',
type: 'bool',
name: 'sp',
id: 15
}, {
rule: 'optional',
type: 'string',
name: 'detail',
id: 16
}, {
rule: 'optional',
type: 'int64',
name: 'lastUnreadNotifTime',
id: 17
}, {
rule: 'optional',
type: 'int64',
name: 'lastPatchTime',
id: 18
}, {
rule: 'optional',
type: 'int64',
name: 'configBitmap',
id: 19
}]
}, {
name: 'ErrorCommand',
syntax: 'proto2',
fields: [{
rule: 'required',
type: 'int32',
name: 'code',
id: 1
}, {
rule: 'required',
type: 'string',
name: 'reason',
id: 2
}, {
rule: 'optional',
type: 'int32',
name: 'appCode',
id: 3
}, {
rule: 'optional',
type: 'string',
name: 'detail',
id: 4
}, {
rule: 'repeated',
type: 'string',
name: 'pids',
id: 5
}, {
rule: 'optional',
type: 'string',
name: 'appMsg',
id: 6
}]
}, {
name: 'DirectCommand',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'string',
name: 'msg',
id: 1
}, {
rule: 'optional',
type: 'string',
name: 'uid',
id: 2
}, {
rule: 'optional',
type: 'string',
name: 'fromPeerId',
id: 3
}, {
rule: 'optional',
type: 'int64',
name: 'timestamp',
id: 4
}, {
rule: 'optional',
type: 'bool',
name: 'offline',
id: 5
}, {
rule: 'optional',
type: 'bool',
name: 'hasMore',
id: 6
}, {
rule: 'repeated',
type: 'string',
name: 'toPeerIds',
id: 7
}, {
rule: 'optional',
type: 'bool',
name: 'r',
id: 10
}, {
rule: 'optional',
type: 'string',
name: 'cid',
id: 11
}, {
rule: 'optional',
type: 'string',
name: 'id',
id: 12
}, {
rule: 'optional',
type: 'bool',
name: 'transient',
id: 13
}, {
rule: 'optional',
type: 'string',
name: 'dt',
id: 14
}, {
rule: 'optional',
type: 'string',
name: 'roomId',
id: 15
}, {
rule: 'optional',
type: 'string',
name: 'pushData',
id: 16
}, {
rule: 'optional',
type: 'bool',
name: 'will',
id: 17
}, {
rule: 'optional',
type: 'int64',
name: 'patchTimestamp',
id: 18
}, {
rule: 'optional',
type: 'bytes',
name: 'binaryMsg',
id: 19
}, {
rule: 'repeated',
type: 'string',
name: 'mentionPids',
id: 20
}, {
rule: 'optional',
type: 'bool',
name: 'mentionAll',
id: 21
}, {
rule: 'optional',
type: 'int32',
name: 'convType',
id: 22
}]
}, {
name: 'AckCommand',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'int32',
name: 'code',
id: 1
}, {
rule: 'optional',
type: 'string',
name: 'reason',
id: 2
}, {
rule: 'optional',
type: 'string',
name: 'mid',
id: 3
}, {
rule: 'optional',
type: 'string',
name: 'cid',
id: 4
}, {
rule: 'optional',
type: 'int64',
name: 't',
id: 5
}, {
rule: 'optional',
type: 'string',
name: 'uid',
id: 6
}, {
rule: 'optional',
type: 'int64',
name: 'fromts',
id: 7
}, {
rule: 'optional',
type: 'int64',
name: 'tots',
id: 8
}, {
rule: 'optional',
type: 'string',
name: 'type',
id: 9
}, {
rule: 'repeated',
type: 'string',
name: 'ids',
id: 10
}, {
rule: 'optional',
type: 'int32',
name: 'appCode',
id: 11
}, {
rule: 'optional',
type: 'string',
name: 'appMsg',
id: 12
}]
}, {
name: 'UnreadCommand',
syntax: 'proto2',
fields: [{
rule: 'repeated',
type: 'UnreadTuple',
name: 'convs',
id: 1
}, {
rule: 'optional',
type: 'int64',
name: 'notifTime',
id: 2
}]
}, {
name: 'ConvCommand',
syntax: 'proto2',
fields: [{
rule: 'repeated',
type: 'string',
name: 'm',
id: 1
}, {
rule: 'optional',
type: 'bool',
name: 'transient',
id: 2
}, {
rule: 'optional',
type: 'bool',
name: 'unique',
id: 3
}, {
rule: 'optional',
type: 'string',
name: 'cid',
id: 4
}, {
rule: 'optional',
type: 'string',
name: 'cdate',
id: 5
}, {
rule: 'optional',
type: 'string',
name: 'initBy',
id: 6
}, {
rule: 'optional',
type: 'string',
name: 'sort',
id: 7
}, {
rule: 'optional',
type: 'int32',
name: 'limit',
id: 8
}, {
rule: 'optional',
type: 'int32',
name: 'skip',
id: 9
}, {
rule: 'optional',
type: 'int32',
name: 'flag',
id: 10
}, {
rule: 'optional',
type: 'int32',
name: 'count',
id: 11
}, {
rule: 'optional',
type: 'string',
name: 'udate',
id: 12
}, {
rule: 'optional',
type: 'int64',
name: 't',
id: 13
}, {
rule: 'optional',
type: 'string',
name: 'n',
id: 14
}, {
rule: 'optional',
type: 'string',
name: 's',
id: 15
}, {
rule: 'optional',
type: 'bool',
name: 'statusSub',
id: 16
}, {
rule: 'optional',
type: 'bool',
name: 'statusPub',
id: 17
}, {
rule: 'optional',
type: 'int32',
name: 'statusTTL',
id: 18
}, {
rule: 'optional',
type: 'string',
name: 'uniqueId',
id: 19
}, {
rule: 'optional',
type: 'string',
name: 'targetClientId',
id: 20
}, {
rule: 'optional',
type: 'int64',
name: 'maxReadTimestamp',
id: 21
}, {
rule: 'optional',
type: 'int64',
name: 'maxAckTimestamp',
id: 22
}, {
rule: 'optional',
type: 'bool',
name: 'queryAllMembers',
id: 23
}, {
rule: 'repeated',
type: 'MaxReadTuple',
name: 'maxReadTuples',
id: 24
}, {
rule: 'repeated',
type: 'string',
name: 'cids',
id: 25
}, {
rule: 'optional',
type: 'ConvMemberInfo',
name: 'info',
id: 26
}, {
rule: 'optional',
type: 'bool',
name: 'tempConv',
id: 27
}, {
rule: 'optional',
type: 'int32',
name: 'tempConvTTL',
id: 28
}, {
rule: 'repeated',
type: 'string',
name: 'tempConvIds',
id: 29
}, {
rule: 'repeated',
type: 'string',
name: 'allowedPids',
id: 30
}, {
rule: 'repeated',
type: 'ErrorCommand',
name: 'failedPids',
id: 31
}, {
rule: 'optional',
type: 'string',
name: 'next',
id: 40
}, {
rule: 'optional',
type: 'JsonObjectMessage',
name: 'results',
id: 100
}, {
rule: 'optional',
type: 'JsonObjectMessage',
name: 'where',
id: 101
}, {
rule: 'optional',
type: 'JsonObjectMessage',
name: 'attr',
id: 103
}, {
rule: 'optional',
type: 'JsonObjectMessage',
name: 'attrModified',
id: 104
}]
}, {
name: 'RoomCommand',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'string',
name: 'roomId',
id: 1
}, {
rule: 'optional',
type: 'string',
name: 's',
id: 2
}, {
rule: 'optional',
type: 'int64',
name: 't',
id: 3
}, {
rule: 'optional',
type: 'string',
name: 'n',
id: 4
}, {
rule: 'optional',
type: 'bool',
name: 'transient',
id: 5
}, {
rule: 'repeated',
type: 'string',
name: 'roomPeerIds',
id: 6
}, {
rule: 'optional',
type: 'string',
name: 'byPeerId',
id: 7
}]
}, {
name: 'LogsCommand',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'string',
name: 'cid',
id: 1
}, {
rule: 'optional',
type: 'int32',
name: 'l',
id: 2
}, {
rule: 'optional',
type: 'int32',
name: 'limit',
id: 3
}, {
rule: 'optional',
type: 'int64',
name: 't',
id: 4
}, {
rule: 'optional',
type: 'int64',
name: 'tt',
id: 5
}, {
rule: 'optional',
type: 'string',
name: 'tmid',
id: 6
}, {
rule: 'optional',
type: 'string',
name: 'mid',
id: 7
}, {
rule: 'optional',
type: 'string',
name: 'checksum',
id: 8
}, {
rule: 'optional',
type: 'bool',
name: 'stored',
id: 9
}, {
rule: 'optional',
type: 'QueryDirection',
name: 'direction',
id: 10,
options: {
"default": 'OLD'
}
}, {
rule: 'optional',
type: 'bool',
name: 'tIncluded',
id: 11
}, {
rule: 'optional',
type: 'bool',
name: 'ttIncluded',
id: 12
}, {
rule: 'optional',
type: 'int32',
name: 'lctype',
id: 13
}, {
rule: 'repeated',
type: 'LogItem',
name: 'logs',
id: 105
}],
enums: [{
name: 'QueryDirection',
syntax: 'proto2',
values: [{
name: 'OLD',
id: 1
}, {
name: 'NEW',
id: 2
}]
}]
}, {
name: 'RcpCommand',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'string',
name: 'id',
id: 1
}, {
rule: 'optional',
type: 'string',
name: 'cid',
id: 2
}, {
rule: 'optional',
type: 'int64',
name: 't',
id: 3
}, {
rule: 'optional',
type: 'bool',
name: 'read',
id: 4
}, {
rule: 'optional',
type: 'string',
name: 'from',
id: 5
}]
}, {
name: 'ReadTuple',
syntax: 'proto2',
fields: [{
rule: 'required',
type: 'string',
name: 'cid',
id: 1
}, {
rule: 'optional',
type: 'int64',
name: 'timestamp',
id: 2
}, {
rule: 'optional',
type: 'string',
name: 'mid',
id: 3
}]
}, {
name: 'MaxReadTuple',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'string',
name: 'pid',
id: 1
}, {
rule: 'optional',
type: 'int64',
name: 'maxAckTimestamp',
id: 2
}, {
rule: 'optional',
type: 'int64',
name: 'maxReadTimestamp',
id: 3
}]
}, {
name: 'ReadCommand',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'string',
name: 'cid',
id: 1
}, {
rule: 'repeated',
type: 'string',
name: 'cids',
id: 2
}, {
rule: 'repeated',
type: 'ReadTuple',
name: 'convs',
id: 3
}]
}, {
name: 'PresenceCommand',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'StatusType',
name: 'status',
id: 1
}, {
rule: 'repeated',
type: 'string',
name: 'sessionPeerIds',
id: 2
}, {
rule: 'optional',
type: 'string',
name: 'cid',
id: 3
}]
}, {
name: 'ReportCommand',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'bool',
name: 'initiative',
id: 1
}, {
rule: 'optional',
type: 'string',
name: 'type',
id: 2
}, {
rule: 'optional',
type: 'string',
name: 'data',
id: 3
}]
}, {
name: 'PatchItem',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'string',
name: 'cid',
id: 1
}, {
rule: 'optional',
type: 'string',
name: 'mid',
id: 2
}, {
rule: 'optional',
type: 'int64',
name: 'timestamp',
id: 3
}, {
rule: 'optional',
type: 'bool',
name: 'recall',
id: 4
}, {
rule: 'optional',
type: 'string',
name: 'data',
id: 5
}, {
rule: 'optional',
type: 'int64',
name: 'patchTimestamp',
id: 6
}, {
rule: 'optional',
type: 'string',
name: 'from',
id: 7
}, {
rule: 'optional',
type: 'bytes',
name: 'binaryMsg',
id: 8
}, {
rule: 'optional',
type: 'bool',
name: 'mentionAll',
id: 9
}, {
rule: 'repeated',
type: 'string',
name: 'mentionPids',
id: 10
}, {
rule: 'optional',
type: 'int64',
name: 'patchCode',
id: 11
}, {
rule: 'optional',
type: 'string',
name: 'patchReason',
id: 12
}]
}, {
name: 'PatchCommand',
syntax: 'proto2',
fields: [{
rule: 'repeated',
type: 'PatchItem',
name: 'patches',
id: 1
}, {
rule: 'optional',
type: 'int64',
name: 'lastPatchTime',
id: 2
}]
}, {
name: 'PubsubCommand',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'string',
name: 'cid',
id: 1
}, {
rule: 'repeated',
type: 'string',
name: 'cids',
id: 2
}, {
rule: 'optional',
type: 'string',
name: 'topic',
id: 3
}, {
rule: 'optional',
type: 'string',
name: 'subtopic',
id: 4
}, {
rule: 'repeated',
type: 'string',
name: 'topics',
id: 5
}, {
rule: 'repeated',
type: 'string',
name: 'subtopics',
id: 6
}, {
rule: 'optional',
type: 'JsonObjectMessage',
name: 'results',
id: 7
}]
}, {
name: 'BlacklistCommand',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'string',
name: 'srcCid',
id: 1
}, {
rule: 'repeated',
type: 'string',
name: 'toPids',
id: 2
}, {
rule: 'optional',
type: 'string',
name: 'srcPid',
id: 3
}, {
rule: 'repeated',
type: 'string',
name: 'toCids',
id: 4
}, {
rule: 'optional',
type: 'int32',
name: 'limit',
id: 5
}, {
rule: 'optional',
type: 'string',
name: 'next',
id: 6
}, {
rule: 'repeated',
type: 'string',
name: 'blockedPids',
id: 8
}, {
rule: 'repeated',
type: 'string',
name: 'blockedCids',
id: 9
}, {
rule: 'repeated',
type: 'string',
name: 'allowedPids',
id: 10
}, {
rule: 'repeated',
type: 'ErrorCommand',
name: 'failedPids',
id: 11
}, {
rule: 'optional',
type: 'int64',
name: 't',
id: 12
}, {
rule: 'optional',
type: 'string',
name: 'n',
id: 13
}, {
rule: 'optional',
type: 'string',
name: 's',
id: 14
}]
}, {
name: 'GenericCommand',
syntax: 'proto2',
fields: [{
rule: 'optional',
type: 'CommandType',
name: 'cmd',
id: 1
}, {
rule: 'optional',
type: 'OpType',
name: 'op',
id: 2
}, {
rule: 'optional',
type: 'string',
name: 'appId',
id: 3
}, {
rule: 'optional',
type: 'string',
name: 'peerId',
id: 4
}, {
rule: 'optional',
type: 'int32',
name: 'i',
id: 5
}, {
rule: 'optional',
type: 'string',
name: 'installationId',
id: 6
}, {
rule: 'optional',
type: 'int32',
name: 'priority',
id: 7
}, {
rule: 'optional',
type: 'int32',
name: 'service',
id: 8
}, {
rule: 'optional',
type: 'int64',
name: 'serverTs',
id: 9
}, {
rule: 'optional',
type: 'int64',
name: 'clientTs',
id: 10
}, {
rule: 'optional',
type: 'int32',
name: 'notificationType',
id: 11
}, {
rule: 'optional',
type: 'DataCommand',
name: 'dataMessage',
id: 101
}, {
rule: 'optional',
type: 'SessionCommand',
name: 'sessionMessage',
id: 102
}, {
rule: 'optional',
type: 'ErrorCommand',
name: 'errorMessage',
id: 103
}, {
rule: 'optional',
type: 'DirectCommand',
name: 'directMessage',
id: 104
}, {
rule: 'optional',
type: 'AckCommand',
name: 'ackMessage',
id: 105
}, {
rule: 'optional',
type: 'UnreadCommand',
name: 'unreadMessage',
id: 106
}, {
rule: 'optional',
type: 'ReadCommand',
name: 'readMessage',
id: 107
}, {
rule: 'optional',
type: 'RcpCommand',
name: 'rcpMessage',
id: 108
}, {
rule: 'optional',
type: 'LogsCommand',
name: 'logsMessage',
id: 109
}, {
rule: 'optional',
type: 'ConvCommand',
name: 'convMessage',
id: 110
}, {
rule: 'optional',
type: 'RoomCommand',
name: 'roomMessage',
id: 111
}, {
rule: 'optional',
type: 'PresenceCommand',
name: 'presenceMessage',
id: 112
}, {
rule: 'optional',
type: 'ReportCommand',
name: 'reportMessage',
id: 113
}, {
rule: 'optional',
type: 'PatchCommand',
name: 'patchMessage',
id: 114
}, {
rule: 'optional',
type: 'PubsubCommand',
name: 'pubsubMessage',
id: 115
}, {
rule: 'optional',
type: 'BlacklistCommand',
name: 'blacklistMessage',
id: 116
}]
}],
enums: [{
name: 'CommandType',
syntax: 'proto2',
values: [{
name: 'session',
id: 0
}, {
name: 'conv',
id: 1
}, {
name: 'direct',
id: 2
}, {
name: 'ack',
id: 3
}, {
name: 'rcp',
id: 4
}, {
name: 'unread',
id: 5
}, {
name: 'logs',
id: 6
}, {
name: 'error',
id: 7
}, {
name: 'login',
id: 8
}, {
name: 'data',
id: 9
}, {
name: 'room',
id: 10
}, {
name: 'read',
id: 11
}, {
name: 'presence',
id: 12
}, {
name: 'report',
id: 13
}, {
name: 'echo',
id: 14
}, {
name: 'loggedin',
id: 15
}, {
name: 'logout',
id: 16
}, {
name: 'loggedout',
id: 17
}, {
name: 'patch',
id: 18
}, {
name: 'pubsub',
id: 19
}, {
name: 'blacklist',
id: 20
}, {
name: 'goaway',
id: 21
}]
}, {
name: 'OpType',
syntax: 'proto2',
values: [{
name: 'open',
id: 1
}, {
name: 'add',
id: 2
}, {
name: 'remove',
id: 3
}, {
name: 'close',
id: 4
}, {
name: 'opened',
id: 5
}, {
name: 'closed',
id: 6
}, {
name: 'query',
id: 7
}, {
name: 'query_result',
id: 8
}, {
name: 'conflict',
id: 9
}, {
name: 'added',
id: 10
}, {
name: 'removed',
id: 11
}, {
name: 'refresh',
id: 12
}, {
name: 'refreshed',
id: 13
}, {
name: 'start',
id: 30
}, {
name: 'started',
id: 31
}, {
name: 'joined',
id: 32
}, {
name: 'members_joined',
id: 33
}, {
name: 'left',
id: 39
}, {
name: 'members_left',
id: 40
}, {
name: 'results',
id: 42
}, {
name: 'count',
id: 43
}, {
name: 'result',
id: 44
}, {
name: 'update',
id: 45
}, {
name: 'updated',
id: 46
}, {
name: 'mute',
id: 47
}, {
name: 'unmute',
id: 48
}, {
name: 'status',
id: 49
}, {
name: 'members',
id: 50
}, {
name: 'max_read',
id: 51
}, {
name: 'is_member',
id: 52
}, {
name: 'member_info_update',
id: 53
}, {
name: 'member_info_updated',
id: 54
}, {
name: 'member_info_changed',
id: 55
}, {
name: 'join',
id: 80
}, {
name: 'invite',
id: 81
}, {
name: 'leave',
id: 82
}, {
name: 'kick',
id: 83
}, {
name: 'reject',
id: 84
}, {
name: 'invited',
id: 85
}, {
name: 'kicked',
id: 86
}, {
name: 'upload',
id: 100
}, {
name: 'uploaded',
id: 101
}, {
name: 'subscribe',
id: 120
}, {
name: 'subscribed',
id: 121
}, {
name: 'unsubscribe',
id: 122
}, {
name: 'unsubscribed',
id: 123
}, {
name: 'is_subscribed',
id: 124
}, {
name: 'modify',
id: 150
}, {
name: 'modified',
id: 151
}, {
name: 'block',
id: 170
}, {
name: 'unblock',
id: 171
}, {
name: 'blocked',
id: 172
}, {
name: 'unblocked',
id: 173
}, {
name: 'members_blocked',
id: 174
}, {
name: 'members_unblocked',
id: 175
}, {
name: 'check_block',
id: 176
}, {
name: 'check_result',
id: 177
}, {
name: 'add_shutup',
id: 180
}, {
name: 'remove_shutup',
id: 181
}, {
name: 'query_shutup',
id: 182
}, {
name: 'shutup_added',
id: 183
}, {
name: 'shutup_removed',
id: 184
}, {
name: 'shutup_result',
id: 185
}, {
name: 'shutuped',
id: 186
}, {
name: 'unshutuped',
id: 187
}, {
name: 'members_shutuped',
id: 188
}, {
name: 'members_unshutuped',
id: 189
}, {
name: 'check_shutup',
id: 190
}]
}, {
name: 'StatusType',
syntax: 'proto2',
values: [{
name: 'on',
id: 1
}, {
name: 'off',
id: 2
}]
}],
isNamespace: true
}).build();
var _messages$push_server = messageCompiled.push_server.messages2,
JsonObjectMessage = _messages$push_server.JsonObjectMessage,
UnreadTuple = _messages$push_server.UnreadTuple,
LogItem = _messages$push_server.LogItem,
DataCommand = _messages$push_server.DataCommand,
SessionCommand = _messages$push_server.SessionCommand,
ErrorCommand = _messages$push_server.ErrorCommand,
DirectCommand = _messages$push_server.DirectCommand,
AckCommand = _messages$push_server.AckCommand,
UnreadCommand = _messages$push_server.UnreadCommand,
ConvCommand = _messages$push_server.ConvCommand,
RoomCommand = _messages$push_server.RoomCommand,
LogsCommand = _messages$push_server.LogsCommand,
RcpCommand = _messages$push_server.RcpCommand,
ReadTuple = _messages$push_server.ReadTuple,
MaxReadTuple = _messages$push_server.MaxReadTuple,
ReadCommand = _messages$push_server.ReadCommand,
PresenceCommand = _messages$push_server.PresenceCommand,
ReportCommand = _messages$push_server.ReportCommand,
GenericCommand = _messages$push_server.GenericCommand,
BlacklistCommand = _messages$push_server.BlacklistCommand,
PatchCommand = _messages$push_server.PatchCommand,
PatchItem = _messages$push_server.PatchItem,
ConvMemberInfo = _messages$push_server.ConvMemberInfo,
CommandType = _messages$push_server.CommandType,
OpType = _messages$push_server.OpType,
StatusType = _messages$push_server.StatusType;
var message = /*#__PURE__*/Object.freeze({
__proto__: null,
JsonObjectMessage: JsonObjectMessage,
UnreadTuple: UnreadTuple,
LogItem: LogItem,
DataCommand: DataCommand,
SessionCommand: SessionCommand,
ErrorCommand: ErrorCommand,
DirectCommand: DirectCommand,
AckCommand: AckCommand,
UnreadCommand: UnreadCommand,
ConvCommand: ConvCommand,
RoomCommand: RoomCommand,
LogsCommand: LogsCommand,
RcpCommand: RcpCommand,
ReadTuple: ReadTuple,
MaxReadTuple: MaxReadTuple,
ReadCommand: ReadCommand,
PresenceCommand: PresenceCommand,
ReportCommand: ReportCommand,
GenericCommand: GenericCommand,
BlacklistCommand: BlacklistCommand,
PatchCommand: PatchCommand,
PatchItem: PatchItem,
ConvMemberInfo: ConvMemberInfo,
CommandType: CommandType,
OpType: OpType,
StatusType: StatusType
});
var adapters = {};
var getAdapter = function getAdapter(name) {
var adapter = adapters[name];
if (adapter === undefined) {
throw new Error("".concat(name, " adapter is not configured"));
}
return adapter;
};
/**
* 指定 Adapters
* @function
* @memberof module:leancloud-realtime
* @param {Adapters} newAdapters Adapters 的类型请参考 {@link https://url.leanapp.cn/adapter-type-definitions @leancloud/adapter-types} 中的定义
*/
var setAdapters = function setAdapters(newAdapters) {
Object.assign(adapters, newAdapters);
};
/* eslint-disable */
var global$1 = typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : {};
var EXPIRED = Symbol('expired');
var debug = d('LC:Expirable');
var Expirable = /*#__PURE__*/function () {
function Expirable(value, ttl) {
this.originalValue = value;
if (typeof ttl === 'number') {
this.expiredAt = Date.now() + ttl;
}
}
_createClass(Expirable, [{
key: "value",
get: function get() {
var expired = this.expiredAt && this.expiredAt <= Date.now();
if (expired) debug("expired: ".concat(this.originalValue));
return expired ? EXPIRED : this.originalValue;
}
}]);
return Expirable;
}();
Expirable.EXPIRED = EXPIRED;
var debug$1 = d('LC:Cache');
var Cache = /*#__PURE__*/function () {
function Cache() {
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'anonymous';
this.name = name;
this._map = {};
}
var _proto = Cache.prototype;
_proto.get = function get(key) {
var cache = this._map[key];
if (cache) {
var value = cache.value;
if (value !== Expirable.EXPIRED) {
debug$1('[%s] hit: %s', this.name, key);
return value;
}
delete this._map[key];
}
debug$1("[".concat(this.name, "] missed: ").concat(key));
return null;
};
_proto.set = function set(key, value, ttl) {
debug$1('[%s] set: %s %d', this.name, key, ttl);
this._map[key] = new Expirable(value, ttl);
};
return Cache;
}();
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
/**
* 调试日志控制器
* @const
* @memberof module:leancloud-realtime
* @example
* debug.enable(); // 启用调试日志
* debug.disable(); // 关闭调试日志
*/
var debug$2 = {
enable: function enable() {
var namespaces = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'LC*';
return d.enable(namespaces);
},
disable: d.disable
};
var tryAll = function tryAll(promiseConstructors) {
var promise = new Promise(promiseConstructors[0]);
if (promiseConstructors.length === 1) {
return promise;
}
return promise["catch"](function () {
return tryAll(promiseConstructors.slice(1));
});
}; // eslint-disable-next-line no-sequences
var tap = function tap(interceptor) {
return function (value) {
return interceptor(value), value;
};
};
var finalize = function finalize(callback) {
return [// eslint-disable-next-line no-sequences
function (value) {
return callback(), value;
}, function (error) {
callback();
throw error;
}];
};
/**
* 将对象转换为 Date,支持 string、number、ProtoBuf Long 以及 LeanCloud 的 Date 类型,
* 其他情况下(包括对象为 falsy)返回原值。
* @private
*/
var decodeDate = function decodeDate(date) {
if (!date) return date;
if (typeof date === 'string' || typeof date === 'number') {
return new Date(date);
}
if (date.__type === 'Date' && date.iso) {
return new Date(date.iso);
} // Long
if (typeof date.toNumber === 'function') {
return new Date(date.toNumber());
}
return date;
};
/**
* 获取 Date 的毫秒数,如果不是一个 Date 返回 undefined。
* @private
*/
var getTime = function getTime(date) {
return date && date.getTime ? date.getTime() : undefined;
};
/**
* 解码对象中的 LeanCloud 数据结构。
* 目前仅会处理 Date 类型。
* @private
*/
var decode = function decode(value) {
if (!value) return value;
if (value.__type === 'Date' && value.iso) {
return new Date(value.iso);
}
if (Array.isArray(value)) {
return value.map(decode);
}
if (isPlainObject(value)) {
return Object.keys(value).reduce(function (result, key) {
return _objectSpread(_objectSpread({}, result), {}, _defineProperty({}, key, decode(value[key])));
}, {});
}
return value;
};
/**
* 将对象中的特殊类型编码为 LeanCloud 数据结构。
* 目前仅会处理 Date 类型。
* @private
*/
var encode = function encode(value) {
if (value instanceof Date) return {
__type: 'Date',
iso: value.toJSON()
};
if (Array.isArray(value)) {
return value.map(encode);
}
if (isPlainObject(value)) {
return Object.keys(value).reduce(function (result, key) {
return _objectSpread(_objectSpread({}, result), {}, _defineProperty({}, key, encode(value[key])));
}, {});
}
return value;
};
var keyRemap = function keyRemap(keymap, obj) {
return Object.keys(obj).reduce(function (newObj, key) {
var newKey = keymap[key] || key;
return Object.assign(newObj, _defineProperty({}, newKey, obj[key]));
}, {});
};
var isIE10 = global$1.navigator && global$1.navigator.userAgent && global$1.navigator.userAgent.indexOf('MSIE 10.') !== -1;
/* eslint-disable no-proto */
var getStaticProperty = function getStaticProperty(klass, property) {
return klass[property] || (klass.__proto__ ? getStaticProperty(klass.__proto__, property) : undefined);
};
/* eslint-enable no-proto */
var union = function union(a, b) {
return Array.from(new Set([].concat(_toConsumableArray(a), _toConsumableArray(b))));
};
var difference = function difference(a, b) {
return Array.from(function (bSet) {
return new Set(a.filter(function (x) {
return !bSet.has(x);
}));
}(new Set(b)));
};
var map = new WeakMap(); // protected property helper
var internal = function internal(object) {
if (!map.has(object)) {
map.set(object, {});
}
return map.get(object);
};
var compact = function compact(obj, filter) {
if (!isPlainObject(obj)) return obj;
var object = _objectSpread({}, obj);
Object.keys(object).forEach(function (prop) {
var value = object[prop];
if (value === filter) {
delete object[prop];
} else {
object[prop] = compact(value, filter);
}
});
return object;
}; // debug utility
var removeNull = function removeNull(obj) {
return compact(obj, null);
};
var trim = function trim(message) {
return removeNull(JSON.parse(JSON.stringify(message)));
};
var ensureArray = function ensureArray(target) {
if (Array.isArray(target)) {
return target;
}
if (target === undefined || target === null) {
return [];
}
return [target];
};
var setValue = function setValue(target, key, value) {
// '.' is not allowed in Class keys, escaping is not in concern now.
var segs = key.split('.');
var lastSeg = segs.pop();
var currentTarget = target;
segs.forEach(function (seg) {
if (currentTarget[seg] === undefined) currentTarget[seg] = {};
currentTarget = currentTarget[seg];
});
currentTarget[lastSeg] = value;
return target;
};
var isWeapp = // eslint-disable-next-line no-undef
(typeof wx === "undefined" ? "undefined" : _typeof(wx)) === 'object' && typeof wx.connectSocket === 'function'; // throttle decorator
var throttle = function throttle(wait) {
return function (target, property, descriptor) {
var callback = descriptor.value; // very naive, internal use only
if (callback.length) {
throw new Error('throttled function should not accept any arguments');
}
return _objectSpread(_objectSpread({}, descriptor), {}, {
value: function value() {
var _this = this;
var _internal = internal(this),
throttleMeta = _internal.throttleMeta;
if (!throttleMeta) {
throttleMeta = {};
internal(this).throttleMeta = throttleMeta;
}
var _throttleMeta = throttleMeta,
propertyMeta = _throttleMeta[property];
if (!propertyMeta) {
propertyMeta = {};
throttleMeta[property] = propertyMeta;
}
var _propertyMeta = propertyMeta,
_propertyMeta$previou = _propertyMeta.previouseTimestamp,
previouseTimestamp = _propertyMeta$previou === void 0 ? 0 : _propertyMeta$previou,
timeout = _propertyMeta.timeout;
var now = Date.now();
var remainingTime = wait - (now - previouseTimestamp);
if (remainingTime <= 0) {
throttleMeta[property].previouseTimestamp = now;
callback.apply(this);
} else if (!timeout) {
propertyMeta.timeout = setTimeout(function () {
propertyMeta.previouseTimestamp = Date.now();
delete propertyMeta.timeout;
callback.apply(_this);
}, remainingTime);
}
}
});
};
};
var isCNApp = function isCNApp(appId) {
return appId.slice(-9) !== '-MdYXbMMI';
};
var equalBuffer = function equalBuffer(buffer1, buffer2) {
if (!buffer1 || !buffer2) return false;
if (buffer1.byteLength !== buffer2.byteLength) return false;
var a = new Uint8Array(buffer1);
var b = new Uint8Array(buffer2);
return !a.some(function (value, index) {
return value !== b[index];
});
};
var _class;
function ownKeys$1(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$1(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$1(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
var debug$3 = d('LC:WebSocketPlus');
var OPEN = 'open';
var DISCONNECT = 'disconnect';
var RECONNECT = 'reconnect';
var RETRY = 'retry';
var SCHEDULE = 'schedule';
var OFFLINE = 'offline';
var ONLINE = 'online';
var ERROR = 'error';
var MESSAGE = 'message';
var HEARTBEAT_TIME = 180000;
var TIMEOUT_TIME = 380000;
var DEFAULT_RETRY_STRATEGY = function DEFAULT_RETRY_STRATEGY(attempt) {
return Math.min(1000 * Math.pow(2, attempt), 300000);
};
var requireConnected = function requireConnected(target, name, descriptor) {
return _objectSpread$1(_objectSpread$1({}, descriptor), {}, {
value: function requireConnectedWrapper() {
var _descriptor$value;
this.checkConnectionAvailability(name);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return (_descriptor$value = descriptor.value).call.apply(_descriptor$value, [this].concat(args));
}
});
};
var WebSocketPlus = (_class = /*#__PURE__*/function (_EventEmitter) {
_inheritsLoose(WebSocketPlus, _EventEmitter);
_createClass(WebSocketPlus, [{
key: "urls",
get: function get() {
return this._urls;
},
set: function set(urls) {
this._urls = ensureArray(urls);
}
}]);
function WebSocketPlus(getUrls, protocol) {
var _this;
_this = _EventEmitter.call(this) || this;
_this.init();
_this._protocol = protocol;
Promise.resolve(typeof getUrls === 'function' ? getUrls() : getUrls).then(ensureArray).then(function (urls) {
_this._urls = urls;
return _this._open();
}).then(function () {
_this.__postponeTimeoutTimer = _this._postponeTimeoutTimer.bind(_assertThisInitialized(_this));
if (global$1.addEventListener) {
_this.__pause = function () {
if (_this.can('pause')) _this.pause();
};
_this.__resume = function () {
if (_this.can('resume')) _this.resume();
};
global$1.addEventListener('offline', _this.__pause);
global$1.addEventListener('online', _this.__resume);
}
_this.open();
})["catch"](_this["throw"].bind(_assertThisInitialized(_this)));
return _this;
}
var _proto = WebSocketPlus.prototype;
_proto._open = function _open() {
var _this2 = this;
return this._createWs(this._urls, this._protocol).then(function (ws) {
var _this2$_urls = _toArray(_this2._urls),
first = _this2$_urls[0],
reset = _this2$_urls.slice(1);
_this2._urls = [].concat(_toConsumableArray(reset), [first]);
return ws;
});
};
_proto._createWs = function _createWs(urls, protocol) {
var _this3 = this;
return tryAll(urls.map(function (url) {
return function (resolve, reject) {
debug$3("connect [".concat(url, "] ").concat(protocol));
var WebSocket = getAdapter('WebSocket');
var ws = protocol ? new WebSocket(url, protocol) : new WebSocket(url);
ws.binaryType = _this3.binaryType || 'arraybuffer';
ws.onopen = function () {
return resolve(ws);
};
ws.onclose = function (error) {
if (error instanceof Error) {
return reject(error);
} // in browser, error event is useless
return reject(new Error("Failed to connect [".concat(url, "]")));
};
ws.onerror = ws.onclose;
};
})).then(function (ws) {
_this3._ws = ws;
_this3._ws.onclose = _this3._handleClose.bind(_this3);
_this3._ws.onmessage = _this3._handleMessage.bind(_this3);
return ws;
});
};
_proto._destroyWs = function _destroyWs() {
var ws = this._ws;
if (!ws) return;
ws.onopen = null;
ws.onclose = null;
ws.onerror = null;
ws.onmessage = null;
this._ws = null;
ws.close();
} // eslint-disable-next-line class-methods-use-this
;
_proto.onbeforeevent = function onbeforeevent(event, from, to) {
for (var _len2 = arguments.length, payload = new Array(_len2 > 3 ? _len2 - 3 : 0), _key2 = 3; _key2 < _len2; _key2++) {
payload[_key2 - 3] = arguments[_key2];
}
debug$3("".concat(event, ": ").concat(from, " -> ").concat(to, " %o"), payload);
};
_proto.onopen = function onopen() {
this.emit(OPEN);
};
_proto.onconnected = function onconnected() {
this._startConnectionKeeper();
};
_proto.onleaveconnected = function onleaveconnected(event, from, to) {
this._stopConnectionKeeper();
this._destroyWs();
if (to === 'offline' || to === 'disconnected') {
this.emit(DISCONNECT);
}
};
_proto.onpause = function onpause() {
this.emit(OFFLINE);
};
_proto.onbeforeresume = function onbeforeresume() {
this.emit(ONLINE);
};
_proto.onreconnect = function onreconnect() {
this.emit(RECONNECT);
};
_proto.ondisconnected = function ondisconnected(event, from, to) {
var _this4 = this;
var attempt = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
var delay = from === OFFLINE ? 0 : DEFAULT_RETRY_STRATEGY.call(null, attempt);
debug$3("schedule attempt=".concat(attempt, " delay=").concat(delay));
this.emit(SCHEDULE, attempt, delay);
if (this.__scheduledRetry) {
clearTimeout(this.__scheduledRetry);
}
this.__scheduledRetry = setTimeout(function () {
if (_this4.is('disconnected')) {
_this4.retry(attempt);
}
}, delay);
};
_proto.onretry = function onretry(event, from, to) {
var _this5 = this;
var attempt = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
this.emit(RETRY, attempt);
this._open().then(function () {
return _this5.can('reconnect') && _this5.reconnect();
}, function () {
return _this5.can('fail') && _this5.fail(attempt + 1);
});
};
_proto.onerror = function onerror(event, from, to, error) {
this.emit(ERROR, error);
};
_proto.onclose = function onclose() {
if (global$1.removeEventListener) {
if (this.__pause) global$1.removeEventListener('offline', this.__pause);
if (this.__resume) global$1.removeEventListener('online', this.__resume);
}
};
_proto.checkConnectionAvailability = function checkConnectionAvailability() {
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'API';
if (!this.is('connected')) {
var currentState = this.current;
console.warn("".concat(name, " should not be called when the connection is ").concat(currentState));
if (this.is('disconnected') || this.is('reconnecting')) {
console.warn('disconnect and reconnect event should be handled to avoid such calls.');
}
throw new Error('Connection unavailable');
}
} // jsdoc-ignore-start
;
_proto. // jsdoc-ignore-end
_ping = function _ping() {
debug$3('ping');
try {
this.ping();
} catch (error) {
console.warn("websocket ping error: ".concat(error.message));
}
};
_proto.ping = function ping() {
if (this._ws.ping) {
this._ws.ping();
} else {
console.warn("The WebSocket implement does not support sending ping frame.\n Override ping method to use application defined ping/pong mechanism.");
}
};
_proto._postponeTimeoutTimer = function _postponeTimeoutTimer() {
var _this6 = this;
debug$3('_postponeTimeoutTimer');
this._clearTimeoutTimers();
this._timeoutTimer = setTimeout(function () {
debug$3('timeout');
_this6.disconnect();
}, TIMEOUT_TIME);
};
_proto._clearTimeoutTimers = function _clearTimeoutTimers() {