UNPKG

@ua/react-native-airship

Version:
125 lines (118 loc) 3.01 kB
/* Copyright Airship and Contributors */ 'use strict'; /** * Attribute operation * @hidden */ /** * Editor for attributes. */ export class AttributeEditor { /** * AttributeEditor constructor * * @hidden * @param onApply The apply function */ constructor(onApply) { 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; } /** * Adds a JSON attribute. * * @param name The attribute name. * @param value The attribute value. * @param instanceId The instance ID. * @param json The json value. Must not contain `exp` as top level key. * @param expiration: Optional expiration. * @return The attribute editor instance. */ setJsonAttribute(name, instanceId, json, expiration) { var operation = { action: 'set', value: json, key: name, instance_id: instanceId, type: 'json' }; if (expiration != null) { operation.expiration_milliseconds = expiration.getTime(); } 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; } /** * Removes a JSON attribute. * @param name The name of the attribute to remove. * @param instanceId The instance ID. * @return The attribute editor instance. */ removeJsonAttribute(name, instanceId) { const operation = { action: 'remove', key: name, instance_id: instanceId }; this.operations.push(operation); return this; } /** * Applies the attribute operations. */ apply() { return this.onApply(this.operations); } } //# sourceMappingURL=AttributeEditor.js.map