UNPKG

@wordpress/core-data

Version:
63 lines (58 loc) 2.3 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = logEntityDeprecation; var _deprecated = _interopRequireDefault(require("@wordpress/deprecated")); var _entities = require("../entities"); /** * WordPress dependencies */ /** * Internal dependencies */ let loggedAlready = false; /** * Logs a deprecation warning for an entity, if it's deprecated. * * @param kind The kind of the entity. * @param name The name of the entity. * @param functionName The name of the function that was called with a deprecated entity. * @param options The options for the deprecation warning. * @param options.alternativeFunctionName The name of the alternative function that should be used instead. * @param options.isShorthandSelector Whether the function is a shorthand selector. */ function logEntityDeprecation(kind, name, functionName, { alternativeFunctionName, isShorthandSelector = false } = {}) { const deprecation = _entities.deprecatedEntities[kind]?.[name]; if (!deprecation) { return; } if (!loggedAlready) { const { alternative } = deprecation; const message = isShorthandSelector ? `'${functionName}'` : `The '${kind}', '${name}' entity (used via '${functionName}')`; let alternativeMessage = `the '${alternative.kind}', '${alternative.name}' entity`; if (alternativeFunctionName) { alternativeMessage += ` via the '${alternativeFunctionName}' function`; } (0, _deprecated.default)(message, { ...deprecation, alternative: alternativeMessage }); } // Only log an entity deprecation once per call stack, // else there's spurious logging when selections or actions call through to other selectors or actions. // Note: this won't prevent the deprecation warning being logged if a selector or action makes an async call // to another selector or action, but this is probably the best we can do. loggedAlready = true; // At the end of the call stack, reset the flag. setTimeout(() => { loggedAlready = false; }, 0); } //# sourceMappingURL=log-entity-deprecation.js.map