Пример #1
0
Class.prototype.bindCallback = function call(callback, options) {
  var self = this,
    timerState;

  // Massage options
  if (is.integer(options)) {
    options = {
      "timeout": options
    };
  } else if (is.array(options)) {
    options = {
      "arguments": options
    };
  }

  if (!is.array(options.arguments)) {
    options.arguments = [];
  }

  function boundCallback() {
    if (timerState) {
      if (timerState.timedOut) {
        return self.emit("error", "Callback timed out (" + String(Date.now() - timerState.now) + "), but was invoked anyway", callback.toString());
      }

      clearTimeout(timerState.timer);
    }

    return callback.apply(self, options.arguments.concat([].slice.call(arguments, 0)));
  }

  // Manage timeout expectations
  if (is.integer(options.timeout)) {
    timerState = {
      "now": Date.now(),
      "timedOut": false,
      "onTimeout": function () {
        timerState.timedOut = true;
      }
    };

    timerState.timer = setTimeout(timerState.onTimeout, options.timeout);

    boundCallback.moreTime = function (time) {
      if (timerState.timedOut) {
        return self.emit("error", "Asked for more time after timeout(" + String(Date.now() - timerState.now) + ")", callback.toString());
      }

      clearTimeout(timerState.timer);
      timerState.timer = setTimeout(timerState.onTimeout, time);
    };
  }

  return boundCallback;
};
Пример #2
0
 constructor: function (daoName, async, pageSize) {
     this.daoName = daoName;
     this.async = async;
     this.pageSize = 0;
     if (is.integer(pageSize) && pageSize >= 0) {
         this.pageSize = pageSize;
     }
 },
Пример #3
0
 integer: (value, min, max) => {
     min = is.defined(min) ? min : -Infinity;
     max = is.defined(max) ? max : Infinity;
     if (!is.integer(value)) {
         return false;
     }
     if (value < min || value > max) {
         throw new Error(`Value must be within [${min}, ${max}] inclusive, but was: ${value}`);
     }
     return true;
 },
Пример #4
0
  /**
   * Encodes a JavaScript value into the Firestore 'Value' representation.
   *
   * @private
   * @param {Object} val The object to encode
   * @returns {object|null} The Firestore Proto or null if we are deleting a
   * field.
   */
  static encodeValue(val) {
    if (val instanceof FieldTransform) {
      return null;
    }

    if (is.string(val)) {
      return {
        valueType: 'stringValue',
        stringValue: val,
      };
    }

    if (is.boolean(val)) {
      return {
        valueType: 'booleanValue',
        booleanValue: val,
      };
    }

    if (is.integer(val)) {
      return {
        valueType: 'integerValue',
        integerValue: val,
      };
    }

    // Integers are handled above, the remaining numbers are treated as doubles
    if (is.number(val)) {
      return {
        valueType: 'doubleValue',
        doubleValue: val,
      };
    }

    if (is.date(val)) {
      let epochSeconds = Math.floor(val.getTime() / 1000);
      let timestamp = {
        seconds: epochSeconds,
        nanos: (val.getTime() - epochSeconds * 1000) * MS_TO_NANOS,
      };
      return {
        valueType: 'timestampValue',
        timestampValue: timestamp,
      };
    }

    if (is.array(val)) {
      const array = {
        valueType: 'arrayValue',
        arrayValue: {},
      };

      if (val.length > 0) {
        array.arrayValue.values = [];
        for (let i = 0; i < val.length; ++i) {
          let enc = DocumentSnapshot.encodeValue(val[i]);
          if (enc) {
            array.arrayValue.values.push(enc);
          }
        }
      }

      return array;
    }

    if (is.nil(val)) {
      return {
        valueType: 'nullValue',
        nullValue: 'NULL_VALUE',
      };
    }

    if (is.instance(val, DocumentReference) || is.instance(val, ResourcePath)) {
      return {
        valueType: 'referenceValue',
        referenceValue: val.formattedName,
      };
    }

    if (is.instance(val, GeoPoint)) {
      return {
        valueType: 'geoPointValue',
        geoPointValue: val.toProto(),
      };
    }

    if (is.instanceof(val, Buffer) || is.instanceof(val, Uint8Array)) {
      return {
        valueType: 'bytesValue',
        bytesValue: val,
      };
    }

    if (isPlainObject(val)) {
      const map = {
        valueType: 'mapValue',
        mapValue: {},
      };

      // If we encounter an empty object, we always need to send it to make sure
      // the server creates a map entry.
      if (!is.empty(val)) {
        map.mapValue.fields = DocumentSnapshot.encodeFields(val);
        if (is.empty(map.mapValue.fields)) {
          return null;
        }
      }

      return map;
    }

    throw validate.customObjectError(val);
  }
Пример #5
0
                success: function (rawData, status, xhr) {
                    var errorCode = parseInt(xhr.getResponseHeader('error_code'));
                    if (errorCode === 200) {
                        // success
                        if (!is.empty(rawData)) {
                            rawData = JSON.parse(rawData);
                        }
                        var errCode = 0;
                        if (rawData && rawData.errorCode != null && rawData.errorCode != undefined) {
                            errCode = rawData.errorCode;
                        }
                        var errMsg = 'succeed';
                        if (rawData && rawData.errorMsg != null && rawData.errorMsg != undefined) {
                            errMsg = rawData.errorMsg;
                        }

                        var totalCount = undefined;
                        if (rawData && is.integer(rawData.totalCount)) {
                            totalCount = rawData.totalCount;
                        }
                        var pageCount = undefined;
                        if (rawData && is.integer(rawData.pageCount)) {
                            pageCount = rawData.pageCount;
                        }

                        var resultData = undefined;
                        if (rawData && rawData.data) {
                            resultData = rawData.data;
                        }

                        if (callback) {
                            callback(errCode, errMsg, resultData, totalCount, pageCount);
                        }
                    } else {
                        if (errorCode === 100) {
                            NotLogonException.onException();
                        } else if (errorCode === 101) {
                            PermissionDeniedException.onException();
                        } else if (errorCode === 102) {
                            VersionNotSupportException.onException();
                        } else if (errorCode === 103) {
                            SessionInvalidException.onException();
                        } else if (errorCode === 104) {
                            NoServiceException.onException();
                        } else if (errorCode === 105) {
                            NoInterfaceException.onException();
                        } else if (errorCode === 106) {
                            JsonConvertException.onException();
                        } else if (errorCode === 107) {
                            ParameterCountException.onException();
                        } else if (errorCode === 108) {
                            ParameterTypeException.onException();
                        } else if (errorCode === 109) {
                            JsonToJavaException.onException();
                        } else if (errorCode === 110) {
                            BusinessProcessException.onException();
                        } else if (errorCode === 111) {
                            EntityWithoutIdException.onException();
                        } else if (errorCode === 112) {
                            DBConnectException.onException();
                        } else if (errorCode == 113) {
                            FieldValidateException.onException();
                        } else {
                            UnhandledException.onException('other error');
                        }
                        if (callback) {
                            callback(-1, 'server error');
                        }
                    }
                },
Пример #6
0
 setPageSize: function (pageSize) {
     if (is.integer(pageSize) && pageSize >= 0) {
         this.pageSize = pageSize;
     }
 },