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