Aucune description

keyboard-shortcuts.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. this["wp"] = this["wp"] || {}; this["wp"]["keyboardShortcuts"] =
  2. /******/ (function(modules) { // webpackBootstrap
  3. /******/ // The module cache
  4. /******/ var installedModules = {};
  5. /******/
  6. /******/ // The require function
  7. /******/ function __webpack_require__(moduleId) {
  8. /******/
  9. /******/ // Check if module is in cache
  10. /******/ if(installedModules[moduleId]) {
  11. /******/ return installedModules[moduleId].exports;
  12. /******/ }
  13. /******/ // Create a new module (and put it into the cache)
  14. /******/ var module = installedModules[moduleId] = {
  15. /******/ i: moduleId,
  16. /******/ l: false,
  17. /******/ exports: {}
  18. /******/ };
  19. /******/
  20. /******/ // Execute the module function
  21. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  22. /******/
  23. /******/ // Flag the module as loaded
  24. /******/ module.l = true;
  25. /******/
  26. /******/ // Return the exports of the module
  27. /******/ return module.exports;
  28. /******/ }
  29. /******/
  30. /******/
  31. /******/ // expose the modules object (__webpack_modules__)
  32. /******/ __webpack_require__.m = modules;
  33. /******/
  34. /******/ // expose the module cache
  35. /******/ __webpack_require__.c = installedModules;
  36. /******/
  37. /******/ // define getter function for harmony exports
  38. /******/ __webpack_require__.d = function(exports, name, getter) {
  39. /******/ if(!__webpack_require__.o(exports, name)) {
  40. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  41. /******/ }
  42. /******/ };
  43. /******/
  44. /******/ // define __esModule on exports
  45. /******/ __webpack_require__.r = function(exports) {
  46. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  47. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  48. /******/ }
  49. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  50. /******/ };
  51. /******/
  52. /******/ // create a fake namespace object
  53. /******/ // mode & 1: value is a module id, require it
  54. /******/ // mode & 2: merge all properties of value into the ns
  55. /******/ // mode & 4: return value when already ns object
  56. /******/ // mode & 8|1: behave like require
  57. /******/ __webpack_require__.t = function(value, mode) {
  58. /******/ if(mode & 1) value = __webpack_require__(value);
  59. /******/ if(mode & 8) return value;
  60. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  61. /******/ var ns = Object.create(null);
  62. /******/ __webpack_require__.r(ns);
  63. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  64. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  65. /******/ return ns;
  66. /******/ };
  67. /******/
  68. /******/ // getDefaultExport function for compatibility with non-harmony modules
  69. /******/ __webpack_require__.n = function(module) {
  70. /******/ var getter = module && module.__esModule ?
  71. /******/ function getDefault() { return module['default']; } :
  72. /******/ function getModuleExports() { return module; };
  73. /******/ __webpack_require__.d(getter, 'a', getter);
  74. /******/ return getter;
  75. /******/ };
  76. /******/
  77. /******/ // Object.prototype.hasOwnProperty.call
  78. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  79. /******/
  80. /******/ // __webpack_public_path__
  81. /******/ __webpack_require__.p = "";
  82. /******/
  83. /******/
  84. /******/ // Load entry module and return exports
  85. /******/ return __webpack_require__(__webpack_require__.s = "cFS5");
  86. /******/ })
  87. /************************************************************************/
  88. /******/ ({
  89. /***/ "1ZqX":
  90. /***/ (function(module, exports) {
  91. (function() { module.exports = window["wp"]["data"]; }());
  92. /***/ }),
  93. /***/ "K9lf":
  94. /***/ (function(module, exports) {
  95. (function() { module.exports = window["wp"]["compose"]; }());
  96. /***/ }),
  97. /***/ "RxS6":
  98. /***/ (function(module, exports) {
  99. (function() { module.exports = window["wp"]["keycodes"]; }());
  100. /***/ }),
  101. /***/ "YLtl":
  102. /***/ (function(module, exports) {
  103. (function() { module.exports = window["lodash"]; }());
  104. /***/ }),
  105. /***/ "cFS5":
  106. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  107. "use strict";
  108. // ESM COMPAT FLAG
  109. __webpack_require__.r(__webpack_exports__);
  110. // EXPORTS
  111. __webpack_require__.d(__webpack_exports__, "store", function() { return /* reexport */ store; });
  112. __webpack_require__.d(__webpack_exports__, "useShortcut", function() { return /* reexport */ use_shortcut; });
  113. __webpack_require__.d(__webpack_exports__, "__unstableUseShortcutEventMatch", function() { return /* reexport */ useShortcutEventMatch; });
  114. // NAMESPACE OBJECT: ./node_modules/@wordpress/keyboard-shortcuts/build-module/store/actions.js
  115. var actions_namespaceObject = {};
  116. __webpack_require__.r(actions_namespaceObject);
  117. __webpack_require__.d(actions_namespaceObject, "registerShortcut", function() { return registerShortcut; });
  118. __webpack_require__.d(actions_namespaceObject, "unregisterShortcut", function() { return unregisterShortcut; });
  119. // NAMESPACE OBJECT: ./node_modules/@wordpress/keyboard-shortcuts/build-module/store/selectors.js
  120. var selectors_namespaceObject = {};
  121. __webpack_require__.r(selectors_namespaceObject);
  122. __webpack_require__.d(selectors_namespaceObject, "getShortcutKeyCombination", function() { return getShortcutKeyCombination; });
  123. __webpack_require__.d(selectors_namespaceObject, "getShortcutRepresentation", function() { return getShortcutRepresentation; });
  124. __webpack_require__.d(selectors_namespaceObject, "getShortcutDescription", function() { return getShortcutDescription; });
  125. __webpack_require__.d(selectors_namespaceObject, "getShortcutAliases", function() { return getShortcutAliases; });
  126. __webpack_require__.d(selectors_namespaceObject, "getAllShortcutKeyCombinations", function() { return selectors_getAllShortcutKeyCombinations; });
  127. __webpack_require__.d(selectors_namespaceObject, "getAllShortcutRawKeyCombinations", function() { return getAllShortcutRawKeyCombinations; });
  128. __webpack_require__.d(selectors_namespaceObject, "getCategoryShortcuts", function() { return getCategoryShortcuts; });
  129. // EXTERNAL MODULE: external ["wp","data"]
  130. var external_wp_data_ = __webpack_require__("1ZqX");
  131. // EXTERNAL MODULE: external "lodash"
  132. var external_lodash_ = __webpack_require__("YLtl");
  133. // CONCATENATED MODULE: ./node_modules/@wordpress/keyboard-shortcuts/build-module/store/reducer.js
  134. /**
  135. * External dependencies
  136. */
  137. /**
  138. * Reducer returning the registered shortcuts
  139. *
  140. * @param {Object} state Current state.
  141. * @param {Object} action Dispatched action.
  142. *
  143. * @return {Object} Updated state.
  144. */
  145. function reducer(state = {}, action) {
  146. switch (action.type) {
  147. case 'REGISTER_SHORTCUT':
  148. return { ...state,
  149. [action.name]: {
  150. category: action.category,
  151. keyCombination: action.keyCombination,
  152. aliases: action.aliases,
  153. description: action.description
  154. }
  155. };
  156. case 'UNREGISTER_SHORTCUT':
  157. return Object(external_lodash_["omit"])(state, action.name);
  158. }
  159. return state;
  160. }
  161. /* harmony default export */ var store_reducer = (reducer);
  162. // CONCATENATED MODULE: ./node_modules/@wordpress/keyboard-shortcuts/build-module/store/actions.js
  163. /** @typedef {import('@wordpress/keycodes').WPKeycodeModifier} WPKeycodeModifier */
  164. /**
  165. * Keyboard key combination.
  166. *
  167. * @typedef {Object} WPShortcutKeyCombination
  168. *
  169. * @property {string} character Character.
  170. * @property {WPKeycodeModifier|undefined} modifier Modifier.
  171. */
  172. /**
  173. * Configuration of a registered keyboard shortcut.
  174. *
  175. * @typedef {Object} WPShortcutConfig
  176. *
  177. * @property {string} name Shortcut name.
  178. * @property {string} category Shortcut category.
  179. * @property {string} description Shortcut description.
  180. * @property {WPShortcutKeyCombination} keyCombination Shortcut key combination.
  181. * @property {WPShortcutKeyCombination[]} [aliases] Shortcut aliases.
  182. */
  183. /**
  184. * Returns an action object used to register a new keyboard shortcut.
  185. *
  186. * @param {WPShortcutConfig} config Shortcut config.
  187. *
  188. * @return {Object} action.
  189. */
  190. function registerShortcut({
  191. name,
  192. category,
  193. description,
  194. keyCombination,
  195. aliases
  196. }) {
  197. return {
  198. type: 'REGISTER_SHORTCUT',
  199. name,
  200. category,
  201. keyCombination,
  202. aliases,
  203. description
  204. };
  205. }
  206. /**
  207. * Returns an action object used to unregister a keyboard shortcut.
  208. *
  209. * @param {string} name Shortcut name.
  210. *
  211. * @return {Object} action.
  212. */
  213. function unregisterShortcut(name) {
  214. return {
  215. type: 'UNREGISTER_SHORTCUT',
  216. name
  217. };
  218. }
  219. // EXTERNAL MODULE: ./node_modules/rememo/es/rememo.js
  220. var rememo = __webpack_require__("pPDe");
  221. // EXTERNAL MODULE: external ["wp","keycodes"]
  222. var external_wp_keycodes_ = __webpack_require__("RxS6");
  223. // CONCATENATED MODULE: ./node_modules/@wordpress/keyboard-shortcuts/build-module/store/selectors.js
  224. /**
  225. * External dependencies
  226. */
  227. /**
  228. * WordPress dependencies
  229. */
  230. /** @typedef {import('./actions').WPShortcutKeyCombination} WPShortcutKeyCombination */
  231. /** @typedef {import('@wordpress/keycodes').WPKeycodeHandlerByModifier} WPKeycodeHandlerByModifier */
  232. /**
  233. * Shared reference to an empty array for cases where it is important to avoid
  234. * returning a new array reference on every invocation.
  235. *
  236. * @type {Array<any>}
  237. */
  238. const EMPTY_ARRAY = [];
  239. /**
  240. * Shortcut formatting methods.
  241. *
  242. * @property {WPKeycodeHandlerByModifier} display Display formatting.
  243. * @property {WPKeycodeHandlerByModifier} rawShortcut Raw shortcut formatting.
  244. * @property {WPKeycodeHandlerByModifier} ariaLabel ARIA label formatting.
  245. */
  246. const FORMATTING_METHODS = {
  247. display: external_wp_keycodes_["displayShortcut"],
  248. raw: external_wp_keycodes_["rawShortcut"],
  249. ariaLabel: external_wp_keycodes_["shortcutAriaLabel"]
  250. };
  251. /**
  252. * Returns a string representing the key combination.
  253. *
  254. * @param {?WPShortcutKeyCombination} shortcut Key combination.
  255. * @param {keyof FORMATTING_METHODS} representation Type of representation
  256. * (display, raw, ariaLabel).
  257. *
  258. * @return {string?} Shortcut representation.
  259. */
  260. function getKeyCombinationRepresentation(shortcut, representation) {
  261. if (!shortcut) {
  262. return null;
  263. }
  264. return shortcut.modifier ? FORMATTING_METHODS[representation][shortcut.modifier](shortcut.character) : shortcut.character;
  265. }
  266. /**
  267. * Returns the main key combination for a given shortcut name.
  268. *
  269. * @param {Object} state Global state.
  270. * @param {string} name Shortcut name.
  271. *
  272. * @return {WPShortcutKeyCombination?} Key combination.
  273. */
  274. function getShortcutKeyCombination(state, name) {
  275. return state[name] ? state[name].keyCombination : null;
  276. }
  277. /**
  278. * Returns a string representing the main key combination for a given shortcut name.
  279. *
  280. * @param {Object} state Global state.
  281. * @param {string} name Shortcut name.
  282. * @param {keyof FORMATTING_METHODS} representation Type of representation
  283. * (display, raw, ariaLabel).
  284. *
  285. * @return {string?} Shortcut representation.
  286. */
  287. function getShortcutRepresentation(state, name, representation = 'display') {
  288. const shortcut = getShortcutKeyCombination(state, name);
  289. return getKeyCombinationRepresentation(shortcut, representation);
  290. }
  291. /**
  292. * Returns the shortcut description given its name.
  293. *
  294. * @param {Object} state Global state.
  295. * @param {string} name Shortcut name.
  296. *
  297. * @return {string?} Shortcut description.
  298. */
  299. function getShortcutDescription(state, name) {
  300. return state[name] ? state[name].description : null;
  301. }
  302. /**
  303. * Returns the aliases for a given shortcut name.
  304. *
  305. * @param {Object} state Global state.
  306. * @param {string} name Shortcut name.
  307. *
  308. * @return {WPShortcutKeyCombination[]} Key combinations.
  309. */
  310. function getShortcutAliases(state, name) {
  311. return state[name] && state[name].aliases ? state[name].aliases : EMPTY_ARRAY;
  312. }
  313. const selectors_getAllShortcutKeyCombinations = Object(rememo["a" /* default */])((state, name) => {
  314. return Object(external_lodash_["compact"])([getShortcutKeyCombination(state, name), ...getShortcutAliases(state, name)]);
  315. }, (state, name) => [state[name]]);
  316. /**
  317. * Returns the raw representation of all the keyboard combinations of a given shortcut name.
  318. *
  319. * @param {Object} state Global state.
  320. * @param {string} name Shortcut name.
  321. *
  322. * @return {string[]} Shortcuts.
  323. */
  324. const getAllShortcutRawKeyCombinations = Object(rememo["a" /* default */])((state, name) => {
  325. return selectors_getAllShortcutKeyCombinations(state, name).map(combination => getKeyCombinationRepresentation(combination, 'raw'));
  326. }, (state, name) => [state[name]]);
  327. /**
  328. * Returns the shortcut names list for a given category name.
  329. *
  330. * @param {Object} state Global state.
  331. * @param {string} name Category name.
  332. *
  333. * @return {string[]} Shortcut names.
  334. */
  335. const getCategoryShortcuts = Object(rememo["a" /* default */])((state, categoryName) => {
  336. return Object.entries(state).filter(([, shortcut]) => shortcut.category === categoryName).map(([name]) => name);
  337. }, state => [state]);
  338. // CONCATENATED MODULE: ./node_modules/@wordpress/keyboard-shortcuts/build-module/store/index.js
  339. /**
  340. * WordPress dependencies
  341. */
  342. /**
  343. * Internal dependencies
  344. */
  345. const STORE_NAME = 'core/keyboard-shortcuts';
  346. /**
  347. * Store definition for the keyboard shortcuts namespace.
  348. *
  349. * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
  350. *
  351. * @type {Object}
  352. */
  353. const store = Object(external_wp_data_["createReduxStore"])(STORE_NAME, {
  354. reducer: store_reducer,
  355. actions: actions_namespaceObject,
  356. selectors: selectors_namespaceObject
  357. });
  358. Object(external_wp_data_["register"])(store);
  359. // EXTERNAL MODULE: external ["wp","compose"]
  360. var external_wp_compose_ = __webpack_require__("K9lf");
  361. // CONCATENATED MODULE: ./node_modules/@wordpress/keyboard-shortcuts/build-module/hooks/use-shortcut.js
  362. /**
  363. * WordPress dependencies
  364. */
  365. /**
  366. * Internal dependencies
  367. */
  368. /**
  369. * Attach a keyboard shortcut handler.
  370. *
  371. * @param {string} name Shortcut name.
  372. * @param {Function} callback Shortcut callback.
  373. * @param {Object} options Shortcut options.
  374. */
  375. function useShortcut(name, callback, options) {
  376. const shortcuts = Object(external_wp_data_["useSelect"])(select => {
  377. return select(store).getAllShortcutRawKeyCombinations(name);
  378. }, [name]);
  379. Object(external_wp_compose_["useKeyboardShortcut"])(shortcuts, callback, options);
  380. }
  381. /* harmony default export */ var use_shortcut = (useShortcut);
  382. // CONCATENATED MODULE: ./node_modules/@wordpress/keyboard-shortcuts/build-module/hooks/use-shortcut-event-match.js
  383. /**
  384. * WordPress dependencies
  385. */
  386. /**
  387. * Internal dependencies
  388. */
  389. /**
  390. * Returns a function to check if a keyboard event matches a shortcut name.
  391. *
  392. * @return {Function} A function to to check if a keyboard event matches a
  393. * predefined shortcut combination.
  394. */
  395. function useShortcutEventMatch() {
  396. const {
  397. getAllShortcutKeyCombinations
  398. } = Object(external_wp_data_["useSelect"])(store);
  399. /**
  400. * A function to check if a keyboard event matches a predefined shortcut
  401. * combination.
  402. *
  403. * @param {string} name Shortcut name.
  404. * @param {KeyboardEvent} event Event to check.
  405. *
  406. * @return {boolean} True if the event matches any shortcuts, false if not.
  407. */
  408. function isMatch(name, event) {
  409. return getAllShortcutKeyCombinations(name).some(({
  410. modifier,
  411. character
  412. }) => {
  413. return external_wp_keycodes_["isKeyboardEvent"][modifier](event, character);
  414. });
  415. }
  416. return isMatch;
  417. }
  418. // CONCATENATED MODULE: ./node_modules/@wordpress/keyboard-shortcuts/build-module/index.js
  419. /***/ }),
  420. /***/ "pPDe":
  421. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  422. "use strict";
  423. var LEAF_KEY, hasWeakMap;
  424. /**
  425. * Arbitrary value used as key for referencing cache object in WeakMap tree.
  426. *
  427. * @type {Object}
  428. */
  429. LEAF_KEY = {};
  430. /**
  431. * Whether environment supports WeakMap.
  432. *
  433. * @type {boolean}
  434. */
  435. hasWeakMap = typeof WeakMap !== 'undefined';
  436. /**
  437. * Returns the first argument as the sole entry in an array.
  438. *
  439. * @param {*} value Value to return.
  440. *
  441. * @return {Array} Value returned as entry in array.
  442. */
  443. function arrayOf( value ) {
  444. return [ value ];
  445. }
  446. /**
  447. * Returns true if the value passed is object-like, or false otherwise. A value
  448. * is object-like if it can support property assignment, e.g. object or array.
  449. *
  450. * @param {*} value Value to test.
  451. *
  452. * @return {boolean} Whether value is object-like.
  453. */
  454. function isObjectLike( value ) {
  455. return !! value && 'object' === typeof value;
  456. }
  457. /**
  458. * Creates and returns a new cache object.
  459. *
  460. * @return {Object} Cache object.
  461. */
  462. function createCache() {
  463. var cache = {
  464. clear: function() {
  465. cache.head = null;
  466. },
  467. };
  468. return cache;
  469. }
  470. /**
  471. * Returns true if entries within the two arrays are strictly equal by
  472. * reference from a starting index.
  473. *
  474. * @param {Array} a First array.
  475. * @param {Array} b Second array.
  476. * @param {number} fromIndex Index from which to start comparison.
  477. *
  478. * @return {boolean} Whether arrays are shallowly equal.
  479. */
  480. function isShallowEqual( a, b, fromIndex ) {
  481. var i;
  482. if ( a.length !== b.length ) {
  483. return false;
  484. }
  485. for ( i = fromIndex; i < a.length; i++ ) {
  486. if ( a[ i ] !== b[ i ] ) {
  487. return false;
  488. }
  489. }
  490. return true;
  491. }
  492. /**
  493. * Returns a memoized selector function. The getDependants function argument is
  494. * called before the memoized selector and is expected to return an immutable
  495. * reference or array of references on which the selector depends for computing
  496. * its own return value. The memoize cache is preserved only as long as those
  497. * dependant references remain the same. If getDependants returns a different
  498. * reference(s), the cache is cleared and the selector value regenerated.
  499. *
  500. * @param {Function} selector Selector function.
  501. * @param {Function} getDependants Dependant getter returning an immutable
  502. * reference or array of reference used in
  503. * cache bust consideration.
  504. *
  505. * @return {Function} Memoized selector.
  506. */
  507. /* harmony default export */ __webpack_exports__["a"] = (function( selector, getDependants ) {
  508. var rootCache, getCache;
  509. // Use object source as dependant if getter not provided
  510. if ( ! getDependants ) {
  511. getDependants = arrayOf;
  512. }
  513. /**
  514. * Returns the root cache. If WeakMap is supported, this is assigned to the
  515. * root WeakMap cache set, otherwise it is a shared instance of the default
  516. * cache object.
  517. *
  518. * @return {(WeakMap|Object)} Root cache object.
  519. */
  520. function getRootCache() {
  521. return rootCache;
  522. }
  523. /**
  524. * Returns the cache for a given dependants array. When possible, a WeakMap
  525. * will be used to create a unique cache for each set of dependants. This
  526. * is feasible due to the nature of WeakMap in allowing garbage collection
  527. * to occur on entries where the key object is no longer referenced. Since
  528. * WeakMap requires the key to be an object, this is only possible when the
  529. * dependant is object-like. The root cache is created as a hierarchy where
  530. * each top-level key is the first entry in a dependants set, the value a
  531. * WeakMap where each key is the next dependant, and so on. This continues
  532. * so long as the dependants are object-like. If no dependants are object-
  533. * like, then the cache is shared across all invocations.
  534. *
  535. * @see isObjectLike
  536. *
  537. * @param {Array} dependants Selector dependants.
  538. *
  539. * @return {Object} Cache object.
  540. */
  541. function getWeakMapCache( dependants ) {
  542. var caches = rootCache,
  543. isUniqueByDependants = true,
  544. i, dependant, map, cache;
  545. for ( i = 0; i < dependants.length; i++ ) {
  546. dependant = dependants[ i ];
  547. // Can only compose WeakMap from object-like key.
  548. if ( ! isObjectLike( dependant ) ) {
  549. isUniqueByDependants = false;
  550. break;
  551. }
  552. // Does current segment of cache already have a WeakMap?
  553. if ( caches.has( dependant ) ) {
  554. // Traverse into nested WeakMap.
  555. caches = caches.get( dependant );
  556. } else {
  557. // Create, set, and traverse into a new one.
  558. map = new WeakMap();
  559. caches.set( dependant, map );
  560. caches = map;
  561. }
  562. }
  563. // We use an arbitrary (but consistent) object as key for the last item
  564. // in the WeakMap to serve as our running cache.
  565. if ( ! caches.has( LEAF_KEY ) ) {
  566. cache = createCache();
  567. cache.isUniqueByDependants = isUniqueByDependants;
  568. caches.set( LEAF_KEY, cache );
  569. }
  570. return caches.get( LEAF_KEY );
  571. }
  572. // Assign cache handler by availability of WeakMap
  573. getCache = hasWeakMap ? getWeakMapCache : getRootCache;
  574. /**
  575. * Resets root memoization cache.
  576. */
  577. function clear() {
  578. rootCache = hasWeakMap ? new WeakMap() : createCache();
  579. }
  580. // eslint-disable-next-line jsdoc/check-param-names
  581. /**
  582. * The augmented selector call, considering first whether dependants have
  583. * changed before passing it to underlying memoize function.
  584. *
  585. * @param {Object} source Source object for derivation.
  586. * @param {...*} extraArgs Additional arguments to pass to selector.
  587. *
  588. * @return {*} Selector result.
  589. */
  590. function callSelector( /* source, ...extraArgs */ ) {
  591. var len = arguments.length,
  592. cache, node, i, args, dependants;
  593. // Create copy of arguments (avoid leaking deoptimization).
  594. args = new Array( len );
  595. for ( i = 0; i < len; i++ ) {
  596. args[ i ] = arguments[ i ];
  597. }
  598. dependants = getDependants.apply( null, args );
  599. cache = getCache( dependants );
  600. // If not guaranteed uniqueness by dependants (primitive type or lack
  601. // of WeakMap support), shallow compare against last dependants and, if
  602. // references have changed, destroy cache to recalculate result.
  603. if ( ! cache.isUniqueByDependants ) {
  604. if ( cache.lastDependants && ! isShallowEqual( dependants, cache.lastDependants, 0 ) ) {
  605. cache.clear();
  606. }
  607. cache.lastDependants = dependants;
  608. }
  609. node = cache.head;
  610. while ( node ) {
  611. // Check whether node arguments match arguments
  612. if ( ! isShallowEqual( node.args, args, 1 ) ) {
  613. node = node.next;
  614. continue;
  615. }
  616. // At this point we can assume we've found a match
  617. // Surface matched node to head if not already
  618. if ( node !== cache.head ) {
  619. // Adjust siblings to point to each other.
  620. node.prev.next = node.next;
  621. if ( node.next ) {
  622. node.next.prev = node.prev;
  623. }
  624. node.next = cache.head;
  625. node.prev = null;
  626. cache.head.prev = node;
  627. cache.head = node;
  628. }
  629. // Return immediately
  630. return node.val;
  631. }
  632. // No cached value found. Continue to insertion phase:
  633. node = {
  634. // Generate the result from original function
  635. val: selector.apply( null, args ),
  636. };
  637. // Avoid including the source object in the cache.
  638. args[ 0 ] = null;
  639. node.args = args;
  640. // Don't need to check whether node is already head, since it would
  641. // have been returned above already if it was
  642. // Shift existing head down list
  643. if ( cache.head ) {
  644. cache.head.prev = node;
  645. node.next = cache.head;
  646. }
  647. cache.head = node;
  648. return node.val;
  649. }
  650. callSelector.getDependants = getDependants;
  651. callSelector.clear = clear;
  652. clear();
  653. return callSelector;
  654. });
  655. /***/ })
  656. /******/ });