reactotron-core-client
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -1,871 +0,1 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
function _defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
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; | ||
} | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
function _toConsumableArray(arr) { | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); | ||
} | ||
function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) { | ||
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
} | ||
function _iterableToArray(iter) { | ||
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); | ||
} | ||
function _nonIterableSpread() { | ||
throw new TypeError("Invalid attempt to spread non-iterable instance"); | ||
} | ||
var isCreateSocketValid = function isCreateSocketValid(createSocket) { | ||
return typeof createSocket !== "undefined" && createSocket !== null; | ||
}; | ||
var isHostValid = function isHostValid(host) { | ||
return typeof host === "string" && host && host !== ""; | ||
}; | ||
var isPortValid = function isPortValid(port) { | ||
return typeof port === "number" && port >= 1 && port <= 65535; | ||
}; | ||
var onCommandValid = function onCommandValid(fn) { | ||
return typeof fn === "function"; | ||
}; | ||
/** | ||
* Ensures the options are sane to run this baby. Throw if not. These | ||
* are basically sanity checks. | ||
*/ | ||
var validate = function validate(options) { | ||
var createSocket = options.createSocket, | ||
host = options.host, | ||
port = options.port, | ||
onCommand = options.onCommand; | ||
if (!isCreateSocketValid(createSocket)) { | ||
throw new Error("invalid createSocket function"); | ||
} | ||
if (!isHostValid(host)) { | ||
throw new Error("invalid host"); | ||
} | ||
if (!isPortValid(port)) { | ||
throw new Error("invalid port"); | ||
} | ||
if (!onCommandValid(onCommand)) { | ||
throw new Error("invalid onCommand handler"); | ||
} | ||
}; | ||
/** | ||
* Provides 4 features for logging. log & debug are the same. | ||
*/ | ||
var logger = (function () { | ||
return function (reactotron) { | ||
return { | ||
features: { | ||
log: function log() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var content = args && args.length === 1 ? args[0] : args; | ||
reactotron.send("log", { | ||
level: "debug", | ||
message: content | ||
}, false); | ||
}, | ||
logImportant: function logImportant() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
var content = args && args.length === 1 ? args[0] : args; | ||
reactotron.send("log", { | ||
level: "debug", | ||
message: content | ||
}, true); | ||
}, | ||
debug: function debug(message) { | ||
var important = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
return reactotron.send("log", { | ||
level: "debug", | ||
message: message | ||
}, !!important); | ||
}, | ||
warn: function warn(message) { | ||
return reactotron.send("log", { | ||
level: "warn", | ||
message: message | ||
}, true); | ||
}, | ||
error: function error(message, stack) { | ||
return reactotron.send("log", { | ||
level: "error", | ||
message: message, | ||
stack: stack | ||
}, true); | ||
} | ||
} | ||
}; | ||
}; | ||
}); | ||
/** | ||
* Provides an image. | ||
*/ | ||
var image = (function () { | ||
return function (reactotron) { | ||
return { | ||
features: { | ||
// expanded just to show the specs | ||
image: function image(_ref) { | ||
var uri = _ref.uri, | ||
preview = _ref.preview, | ||
filename = _ref.filename, | ||
width = _ref.width, | ||
height = _ref.height, | ||
caption = _ref.caption; | ||
return reactotron.send("image", { | ||
uri: uri, | ||
preview: preview, | ||
filename: filename, | ||
width: width, | ||
height: height, | ||
caption: caption | ||
}); | ||
} | ||
} | ||
}; | ||
}; | ||
}); | ||
/** | ||
* Runs small high-unscientific benchmarks for you. | ||
*/ | ||
var benchmark = (function () { | ||
return function (reactotron) { | ||
var startTimer = reactotron.startTimer; | ||
var benchmark = function benchmark(title) { | ||
var steps = []; | ||
var elapsed = startTimer(); | ||
var step = function step(stepTitle) { | ||
var previousTime = steps.length === 0 ? 0 : steps[steps.length - 1].time; | ||
var nextTime = elapsed(); | ||
steps.push({ | ||
title: stepTitle, | ||
time: nextTime, | ||
delta: nextTime - previousTime | ||
}); | ||
}; | ||
steps.push({ | ||
title: title, | ||
time: 0, | ||
delta: 0 | ||
}); | ||
var stop = function stop(stopTitle) { | ||
step(stopTitle); | ||
reactotron.send("benchmark.report", { | ||
title: title, | ||
steps: steps | ||
}); | ||
}; | ||
return { | ||
step: step, | ||
stop: stop, | ||
last: stop | ||
}; | ||
}; | ||
return { | ||
features: { | ||
benchmark: benchmark | ||
} | ||
}; | ||
}; | ||
}); | ||
/** | ||
* Provides helper functions for send state responses. | ||
*/ | ||
var stateResponses = (function () { | ||
return function (reactotron) { | ||
return { | ||
features: { | ||
stateActionComplete: function stateActionComplete(name, action) { | ||
var important = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
return reactotron.send("state.action.complete", { | ||
name: name, | ||
action: action | ||
}, !!important); | ||
}, | ||
stateValuesResponse: function stateValuesResponse(path, value) { | ||
var valid = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; | ||
return reactotron.send("state.values.response", { | ||
path: path, | ||
value: value, | ||
valid: valid | ||
}); | ||
}, | ||
stateKeysResponse: function stateKeysResponse(path, keys) { | ||
var valid = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; | ||
return reactotron.send("state.keys.response", { | ||
path: path, | ||
keys: keys, | ||
valid: valid | ||
}); | ||
}, | ||
stateValuesChange: function stateValuesChange(changes) { | ||
return reactotron.send("state.values.change", { | ||
changes: changes | ||
}); | ||
}, | ||
// sends the state backup over to the server | ||
stateBackupResponse: function stateBackupResponse(state) { | ||
return reactotron.send("state.backup.response", { | ||
state: state | ||
}); | ||
} | ||
} | ||
}; | ||
}; | ||
}); | ||
/** | ||
* Sends API request/response information. | ||
*/ | ||
var apiResponse = (function () { | ||
return function (reactotron) { | ||
return { | ||
features: { | ||
apiResponse: function apiResponse(request, response, duration) { | ||
var ok = response && response.status && typeof response.status === "number" && response.status >= 200 && response.status <= 299; | ||
var important = !ok; | ||
reactotron.send("api.response", { | ||
request: request, | ||
response: response, | ||
duration: duration | ||
}, important); | ||
} | ||
} | ||
}; | ||
}; | ||
}); | ||
/** | ||
* Clears the reactotron server. | ||
*/ | ||
var clear = (function () { | ||
return function (reactotron) { | ||
return { | ||
features: { | ||
clear: function clear() { | ||
return reactotron.send("clear"); | ||
} | ||
} | ||
}; | ||
}; | ||
}); | ||
// JSON.stringify() doesn't support circular dependencies or keeping | ||
// falsy values. This does. | ||
// | ||
// Mostly adapted from https://github.com/isaacs/json-stringify-safe | ||
// replacement tokens | ||
var UNDEFINED = "~~~ undefined ~~~"; | ||
var NULL = "~~~ null ~~~"; | ||
var FALSE = "~~~ false ~~~"; | ||
var ZERO = "~~~ zero ~~~"; | ||
var EMPTY_STRING = "~~~ empty string ~~~"; | ||
var CIRCULAR = "~~~ Circular Reference ~~~"; | ||
var ANONYMOUS = "~~~ anonymous function ~~~"; | ||
var INFINITY = "~~~ Infinity ~~~"; | ||
var NEGATIVE_INFINITY = "~~~ -Infinity ~~~"; // const NAN = '~~~ NaN ~~~' | ||
/** | ||
* Attempts to give a name to a function. | ||
* | ||
* @param {Function} fn - The function to name. | ||
*/ | ||
function getFunctionName(fn) { | ||
var n = fn.name; | ||
if (n === null || n === undefined || n === "") { | ||
return ANONYMOUS; | ||
} else { | ||
return "~~~ ".concat(n, "() ~~~"); | ||
} | ||
} | ||
/** | ||
* Serializes an object to JSON. | ||
* | ||
* @param {any} source - The victim. | ||
*/ | ||
function serialize(source) { | ||
var proxyHack = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
var stack = []; | ||
/** | ||
* Replace this object node with something potentially custom. | ||
* | ||
* @param {*} key - The key currently visited. | ||
* @param {*} value - The value to replace. | ||
*/ | ||
function serializer(replacer) { | ||
return function (key, value) { | ||
// slam dunks | ||
if (value === true) return true; // weird stuff | ||
// if (Object.is(value, NaN)) return NAN // OK, apparently this is hard... leaving out for now | ||
if (value === Infinity) return INFINITY; | ||
if (value === -Infinity) return NEGATIVE_INFINITY; | ||
if (value === 0) return ZERO; // classic javascript | ||
if (value === undefined) return UNDEFINED; | ||
if (value === null) return NULL; | ||
if (value === false) return FALSE; // head shakers | ||
if (value === -0) return ZERO; // eslint-disable-line | ||
if (value === "") return EMPTY_STRING; | ||
if (proxyHack && _typeof(value) === "object" && value.nativeEvent) { | ||
return value.nativeEvent; | ||
} // known types that have easy resolving | ||
switch (_typeof(value)) { | ||
case "string": | ||
return value; | ||
case "number": | ||
return value; | ||
case "function": | ||
return getFunctionName(value); | ||
} // Tough things to crack | ||
// If we have an iterator but are not an array (because arrays are easily seralizeable already)... | ||
if (value[Symbol.iterator] && !Array.isArray(value)) { | ||
// Convert to an array! | ||
return _toConsumableArray(value); | ||
} | ||
if (stack.length > 0) { | ||
// check for prior existance | ||
var thisPos = stack.indexOf(this); | ||
~thisPos ? stack.splice(thisPos + 1) : stack.push(this); | ||
if (~stack.indexOf(value)) value = CIRCULAR; | ||
} else { | ||
stack.push(value); | ||
} | ||
return replacer == null ? value : replacer.call(this, key, value); | ||
}; | ||
} | ||
return JSON.stringify(source, serializer(null)); | ||
} | ||
var defaultPerformanceNow = function defaultPerformanceNow(started) { | ||
return Date.now(); | ||
}; // try to find the browser-based performance timer | ||
var nativePerformance = typeof window !== "undefined" && window && (window.performance || window.msPerformance || window.webkitPerformance); // the function we're trying to assign | ||
var performanceNow = defaultPerformanceNow; // accepts an already started time and returns the number of milliseconds | ||
var delta = function delta(started) { | ||
return performanceNow() - started; | ||
}; | ||
if (global.nativePerformanceNow) { | ||
// react native 47 | ||
performanceNow = global.nativePerformanceNow; | ||
} else if (nativePerformance) { | ||
// browsers + safely check for react native < 47 | ||
performanceNow = function performanceNow() { | ||
return nativePerformance.now && nativePerformance.now(); | ||
}; | ||
} | ||
/** | ||
* Starts a lame, low-res timer. Returns a function which when invoked, | ||
* gives you the number of milliseconds since passing. ish. | ||
*/ | ||
var start = function start() { | ||
// record the start time | ||
var started = performanceNow(); | ||
return function () { | ||
return delta(started); | ||
}; | ||
}; | ||
var corePlugins = [image(), logger(), benchmark(), stateResponses(), apiResponse(), clear()]; | ||
var DEFAULT_OPTIONS = { | ||
createSocket: null, | ||
host: "localhost", | ||
port: 9090, | ||
name: "reactotron-core-client", | ||
secure: false, | ||
plugins: corePlugins, | ||
safeRecursion: true, | ||
onCommand: function onCommand(command) { | ||
return null; | ||
}, | ||
onConnect: function onConnect() { | ||
return null; | ||
}, | ||
onDisconnect: function onDisconnect() { | ||
return null; | ||
} // these are not for you. | ||
}; | ||
var reservedFeatures = ["options", "connected", "socket", "plugins", "configure", "connect", "send", "use", "startTimer"]; | ||
var isReservedFeature = function isReservedFeature(value) { | ||
return reservedFeatures.some(function (res) { | ||
return res === value; | ||
}); | ||
}; | ||
function emptyPromise() { | ||
return Promise.resolve(""); | ||
} | ||
var Reactotron = | ||
/*#__PURE__*/ | ||
function () { | ||
function Reactotron() { | ||
var _this = this; | ||
_classCallCheck(this, Reactotron); | ||
_defineProperty(this, "options", Object.assign({}, DEFAULT_OPTIONS)); | ||
_defineProperty(this, "connected", false); | ||
_defineProperty(this, "socket", null); | ||
_defineProperty(this, "plugins", []); | ||
_defineProperty(this, "sendQueue", []); | ||
_defineProperty(this, "isReady", false); | ||
_defineProperty(this, "lastMessageDate", new Date()); | ||
_defineProperty(this, "customCommands", []); | ||
_defineProperty(this, "customCommandLatestId", 1); | ||
_defineProperty(this, "startTimer", function () { | ||
return start(); | ||
}); | ||
_defineProperty(this, "send", function (type) { | ||
var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var important = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
// jet if we don't have a socket | ||
if (!_this.socket) { | ||
return; | ||
} // set the timing info | ||
var date = new Date(); | ||
var deltaTime = date.getTime() - _this.lastMessageDate.getTime(); // glitches in the matrix | ||
if (deltaTime < 0) { | ||
deltaTime = 0; | ||
} | ||
_this.lastMessageDate = date; | ||
var fullMessage = { | ||
type: type, | ||
payload: payload, | ||
important: !!important, | ||
date: date.toISOString(), | ||
deltaTime: deltaTime | ||
}; | ||
var serializedMessage = serialize(fullMessage, _this.options.proxyHack); | ||
if (_this.isReady) { | ||
// send this command | ||
_this.socket.send(serializedMessage); | ||
} else { | ||
// queue it up until we can connect | ||
_this.sendQueue.push(serializedMessage); | ||
} | ||
}); | ||
} | ||
_createClass(Reactotron, [{ | ||
key: "configure", | ||
/** | ||
* Set the configuration options. | ||
*/ | ||
value: function configure() { | ||
var _this2 = this; | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
// options get merged & validated before getting set | ||
var newOptions = Object.assign({}, this.options, options); | ||
validate(newOptions); | ||
this.options = newOptions; // if we have plugins, let's add them here | ||
if (Array.isArray(this.options.plugins)) { | ||
this.options.plugins.forEach(function (p) { | ||
return _this2.use(p); | ||
}); | ||
} | ||
return this; | ||
} | ||
}, { | ||
key: "close", | ||
value: function close() { | ||
this.connected = false; | ||
this.socket && this.socket.close && this.socket.close(); | ||
} | ||
/** | ||
* Connect to the Reactotron server. | ||
*/ | ||
}, { | ||
key: "connect", | ||
value: function connect() { | ||
var _this3 = this; | ||
this.connected = true; | ||
var _this$options = this.options, | ||
createSocket = _this$options.createSocket, | ||
secure = _this$options.secure, | ||
host = _this$options.host, | ||
environment = _this$options.environment, | ||
port = _this$options.port, | ||
name = _this$options.name, | ||
_this$options$client = _this$options.client, | ||
client = _this$options$client === void 0 ? {} : _this$options$client, | ||
getClientId = _this$options.getClientId; | ||
var _this$options2 = this.options, | ||
onCommand = _this$options2.onCommand, | ||
onConnect = _this$options2.onConnect, | ||
onDisconnect = _this$options2.onDisconnect; // establish a connection to the server | ||
var protocol = secure ? "wss" : "ws"; | ||
var socket = createSocket("".concat(protocol, "://").concat(host, ":").concat(port)); // fires when we talk to the server | ||
var onOpen = function onOpen() { | ||
// fire our optional onConnect handler | ||
onConnect && onConnect(); // trigger our plugins onConnect | ||
_this3.plugins.forEach(function (p) { | ||
return p.onConnect && p.onConnect(); | ||
}); | ||
var getClientIdPromise = getClientId ? getClientId : emptyPromise; | ||
getClientIdPromise().then(function (clientId) { | ||
_this3.isReady = true; // introduce ourselves | ||
_this3.send("client.intro", _objectSpread({ | ||
environment: environment | ||
}, client, { | ||
name: name, | ||
clientId: clientId, | ||
"reactotronCoreClientVersion": "2.2.2" | ||
})); // flush the send queue | ||
while (_this3.sendQueue.length > 0) { | ||
var h = _this3.sendQueue[0]; | ||
_this3.sendQueue = _this3.sendQueue.slice(1); | ||
_this3.socket.send(h); | ||
} | ||
}); | ||
}; // fires when we disconnect | ||
var onClose = function onClose() { | ||
_this3.isReady = false; // trigger our disconnect handler | ||
onDisconnect && onDisconnect(); // as well as the plugin's onDisconnect | ||
_this3.plugins.forEach(function (p) { | ||
return p.onDisconnect && p.onDisconnect(); | ||
}); | ||
}; // fires when we receive a command, just forward it off | ||
var onMessage = function onMessage(data) { | ||
var command = JSON.parse(data); // trigger our own command handler | ||
onCommand && onCommand(command); // trigger our plugins onCommand | ||
_this3.plugins.forEach(function (p) { | ||
return p.onCommand && p.onCommand(command); | ||
}); // trigger our registered custom commands | ||
if (command.type === "custom") { | ||
_this3.customCommands.filter(function (cc) { | ||
return cc.command === command.payload; | ||
}).forEach(function (cc) { | ||
return cc.handler(); | ||
}); | ||
} else if (command.type === "setClientId") { | ||
_this3.options.setClientId && _this3.options.setClientId(command.payload); | ||
} | ||
}; // this is ws style from require('ws') on node js | ||
if (socket.on) { | ||
socket.on("open", onOpen); | ||
socket.on("close", onClose); | ||
socket.on("message", onMessage); | ||
} else { | ||
// this is a browser | ||
socket.onopen = onOpen; | ||
socket.onclose = onClose; | ||
socket.onmessage = function (evt) { | ||
return onMessage(evt.data); | ||
}; | ||
} // assign the socket to the instance | ||
this.socket = socket; | ||
return this; | ||
} | ||
/** | ||
* Sends a command to the server | ||
*/ | ||
}, { | ||
key: "display", | ||
/** | ||
* Sends a custom command to the server to displays nicely. | ||
*/ | ||
value: function display() { | ||
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var name = config.name, | ||
value = config.value, | ||
preview = config.preview, | ||
img = config.image, | ||
_config$important = config.important, | ||
important = _config$important === void 0 ? false : _config$important; | ||
var payload = { | ||
name: name, | ||
value: value || null, | ||
preview: preview || null, | ||
image: img || null | ||
}; | ||
this.send("display", payload, important); | ||
} | ||
/** | ||
* Client libraries can hijack this to report errors. | ||
*/ | ||
}, { | ||
key: "reportError", | ||
value: function reportError(error) { | ||
this.error(error); | ||
} | ||
/** | ||
* Adds a plugin to the system | ||
*/ | ||
}, { | ||
key: "use", | ||
value: function use(pluginCreator) { | ||
var _this4 = this; | ||
// we're supposed to be given a function | ||
if (typeof pluginCreator !== "function") { | ||
throw new Error("plugins must be a function"); | ||
} // execute it immediately passing the send function | ||
var plugin = pluginCreator.bind(this)(this); // ensure we get an Object-like creature back | ||
if (_typeof(plugin) !== "object") { | ||
throw new Error("plugins must return an object"); | ||
} // do we have features to mixin? | ||
if (plugin.features) { | ||
// validate | ||
if (_typeof(plugin.features) !== "object") { | ||
throw new Error("features must be an object"); | ||
} // here's how we're going to inject these in | ||
var inject = function inject(key) { | ||
// grab the function | ||
var featureFunction = plugin.features[key]; // only functions may pass | ||
if (typeof featureFunction !== "function") { | ||
throw new Error("feature ".concat(key, " is not a function")); | ||
} // ditch reserved names | ||
if (isReservedFeature(key)) { | ||
throw new Error("feature ".concat(key, " is a reserved name")); | ||
} // ok, let's glue it up... and lose all respect from elite JS champions. | ||
_this4[key] = featureFunction; | ||
}; // let's inject | ||
Object.keys(plugin.features).forEach(function (key) { | ||
return inject(key); | ||
}); | ||
} // add it to the list | ||
this.plugins.push(plugin); // call the plugins onPlugin | ||
plugin.onPlugin && typeof plugin.onPlugin === "function" && plugin.onPlugin.bind(this)(this); // chain-friendly | ||
return this; | ||
} | ||
}, { | ||
key: "onCustomCommand", | ||
value: function onCustomCommand(command, handler) { | ||
var _this5 = this; | ||
// Create this command handlers object | ||
var customHandler = { | ||
id: this.customCommandLatestId, | ||
command: command, | ||
handler: handler // Increment our id counter | ||
}; | ||
this.customCommandLatestId += 1; // Add it to our array | ||
this.customCommands.push(customHandler); | ||
this.send("customCommand.register", { | ||
id: customHandler.id, | ||
command: customHandler.command | ||
}); | ||
return function () { | ||
_this5.customCommands = _this5.customCommands.filter(function (cc) { | ||
return cc.id !== customHandler.id; | ||
}); | ||
_this5.send("customCommand.unregister", { | ||
id: customHandler.id, | ||
command: customHandler.command | ||
}); | ||
}; | ||
} | ||
}]); | ||
return Reactotron; | ||
}(); // convenience factory function | ||
function createClient(options) { | ||
var client = new Reactotron(); | ||
client.configure(options); | ||
return client; | ||
} | ||
exports.corePlugins = corePlugins; | ||
exports.Reactotron = Reactotron; | ||
exports.createClient = createClient; | ||
'use strict';Object.defineProperty(exports,"__esModule",{value:!0});function _typeof(a){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _objectSpread(a){for(var b=1;b<arguments.length;b++){var c=null==arguments[b]?{}:arguments[b],d=Object.keys(c);"function"==typeof Object.getOwnPropertySymbols&&(d=d.concat(Object.getOwnPropertySymbols(c).filter(function(a){return Object.getOwnPropertyDescriptor(c,a).enumerable}))),d.forEach(function(b){_defineProperty(a,b,c[b])})}return a}function _toConsumableArray(a){return _arrayWithoutHoles(a)||_iterableToArray(a)||_nonIterableSpread()}function _arrayWithoutHoles(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b<a.length;b++)c[b]=a[b];return c}}function _iterableToArray(a){if(Symbol.iterator in Object(a)||"[object Arguments]"===Object.prototype.toString.call(a))return Array.from(a)}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}var isCreateSocketValid=function(a){return"undefined"!=typeof a&&null!==a},isHostValid=function(a){return"string"==typeof a&&a&&""!==a},isPortValid=function(a){return"number"==typeof a&&1<=a&&65535>=a},onCommandValid=function(a){return"function"==typeof a},validate=function(a){var b=a.createSocket,c=a.host,d=a.port,e=a.onCommand;if(!isCreateSocketValid(b))throw new Error("invalid createSocket function");if(!isHostValid(c))throw new Error("invalid host");if(!isPortValid(d))throw new Error("invalid port");if(!onCommandValid(e))throw new Error("invalid onCommand handler")},logger=function(){return function(a){return{features:{log:function(){for(var b=arguments.length,c=Array(b),d=0;d<b;d++)c[d]=arguments[d];var e=c&&1===c.length?c[0]:c;a.send("log",{level:"debug",message:e},!1)},logImportant:function(){for(var b=arguments.length,c=Array(b),d=0;d<b;d++)c[d]=arguments[d];var e=c&&1===c.length?c[0]:c;a.send("log",{level:"debug",message:e},!0)},debug:function(b){var c=!!(1<arguments.length&&arguments[1]!==void 0)&&arguments[1];return a.send("log",{level:"debug",message:b},!!c)},warn:function(b){return a.send("log",{level:"warn",message:b},!0)},error:function(b,c){return a.send("log",{level:"error",message:b,stack:c},!0)}}}}},image=function(){return function(a){return{features:{image:function(b){var c=b.uri,d=b.preview,e=b.filename,f=b.width,g=b.height,h=b.caption;return a.send("image",{uri:c,preview:d,filename:e,width:f,height:g,caption:h})}}}}},benchmark=function(){return function(a){var b=a.startTimer;return{features:{benchmark:function(c){var d=[],e=b(),f=function(a){var b=0===d.length?0:d[d.length-1].time,c=e();d.push({title:a,time:c,delta:c-b})};d.push({title:c,time:0,delta:0});var g=function(b){f(b),a.send("benchmark.report",{title:c,steps:d})};return{step:f,stop:g,last:g}}}}}},stateResponses=function(){return function(a){return{features:{stateActionComplete:function(b,c){var d=!!(2<arguments.length&&arguments[2]!==void 0)&&arguments[2];return a.send("state.action.complete",{name:b,action:c},!!d)},stateValuesResponse:function(b,c){var d=!(2<arguments.length&&arguments[2]!==void 0)||arguments[2];return a.send("state.values.response",{path:b,value:c,valid:d})},stateKeysResponse:function(b,c){var d=!(2<arguments.length&&arguments[2]!==void 0)||arguments[2];return a.send("state.keys.response",{path:b,keys:c,valid:d})},stateValuesChange:function(b){return a.send("state.values.change",{changes:b})},stateBackupResponse:function(b){return a.send("state.backup.response",{state:b})}}}}},apiResponse=function(){return function(a){return{features:{apiResponse:function(b,c,d){var e=c&&c.status&&"number"==typeof c.status&&200<=c.status&&299>=c.status;a.send("api.response",{request:b,response:c,duration:d},!e)}}}}},clear=function(){return function(a){return{features:{clear:function(){return a.send("clear")}}}}},UNDEFINED="~~~ undefined ~~~",NULL="~~~ null ~~~",FALSE="~~~ false ~~~",ZERO="~~~ zero ~~~",EMPTY_STRING="~~~ empty string ~~~",CIRCULAR="~~~ Circular Reference ~~~",ANONYMOUS="~~~ anonymous function ~~~",INFINITY="~~~ Infinity ~~~",NEGATIVE_INFINITY="~~~ -Infinity ~~~";function getFunctionName(a){var b=a.name;return null===b||void 0===b||""===b?ANONYMOUS:"~~~ ".concat(b,"() ~~~")}function serialize(a){var b=!!(1<arguments.length&&arguments[1]!==void 0)&&arguments[1],c=[];return JSON.stringify(a,function(a){return function(d,e){if(!0===e)return!0;if(e===1/0)return INFINITY;if(e===-Infinity)return NEGATIVE_INFINITY;if(0===e)return ZERO;if(void 0===e)return UNDEFINED;if(null===e)return NULL;if(!1===e)return FALSE;if(-0===e)return ZERO;if(""===e)return EMPTY_STRING;if(b&&"object"===_typeof(e)&&e.nativeEvent)return e.nativeEvent;switch(_typeof(e)){case"string":return e;case"number":return e;case"function":return getFunctionName(e);}if(e[Symbol.iterator]&&!Array.isArray(e))return _toConsumableArray(e);if(0<c.length){var f=c.indexOf(this);~f?c.splice(f+1):c.push(this),~c.indexOf(e)&&(e=CIRCULAR)}else c.push(e);return null==a?e:a.call(this,d,e)}}(null))}var defaultPerformanceNow=function(){return Date.now()},nativePerformance="undefined"!=typeof window&&window&&(window.performance||window.msPerformance||window.webkitPerformance),performanceNow=defaultPerformanceNow,delta=function(a){return performanceNow()-a};global.nativePerformanceNow?performanceNow=global.nativePerformanceNow:nativePerformance&&(performanceNow=function(){return nativePerformance.now&&nativePerformance.now()});var start=function(){var a=performanceNow();return function(){return delta(a)}},corePlugins=[image(),logger(),benchmark(),stateResponses(),apiResponse(),clear()],DEFAULT_OPTIONS={createSocket:null,host:"localhost",port:9090,name:"reactotron-core-client",secure:!1,plugins:corePlugins,safeRecursion:!0,onCommand:function(){return null},onConnect:function(){return null},onDisconnect:function(){return null}},reservedFeatures=["options","connected","socket","plugins","configure","connect","send","use","startTimer"],isReservedFeature=function(a){return reservedFeatures.some(function(b){return b===a})};function emptyPromise(){return Promise.resolve("")}var Reactotron=function(){function a(){var b=this;_classCallCheck(this,a),_defineProperty(this,"options",Object.assign({},DEFAULT_OPTIONS)),_defineProperty(this,"connected",!1),_defineProperty(this,"socket",null),_defineProperty(this,"plugins",[]),_defineProperty(this,"sendQueue",[]),_defineProperty(this,"isReady",!1),_defineProperty(this,"lastMessageDate",new Date),_defineProperty(this,"customCommands",[]),_defineProperty(this,"customCommandLatestId",1),_defineProperty(this,"startTimer",function(){return start()}),_defineProperty(this,"send",function(a){var c=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},d=!!(2<arguments.length&&void 0!==arguments[2])&&arguments[2];if(b.socket){var e=new Date,f=e.getTime()-b.lastMessageDate.getTime();0>f&&(f=0),b.lastMessageDate=e;var g={type:a,payload:c,important:!!d,date:e.toISOString(),deltaTime:f},h=serialize(g,b.options.proxyHack);b.isReady?b.socket.send(h):b.sendQueue.push(h)}})}return _createClass(a,[{key:"configure",value:function(){var a=this,b=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},c=Object.assign({},this.options,b);return validate(c),this.options=c,Array.isArray(this.options.plugins)&&this.options.plugins.forEach(function(b){return a.use(b)}),this}},{key:"close",value:function(){this.connected=!1,this.socket&&this.socket.close&&this.socket.close()}},{key:"connect",value:function(){var a=this;this.connected=!0;var b=this.options,c=b.createSocket,d=b.secure,e=b.host,f=b.environment,g=b.port,i=b.name,j=b.client,k=void 0===j?{}:j,l=b.getClientId,m=this.options,n=m.onCommand,o=m.onConnect,p=m.onDisconnect,q=d?"wss":"ws",r=c("".concat(q,"://").concat(e,":").concat(g)),s=function(){o&&o(),a.plugins.forEach(function(a){return a.onConnect&&a.onConnect()});var b=l?l:emptyPromise;b().then(function(b){for(a.isReady=!0,a.send("client.intro",_objectSpread({environment:f},k,{name:i,clientId:b,reactotronCoreClientVersion:"2.3.0"}));0<a.sendQueue.length;){var c=a.sendQueue[0];a.sendQueue=a.sendQueue.slice(1),a.socket.send(c)}})},t=function(){a.isReady=!1,p&&p(),a.plugins.forEach(function(a){return a.onDisconnect&&a.onDisconnect()})},u=function(b){var c=JSON.parse(b);n&&n(c),a.plugins.forEach(function(a){return a.onCommand&&a.onCommand(c)}),"custom"===c.type?a.customCommands.filter(function(a){return a.command===c.payload}).forEach(function(a){return a.handler()}):"setClientId"===c.type&&a.options.setClientId&&a.options.setClientId(c.payload)};return r.on?(r.on("open",s),r.on("close",t),r.on("message",u)):(r.onopen=s,r.onclose=t,r.onmessage=function(a){return u(a.data)}),this.socket=r,this}},{key:"display",value:function(){var a=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},b=a.name,c=a.value,d=a.preview,e=a.image,f=a.important;this.send("display",{name:b,value:c||null,preview:d||null,image:e||null},void 0!==f&&f)}},{key:"reportError",value:function(a){this.error(a)}},{key:"use",value:function(a){var b=this;if("function"!=typeof a)throw new Error("plugins must be a function");var c=a.bind(this)(this);if("object"!==_typeof(c))throw new Error("plugins must return an object");if(c.features){if("object"!==_typeof(c.features))throw new Error("features must be an object");var d=function(a){var d=c.features[a];if("function"!=typeof d)throw new Error("feature ".concat(a," is not a function"));if(isReservedFeature(a))throw new Error("feature ".concat(a," is a reserved name"));b[a]=d};Object.keys(c.features).forEach(function(a){return d(a)})}return this.plugins.push(c),c.onPlugin&&"function"==typeof c.onPlugin&&c.onPlugin.bind(this)(this),this}},{key:"onCustomCommand",value:function(a,b){var c=this,d={id:this.customCommandLatestId,command:a,handler:b};return this.customCommandLatestId+=1,this.customCommands.push(d),this.send("customCommand.register",{id:d.id,command:d.command}),function(){c.customCommands=c.customCommands.filter(function(a){return a.id!==d.id}),c.send("customCommand.unregister",{id:d.id,command:d.command})}}}]),a}();function createClient(a){var b=new Reactotron;return b.configure(a),b}exports.corePlugins=corePlugins,exports.Reactotron=Reactotron,exports.createClient=createClient; |
{ | ||
"name": "reactotron-core-client", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Grants Reactotron clients the ability to talk to a Reactotron server.", | ||
@@ -17,5 +17,7 @@ "files": [ | ||
"build": "npm-run-all clean tsc compile", | ||
"build:dev": "npm-run-all clean tsc compile:dev", | ||
"clean": "trash dist", | ||
"lint": "eslint index.js src test --ext .ts,.tsx", | ||
"compile": "NODE_ENV=production rollup -c", | ||
"compile:dev": "NODE_ENV=development rollup -c", | ||
"tsc": "tsc", | ||
@@ -56,2 +58,4 @@ "ci:test": "yarn test", | ||
"rollup-plugin-babel": "^4.3.2", | ||
"rollup-plugin-babel-minify": "^7.0.0", | ||
"rollup-plugin-filesize": "^6.0.1", | ||
"rollup-plugin-node-resolve": "^4.0.0", | ||
@@ -58,0 +62,0 @@ "rollup-plugin-replace": "^2.1.0", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
35482
35
330