UNPKG

4.32 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.subLocal = subLocal;
7exports.subDoc = subDoc;
8exports.subQuery = subQuery;
9exports.subValue = subValue;
10exports.subApi = subApi;
11
12var _isString = require('lodash/isString');
13
14var _isString2 = _interopRequireDefault(_isString);
15
16var _isArray = require('lodash/isArray');
17
18var _isArray2 = _interopRequireDefault(_isArray);
19
20var _isBoolean = require('lodash/isBoolean');
21
22var _isBoolean2 = _interopRequireDefault(_isBoolean);
23
24var _isNumber = require('lodash/isNumber');
25
26var _isNumber2 = _interopRequireDefault(_isNumber);
27
28var _isExtraQuery = require('./isExtraQuery');
29
30function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
32function subLocal(localPath, defaultValue) {
33 if (typeof localPath !== 'string') {
34 throw new Error('[react-sharedb] subLocal(): localPath must be a String. Got: ' + localPath);
35 }
36 return {
37 __subscriptionType: 'Local',
38 params: localPath
39 };
40}
41
42function subDoc(collection, docId) {
43 var invalid = void 0;
44 if (typeof collection !== 'string') {
45 throw new Error('[react-sharedb] subDoc(): `collection` must be a String. Got: ' + collection);
46 }
47 if (docId == null) {
48 console.warn('\n [react-sharedb] subDoc(): You are trying to subscribe to an undefined document id:\n ' + collection + '.' + docId + '\n Falling back to \'__NULL__\' document to prevent critical crash.\n You should prevent situations when the `docId` is undefined.\n ');
49 invalid = true;
50 }
51 if (invalid) docId = '__NULL__';
52 return {
53 __subscriptionType: 'Doc',
54 __subscriptionInvalid: invalid,
55 params: [collection, docId]
56 };
57}
58
59function subQuery(collection, query) {
60 var invalid = void 0;
61 if (typeof collection !== 'string') {
62 throw new Error('[react-sharedb] subQuery(): Collection must be String. Got: ' + collection);
63 }
64 if (query == null) {
65 console.warn('\n [react-sharedb] subQuery(): Query is undefined. Got:\n ' + collection + ', ' + query + '\n Falling back to {_id: \'__NON_EXISTENT__\'} query to prevent critical crash.\n You should prevent situations when the `query` is undefined.\n ');
66 invalid = true;
67 }
68 if ((0, _isString2.default)(query) || (0, _isArray2.default)(query) || (0, _isBoolean2.default)(query) || (0, _isNumber2.default)(query)) {
69 throw new Error('\n [react-sharedb] subQuery(): Query is not an Object. Got:\n ' + collection + ', ' + query + '\n Query must always be an Object.\n ');
70 }
71 if (invalid) query = { _id: '__NON_EXISTENT__' };
72 return {
73 __subscriptionType: (0, _isExtraQuery.isExtraQuery)(query) ? 'QueryExtra' : 'Query',
74 __subscriptionInvalid: invalid,
75 params: [collection, query]
76 };
77}
78
79function subValue(value) {
80 return {
81 __subscriptionType: 'Value',
82 params: value
83 };
84}
85
86function subApi(path, fn, inputs, options) {
87 if (typeof path === 'function') {
88 options = inputs;
89 inputs = fn;
90 fn = path;
91 path = undefined;
92 }
93 if (typeof fn !== 'function') {
94 throw new Error('[react-sharedb] subApi(): api Function (which must return promise) was not provided. Got: ' + fn);
95 }
96 if (path != null && (typeof path !== 'string' || path === '')) {
97 throw new Error('[react-sharedb] subApi(): path must be a non-empty string. Got: ' + path);
98 }
99 if (inputs != null && !(0, _isArray2.default)(inputs)) {
100 if ((0, _isString2.default)(inputs) || (0, _isBoolean2.default)(inputs) || (0, _isNumber2.default)(inputs)) {
101 throw new Error('[react-sharedb] subApi(): inputs must be an array and ' + ('options must be an object. Got: inputs - ' + inputs + '; options - ' + options));
102 }
103 options = inputs;
104 inputs = undefined;
105 }
106 if (options != null && ((0, _isArray2.default)(options) || (0, _isString2.default)(options) || (0, _isBoolean2.default)(options))) {
107 throw new Error('[react-sharedb] subApi(): options must be an object. Got: ' + options);
108 }
109 if (options && options.debounce) {
110 if (!(0, _isNumber2.default)(options.debounce)) {
111 throw new Error('[react-sharedb] subApi(): debounce must be a number (milliseconds). Got: ' + options.debounce);
112 }
113 }
114 return {
115 __subscriptionType: 'Api',
116 params: [path, fn, inputs, options]
117 };
118}
\No newline at end of file