版博士V2.0程序
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

runtime.js 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __defProps = Object.defineProperties;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
  6. var __getOwnPropNames = Object.getOwnPropertyNames;
  7. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  8. var __hasOwnProp = Object.prototype.hasOwnProperty;
  9. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  10. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  11. var __spreadValues = (a, b) => {
  12. for (var prop in b || (b = {}))
  13. if (__hasOwnProp.call(b, prop))
  14. __defNormalProp(a, prop, b[prop]);
  15. if (__getOwnPropSymbols)
  16. for (var prop of __getOwnPropSymbols(b)) {
  17. if (__propIsEnum.call(b, prop))
  18. __defNormalProp(a, prop, b[prop]);
  19. }
  20. return a;
  21. };
  22. var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
  23. var __export = (target, all) => {
  24. for (var name in all)
  25. __defProp(target, name, { get: all[name], enumerable: true });
  26. };
  27. var __copyProps = (to, from, except, desc) => {
  28. if (from && typeof from === "object" || typeof from === "function") {
  29. for (let key of __getOwnPropNames(from))
  30. if (!__hasOwnProp.call(to, key) && key !== except)
  31. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  32. }
  33. return to;
  34. };
  35. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  36. // src/runtime.ts
  37. var runtime_exports = {};
  38. __export(runtime_exports, {
  39. _HasDataLoaderMeta: () => HasDataLoaderMeta,
  40. _defineLoader: () => defineLoader,
  41. _definePage: () => _definePage,
  42. _mergeRouteRecord: () => _mergeRouteRecord,
  43. _setupDataFetchingGuard: () => setupDataFetchingGuard,
  44. _stopDataFetchingScope: () => stopScope
  45. });
  46. module.exports = __toCommonJS(runtime_exports);
  47. // src/data-fetching/defineLoader.ts
  48. var import_vue_router = require("vue-router");
  49. // src/data-fetching/dataCache.ts
  50. var import_vue = require("vue");
  51. function isCacheExpired(entry, options) {
  52. const { cacheTime } = options;
  53. return (
  54. // cacheTime == 0 means no cache
  55. !cacheTime || // did we hit the expiration time
  56. Date.now() - entry.when >= cacheTime || Array.from(entry.children).some(
  57. (childEntry) => isCacheExpired(childEntry, options)
  58. )
  59. );
  60. }
  61. function createDataLoaderEntry(options, initialData) {
  62. return withinScope(() => ({
  63. pending: (0, import_vue.ref)(false),
  64. error: (0, import_vue.ref)(),
  65. // set to 0 to when there is an initialData so the next request will always trigger the data loaders
  66. when: initialData === void 0 ? Date.now() : 0,
  67. children: /* @__PURE__ */ new Set(),
  68. // @ts-expect-error: data always start as empty
  69. data: (0, import_vue.ref)(initialData),
  70. params: {},
  71. query: {},
  72. // hash: null,
  73. isReady: false
  74. // this was just too annoying to type
  75. }));
  76. }
  77. function updateDataLoaderEntry(entry, data, params, query, hash) {
  78. entry.when = Date.now();
  79. entry.params = params;
  80. entry.query = query;
  81. entry.hash = hash.v;
  82. entry.isReady = true;
  83. entry.data.value = data;
  84. }
  85. var scope;
  86. function withinScope(fn) {
  87. return (scope = scope || (0, import_vue.effectScope)(true)).run(fn);
  88. }
  89. function stopScope() {
  90. if (scope) {
  91. scope.stop();
  92. scope = void 0;
  93. }
  94. }
  95. var currentContext;
  96. function getCurrentContext() {
  97. return currentContext || [];
  98. }
  99. function setCurrentContext(context) {
  100. currentContext = context;
  101. }
  102. // src/data-fetching/locationUtils.ts
  103. function includesParams(outer, inner) {
  104. for (const key in inner) {
  105. const innerValue = inner[key];
  106. const outerValue = outer[key];
  107. if (typeof innerValue === "string") {
  108. if (innerValue !== outerValue)
  109. return false;
  110. } else if (!innerValue || !outerValue) {
  111. if (innerValue !== outerValue)
  112. return false;
  113. } else {
  114. if (!Array.isArray(outerValue) || outerValue.length !== innerValue.length || innerValue.some((value, i) => value !== outerValue[i]))
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. // src/data-fetching/defineLoader.ts
  121. var DEFAULT_DEFINE_LOADER_OPTIONS = {
  122. cacheTime: 1e3 * 5,
  123. // 5s
  124. lazy: false,
  125. // same as no key
  126. key: ""
  127. };
  128. function defineLoader(nameOrLoader, _loaderOrOptions, opts) {
  129. const loader = typeof nameOrLoader === "function" ? nameOrLoader : _loaderOrOptions;
  130. opts = typeof _loaderOrOptions === "object" ? _loaderOrOptions : opts;
  131. const options = __spreadValues(__spreadValues({}, DEFAULT_DEFINE_LOADER_OPTIONS), opts);
  132. const entries = /* @__PURE__ */ new WeakMap();
  133. let pendingPromise;
  134. let currentNavigation;
  135. const pendingLoad = () => pendingPromise;
  136. const dataLoader = () => {
  137. let [parentEntry, _router, _route] = getCurrentContext();
  138. const router = _router || (0, import_vue_router.useRouter)();
  139. const route = _route || (0, import_vue_router.useRoute)();
  140. if (
  141. // no cache: we need to load
  142. !entries.has(router) || // invoked by the parent, we should try to load again
  143. parentEntry
  144. ) {
  145. load(route, router, parentEntry);
  146. }
  147. const entry = entries.get(router);
  148. const promise = Promise.resolve(pendingPromise).then(() => dataLoaderResult).finally(() => {
  149. if (parentEntry) {
  150. parentEntry.children.add(entry);
  151. }
  152. setCurrentContext(parentEntry && [parentEntry, router, route]);
  153. });
  154. const { data, pending, error } = entry;
  155. function refresh() {
  156. invalidate();
  157. load(route, router, parentEntry);
  158. return pendingPromise.catch(() => {
  159. });
  160. }
  161. function invalidate() {
  162. entry.when = 0;
  163. }
  164. const dataLoaderResult = {
  165. data,
  166. pending,
  167. error,
  168. refresh,
  169. invalidate,
  170. pendingLoad
  171. };
  172. return Object.assign(promise, dataLoaderResult);
  173. };
  174. function load(route, router, parent, initialRootData) {
  175. const hasCacheEntry = entries.has(router);
  176. const initialData = initialRootData && initialRootData[options.key];
  177. if (!hasCacheEntry) {
  178. entries.set(router, createDataLoaderEntry(options, initialData));
  179. }
  180. const entry = entries.get(router);
  181. if (initialData) {
  182. entry.when = 0;
  183. return Promise.resolve();
  184. }
  185. const needsNewLoad = !hasCacheEntry || shouldFetchAgain(entry, route);
  186. const { isReady, pending, error } = entry;
  187. const { lazy } = options;
  188. const isExpired = isCacheExpired(entry, options);
  189. if (pendingPromise && // if we need to fetch again due to param/query changes
  190. !needsNewLoad && // if it's a new navigation and there is no entry, we cannot rely on the pendingPromise as we don't know what
  191. // params and query were used and could have changed. If we had an entry, then we can rely on the result of
  192. // `needsNewLoad`
  193. currentNavigation === route && // if we are not ready but we have a pendingPromise, we are already fetching so we can reuse it
  194. (!isReady || !isExpired)) {
  195. return lazy ? Promise.resolve() : pendingPromise;
  196. }
  197. if (needsNewLoad || // if we never finished loading we cannot rely on needsNewLoad
  198. !isReady && currentNavigation !== route || // we did a load but the cache expired
  199. isReady && isExpired) {
  200. pending.value = true;
  201. error.value = null;
  202. currentNavigation = route;
  203. const [trackedRoute, params, query, hash] = trackRoute(route);
  204. if (!pendingPromise) {
  205. setCurrentContext([entry, router, route]);
  206. }
  207. const thisPromise = pendingPromise = loader(trackedRoute).then((data) => {
  208. if (pendingPromise === thisPromise) {
  209. updateDataLoaderEntry(entry, data, params, query, hash);
  210. }
  211. }).catch((err) => {
  212. error.value = err;
  213. return Promise.reject(err);
  214. }).finally(() => {
  215. if (pendingPromise === thisPromise) {
  216. pendingPromise = null;
  217. pending.value = false;
  218. }
  219. setCurrentContext(parent && [parent, router, route]);
  220. });
  221. }
  222. return lazy || // lazy resolves immediately to not block navigation guards
  223. !pendingPromise ? Promise.resolve() : (
  224. // pendingPromise is thisPromise
  225. pendingPromise
  226. );
  227. }
  228. dataLoader._ = {
  229. // loader,
  230. entries,
  231. load,
  232. options
  233. };
  234. dataLoader[IsLoader] = true;
  235. return dataLoader;
  236. }
  237. function shouldFetchAgain(entry, route) {
  238. return (
  239. // manually invalidated
  240. !entry.when || !includesParams(route.params, entry.params) || !includesParams(route.query, entry.query) || entry.hash != null && entry.hash !== route.hash || Array.from(entry.children).some(
  241. (childEntry) => shouldFetchAgain(childEntry, route)
  242. )
  243. );
  244. }
  245. var IsLoader = Symbol();
  246. function isDataLoader(loader) {
  247. return loader && loader[IsLoader];
  248. }
  249. function trackRoute(route) {
  250. const [params, paramReads] = trackObjectReads(route.params);
  251. const [query, queryReads] = trackObjectReads(route.query);
  252. let hash = { v: null };
  253. return [
  254. __spreadProps(__spreadValues({}, route), {
  255. // track the hash
  256. get hash() {
  257. return hash.v = route.hash;
  258. },
  259. params,
  260. query
  261. }),
  262. paramReads,
  263. queryReads,
  264. hash
  265. ];
  266. }
  267. function trackObjectReads(obj) {
  268. const reads = {};
  269. return [
  270. new Proxy(obj, {
  271. get(target, p, receiver) {
  272. const value = Reflect.get(target, p, receiver);
  273. reads[p] = value;
  274. return value;
  275. }
  276. }),
  277. reads
  278. ];
  279. }
  280. // src/data-fetching/dataFetchingGuard.ts
  281. var HasDataLoaderMeta = Symbol();
  282. var ADDED_SYMBOL = Symbol();
  283. function setupDataFetchingGuard(router, { initialData } = {}) {
  284. if (process.env.NODE_ENV !== "production") {
  285. if (ADDED_SYMBOL in router) {
  286. console.warn(
  287. "[vue-router]: Data fetching guard added twice. Make sure to remove the extra call."
  288. );
  289. return;
  290. }
  291. router[ADDED_SYMBOL] = true;
  292. }
  293. const fetchedState = {};
  294. let isFetched;
  295. router.beforeEach((to) => {
  296. return Promise.all(
  297. // retrieve all loaders as a flat array
  298. to.matched.flatMap((route) => route.meta[HasDataLoaderMeta]).filter(Boolean).map(
  299. (moduleImport) => moduleImport().then((mod) => {
  300. const loaders = Object.keys(mod).filter((exportName) => isDataLoader(mod[exportName])).map((loaderName) => mod[loaderName]);
  301. return Promise.all(
  302. // load will ensure only one request is happening at a time
  303. loaders.map((loader) => {
  304. const {
  305. options: { key },
  306. entries
  307. } = loader._;
  308. return loader._.load(
  309. to,
  310. router,
  311. void 0,
  312. initialData
  313. // FIXME: could the data.value be passed as an argument here?
  314. ).then(() => {
  315. if (!initialData) {
  316. if (key) {
  317. fetchedState[key] = entries.get(router).data.value;
  318. }
  319. } else if (process.env.NODE_ENV !== "production" && !key && !isFetched) {
  320. }
  321. });
  322. })
  323. );
  324. })
  325. )
  326. ).then(() => {
  327. initialData = void 0;
  328. isFetched = true;
  329. });
  330. });
  331. return initialData ? null : fetchedState;
  332. }
  333. // src/runtime.ts
  334. var _definePage = (route) => route;
  335. function _mergeRouteRecord(main, ...routeRecords) {
  336. return routeRecords.reduce((acc, routeRecord) => {
  337. const meta = Object.assign({}, acc.meta, routeRecord.meta);
  338. const alias = [].concat(
  339. acc.alias || [],
  340. routeRecord.alias || []
  341. );
  342. Object.assign(acc, routeRecord);
  343. acc.meta = meta;
  344. acc.alias = alias;
  345. return acc;
  346. }, main);
  347. }
  348. // Annotate the CommonJS export names for ESM import in node:
  349. 0 && (module.exports = {
  350. _HasDataLoaderMeta,
  351. _defineLoader,
  352. _definePage,
  353. _mergeRouteRecord,
  354. _setupDataFetchingGuard,
  355. _stopDataFetchingScope
  356. });