vite-plugin-mock-dev-server 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/core/defineMock.ts
2
+ var _utils = require('@pengzhanbo/utils');
3
+ function defineMock(config) {
4
+ return config;
5
+ }
6
+ function createDefineMock(transformer) {
7
+ const define = (config) => {
8
+ if (_utils.isArray.call(void 0, config))
9
+ config = config.map((item) => transformer(item) || item);
10
+ else
11
+ config = transformer(config) || config;
12
+ return config;
13
+ };
14
+ return define;
15
+ }
16
+
17
+ // src/core/defineMockData.ts
18
+
19
+ var mockDataCache = /* @__PURE__ */ new Map();
20
+ var responseCache = /* @__PURE__ */ new WeakMap();
21
+ var staleInterval = 70;
22
+ var CacheImpl = class {
23
+
24
+ // 初始化数据的备份,用于 判断 传入的初始化数据是否发生变更
25
+ #initialValue;
26
+ #lastUpdate;
27
+ constructor(value) {
28
+ this.value = value;
29
+ this.#initialValue = _utils.deepClone.call(void 0, value);
30
+ this.#lastUpdate = Date.now();
31
+ }
32
+ hotUpdate(value) {
33
+ if (Date.now() - this.#lastUpdate < staleInterval)
34
+ return;
35
+ if (!_utils.deepEqual.call(void 0, value, this.#initialValue)) {
36
+ this.value = value;
37
+ this.#initialValue = _utils.deepClone.call(void 0, value);
38
+ this.#lastUpdate = Date.now();
39
+ }
40
+ }
41
+ };
42
+ function defineMockData(key, initialData) {
43
+ if (!mockDataCache.has(key))
44
+ mockDataCache.set(key, new CacheImpl(initialData));
45
+ const cache = mockDataCache.get(key);
46
+ cache.hotUpdate(initialData);
47
+ if (responseCache.has(cache))
48
+ return responseCache.get(cache);
49
+ const res = [
50
+ () => cache.value,
51
+ (val) => {
52
+ if (_utils.isFunction.call(void 0, val))
53
+ val = _nullishCoalesce(val(cache.value), () => ( cache.value));
54
+ cache.value = val;
55
+ }
56
+ ];
57
+ Object.defineProperty(res, "value", {
58
+ get() {
59
+ return cache.value;
60
+ },
61
+ set(val) {
62
+ cache.value = val;
63
+ }
64
+ });
65
+ responseCache.set(cache, res);
66
+ return res;
67
+ }
68
+
69
+
70
+
71
+
72
+
73
+ exports.defineMock = defineMock; exports.createDefineMock = createDefineMock; exports.defineMockData = defineMockData;