urbanairship-react-native
Version:
Airship plugin for React Native apps.
103 lines (87 loc) • 2.41 kB
JavaScript
/* Copyright Airship and Contributors */
;
/**
* Attribute operation
* @hidden
*/
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AttributeEditor = void 0;
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/**
* Editor for attributes.
*/
class AttributeEditor {
/**
* AttributeEditor constructor
*
* @hidden
* @param onApply The apply function
*/
constructor(onApply) {
_defineProperty(this, "onApply", void 0);
_defineProperty(this, "operations", void 0);
this.onApply = onApply;
this.operations = [];
}
/**
* Adds an attribute.
*
* @param value The attribute value.
* @param name The attribute name.
* @return The attribute editor instance.
*/
setAttribute(name, value) {
var attributeValue;
var attributeType;
if (typeof value == "boolean") {
// No boolean attribute type. Convert value to string.
attributeValue = value.toString();
attributeType = "string";
} else {
attributeValue = value;
if (typeof value === "string") {
attributeType = "string";
} else if (typeof attributeValue === "number") {
attributeType = "number";
} else if (value instanceof Date) {
// JavaScript's date type doesn't pass through the JS to native bridge.
// Dates are instead serialized as milliseconds since epoch.
attributeType = "date";
attributeValue = value.getTime();
} else {
throw "Unsupported attribute type: " + typeof attributeValue;
}
}
const operation = {
"action": "set",
"value": attributeValue,
"key": name,
type: attributeType
};
this.operations.push(operation);
return this;
}
/**
* Removes an attribute.
* @param name The name of the attribute to remove.
* @return The attribute editor instance.
*/
removeAttribute(name) {
const operation = {
"action": "remove",
"key": name
};
this.operations.push(operation);
return this;
}
/**
* Applies the attribute operations.
*/
apply() {
this.onApply(this.operations);
}
}
exports.AttributeEditor = AttributeEditor;
//# sourceMappingURL=AttributeEditor.js.map