Node-Red configuration
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

vuex.esm-browser.prod.js 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /*!
  2. * vuex v4.1.0
  3. * (c) 2022 Evan You
  4. * @license MIT
  5. */
  6. import { inject, effectScope, reactive, watch, computed } from 'vue';
  7. import { setupDevtoolsPlugin } from '@vue/devtools-api';
  8. var storeKey = 'store';
  9. function useStore (key) {
  10. if ( key === void 0 ) key = null;
  11. return inject(key !== null ? key : storeKey)
  12. }
  13. /**
  14. * Get the first item that pass the test
  15. * by second argument function
  16. *
  17. * @param {Array} list
  18. * @param {Function} f
  19. * @return {*}
  20. */
  21. function find (list, f) {
  22. return list.filter(f)[0]
  23. }
  24. /**
  25. * Deep copy the given object considering circular structure.
  26. * This function caches all nested objects and its copies.
  27. * If it detects circular structure, use cached copy to avoid infinite loop.
  28. *
  29. * @param {*} obj
  30. * @param {Array<Object>} cache
  31. * @return {*}
  32. */
  33. function deepCopy (obj, cache) {
  34. if ( cache === void 0 ) cache = [];
  35. // just return if obj is immutable value
  36. if (obj === null || typeof obj !== 'object') {
  37. return obj
  38. }
  39. // if obj is hit, it is in circular structure
  40. var hit = find(cache, function (c) { return c.original === obj; });
  41. if (hit) {
  42. return hit.copy
  43. }
  44. var copy = Array.isArray(obj) ? [] : {};
  45. // put the copy into cache at first
  46. // because we want to refer it in recursive deepCopy
  47. cache.push({
  48. original: obj,
  49. copy: copy
  50. });
  51. Object.keys(obj).forEach(function (key) {
  52. copy[key] = deepCopy(obj[key], cache);
  53. });
  54. return copy
  55. }
  56. /**
  57. * forEach for object
  58. */
  59. function forEachValue (obj, fn) {
  60. Object.keys(obj).forEach(function (key) { return fn(obj[key], key); });
  61. }
  62. function isObject (obj) {
  63. return obj !== null && typeof obj === 'object'
  64. }
  65. function isPromise (val) {
  66. return val && typeof val.then === 'function'
  67. }
  68. function partial (fn, arg) {
  69. return function () {
  70. return fn(arg)
  71. }
  72. }
  73. function genericSubscribe (fn, subs, options) {
  74. if (subs.indexOf(fn) < 0) {
  75. options && options.prepend
  76. ? subs.unshift(fn)
  77. : subs.push(fn);
  78. }
  79. return function () {
  80. var i = subs.indexOf(fn);
  81. if (i > -1) {
  82. subs.splice(i, 1);
  83. }
  84. }
  85. }
  86. function resetStore (store, hot) {
  87. store._actions = Object.create(null);
  88. store._mutations = Object.create(null);
  89. store._wrappedGetters = Object.create(null);
  90. store._modulesNamespaceMap = Object.create(null);
  91. var state = store.state;
  92. // init all modules
  93. installModule(store, state, [], store._modules.root, true);
  94. // reset state
  95. resetStoreState(store, state, hot);
  96. }
  97. function resetStoreState (store, state, hot) {
  98. var oldState = store._state;
  99. var oldScope = store._scope;
  100. // bind store public getters
  101. store.getters = {};
  102. // reset local getters cache
  103. store._makeLocalGettersCache = Object.create(null);
  104. var wrappedGetters = store._wrappedGetters;
  105. var computedObj = {};
  106. var computedCache = {};
  107. // create a new effect scope and create computed object inside it to avoid
  108. // getters (computed) getting destroyed on component unmount.
  109. var scope = effectScope(true);
  110. scope.run(function () {
  111. forEachValue(wrappedGetters, function (fn, key) {
  112. // use computed to leverage its lazy-caching mechanism
  113. // direct inline function use will lead to closure preserving oldState.
  114. // using partial to return function with only arguments preserved in closure environment.
  115. computedObj[key] = partial(fn, store);
  116. computedCache[key] = computed(function () { return computedObj[key](); });
  117. Object.defineProperty(store.getters, key, {
  118. get: function () { return computedCache[key].value; },
  119. enumerable: true // for local getters
  120. });
  121. });
  122. });
  123. store._state = reactive({
  124. data: state
  125. });
  126. // register the newly created effect scope to the store so that we can
  127. // dispose the effects when this method runs again in the future.
  128. store._scope = scope;
  129. // enable strict mode for new state
  130. if (store.strict) {
  131. enableStrictMode(store);
  132. }
  133. if (oldState) {
  134. if (hot) {
  135. // dispatch changes in all subscribed watchers
  136. // to force getter re-evaluation for hot reloading.
  137. store._withCommit(function () {
  138. oldState.data = null;
  139. });
  140. }
  141. }
  142. // dispose previously registered effect scope if there is one.
  143. if (oldScope) {
  144. oldScope.stop();
  145. }
  146. }
  147. function installModule (store, rootState, path, module, hot) {
  148. var isRoot = !path.length;
  149. var namespace = store._modules.getNamespace(path);
  150. // register in namespace map
  151. if (module.namespaced) {
  152. if (store._modulesNamespaceMap[namespace] && false) {
  153. console.error(("[vuex] duplicate namespace " + namespace + " for the namespaced module " + (path.join('/'))));
  154. }
  155. store._modulesNamespaceMap[namespace] = module;
  156. }
  157. // set state
  158. if (!isRoot && !hot) {
  159. var parentState = getNestedState(rootState, path.slice(0, -1));
  160. var moduleName = path[path.length - 1];
  161. store._withCommit(function () {
  162. parentState[moduleName] = module.state;
  163. });
  164. }
  165. var local = module.context = makeLocalContext(store, namespace, path);
  166. module.forEachMutation(function (mutation, key) {
  167. var namespacedType = namespace + key;
  168. registerMutation(store, namespacedType, mutation, local);
  169. });
  170. module.forEachAction(function (action, key) {
  171. var type = action.root ? key : namespace + key;
  172. var handler = action.handler || action;
  173. registerAction(store, type, handler, local);
  174. });
  175. module.forEachGetter(function (getter, key) {
  176. var namespacedType = namespace + key;
  177. registerGetter(store, namespacedType, getter, local);
  178. });
  179. module.forEachChild(function (child, key) {
  180. installModule(store, rootState, path.concat(key), child, hot);
  181. });
  182. }
  183. /**
  184. * make localized dispatch, commit, getters and state
  185. * if there is no namespace, just use root ones
  186. */
  187. function makeLocalContext (store, namespace, path) {
  188. var noNamespace = namespace === '';
  189. var local = {
  190. dispatch: noNamespace ? store.dispatch : function (_type, _payload, _options) {
  191. var args = unifyObjectStyle(_type, _payload, _options);
  192. var payload = args.payload;
  193. var options = args.options;
  194. var type = args.type;
  195. if (!options || !options.root) {
  196. type = namespace + type;
  197. }
  198. return store.dispatch(type, payload)
  199. },
  200. commit: noNamespace ? store.commit : function (_type, _payload, _options) {
  201. var args = unifyObjectStyle(_type, _payload, _options);
  202. var payload = args.payload;
  203. var options = args.options;
  204. var type = args.type;
  205. if (!options || !options.root) {
  206. type = namespace + type;
  207. }
  208. store.commit(type, payload, options);
  209. }
  210. };
  211. // getters and state object must be gotten lazily
  212. // because they will be changed by state update
  213. Object.defineProperties(local, {
  214. getters: {
  215. get: noNamespace
  216. ? function () { return store.getters; }
  217. : function () { return makeLocalGetters(store, namespace); }
  218. },
  219. state: {
  220. get: function () { return getNestedState(store.state, path); }
  221. }
  222. });
  223. return local
  224. }
  225. function makeLocalGetters (store, namespace) {
  226. if (!store._makeLocalGettersCache[namespace]) {
  227. var gettersProxy = {};
  228. var splitPos = namespace.length;
  229. Object.keys(store.getters).forEach(function (type) {
  230. // skip if the target getter is not match this namespace
  231. if (type.slice(0, splitPos) !== namespace) { return }
  232. // extract local getter type
  233. var localType = type.slice(splitPos);
  234. // Add a port to the getters proxy.
  235. // Define as getter property because
  236. // we do not want to evaluate the getters in this time.
  237. Object.defineProperty(gettersProxy, localType, {
  238. get: function () { return store.getters[type]; },
  239. enumerable: true
  240. });
  241. });
  242. store._makeLocalGettersCache[namespace] = gettersProxy;
  243. }
  244. return store._makeLocalGettersCache[namespace]
  245. }
  246. function registerMutation (store, type, handler, local) {
  247. var entry = store._mutations[type] || (store._mutations[type] = []);
  248. entry.push(function wrappedMutationHandler (payload) {
  249. handler.call(store, local.state, payload);
  250. });
  251. }
  252. function registerAction (store, type, handler, local) {
  253. var entry = store._actions[type] || (store._actions[type] = []);
  254. entry.push(function wrappedActionHandler (payload) {
  255. var res = handler.call(store, {
  256. dispatch: local.dispatch,
  257. commit: local.commit,
  258. getters: local.getters,
  259. state: local.state,
  260. rootGetters: store.getters,
  261. rootState: store.state
  262. }, payload);
  263. if (!isPromise(res)) {
  264. res = Promise.resolve(res);
  265. }
  266. if (store._devtoolHook) {
  267. return res.catch(function (err) {
  268. store._devtoolHook.emit('vuex:error', err);
  269. throw err
  270. })
  271. } else {
  272. return res
  273. }
  274. });
  275. }
  276. function registerGetter (store, type, rawGetter, local) {
  277. if (store._wrappedGetters[type]) {
  278. return
  279. }
  280. store._wrappedGetters[type] = function wrappedGetter (store) {
  281. return rawGetter(
  282. local.state, // local state
  283. local.getters, // local getters
  284. store.state, // root state
  285. store.getters // root getters
  286. )
  287. };
  288. }
  289. function enableStrictMode (store) {
  290. watch(function () { return store._state.data; }, function () {
  291. }, { deep: true, flush: 'sync' });
  292. }
  293. function getNestedState (state, path) {
  294. return path.reduce(function (state, key) { return state[key]; }, state)
  295. }
  296. function unifyObjectStyle (type, payload, options) {
  297. if (isObject(type) && type.type) {
  298. options = payload;
  299. payload = type;
  300. type = type.type;
  301. }
  302. return { type: type, payload: payload, options: options }
  303. }
  304. var LABEL_VUEX_BINDINGS = 'vuex bindings';
  305. var MUTATIONS_LAYER_ID = 'vuex:mutations';
  306. var ACTIONS_LAYER_ID = 'vuex:actions';
  307. var INSPECTOR_ID = 'vuex';
  308. var actionId = 0;
  309. function addDevtools (app, store) {
  310. setupDevtoolsPlugin(
  311. {
  312. id: 'org.vuejs.vuex',
  313. app: app,
  314. label: 'Vuex',
  315. homepage: 'https://next.vuex.vuejs.org/',
  316. logo: 'https://vuejs.org/images/icons/favicon-96x96.png',
  317. packageName: 'vuex',
  318. componentStateTypes: [LABEL_VUEX_BINDINGS]
  319. },
  320. function (api) {
  321. api.addTimelineLayer({
  322. id: MUTATIONS_LAYER_ID,
  323. label: 'Vuex Mutations',
  324. color: COLOR_LIME_500
  325. });
  326. api.addTimelineLayer({
  327. id: ACTIONS_LAYER_ID,
  328. label: 'Vuex Actions',
  329. color: COLOR_LIME_500
  330. });
  331. api.addInspector({
  332. id: INSPECTOR_ID,
  333. label: 'Vuex',
  334. icon: 'storage',
  335. treeFilterPlaceholder: 'Filter stores...'
  336. });
  337. api.on.getInspectorTree(function (payload) {
  338. if (payload.app === app && payload.inspectorId === INSPECTOR_ID) {
  339. if (payload.filter) {
  340. var nodes = [];
  341. flattenStoreForInspectorTree(nodes, store._modules.root, payload.filter, '');
  342. payload.rootNodes = nodes;
  343. } else {
  344. payload.rootNodes = [
  345. formatStoreForInspectorTree(store._modules.root, '')
  346. ];
  347. }
  348. }
  349. });
  350. api.on.getInspectorState(function (payload) {
  351. if (payload.app === app && payload.inspectorId === INSPECTOR_ID) {
  352. var modulePath = payload.nodeId;
  353. makeLocalGetters(store, modulePath);
  354. payload.state = formatStoreForInspectorState(
  355. getStoreModule(store._modules, modulePath),
  356. modulePath === 'root' ? store.getters : store._makeLocalGettersCache,
  357. modulePath
  358. );
  359. }
  360. });
  361. api.on.editInspectorState(function (payload) {
  362. if (payload.app === app && payload.inspectorId === INSPECTOR_ID) {
  363. var modulePath = payload.nodeId;
  364. var path = payload.path;
  365. if (modulePath !== 'root') {
  366. path = modulePath.split('/').filter(Boolean).concat( path);
  367. }
  368. store._withCommit(function () {
  369. payload.set(store._state.data, path, payload.state.value);
  370. });
  371. }
  372. });
  373. store.subscribe(function (mutation, state) {
  374. var data = {};
  375. if (mutation.payload) {
  376. data.payload = mutation.payload;
  377. }
  378. data.state = state;
  379. api.notifyComponentUpdate();
  380. api.sendInspectorTree(INSPECTOR_ID);
  381. api.sendInspectorState(INSPECTOR_ID);
  382. api.addTimelineEvent({
  383. layerId: MUTATIONS_LAYER_ID,
  384. event: {
  385. time: Date.now(),
  386. title: mutation.type,
  387. data: data
  388. }
  389. });
  390. });
  391. store.subscribeAction({
  392. before: function (action, state) {
  393. var data = {};
  394. if (action.payload) {
  395. data.payload = action.payload;
  396. }
  397. action._id = actionId++;
  398. action._time = Date.now();
  399. data.state = state;
  400. api.addTimelineEvent({
  401. layerId: ACTIONS_LAYER_ID,
  402. event: {
  403. time: action._time,
  404. title: action.type,
  405. groupId: action._id,
  406. subtitle: 'start',
  407. data: data
  408. }
  409. });
  410. },
  411. after: function (action, state) {
  412. var data = {};
  413. var duration = Date.now() - action._time;
  414. data.duration = {
  415. _custom: {
  416. type: 'duration',
  417. display: (duration + "ms"),
  418. tooltip: 'Action duration',
  419. value: duration
  420. }
  421. };
  422. if (action.payload) {
  423. data.payload = action.payload;
  424. }
  425. data.state = state;
  426. api.addTimelineEvent({
  427. layerId: ACTIONS_LAYER_ID,
  428. event: {
  429. time: Date.now(),
  430. title: action.type,
  431. groupId: action._id,
  432. subtitle: 'end',
  433. data: data
  434. }
  435. });
  436. }
  437. });
  438. }
  439. );
  440. }
  441. // extracted from tailwind palette
  442. var COLOR_LIME_500 = 0x84cc16;
  443. var COLOR_DARK = 0x666666;
  444. var COLOR_WHITE = 0xffffff;
  445. var TAG_NAMESPACED = {
  446. label: 'namespaced',
  447. textColor: COLOR_WHITE,
  448. backgroundColor: COLOR_DARK
  449. };
  450. /**
  451. * @param {string} path
  452. */
  453. function extractNameFromPath (path) {
  454. return path && path !== 'root' ? path.split('/').slice(-2, -1)[0] : 'Root'
  455. }
  456. /**
  457. * @param {*} module
  458. * @return {import('@vue/devtools-api').CustomInspectorNode}
  459. */
  460. function formatStoreForInspectorTree (module, path) {
  461. return {
  462. id: path || 'root',
  463. // all modules end with a `/`, we want the last segment only
  464. // cart/ -> cart
  465. // nested/cart/ -> cart
  466. label: extractNameFromPath(path),
  467. tags: module.namespaced ? [TAG_NAMESPACED] : [],
  468. children: Object.keys(module._children).map(function (moduleName) { return formatStoreForInspectorTree(
  469. module._children[moduleName],
  470. path + moduleName + '/'
  471. ); }
  472. )
  473. }
  474. }
  475. /**
  476. * @param {import('@vue/devtools-api').CustomInspectorNode[]} result
  477. * @param {*} module
  478. * @param {string} filter
  479. * @param {string} path
  480. */
  481. function flattenStoreForInspectorTree (result, module, filter, path) {
  482. if (path.includes(filter)) {
  483. result.push({
  484. id: path || 'root',
  485. label: path.endsWith('/') ? path.slice(0, path.length - 1) : path || 'Root',
  486. tags: module.namespaced ? [TAG_NAMESPACED] : []
  487. });
  488. }
  489. Object.keys(module._children).forEach(function (moduleName) {
  490. flattenStoreForInspectorTree(result, module._children[moduleName], filter, path + moduleName + '/');
  491. });
  492. }
  493. /**
  494. * @param {*} module
  495. * @return {import('@vue/devtools-api').CustomInspectorState}
  496. */
  497. function formatStoreForInspectorState (module, getters, path) {
  498. getters = path === 'root' ? getters : getters[path];
  499. var gettersKeys = Object.keys(getters);
  500. var storeState = {
  501. state: Object.keys(module.state).map(function (key) { return ({
  502. key: key,
  503. editable: true,
  504. value: module.state[key]
  505. }); })
  506. };
  507. if (gettersKeys.length) {
  508. var tree = transformPathsToObjectTree(getters);
  509. storeState.getters = Object.keys(tree).map(function (key) { return ({
  510. key: key.endsWith('/') ? extractNameFromPath(key) : key,
  511. editable: false,
  512. value: canThrow(function () { return tree[key]; })
  513. }); });
  514. }
  515. return storeState
  516. }
  517. function transformPathsToObjectTree (getters) {
  518. var result = {};
  519. Object.keys(getters).forEach(function (key) {
  520. var path = key.split('/');
  521. if (path.length > 1) {
  522. var target = result;
  523. var leafKey = path.pop();
  524. path.forEach(function (p) {
  525. if (!target[p]) {
  526. target[p] = {
  527. _custom: {
  528. value: {},
  529. display: p,
  530. tooltip: 'Module',
  531. abstract: true
  532. }
  533. };
  534. }
  535. target = target[p]._custom.value;
  536. });
  537. target[leafKey] = canThrow(function () { return getters[key]; });
  538. } else {
  539. result[key] = canThrow(function () { return getters[key]; });
  540. }
  541. });
  542. return result
  543. }
  544. function getStoreModule (moduleMap, path) {
  545. var names = path.split('/').filter(function (n) { return n; });
  546. return names.reduce(
  547. function (module, moduleName, i) {
  548. var child = module[moduleName];
  549. if (!child) {
  550. throw new Error(("Missing module \"" + moduleName + "\" for path \"" + path + "\"."))
  551. }
  552. return i === names.length - 1 ? child : child._children
  553. },
  554. path === 'root' ? moduleMap : moduleMap.root._children
  555. )
  556. }
  557. function canThrow (cb) {
  558. try {
  559. return cb()
  560. } catch (e) {
  561. return e
  562. }
  563. }
  564. // Base data struct for store's module, package with some attribute and method
  565. var Module = function Module (rawModule, runtime) {
  566. this.runtime = runtime;
  567. // Store some children item
  568. this._children = Object.create(null);
  569. // Store the origin module object which passed by programmer
  570. this._rawModule = rawModule;
  571. var rawState = rawModule.state;
  572. // Store the origin module's state
  573. this.state = (typeof rawState === 'function' ? rawState() : rawState) || {};
  574. };
  575. var prototypeAccessors$1 = { namespaced: { configurable: true } };
  576. prototypeAccessors$1.namespaced.get = function () {
  577. return !!this._rawModule.namespaced
  578. };
  579. Module.prototype.addChild = function addChild (key, module) {
  580. this._children[key] = module;
  581. };
  582. Module.prototype.removeChild = function removeChild (key) {
  583. delete this._children[key];
  584. };
  585. Module.prototype.getChild = function getChild (key) {
  586. return this._children[key]
  587. };
  588. Module.prototype.hasChild = function hasChild (key) {
  589. return key in this._children
  590. };
  591. Module.prototype.update = function update (rawModule) {
  592. this._rawModule.namespaced = rawModule.namespaced;
  593. if (rawModule.actions) {
  594. this._rawModule.actions = rawModule.actions;
  595. }
  596. if (rawModule.mutations) {
  597. this._rawModule.mutations = rawModule.mutations;
  598. }
  599. if (rawModule.getters) {
  600. this._rawModule.getters = rawModule.getters;
  601. }
  602. };
  603. Module.prototype.forEachChild = function forEachChild (fn) {
  604. forEachValue(this._children, fn);
  605. };
  606. Module.prototype.forEachGetter = function forEachGetter (fn) {
  607. if (this._rawModule.getters) {
  608. forEachValue(this._rawModule.getters, fn);
  609. }
  610. };
  611. Module.prototype.forEachAction = function forEachAction (fn) {
  612. if (this._rawModule.actions) {
  613. forEachValue(this._rawModule.actions, fn);
  614. }
  615. };
  616. Module.prototype.forEachMutation = function forEachMutation (fn) {
  617. if (this._rawModule.mutations) {
  618. forEachValue(this._rawModule.mutations, fn);
  619. }
  620. };
  621. Object.defineProperties( Module.prototype, prototypeAccessors$1 );
  622. var ModuleCollection = function ModuleCollection (rawRootModule) {
  623. // register root module (Vuex.Store options)
  624. this.register([], rawRootModule, false);
  625. };
  626. ModuleCollection.prototype.get = function get (path) {
  627. return path.reduce(function (module, key) {
  628. return module.getChild(key)
  629. }, this.root)
  630. };
  631. ModuleCollection.prototype.getNamespace = function getNamespace (path) {
  632. var module = this.root;
  633. return path.reduce(function (namespace, key) {
  634. module = module.getChild(key);
  635. return namespace + (module.namespaced ? key + '/' : '')
  636. }, '')
  637. };
  638. ModuleCollection.prototype.update = function update$1 (rawRootModule) {
  639. update([], this.root, rawRootModule);
  640. };
  641. ModuleCollection.prototype.register = function register (path, rawModule, runtime) {
  642. var this$1$1 = this;
  643. if ( runtime === void 0 ) runtime = true;
  644. var newModule = new Module(rawModule, runtime);
  645. if (path.length === 0) {
  646. this.root = newModule;
  647. } else {
  648. var parent = this.get(path.slice(0, -1));
  649. parent.addChild(path[path.length - 1], newModule);
  650. }
  651. // register nested modules
  652. if (rawModule.modules) {
  653. forEachValue(rawModule.modules, function (rawChildModule, key) {
  654. this$1$1.register(path.concat(key), rawChildModule, runtime);
  655. });
  656. }
  657. };
  658. ModuleCollection.prototype.unregister = function unregister (path) {
  659. var parent = this.get(path.slice(0, -1));
  660. var key = path[path.length - 1];
  661. var child = parent.getChild(key);
  662. if (!child) {
  663. return
  664. }
  665. if (!child.runtime) {
  666. return
  667. }
  668. parent.removeChild(key);
  669. };
  670. ModuleCollection.prototype.isRegistered = function isRegistered (path) {
  671. var parent = this.get(path.slice(0, -1));
  672. var key = path[path.length - 1];
  673. if (parent) {
  674. return parent.hasChild(key)
  675. }
  676. return false
  677. };
  678. function update (path, targetModule, newModule) {
  679. // update target module
  680. targetModule.update(newModule);
  681. // update nested modules
  682. if (newModule.modules) {
  683. for (var key in newModule.modules) {
  684. if (!targetModule.getChild(key)) {
  685. return
  686. }
  687. update(
  688. path.concat(key),
  689. targetModule.getChild(key),
  690. newModule.modules[key]
  691. );
  692. }
  693. }
  694. }
  695. function createStore (options) {
  696. return new Store(options)
  697. }
  698. var Store = function Store (options) {
  699. var this$1$1 = this;
  700. if ( options === void 0 ) options = {};
  701. var plugins = options.plugins; if ( plugins === void 0 ) plugins = [];
  702. var strict = options.strict; if ( strict === void 0 ) strict = false;
  703. var devtools = options.devtools;
  704. // store internal state
  705. this._committing = false;
  706. this._actions = Object.create(null);
  707. this._actionSubscribers = [];
  708. this._mutations = Object.create(null);
  709. this._wrappedGetters = Object.create(null);
  710. this._modules = new ModuleCollection(options);
  711. this._modulesNamespaceMap = Object.create(null);
  712. this._subscribers = [];
  713. this._makeLocalGettersCache = Object.create(null);
  714. // EffectScope instance. when registering new getters, we wrap them inside
  715. // EffectScope so that getters (computed) would not be destroyed on
  716. // component unmount.
  717. this._scope = null;
  718. this._devtools = devtools;
  719. // bind commit and dispatch to self
  720. var store = this;
  721. var ref = this;
  722. var dispatch = ref.dispatch;
  723. var commit = ref.commit;
  724. this.dispatch = function boundDispatch (type, payload) {
  725. return dispatch.call(store, type, payload)
  726. };
  727. this.commit = function boundCommit (type, payload, options) {
  728. return commit.call(store, type, payload, options)
  729. };
  730. // strict mode
  731. this.strict = strict;
  732. var state = this._modules.root.state;
  733. // init root module.
  734. // this also recursively registers all sub-modules
  735. // and collects all module getters inside this._wrappedGetters
  736. installModule(this, state, [], this._modules.root);
  737. // initialize the store state, which is responsible for the reactivity
  738. // (also registers _wrappedGetters as computed properties)
  739. resetStoreState(this, state);
  740. // apply plugins
  741. plugins.forEach(function (plugin) { return plugin(this$1$1); });
  742. };
  743. var prototypeAccessors = { state: { configurable: true } };
  744. Store.prototype.install = function install (app, injectKey) {
  745. app.provide(injectKey || storeKey, this);
  746. app.config.globalProperties.$store = this;
  747. var useDevtools = this._devtools !== undefined
  748. ? this._devtools
  749. : false;
  750. if (useDevtools) {
  751. addDevtools(app, this);
  752. }
  753. };
  754. prototypeAccessors.state.get = function () {
  755. return this._state.data
  756. };
  757. prototypeAccessors.state.set = function (v) {
  758. };
  759. Store.prototype.commit = function commit (_type, _payload, _options) {
  760. var this$1$1 = this;
  761. // check object-style commit
  762. var ref = unifyObjectStyle(_type, _payload, _options);
  763. var type = ref.type;
  764. var payload = ref.payload;
  765. var mutation = { type: type, payload: payload };
  766. var entry = this._mutations[type];
  767. if (!entry) {
  768. return
  769. }
  770. this._withCommit(function () {
  771. entry.forEach(function commitIterator (handler) {
  772. handler(payload);
  773. });
  774. });
  775. this._subscribers
  776. .slice() // shallow copy to prevent iterator invalidation if subscriber synchronously calls unsubscribe
  777. .forEach(function (sub) { return sub(mutation, this$1$1.state); });
  778. };
  779. Store.prototype.dispatch = function dispatch (_type, _payload) {
  780. var this$1$1 = this;
  781. // check object-style dispatch
  782. var ref = unifyObjectStyle(_type, _payload);
  783. var type = ref.type;
  784. var payload = ref.payload;
  785. var action = { type: type, payload: payload };
  786. var entry = this._actions[type];
  787. if (!entry) {
  788. return
  789. }
  790. try {
  791. this._actionSubscribers
  792. .slice() // shallow copy to prevent iterator invalidation if subscriber synchronously calls unsubscribe
  793. .filter(function (sub) { return sub.before; })
  794. .forEach(function (sub) { return sub.before(action, this$1$1.state); });
  795. } catch (e) {
  796. }
  797. var result = entry.length > 1
  798. ? Promise.all(entry.map(function (handler) { return handler(payload); }))
  799. : entry[0](payload);
  800. return new Promise(function (resolve, reject) {
  801. result.then(function (res) {
  802. try {
  803. this$1$1._actionSubscribers
  804. .filter(function (sub) { return sub.after; })
  805. .forEach(function (sub) { return sub.after(action, this$1$1.state); });
  806. } catch (e) {
  807. }
  808. resolve(res);
  809. }, function (error) {
  810. try {
  811. this$1$1._actionSubscribers
  812. .filter(function (sub) { return sub.error; })
  813. .forEach(function (sub) { return sub.error(action, this$1$1.state, error); });
  814. } catch (e) {
  815. }
  816. reject(error);
  817. });
  818. })
  819. };
  820. Store.prototype.subscribe = function subscribe (fn, options) {
  821. return genericSubscribe(fn, this._subscribers, options)
  822. };
  823. Store.prototype.subscribeAction = function subscribeAction (fn, options) {
  824. var subs = typeof fn === 'function' ? { before: fn } : fn;
  825. return genericSubscribe(subs, this._actionSubscribers, options)
  826. };
  827. Store.prototype.watch = function watch$1 (getter, cb, options) {
  828. var this$1$1 = this;
  829. return watch(function () { return getter(this$1$1.state, this$1$1.getters); }, cb, Object.assign({}, options))
  830. };
  831. Store.prototype.replaceState = function replaceState (state) {
  832. var this$1$1 = this;
  833. this._withCommit(function () {
  834. this$1$1._state.data = state;
  835. });
  836. };
  837. Store.prototype.registerModule = function registerModule (path, rawModule, options) {
  838. if ( options === void 0 ) options = {};
  839. if (typeof path === 'string') { path = [path]; }
  840. this._modules.register(path, rawModule);
  841. installModule(this, this.state, path, this._modules.get(path), options.preserveState);
  842. // reset store to update getters...
  843. resetStoreState(this, this.state);
  844. };
  845. Store.prototype.unregisterModule = function unregisterModule (path) {
  846. var this$1$1 = this;
  847. if (typeof path === 'string') { path = [path]; }
  848. this._modules.unregister(path);
  849. this._withCommit(function () {
  850. var parentState = getNestedState(this$1$1.state, path.slice(0, -1));
  851. delete parentState[path[path.length - 1]];
  852. });
  853. resetStore(this);
  854. };
  855. Store.prototype.hasModule = function hasModule (path) {
  856. if (typeof path === 'string') { path = [path]; }
  857. return this._modules.isRegistered(path)
  858. };
  859. Store.prototype.hotUpdate = function hotUpdate (newOptions) {
  860. this._modules.update(newOptions);
  861. resetStore(this, true);
  862. };
  863. Store.prototype._withCommit = function _withCommit (fn) {
  864. var committing = this._committing;
  865. this._committing = true;
  866. fn();
  867. this._committing = committing;
  868. };
  869. Object.defineProperties( Store.prototype, prototypeAccessors );
  870. /**
  871. * Reduce the code which written in Vue.js for getting the state.
  872. * @param {String} [namespace] - Module's namespace
  873. * @param {Object|Array} states # Object's item can be a function which accept state and getters for param, you can do something for state and getters in it.
  874. * @param {Object}
  875. */
  876. var mapState = normalizeNamespace(function (namespace, states) {
  877. var res = {};
  878. normalizeMap(states).forEach(function (ref) {
  879. var key = ref.key;
  880. var val = ref.val;
  881. res[key] = function mappedState () {
  882. var state = this.$store.state;
  883. var getters = this.$store.getters;
  884. if (namespace) {
  885. var module = getModuleByNamespace(this.$store, 'mapState', namespace);
  886. if (!module) {
  887. return
  888. }
  889. state = module.context.state;
  890. getters = module.context.getters;
  891. }
  892. return typeof val === 'function'
  893. ? val.call(this, state, getters)
  894. : state[val]
  895. };
  896. // mark vuex getter for devtools
  897. res[key].vuex = true;
  898. });
  899. return res
  900. });
  901. /**
  902. * Reduce the code which written in Vue.js for committing the mutation
  903. * @param {String} [namespace] - Module's namespace
  904. * @param {Object|Array} mutations # Object's item can be a function which accept `commit` function as the first param, it can accept another params. You can commit mutation and do any other things in this function. specially, You need to pass anthor params from the mapped function.
  905. * @return {Object}
  906. */
  907. var mapMutations = normalizeNamespace(function (namespace, mutations) {
  908. var res = {};
  909. normalizeMap(mutations).forEach(function (ref) {
  910. var key = ref.key;
  911. var val = ref.val;
  912. res[key] = function mappedMutation () {
  913. var args = [], len = arguments.length;
  914. while ( len-- ) args[ len ] = arguments[ len ];
  915. // Get the commit method from store
  916. var commit = this.$store.commit;
  917. if (namespace) {
  918. var module = getModuleByNamespace(this.$store, 'mapMutations', namespace);
  919. if (!module) {
  920. return
  921. }
  922. commit = module.context.commit;
  923. }
  924. return typeof val === 'function'
  925. ? val.apply(this, [commit].concat(args))
  926. : commit.apply(this.$store, [val].concat(args))
  927. };
  928. });
  929. return res
  930. });
  931. /**
  932. * Reduce the code which written in Vue.js for getting the getters
  933. * @param {String} [namespace] - Module's namespace
  934. * @param {Object|Array} getters
  935. * @return {Object}
  936. */
  937. var mapGetters = normalizeNamespace(function (namespace, getters) {
  938. var res = {};
  939. normalizeMap(getters).forEach(function (ref) {
  940. var key = ref.key;
  941. var val = ref.val;
  942. // The namespace has been mutated by normalizeNamespace
  943. val = namespace + val;
  944. res[key] = function mappedGetter () {
  945. if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
  946. return
  947. }
  948. return this.$store.getters[val]
  949. };
  950. // mark vuex getter for devtools
  951. res[key].vuex = true;
  952. });
  953. return res
  954. });
  955. /**
  956. * Reduce the code which written in Vue.js for dispatch the action
  957. * @param {String} [namespace] - Module's namespace
  958. * @param {Object|Array} actions # Object's item can be a function which accept `dispatch` function as the first param, it can accept anthor params. You can dispatch action and do any other things in this function. specially, You need to pass anthor params from the mapped function.
  959. * @return {Object}
  960. */
  961. var mapActions = normalizeNamespace(function (namespace, actions) {
  962. var res = {};
  963. normalizeMap(actions).forEach(function (ref) {
  964. var key = ref.key;
  965. var val = ref.val;
  966. res[key] = function mappedAction () {
  967. var args = [], len = arguments.length;
  968. while ( len-- ) args[ len ] = arguments[ len ];
  969. // get dispatch function from store
  970. var dispatch = this.$store.dispatch;
  971. if (namespace) {
  972. var module = getModuleByNamespace(this.$store, 'mapActions', namespace);
  973. if (!module) {
  974. return
  975. }
  976. dispatch = module.context.dispatch;
  977. }
  978. return typeof val === 'function'
  979. ? val.apply(this, [dispatch].concat(args))
  980. : dispatch.apply(this.$store, [val].concat(args))
  981. };
  982. });
  983. return res
  984. });
  985. /**
  986. * Rebinding namespace param for mapXXX function in special scoped, and return them by simple object
  987. * @param {String} namespace
  988. * @return {Object}
  989. */
  990. var createNamespacedHelpers = function (namespace) { return ({
  991. mapState: mapState.bind(null, namespace),
  992. mapGetters: mapGetters.bind(null, namespace),
  993. mapMutations: mapMutations.bind(null, namespace),
  994. mapActions: mapActions.bind(null, namespace)
  995. }); };
  996. /**
  997. * Normalize the map
  998. * normalizeMap([1, 2, 3]) => [ { key: 1, val: 1 }, { key: 2, val: 2 }, { key: 3, val: 3 } ]
  999. * normalizeMap({a: 1, b: 2, c: 3}) => [ { key: 'a', val: 1 }, { key: 'b', val: 2 }, { key: 'c', val: 3 } ]
  1000. * @param {Array|Object} map
  1001. * @return {Object}
  1002. */
  1003. function normalizeMap (map) {
  1004. if (!isValidMap(map)) {
  1005. return []
  1006. }
  1007. return Array.isArray(map)
  1008. ? map.map(function (key) { return ({ key: key, val: key }); })
  1009. : Object.keys(map).map(function (key) { return ({ key: key, val: map[key] }); })
  1010. }
  1011. /**
  1012. * Validate whether given map is valid or not
  1013. * @param {*} map
  1014. * @return {Boolean}
  1015. */
  1016. function isValidMap (map) {
  1017. return Array.isArray(map) || isObject(map)
  1018. }
  1019. /**
  1020. * Return a function expect two param contains namespace and map. it will normalize the namespace and then the param's function will handle the new namespace and the map.
  1021. * @param {Function} fn
  1022. * @return {Function}
  1023. */
  1024. function normalizeNamespace (fn) {
  1025. return function (namespace, map) {
  1026. if (typeof namespace !== 'string') {
  1027. map = namespace;
  1028. namespace = '';
  1029. } else if (namespace.charAt(namespace.length - 1) !== '/') {
  1030. namespace += '/';
  1031. }
  1032. return fn(namespace, map)
  1033. }
  1034. }
  1035. /**
  1036. * Search a special module from store by namespace. if module not exist, print error message.
  1037. * @param {Object} store
  1038. * @param {String} helper
  1039. * @param {String} namespace
  1040. * @return {Object}
  1041. */
  1042. function getModuleByNamespace (store, helper, namespace) {
  1043. var module = store._modulesNamespaceMap[namespace];
  1044. return module
  1045. }
  1046. // Credits: borrowed code from fcomb/redux-logger
  1047. function createLogger (ref) {
  1048. if ( ref === void 0 ) ref = {};
  1049. var collapsed = ref.collapsed; if ( collapsed === void 0 ) collapsed = true;
  1050. var filter = ref.filter; if ( filter === void 0 ) filter = function (mutation, stateBefore, stateAfter) { return true; };
  1051. var transformer = ref.transformer; if ( transformer === void 0 ) transformer = function (state) { return state; };
  1052. var mutationTransformer = ref.mutationTransformer; if ( mutationTransformer === void 0 ) mutationTransformer = function (mut) { return mut; };
  1053. var actionFilter = ref.actionFilter; if ( actionFilter === void 0 ) actionFilter = function (action, state) { return true; };
  1054. var actionTransformer = ref.actionTransformer; if ( actionTransformer === void 0 ) actionTransformer = function (act) { return act; };
  1055. var logMutations = ref.logMutations; if ( logMutations === void 0 ) logMutations = true;
  1056. var logActions = ref.logActions; if ( logActions === void 0 ) logActions = true;
  1057. var logger = ref.logger; if ( logger === void 0 ) logger = console;
  1058. return function (store) {
  1059. var prevState = deepCopy(store.state);
  1060. if (typeof logger === 'undefined') {
  1061. return
  1062. }
  1063. if (logMutations) {
  1064. store.subscribe(function (mutation, state) {
  1065. var nextState = deepCopy(state);
  1066. if (filter(mutation, prevState, nextState)) {
  1067. var formattedTime = getFormattedTime();
  1068. var formattedMutation = mutationTransformer(mutation);
  1069. var message = "mutation " + (mutation.type) + formattedTime;
  1070. startMessage(logger, message, collapsed);
  1071. logger.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState));
  1072. logger.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation);
  1073. logger.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState));
  1074. endMessage(logger);
  1075. }
  1076. prevState = nextState;
  1077. });
  1078. }
  1079. if (logActions) {
  1080. store.subscribeAction(function (action, state) {
  1081. if (actionFilter(action, state)) {
  1082. var formattedTime = getFormattedTime();
  1083. var formattedAction = actionTransformer(action);
  1084. var message = "action " + (action.type) + formattedTime;
  1085. startMessage(logger, message, collapsed);
  1086. logger.log('%c action', 'color: #03A9F4; font-weight: bold', formattedAction);
  1087. endMessage(logger);
  1088. }
  1089. });
  1090. }
  1091. }
  1092. }
  1093. function startMessage (logger, message, collapsed) {
  1094. var startMessage = collapsed
  1095. ? logger.groupCollapsed
  1096. : logger.group;
  1097. // render
  1098. try {
  1099. startMessage.call(logger, message);
  1100. } catch (e) {
  1101. logger.log(message);
  1102. }
  1103. }
  1104. function endMessage (logger) {
  1105. try {
  1106. logger.groupEnd();
  1107. } catch (e) {
  1108. logger.log('—— log end ——');
  1109. }
  1110. }
  1111. function getFormattedTime () {
  1112. var time = new Date();
  1113. return (" @ " + (pad(time.getHours(), 2)) + ":" + (pad(time.getMinutes(), 2)) + ":" + (pad(time.getSeconds(), 2)) + "." + (pad(time.getMilliseconds(), 3)))
  1114. }
  1115. function repeat (str, times) {
  1116. return (new Array(times + 1)).join(str)
  1117. }
  1118. function pad (num, maxLength) {
  1119. return repeat('0', maxLength - num.toString().length) + num
  1120. }
  1121. var index = {
  1122. version: '4.1.0',
  1123. Store: Store,
  1124. storeKey: storeKey,
  1125. createStore: createStore,
  1126. useStore: useStore,
  1127. mapState: mapState,
  1128. mapMutations: mapMutations,
  1129. mapGetters: mapGetters,
  1130. mapActions: mapActions,
  1131. createNamespacedHelpers: createNamespacedHelpers,
  1132. createLogger: createLogger
  1133. };
  1134. export default index;
  1135. export { Store, createLogger, createNamespacedHelpers, createStore, mapActions, mapGetters, mapMutations, mapState, storeKey, useStore };