wp/wp-includes/js/dist/private-apis.js
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    40 // EXPORTS
    40 // EXPORTS
    41 __webpack_require__.d(__webpack_exports__, {
    41 __webpack_require__.d(__webpack_exports__, {
    42   __dangerousOptInToUnstableAPIsOnlyForCoreModules: () => (/* reexport */ __dangerousOptInToUnstableAPIsOnlyForCoreModules)
    42   __dangerousOptInToUnstableAPIsOnlyForCoreModules: () => (/* reexport */ __dangerousOptInToUnstableAPIsOnlyForCoreModules)
    43 });
    43 });
    44 
    44 
    45 ;// CONCATENATED MODULE: ./node_modules/@wordpress/private-apis/build-module/implementation.js
    45 ;// ./node_modules/@wordpress/private-apis/build-module/implementation.js
    46 /**
    46 /**
    47  * wordpress/private-apis – the utilities to enable private cross-package
    47  * wordpress/private-apis – the utilities to enable private cross-package
    48  * exports of private APIs.
    48  * exports of private APIs.
    49  *
    49  *
    50  * This "implementation.js" file is needed for the sake of the unit tests. It
    50  * This "implementation.ts" file is needed for the sake of the unit tests. It
    51  * exports more than the public API of the package to aid in testing.
    51  * exports more than the public API of the package to aid in testing.
    52  */
    52  */
    53 
    53 
    54 /**
    54 /**
    55  * The list of core modules allowed to opt-in to the private APIs.
    55  * The list of core modules allowed to opt-in to the private APIs.
    56  */
    56  */
    57 const CORE_MODULES_USING_PRIVATE_APIS = ['@wordpress/block-directory', '@wordpress/block-editor', '@wordpress/block-library', '@wordpress/blocks', '@wordpress/commands', '@wordpress/components', '@wordpress/core-commands', '@wordpress/core-data', '@wordpress/customize-widgets', '@wordpress/data', '@wordpress/edit-post', '@wordpress/edit-site', '@wordpress/edit-widgets', '@wordpress/editor', '@wordpress/format-library', '@wordpress/interface', '@wordpress/patterns', '@wordpress/preferences', '@wordpress/reusable-blocks', '@wordpress/router', '@wordpress/dataviews'];
    57 const CORE_MODULES_USING_PRIVATE_APIS = ['@wordpress/block-directory', '@wordpress/block-editor', '@wordpress/block-library', '@wordpress/blocks', '@wordpress/commands', '@wordpress/components', '@wordpress/core-commands', '@wordpress/core-data', '@wordpress/customize-widgets', '@wordpress/data', '@wordpress/edit-post', '@wordpress/edit-site', '@wordpress/edit-widgets', '@wordpress/editor', '@wordpress/format-library', '@wordpress/patterns', '@wordpress/preferences', '@wordpress/reusable-blocks', '@wordpress/router', '@wordpress/dataviews', '@wordpress/fields', '@wordpress/media-utils', '@wordpress/upload-media'];
    58 
    58 
    59 /**
    59 /**
    60  * A list of core modules that already opted-in to
    60  * A list of core modules that already opted-in to
    61  * the privateApis package.
    61  * the privateApis package.
    62  *
       
    63  * @type {string[]}
       
    64  */
    62  */
    65 const registeredPrivateApis = [];
    63 const registeredPrivateApis = [];
    66 
    64 
    67 /*
    65 /*
    68  * Warning for theme and plugin developers.
    66  * Warning for theme and plugin developers.
    78  * WITHOUT NOTICE. THIS CHANGE WILL BREAK EXISTING THIRD-PARTY CODE. SUCH A
    76  * WITHOUT NOTICE. THIS CHANGE WILL BREAK EXISTING THIRD-PARTY CODE. SUCH A
    79  * CHANGE MAY OCCUR IN EITHER A MAJOR OR MINOR RELEASE.
    77  * CHANGE MAY OCCUR IN EITHER A MAJOR OR MINOR RELEASE.
    80  */
    78  */
    81 const requiredConsent = 'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.';
    79 const requiredConsent = 'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.';
    82 
    80 
    83 /** @type {boolean} */
    81 // The safety measure is meant for WordPress core where IS_WORDPRESS_CORE is set to true.
    84 let allowReRegistration;
    82 const allowReRegistration =  true ? false : 0;
    85 // The safety measure is meant for WordPress core where IS_WORDPRESS_CORE
       
    86 // is set to true.
       
    87 // For the general use-case, the re-registration should be allowed by default
       
    88 // Let's default to true, then. Try/catch will fall back to "true" even if the
       
    89 // environment variable is not explicitly defined.
       
    90 try {
       
    91   allowReRegistration =  true ? false : 0;
       
    92 } catch (error) {
       
    93   allowReRegistration = true;
       
    94 }
       
    95 
    83 
    96 /**
    84 /**
    97  * Called by a @wordpress package wishing to opt-in to accessing or exposing
    85  * Called by a @wordpress package wishing to opt-in to accessing or exposing
    98  * private private APIs.
    86  * private private APIs.
    99  *
    87  *
   100  * @param {string} consent    The consent string.
    88  * @param consent    The consent string.
   101  * @param {string} moduleName The name of the module that is opting in.
    89  * @param moduleName The name of the module that is opting in.
   102  * @return {{lock: typeof lock, unlock: typeof unlock}} An object containing the lock and unlock functions.
    90  * @return An object containing the lock and unlock functions.
   103  */
    91  */
   104 const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (consent, moduleName) => {
    92 const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (consent, moduleName) => {
   105   if (!CORE_MODULES_USING_PRIVATE_APIS.includes(moduleName)) {
    93   if (!CORE_MODULES_USING_PRIVATE_APIS.includes(moduleName)) {
   106     throw new Error(`You tried to opt-in to unstable APIs as module "${moduleName}". ` + 'This feature is only for JavaScript modules shipped with WordPress core. ' + 'Please do not use it in plugins and themes as the unstable APIs will be removed ' + 'without a warning. If you ignore this error and depend on unstable features, ' + 'your product will inevitably break on one of the next WordPress releases.');
    94     throw new Error(`You tried to opt-in to unstable APIs as module "${moduleName}". ` + 'This feature is only for JavaScript modules shipped with WordPress core. ' + 'Please do not use it in plugins and themes as the unstable APIs will be removed ' + 'without a warning. If you ignore this error and depend on unstable features, ' + 'your product will inevitably break on one of the next WordPress releases.');
   107   }
    95   }
   140  *
   128  *
   141  * unlock( object );
   129  * unlock( object );
   142  * // { a: 1 }
   130  * // { a: 1 }
   143  * ```
   131  * ```
   144  *
   132  *
   145  * @param {any} object      The object to bind the private data to.
   133  * @param object      The object to bind the private data to.
   146  * @param {any} privateData The private data to bind to the object.
   134  * @param privateData The private data to bind to the object.
   147  */
   135  */
   148 function lock(object, privateData) {
   136 function lock(object, privateData) {
   149   if (!object) {
   137   if (!object) {
   150     throw new Error('Cannot lock an undefined object.');
   138     throw new Error('Cannot lock an undefined object.');
   151   }
   139   }
   152   if (!(__private in object)) {
   140   const _object = object;
   153     object[__private] = {};
   141   if (!(__private in _object)) {
   154   }
   142     _object[__private] = {};
   155   lockedData.set(object[__private], privateData);
   143   }
       
   144   lockedData.set(_object[__private], privateData);
   156 }
   145 }
   157 
   146 
   158 /**
   147 /**
   159  * Unlocks the private data bound to an object.
   148  * Unlocks the private data bound to an object.
   160  *
   149  *
   173  *
   162  *
   174  * unlock( object );
   163  * unlock( object );
   175  * // { a: 1 }
   164  * // { a: 1 }
   176  * ```
   165  * ```
   177  *
   166  *
   178  * @param {any} object The object to unlock the private data from.
   167  * @param object The object to unlock the private data from.
   179  * @return {any} The private data bound to the object.
   168  * @return The private data bound to the object.
   180  */
   169  */
   181 function unlock(object) {
   170 function unlock(object) {
   182   if (!object) {
   171   if (!object) {
   183     throw new Error('Cannot unlock an undefined object.');
   172     throw new Error('Cannot unlock an undefined object.');
   184   }
   173   }
   185   if (!(__private in object)) {
   174   const _object = object;
       
   175   if (!(__private in _object)) {
   186     throw new Error('Cannot unlock an object that was not locked before. ');
   176     throw new Error('Cannot unlock an object that was not locked before. ');
   187   }
   177   }
   188   return lockedData.get(object[__private]);
   178   return lockedData.get(_object[__private]);
   189 }
   179 }
   190 const lockedData = new WeakMap();
   180 const lockedData = new WeakMap();
   191 
   181 
   192 /**
   182 /**
   193  * Used by lock() and unlock() to uniquely identify the private data
   183  * Used by lock() and unlock() to uniquely identify the private data
   199 
   189 
   200 /**
   190 /**
   201  * Private function to allow the unit tests to allow
   191  * Private function to allow the unit tests to allow
   202  * a mock module to access the private APIs.
   192  * a mock module to access the private APIs.
   203  *
   193  *
   204  * @param {string} name The name of the module.
   194  * @param name The name of the module.
   205  */
   195  */
   206 function allowCoreModule(name) {
   196 function allowCoreModule(name) {
   207   CORE_MODULES_USING_PRIVATE_APIS.push(name);
   197   CORE_MODULES_USING_PRIVATE_APIS.push(name);
   208 }
   198 }
   209 
   199 
   224   while (registeredPrivateApis.length) {
   214   while (registeredPrivateApis.length) {
   225     registeredPrivateApis.pop();
   215     registeredPrivateApis.pop();
   226   }
   216   }
   227 }
   217 }
   228 
   218 
   229 ;// CONCATENATED MODULE: ./node_modules/@wordpress/private-apis/build-module/index.js
   219 ;// ./node_modules/@wordpress/private-apis/build-module/index.js
   230 
   220 
   231 
   221 
   232 (window.wp = window.wp || {}).privateApis = __webpack_exports__;
   222 (window.wp = window.wp || {}).privateApis = __webpack_exports__;
   233 /******/ })()
   223 /******/ })()
   234 ;
   224 ;