@tdesign-react/chat 1.0.0 → 1.0.2-alpha.1
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.
- package/es/_util/reactify.js +1 -1
- package/es/_util/useDynamicStyle.js +1 -1
- package/es/chat-actionbar/_example-js/base.d.ts +2 -0
- package/es/chat-actionbar/_example-js/custom.d.ts +2 -0
- package/es/chat-actionbar/_example-js/style.d.ts +2 -0
- package/es/chat-actionbar/index.js +1 -1
- package/es/chat-attachments/_example-js/base.d.ts +2 -0
- package/es/chat-attachments/_example-js/scroll-x.d.ts +2 -0
- package/es/chat-attachments/_example-js/scroll-y.d.ts +2 -0
- package/es/chat-attachments/index.js +1 -1
- package/es/chat-engine/_example-js/agui-basic.d.ts +10 -0
- package/es/chat-engine/_example-js/agui-comprehensive.d.ts +2 -0
- package/es/chat-engine/_example-js/agui-test.d.ts +11 -0
- package/es/chat-engine/_example-js/agui-toolcall.d.ts +5 -0
- package/es/chat-engine/_example-js/agui-videoclip.d.ts +8 -0
- package/es/chat-engine/_example-js/agui.d.ts +1 -0
- package/es/chat-engine/_example-js/basic.d.ts +9 -0
- package/es/chat-engine/_example-js/components/HotelCard.d.ts +3 -0
- package/es/chat-engine/_example-js/components/HumanInputForm.d.ts +6 -0
- package/es/chat-engine/_example-js/components/HumanInputResult.d.ts +3 -0
- package/es/chat-engine/_example-js/components/ItineraryCard.d.ts +3 -0
- package/es/chat-engine/_example-js/components/PlanningStatePanel.d.ts +4 -0
- package/es/chat-engine/_example-js/components/Toolcall.d.ts +4 -0
- package/es/chat-engine/_example-js/components/WeatherCard.d.ts +3 -0
- package/es/chat-engine/_example-js/components/login.d.ts +1 -0
- package/es/chat-engine/_example-js/comprehensive.d.ts +12 -0
- package/es/chat-engine/_example-js/custom-content.d.ts +1 -0
- package/es/chat-engine/_example-js/hookComponent.d.ts +1 -0
- package/es/chat-engine/_example-js/initial-messages.d.ts +9 -0
- package/es/chat-engine/_example-js/instance-methods.d.ts +13 -0
- package/es/chat-engine/_example-js/travel-actions.d.ts +91 -0
- package/es/chat-engine/_example-js/travelToolcall.d.ts +1 -0
- package/es/chat-engine/components/provider/agent-state.js +1 -1
- package/es/chat-engine/components/toolcall/index.js +1 -1
- package/es/chat-engine/components/toolcall/registry.js +1 -1
- package/es/chat-engine/components/toolcall/render.js +1 -1
- package/es/chat-engine/components/toolcall/types.d.ts +12 -4
- package/es/chat-engine/components/toolcall/types.js +1 -1
- package/es/chat-engine/components/toolcall/types.js.map +1 -1
- package/es/chat-engine/core/adapters/agui/event-mapper.d.ts +104 -0
- package/es/chat-engine/core/adapters/agui/event-mapper.js +348 -0
- package/es/chat-engine/core/adapters/agui/event-mapper.js.map +1 -0
- package/es/chat-engine/core/adapters/agui/events.d.ts +1394 -0
- package/es/chat-engine/core/adapters/agui/events.js +181 -0
- package/es/chat-engine/core/adapters/agui/events.js.map +1 -0
- package/es/chat-engine/core/adapters/agui/index.d.ts +75 -0
- package/es/chat-engine/core/adapters/agui/index.js +165 -0
- package/es/chat-engine/core/adapters/agui/index.js.map +1 -0
- package/es/chat-engine/core/adapters/agui/state-manager.d.ts +99 -0
- package/es/chat-engine/core/adapters/agui/state-manager.js +168 -0
- package/es/chat-engine/core/adapters/agui/state-manager.js.map +1 -0
- package/es/chat-engine/core/adapters/agui/types.d.ts +760 -0
- package/es/chat-engine/core/adapters/agui/types.js +89 -0
- package/es/chat-engine/core/adapters/agui/types.js.map +1 -0
- package/es/chat-engine/core/adapters/agui/utils.d.ts +180 -0
- package/es/chat-engine/core/adapters/agui/utils.js +302 -0
- package/es/chat-engine/core/adapters/agui/utils.js.map +1 -0
- package/es/chat-engine/core/index.d.ts +132 -0
- package/es/chat-engine/core/index.js +597 -0
- package/es/chat-engine/core/index.js.map +1 -0
- package/es/chat-engine/core/processor/index.d.ts +20 -0
- package/es/chat-engine/core/processor/index.js +146 -0
- package/es/chat-engine/core/processor/index.js.map +1 -0
- package/es/chat-engine/core/server/batch-client.d.ts +20 -0
- package/es/chat-engine/core/server/batch-client.js +114 -0
- package/es/chat-engine/core/server/batch-client.js.map +1 -0
- package/es/chat-engine/core/server/connection-manager.d.ts +39 -0
- package/es/chat-engine/core/server/connection-manager.js +84 -0
- package/es/chat-engine/core/server/connection-manager.js.map +1 -0
- package/es/chat-engine/core/server/errors.d.ts +22 -0
- package/es/chat-engine/core/server/errors.js +80 -0
- package/es/chat-engine/core/server/errors.js.map +1 -0
- package/es/chat-engine/core/server/index.d.ts +11 -0
- package/es/chat-engine/core/server/index.js +26 -0
- package/es/chat-engine/core/server/index.js.map +1 -0
- package/es/chat-engine/core/server/llm-service.d.ts +44 -0
- package/es/chat-engine/core/server/llm-service.js +198 -0
- package/es/chat-engine/core/server/llm-service.js.map +1 -0
- package/es/chat-engine/core/server/sse-client.d.ts +77 -0
- package/es/chat-engine/core/server/sse-client.js +362 -0
- package/es/chat-engine/core/server/sse-client.js.map +1 -0
- package/es/chat-engine/core/server/sse-parser.d.ts +49 -0
- package/es/chat-engine/core/server/sse-parser.js +116 -0
- package/es/chat-engine/core/server/sse-parser.js.map +1 -0
- package/es/chat-engine/core/server/types.d.ts +54 -0
- package/es/chat-engine/core/server/types.js +28 -0
- package/es/chat-engine/core/server/types.js.map +1 -0
- package/es/chat-engine/core/store/message.d.ts +27 -0
- package/es/chat-engine/core/store/message.js +263 -0
- package/es/chat-engine/core/store/message.js.map +1 -0
- package/es/chat-engine/core/store/model.d.ts +8 -0
- package/es/chat-engine/core/store/model.js +65 -0
- package/es/chat-engine/core/store/model.js.map +1 -0
- package/es/chat-engine/core/store/reactiveState.d.ts +52 -0
- package/es/chat-engine/core/store/reactiveState.js +1359 -0
- package/es/chat-engine/core/store/reactiveState.js.map +1 -0
- package/es/chat-engine/core/type.d.ts +248 -0
- package/es/chat-engine/core/type.js +7 -0
- package/es/chat-engine/core/type.js.map +1 -0
- package/es/chat-engine/core/utils/eventEmitter.d.ts +10 -0
- package/es/chat-engine/core/utils/eventEmitter.js +67 -0
- package/es/chat-engine/core/utils/eventEmitter.js.map +1 -0
- package/es/chat-engine/core/utils/index.d.ts +32 -0
- package/es/chat-engine/core/utils/index.js +996 -0
- package/es/chat-engine/core/utils/index.js.map +1 -0
- package/es/chat-engine/core/utils/logger.d.ts +30 -0
- package/es/chat-engine/core/utils/logger.js +87 -0
- package/es/chat-engine/core/utils/logger.js.map +1 -0
- package/es/chat-engine/hooks/useAgentState.js +1 -1
- package/es/chat-engine/hooks/useAgentToolcall.js +1 -1
- package/es/chat-engine/hooks/useChat.js +1 -1
- package/es/chat-engine/index.js +1 -1
- package/es/chat-filecard/_example-js/base.d.ts +1 -0
- package/es/chat-filecard/index.js +1 -1
- package/es/chat-loading/_example-js/base.d.ts +2 -0
- package/es/chat-loading/_example-js/text.d.ts +2 -0
- package/es/chat-loading/index.js +1 -1
- package/es/chat-markdown/_example-js/base.d.ts +1 -0
- package/es/chat-markdown/_example-js/custom.d.ts +2 -0
- package/es/chat-markdown/_example-js/event.d.ts +1 -0
- package/es/chat-markdown/_example-js/footnote.d.ts +2 -0
- package/es/chat-markdown/_example-js/plugin.d.ts +2 -0
- package/es/chat-markdown/_example-js/theme.d.ts +2 -0
- package/es/chat-markdown/index.js +1 -1
- package/es/chat-message/_example-js/action.d.ts +1 -0
- package/es/chat-message/_example-js/base.d.ts +1 -0
- package/es/chat-message/_example-js/configure.d.ts +1 -0
- package/es/chat-message/_example-js/content.d.ts +1 -0
- package/es/chat-message/_example-js/custom.d.ts +1 -0
- package/es/chat-message/_example-js/handle-actions.d.ts +7 -0
- package/es/chat-message/_example-js/status.d.ts +1 -0
- package/es/chat-message/_example-js/think.d.ts +1 -0
- package/es/chat-message/index.js +1 -1
- package/es/chat-sender/_example-js/attachment.d.ts +2 -0
- package/es/chat-sender/_example-js/base.d.ts +2 -0
- package/es/chat-sender/_example-js/custom.d.ts +2 -0
- package/es/chat-sender/index.js +1 -1
- package/es/chat-thinking/_example-js/base.d.ts +1 -0
- package/es/chat-thinking/_example-js/style.d.ts +1 -0
- package/es/chat-thinking/index.js +1 -1
- package/es/chatbot/_example-js/agent.d.ts +1 -0
- package/es/chatbot/_example-js/agui.d.ts +13 -0
- package/es/chatbot/_example-js/backup/travel.d.ts +1 -0
- package/es/chatbot/_example-js/basic.d.ts +1 -0
- package/es/chatbot/_example-js/code.d.ts +1 -0
- package/es/chatbot/_example-js/components/ItineraryCard.d.ts +3 -0
- package/es/chatbot/_example-js/components/login.d.ts +1 -0
- package/es/chatbot/_example-js/comprehensive.d.ts +1 -0
- package/es/chatbot/_example-js/custom-content.d.ts +1 -0
- package/es/chatbot/_example-js/custom-merge.d.ts +1 -0
- package/es/chatbot/_example-js/custom.d.ts +1 -0
- package/es/chatbot/_example-js/docs.d.ts +1 -0
- package/es/chatbot/_example-js/image.d.ts +1 -0
- package/es/chatbot/_example-js/initial-messages.d.ts +9 -0
- package/es/chatbot/_example-js/instance-methods.d.ts +14 -0
- package/es/chatbot/_example-js/nostream.d.ts +1 -0
- package/es/chatbot/_example-js/quick-start.d.ts +9 -0
- package/es/chatbot/_example-js/research.d.ts +1 -0
- package/es/chatbot/_example-js/role-message-config.d.ts +22 -0
- package/es/chatbot/_example-js/sender-config.d.ts +20 -0
- package/es/chatbot/_example-js/service-config.d.ts +17 -0
- package/es/chatbot/_example-js/simple.d.ts +1 -0
- package/es/chatbot/_example-js/utils/messageRenderer.d.ts +5 -0
- package/es/chatbot/index.js +1 -1
- package/es/index.d.ts +0 -1
- package/es/index.js +1 -1
- package/es/style/index.js +1 -1
- package/package.json +4 -6
|
@@ -0,0 +1,1359 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tdesign v1.0.1
|
|
3
|
+
* (c) 2025 tdesign
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
|
|
8
|
+
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
9
|
+
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
|
|
10
|
+
import _createClass from '@babel/runtime/helpers/createClass';
|
|
11
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
12
|
+
import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
|
|
13
|
+
import _getPrototypeOf$1 from '@babel/runtime/helpers/getPrototypeOf';
|
|
14
|
+
import _inherits from '@babel/runtime/helpers/inherits';
|
|
15
|
+
import _wrapNativeSuper from '@babel/runtime/helpers/wrapNativeSuper';
|
|
16
|
+
import _typeof from '@babel/runtime/helpers/typeof';
|
|
17
|
+
|
|
18
|
+
function _callSuper(t, o, e) { return o = _getPrototypeOf$1(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf$1(t).constructor) : o.apply(t, e)); }
|
|
19
|
+
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
20
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
21
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
22
|
+
// src/utils/env.ts
|
|
23
|
+
var NOTHING = Symbol["for"]("immer-nothing");
|
|
24
|
+
var DRAFTABLE = Symbol["for"]("immer-draftable");
|
|
25
|
+
var DRAFT_STATE = Symbol["for"]("immer-state");
|
|
26
|
+
|
|
27
|
+
// src/utils/errors.ts
|
|
28
|
+
var errors = process.env.NODE_ENV !== "production" ? [
|
|
29
|
+
// All error codes, starting by 0:
|
|
30
|
+
function (plugin) {
|
|
31
|
+
return "The plugin for '".concat(plugin, "' has not been loaded into Immer. To enable the plugin, import and call `enable").concat(plugin, "()` when initializing your application.");
|
|
32
|
+
}, function (thing) {
|
|
33
|
+
return "produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '".concat(thing, "'");
|
|
34
|
+
}, "This object has been frozen and should not be mutated", function (data) {
|
|
35
|
+
return "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " + data;
|
|
36
|
+
}, "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.", "Immer forbids circular references", "The first or second argument to `produce` must be a function", "The third argument to `produce` must be a function or undefined", "First argument to `createDraft` must be a plain object, an array, or an immerable object", "First argument to `finishDraft` must be a draft returned by `createDraft`", function (thing) {
|
|
37
|
+
return "'current' expects a draft, got: ".concat(thing);
|
|
38
|
+
}, "Object.defineProperty() cannot be used on an Immer draft", "Object.setPrototypeOf() cannot be used on an Immer draft", "Immer only supports deleting array indices", "Immer only supports setting array indices and the 'length' property", function (thing) {
|
|
39
|
+
return "'original' expects a draft, got: ".concat(thing);
|
|
40
|
+
}
|
|
41
|
+
// Note: if more errors are added, the errorOffset in Patches.ts should be increased
|
|
42
|
+
// See Patches.ts for additional errors
|
|
43
|
+
] : [];
|
|
44
|
+
function die(error) {
|
|
45
|
+
if (process.env.NODE_ENV !== "production") {
|
|
46
|
+
var e = errors[error];
|
|
47
|
+
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
48
|
+
args[_key - 1] = arguments[_key];
|
|
49
|
+
}
|
|
50
|
+
var msg = typeof e === "function" ? e.apply(null, args) : e;
|
|
51
|
+
throw new Error("[Immer] ".concat(msg));
|
|
52
|
+
}
|
|
53
|
+
throw new Error("[Immer] minified error nr: ".concat(error, ". Full error at: https://bit.ly/3cXEKWf"));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/utils/common.ts
|
|
57
|
+
var _getPrototypeOf = Object.getPrototypeOf;
|
|
58
|
+
function isDraft(value) {
|
|
59
|
+
return !!value && !!value[DRAFT_STATE];
|
|
60
|
+
}
|
|
61
|
+
function isDraftable(value) {
|
|
62
|
+
var _value$constructor;
|
|
63
|
+
if (!value) return false;
|
|
64
|
+
return isPlainObject(value) || Array.isArray(value) || !!value[DRAFTABLE] || !!((_value$constructor = value.constructor) !== null && _value$constructor !== void 0 && _value$constructor[DRAFTABLE]) || isMap(value) || isSet(value);
|
|
65
|
+
}
|
|
66
|
+
var objectCtorString = Object.prototype.constructor.toString();
|
|
67
|
+
function isPlainObject(value) {
|
|
68
|
+
if (!value || _typeof(value) !== "object") return false;
|
|
69
|
+
var proto = _getPrototypeOf(value);
|
|
70
|
+
if (proto === null) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
var Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
|
|
74
|
+
if (Ctor === Object) return true;
|
|
75
|
+
return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
|
|
76
|
+
}
|
|
77
|
+
function original(value) {
|
|
78
|
+
if (!isDraft(value)) die(15, value);
|
|
79
|
+
return value[DRAFT_STATE].base_;
|
|
80
|
+
}
|
|
81
|
+
function each(obj, iter) {
|
|
82
|
+
if (getArchtype(obj) === 0 /* Object */) {
|
|
83
|
+
Reflect.ownKeys(obj).forEach(function (key) {
|
|
84
|
+
iter(key, obj[key], obj);
|
|
85
|
+
});
|
|
86
|
+
} else {
|
|
87
|
+
obj.forEach(function (entry, index) {
|
|
88
|
+
return iter(index, entry, obj);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function getArchtype(thing) {
|
|
93
|
+
var state = thing[DRAFT_STATE];
|
|
94
|
+
return state ? state.type_ : Array.isArray(thing) ? 1 /* Array */ : isMap(thing) ? 2 /* Map */ : isSet(thing) ? 3 /* Set */ : 0 /* Object */;
|
|
95
|
+
}
|
|
96
|
+
function has(thing, prop) {
|
|
97
|
+
return getArchtype(thing) === 2 /* Map */ ? thing.has(prop) : Object.prototype.hasOwnProperty.call(thing, prop);
|
|
98
|
+
}
|
|
99
|
+
function get(thing, prop) {
|
|
100
|
+
return getArchtype(thing) === 2 /* Map */ ? thing.get(prop) : thing[prop];
|
|
101
|
+
}
|
|
102
|
+
function set(thing, propOrOldValue, value) {
|
|
103
|
+
var t = getArchtype(thing);
|
|
104
|
+
if (t === 2 /* Map */) thing.set(propOrOldValue, value);else if (t === 3 /* Set */) {
|
|
105
|
+
thing.add(value);
|
|
106
|
+
} else thing[propOrOldValue] = value;
|
|
107
|
+
}
|
|
108
|
+
function is(x, y) {
|
|
109
|
+
if (x === y) {
|
|
110
|
+
return x !== 0 || 1 / x === 1 / y;
|
|
111
|
+
} else {
|
|
112
|
+
return x !== x && y !== y;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function isMap(target) {
|
|
116
|
+
return target instanceof Map;
|
|
117
|
+
}
|
|
118
|
+
function isSet(target) {
|
|
119
|
+
return target instanceof Set;
|
|
120
|
+
}
|
|
121
|
+
function latest(state) {
|
|
122
|
+
return state.copy_ || state.base_;
|
|
123
|
+
}
|
|
124
|
+
function shallowCopy(base, strict) {
|
|
125
|
+
if (isMap(base)) {
|
|
126
|
+
return new Map(base);
|
|
127
|
+
}
|
|
128
|
+
if (isSet(base)) {
|
|
129
|
+
return new Set(base);
|
|
130
|
+
}
|
|
131
|
+
if (Array.isArray(base)) return Array.prototype.slice.call(base);
|
|
132
|
+
var isPlain = isPlainObject(base);
|
|
133
|
+
if (strict === true || strict === "class_only" && !isPlain) {
|
|
134
|
+
var descriptors = Object.getOwnPropertyDescriptors(base);
|
|
135
|
+
delete descriptors[DRAFT_STATE];
|
|
136
|
+
var keys = Reflect.ownKeys(descriptors);
|
|
137
|
+
for (var i = 0; i < keys.length; i++) {
|
|
138
|
+
var key = keys[i];
|
|
139
|
+
var desc = descriptors[key];
|
|
140
|
+
if (desc.writable === false) {
|
|
141
|
+
desc.writable = true;
|
|
142
|
+
desc.configurable = true;
|
|
143
|
+
}
|
|
144
|
+
if (desc.get || desc.set) descriptors[key] = {
|
|
145
|
+
configurable: true,
|
|
146
|
+
writable: true,
|
|
147
|
+
// could live with !!desc.set as well here...
|
|
148
|
+
enumerable: desc.enumerable,
|
|
149
|
+
value: base[key]
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
return Object.create(_getPrototypeOf(base), descriptors);
|
|
153
|
+
} else {
|
|
154
|
+
var proto = _getPrototypeOf(base);
|
|
155
|
+
if (proto !== null && isPlain) {
|
|
156
|
+
return _objectSpread({}, base);
|
|
157
|
+
}
|
|
158
|
+
var obj = Object.create(proto);
|
|
159
|
+
return Object.assign(obj, base);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
function freeze(obj) {
|
|
163
|
+
var deep = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
164
|
+
if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj;
|
|
165
|
+
if (getArchtype(obj) > 1) {
|
|
166
|
+
obj.set = obj.add = obj.clear = obj["delete"] = dontMutateFrozenCollections;
|
|
167
|
+
}
|
|
168
|
+
Object.freeze(obj);
|
|
169
|
+
if (deep) Object.entries(obj).forEach(function (_ref) {
|
|
170
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
171
|
+
key = _ref2[0],
|
|
172
|
+
value = _ref2[1];
|
|
173
|
+
return freeze(value, true);
|
|
174
|
+
});
|
|
175
|
+
return obj;
|
|
176
|
+
}
|
|
177
|
+
function dontMutateFrozenCollections() {
|
|
178
|
+
die(2);
|
|
179
|
+
}
|
|
180
|
+
function isFrozen(obj) {
|
|
181
|
+
return Object.isFrozen(obj);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// src/utils/plugins.ts
|
|
185
|
+
var plugins = {};
|
|
186
|
+
function getPlugin(pluginKey) {
|
|
187
|
+
var plugin = plugins[pluginKey];
|
|
188
|
+
if (!plugin) {
|
|
189
|
+
die(0, pluginKey);
|
|
190
|
+
}
|
|
191
|
+
return plugin;
|
|
192
|
+
}
|
|
193
|
+
function loadPlugin(pluginKey, implementation) {
|
|
194
|
+
if (!plugins[pluginKey]) plugins[pluginKey] = implementation;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// src/core/scope.ts
|
|
198
|
+
var currentScope;
|
|
199
|
+
function getCurrentScope() {
|
|
200
|
+
return currentScope;
|
|
201
|
+
}
|
|
202
|
+
function createScope(parent_, immer_) {
|
|
203
|
+
return {
|
|
204
|
+
drafts_: [],
|
|
205
|
+
parent_: parent_,
|
|
206
|
+
immer_: immer_,
|
|
207
|
+
// Whenever the modified draft contains a draft from another scope, we
|
|
208
|
+
// need to prevent auto-freezing so the unowned draft can be finalized.
|
|
209
|
+
canAutoFreeze_: true,
|
|
210
|
+
unfinalizedDrafts_: 0
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function usePatchesInScope(scope, patchListener) {
|
|
214
|
+
if (patchListener) {
|
|
215
|
+
getPlugin("Patches");
|
|
216
|
+
scope.patches_ = [];
|
|
217
|
+
scope.inversePatches_ = [];
|
|
218
|
+
scope.patchListener_ = patchListener;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function revokeScope(scope) {
|
|
222
|
+
leaveScope(scope);
|
|
223
|
+
scope.drafts_.forEach(revokeDraft);
|
|
224
|
+
scope.drafts_ = null;
|
|
225
|
+
}
|
|
226
|
+
function leaveScope(scope) {
|
|
227
|
+
if (scope === currentScope) {
|
|
228
|
+
currentScope = scope.parent_;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function enterScope(immer2) {
|
|
232
|
+
return currentScope = createScope(currentScope, immer2);
|
|
233
|
+
}
|
|
234
|
+
function revokeDraft(draft) {
|
|
235
|
+
var state = draft[DRAFT_STATE];
|
|
236
|
+
if (state.type_ === 0 /* Object */ || state.type_ === 1 /* Array */) state.revoke_();else state.revoked_ = true;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// src/core/finalize.ts
|
|
240
|
+
function processResult(result, scope) {
|
|
241
|
+
scope.unfinalizedDrafts_ = scope.drafts_.length;
|
|
242
|
+
var baseDraft = scope.drafts_[0];
|
|
243
|
+
var isReplaced = result !== void 0 && result !== baseDraft;
|
|
244
|
+
if (isReplaced) {
|
|
245
|
+
if (baseDraft[DRAFT_STATE].modified_) {
|
|
246
|
+
revokeScope(scope);
|
|
247
|
+
die(4);
|
|
248
|
+
}
|
|
249
|
+
if (isDraftable(result)) {
|
|
250
|
+
result = finalize(scope, result);
|
|
251
|
+
if (!scope.parent_) maybeFreeze(scope, result);
|
|
252
|
+
}
|
|
253
|
+
if (scope.patches_) {
|
|
254
|
+
getPlugin("Patches").generateReplacementPatches_(baseDraft[DRAFT_STATE].base_, result, scope.patches_, scope.inversePatches_);
|
|
255
|
+
}
|
|
256
|
+
} else {
|
|
257
|
+
result = finalize(scope, baseDraft, []);
|
|
258
|
+
}
|
|
259
|
+
revokeScope(scope);
|
|
260
|
+
if (scope.patches_) {
|
|
261
|
+
scope.patchListener_(scope.patches_, scope.inversePatches_);
|
|
262
|
+
}
|
|
263
|
+
return result !== NOTHING ? result : void 0;
|
|
264
|
+
}
|
|
265
|
+
function finalize(rootScope, value, path) {
|
|
266
|
+
if (isFrozen(value)) return value;
|
|
267
|
+
var state = value[DRAFT_STATE];
|
|
268
|
+
if (!state) {
|
|
269
|
+
each(value, function (key, childValue) {
|
|
270
|
+
return finalizeProperty(rootScope, state, value, key, childValue, path);
|
|
271
|
+
});
|
|
272
|
+
return value;
|
|
273
|
+
}
|
|
274
|
+
if (state.scope_ !== rootScope) return value;
|
|
275
|
+
if (!state.modified_) {
|
|
276
|
+
maybeFreeze(rootScope, state.base_, true);
|
|
277
|
+
return state.base_;
|
|
278
|
+
}
|
|
279
|
+
if (!state.finalized_) {
|
|
280
|
+
state.finalized_ = true;
|
|
281
|
+
state.scope_.unfinalizedDrafts_--;
|
|
282
|
+
var result = state.copy_;
|
|
283
|
+
var resultEach = result;
|
|
284
|
+
var isSet2 = false;
|
|
285
|
+
if (state.type_ === 3 /* Set */) {
|
|
286
|
+
resultEach = new Set(result);
|
|
287
|
+
result.clear();
|
|
288
|
+
isSet2 = true;
|
|
289
|
+
}
|
|
290
|
+
each(resultEach, function (key, childValue) {
|
|
291
|
+
return finalizeProperty(rootScope, state, result, key, childValue, path, isSet2);
|
|
292
|
+
});
|
|
293
|
+
maybeFreeze(rootScope, result, false);
|
|
294
|
+
if (path && rootScope.patches_) {
|
|
295
|
+
getPlugin("Patches").generatePatches_(state, path, rootScope.patches_, rootScope.inversePatches_);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return state.copy_;
|
|
299
|
+
}
|
|
300
|
+
function finalizeProperty(rootScope, parentState, targetObject, prop, childValue, rootPath, targetIsSet) {
|
|
301
|
+
if (process.env.NODE_ENV !== "production" && childValue === targetObject) die(5);
|
|
302
|
+
if (isDraft(childValue)) {
|
|
303
|
+
var path = rootPath && parentState && parentState.type_ !== 3 /* Set */ &&
|
|
304
|
+
// Set objects are atomic since they have no keys.
|
|
305
|
+
!has(parentState.assigned_, prop) ? rootPath.concat(prop) : void 0;
|
|
306
|
+
var res = finalize(rootScope, childValue, path);
|
|
307
|
+
set(targetObject, prop, res);
|
|
308
|
+
if (isDraft(res)) {
|
|
309
|
+
rootScope.canAutoFreeze_ = false;
|
|
310
|
+
} else return;
|
|
311
|
+
} else if (targetIsSet) {
|
|
312
|
+
targetObject.add(childValue);
|
|
313
|
+
}
|
|
314
|
+
if (isDraftable(childValue) && !isFrozen(childValue)) {
|
|
315
|
+
if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
finalize(rootScope, childValue);
|
|
319
|
+
if ((!parentState || !parentState.scope_.parent_) && _typeof(prop) !== "symbol" && Object.prototype.propertyIsEnumerable.call(targetObject, prop)) maybeFreeze(rootScope, childValue);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function maybeFreeze(scope, value) {
|
|
323
|
+
var deep = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
324
|
+
if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {
|
|
325
|
+
freeze(value, deep);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// src/core/proxy.ts
|
|
330
|
+
function createProxyProxy(base, parent) {
|
|
331
|
+
var isArray = Array.isArray(base);
|
|
332
|
+
var state = {
|
|
333
|
+
type_: isArray ? 1 /* Array */ : 0 /* Object */,
|
|
334
|
+
|
|
335
|
+
// Track which produce call this is associated with.
|
|
336
|
+
scope_: parent ? parent.scope_ : getCurrentScope(),
|
|
337
|
+
// True for both shallow and deep changes.
|
|
338
|
+
modified_: false,
|
|
339
|
+
// Used during finalization.
|
|
340
|
+
finalized_: false,
|
|
341
|
+
// Track which properties have been assigned (true) or deleted (false).
|
|
342
|
+
assigned_: {},
|
|
343
|
+
// The parent draft state.
|
|
344
|
+
parent_: parent,
|
|
345
|
+
// The base state.
|
|
346
|
+
base_: base,
|
|
347
|
+
// The base proxy.
|
|
348
|
+
draft_: null,
|
|
349
|
+
// set below
|
|
350
|
+
// The base copy with any updated values.
|
|
351
|
+
copy_: null,
|
|
352
|
+
// Called by the `produce` function.
|
|
353
|
+
revoke_: null,
|
|
354
|
+
isManual_: false
|
|
355
|
+
};
|
|
356
|
+
var target = state;
|
|
357
|
+
var traps = objectTraps;
|
|
358
|
+
if (isArray) {
|
|
359
|
+
target = [state];
|
|
360
|
+
traps = arrayTraps;
|
|
361
|
+
}
|
|
362
|
+
var _Proxy$revocable = Proxy.revocable(target, traps),
|
|
363
|
+
revoke = _Proxy$revocable.revoke,
|
|
364
|
+
proxy = _Proxy$revocable.proxy;
|
|
365
|
+
state.draft_ = proxy;
|
|
366
|
+
state.revoke_ = revoke;
|
|
367
|
+
return proxy;
|
|
368
|
+
}
|
|
369
|
+
var objectTraps = {
|
|
370
|
+
get: function get(state, prop) {
|
|
371
|
+
if (prop === DRAFT_STATE) return state;
|
|
372
|
+
var source = latest(state);
|
|
373
|
+
if (!has(source, prop)) {
|
|
374
|
+
return readPropFromProto(state, source, prop);
|
|
375
|
+
}
|
|
376
|
+
var value = source[prop];
|
|
377
|
+
if (state.finalized_ || !isDraftable(value)) {
|
|
378
|
+
return value;
|
|
379
|
+
}
|
|
380
|
+
if (value === peek(state.base_, prop)) {
|
|
381
|
+
prepareCopy(state);
|
|
382
|
+
return state.copy_[prop] = createProxy(value, state);
|
|
383
|
+
}
|
|
384
|
+
return value;
|
|
385
|
+
},
|
|
386
|
+
has: function has(state, prop) {
|
|
387
|
+
return prop in latest(state);
|
|
388
|
+
},
|
|
389
|
+
ownKeys: function ownKeys(state) {
|
|
390
|
+
return Reflect.ownKeys(latest(state));
|
|
391
|
+
},
|
|
392
|
+
set: function set(state, prop, value) {
|
|
393
|
+
var desc = getDescriptorFromProto(latest(state), prop);
|
|
394
|
+
if (desc !== null && desc !== void 0 && desc.set) {
|
|
395
|
+
desc.set.call(state.draft_, value);
|
|
396
|
+
return true;
|
|
397
|
+
}
|
|
398
|
+
if (!state.modified_) {
|
|
399
|
+
var current2 = peek(latest(state), prop);
|
|
400
|
+
var currentState = current2 === null || current2 === void 0 ? void 0 : current2[DRAFT_STATE];
|
|
401
|
+
if (currentState && currentState.base_ === value) {
|
|
402
|
+
state.copy_[prop] = value;
|
|
403
|
+
state.assigned_[prop] = false;
|
|
404
|
+
return true;
|
|
405
|
+
}
|
|
406
|
+
if (is(value, current2) && (value !== void 0 || has(state.base_, prop))) return true;
|
|
407
|
+
prepareCopy(state);
|
|
408
|
+
markChanged(state);
|
|
409
|
+
}
|
|
410
|
+
if (state.copy_[prop] === value && (
|
|
411
|
+
// special case: handle new props with value 'undefined'
|
|
412
|
+
value !== void 0 || prop in state.copy_) ||
|
|
413
|
+
// special case: NaN
|
|
414
|
+
Number.isNaN(value) && Number.isNaN(state.copy_[prop])) return true;
|
|
415
|
+
state.copy_[prop] = value;
|
|
416
|
+
state.assigned_[prop] = true;
|
|
417
|
+
return true;
|
|
418
|
+
},
|
|
419
|
+
deleteProperty: function deleteProperty(state, prop) {
|
|
420
|
+
if (peek(state.base_, prop) !== void 0 || prop in state.base_) {
|
|
421
|
+
state.assigned_[prop] = false;
|
|
422
|
+
prepareCopy(state);
|
|
423
|
+
markChanged(state);
|
|
424
|
+
} else {
|
|
425
|
+
delete state.assigned_[prop];
|
|
426
|
+
}
|
|
427
|
+
if (state.copy_) {
|
|
428
|
+
delete state.copy_[prop];
|
|
429
|
+
}
|
|
430
|
+
return true;
|
|
431
|
+
},
|
|
432
|
+
// Note: We never coerce `desc.value` into an Immer draft, because we can't make
|
|
433
|
+
// the same guarantee in ES5 mode.
|
|
434
|
+
getOwnPropertyDescriptor: function getOwnPropertyDescriptor(state, prop) {
|
|
435
|
+
var owner = latest(state);
|
|
436
|
+
var desc = Reflect.getOwnPropertyDescriptor(owner, prop);
|
|
437
|
+
if (!desc) return desc;
|
|
438
|
+
return {
|
|
439
|
+
writable: true,
|
|
440
|
+
configurable: state.type_ !== 1 /* Array */ || prop !== "length",
|
|
441
|
+
enumerable: desc.enumerable,
|
|
442
|
+
value: owner[prop]
|
|
443
|
+
};
|
|
444
|
+
},
|
|
445
|
+
defineProperty: function defineProperty() {
|
|
446
|
+
die(11);
|
|
447
|
+
},
|
|
448
|
+
getPrototypeOf: function getPrototypeOf(state) {
|
|
449
|
+
return _getPrototypeOf(state.base_);
|
|
450
|
+
},
|
|
451
|
+
setPrototypeOf: function setPrototypeOf() {
|
|
452
|
+
die(12);
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
var arrayTraps = {};
|
|
456
|
+
each(objectTraps, function (key, fn) {
|
|
457
|
+
arrayTraps[key] = function () {
|
|
458
|
+
arguments[0] = arguments[0][0];
|
|
459
|
+
return fn.apply(this, arguments);
|
|
460
|
+
};
|
|
461
|
+
});
|
|
462
|
+
arrayTraps.deleteProperty = function (state, prop) {
|
|
463
|
+
if (process.env.NODE_ENV !== "production" && isNaN(parseInt(prop))) die(13);
|
|
464
|
+
return arrayTraps.set.call(this, state, prop, void 0);
|
|
465
|
+
};
|
|
466
|
+
arrayTraps.set = function (state, prop, value) {
|
|
467
|
+
if (process.env.NODE_ENV !== "production" && prop !== "length" && isNaN(parseInt(prop))) die(14);
|
|
468
|
+
return objectTraps.set.call(this, state[0], prop, value, state[0]);
|
|
469
|
+
};
|
|
470
|
+
function peek(draft, prop) {
|
|
471
|
+
var state = draft[DRAFT_STATE];
|
|
472
|
+
var source = state ? latest(state) : draft;
|
|
473
|
+
return source[prop];
|
|
474
|
+
}
|
|
475
|
+
function readPropFromProto(state, source, prop) {
|
|
476
|
+
var _desc$get;
|
|
477
|
+
var desc = getDescriptorFromProto(source, prop);
|
|
478
|
+
return desc ? "value" in desc ? desc.value : // This is a very special case, if the prop is a getter defined by the
|
|
479
|
+
// prototype, we should invoke it with the draft as context!
|
|
480
|
+
(_desc$get = desc.get) === null || _desc$get === void 0 ? void 0 : _desc$get.call(state.draft_) : void 0;
|
|
481
|
+
}
|
|
482
|
+
function getDescriptorFromProto(source, prop) {
|
|
483
|
+
if (!(prop in source)) return void 0;
|
|
484
|
+
var proto = _getPrototypeOf(source);
|
|
485
|
+
while (proto) {
|
|
486
|
+
var desc = Object.getOwnPropertyDescriptor(proto, prop);
|
|
487
|
+
if (desc) return desc;
|
|
488
|
+
proto = _getPrototypeOf(proto);
|
|
489
|
+
}
|
|
490
|
+
return void 0;
|
|
491
|
+
}
|
|
492
|
+
function markChanged(state) {
|
|
493
|
+
if (!state.modified_) {
|
|
494
|
+
state.modified_ = true;
|
|
495
|
+
if (state.parent_) {
|
|
496
|
+
markChanged(state.parent_);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
function prepareCopy(state) {
|
|
501
|
+
if (!state.copy_) {
|
|
502
|
+
state.copy_ = shallowCopy(state.base_, state.scope_.immer_.useStrictShallowCopy_);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// src/core/immerClass.ts
|
|
507
|
+
var Immer2 = /*#__PURE__*/function () {
|
|
508
|
+
function Immer2(config) {
|
|
509
|
+
var _this = this;
|
|
510
|
+
_classCallCheck(this, Immer2);
|
|
511
|
+
this.autoFreeze_ = true;
|
|
512
|
+
this.useStrictShallowCopy_ = false;
|
|
513
|
+
/**
|
|
514
|
+
* The `produce` function takes a value and a "recipe function" (whose
|
|
515
|
+
* return value often depends on the base state). The recipe function is
|
|
516
|
+
* free to mutate its first argument however it wants. All mutations are
|
|
517
|
+
* only ever applied to a __copy__ of the base state.
|
|
518
|
+
*
|
|
519
|
+
* Pass only a function to create a "curried producer" which relieves you
|
|
520
|
+
* from passing the recipe function every time.
|
|
521
|
+
*
|
|
522
|
+
* Only plain objects and arrays are made mutable. All other objects are
|
|
523
|
+
* considered uncopyable.
|
|
524
|
+
*
|
|
525
|
+
* Note: This function is __bound__ to its `Immer` instance.
|
|
526
|
+
*
|
|
527
|
+
* @param {any} base - the initial state
|
|
528
|
+
* @param {Function} recipe - function that receives a proxy of the base state as first argument and which can be freely modified
|
|
529
|
+
* @param {Function} patchListener - optional function that will be called with all the patches produced here
|
|
530
|
+
* @returns {any} a new state, or the initial state if nothing was modified
|
|
531
|
+
*/
|
|
532
|
+
this.produce = function (base, recipe, patchListener) {
|
|
533
|
+
if (typeof base === "function" && typeof recipe !== "function") {
|
|
534
|
+
var defaultBase = recipe;
|
|
535
|
+
recipe = base;
|
|
536
|
+
var self = _this;
|
|
537
|
+
return function curriedProduce() {
|
|
538
|
+
var _this2 = this;
|
|
539
|
+
var base2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultBase;
|
|
540
|
+
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
541
|
+
args[_key2 - 1] = arguments[_key2];
|
|
542
|
+
}
|
|
543
|
+
return self.produce(base2, function (draft) {
|
|
544
|
+
var _recipe;
|
|
545
|
+
return (_recipe = recipe).call.apply(_recipe, [_this2, draft].concat(args));
|
|
546
|
+
});
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
if (typeof recipe !== "function") die(6);
|
|
550
|
+
if (patchListener !== void 0 && typeof patchListener !== "function") die(7);
|
|
551
|
+
var result;
|
|
552
|
+
if (isDraftable(base)) {
|
|
553
|
+
var scope = enterScope(_this);
|
|
554
|
+
var proxy = createProxy(base, void 0);
|
|
555
|
+
var hasError = true;
|
|
556
|
+
try {
|
|
557
|
+
result = recipe(proxy);
|
|
558
|
+
hasError = false;
|
|
559
|
+
} finally {
|
|
560
|
+
if (hasError) revokeScope(scope);else leaveScope(scope);
|
|
561
|
+
}
|
|
562
|
+
usePatchesInScope(scope, patchListener);
|
|
563
|
+
return processResult(result, scope);
|
|
564
|
+
} else if (!base || _typeof(base) !== "object") {
|
|
565
|
+
result = recipe(base);
|
|
566
|
+
if (result === void 0) result = base;
|
|
567
|
+
if (result === NOTHING) result = void 0;
|
|
568
|
+
if (_this.autoFreeze_) freeze(result, true);
|
|
569
|
+
if (patchListener) {
|
|
570
|
+
var p = [];
|
|
571
|
+
var ip = [];
|
|
572
|
+
getPlugin("Patches").generateReplacementPatches_(base, result, p, ip);
|
|
573
|
+
patchListener(p, ip);
|
|
574
|
+
}
|
|
575
|
+
return result;
|
|
576
|
+
} else die(1, base);
|
|
577
|
+
};
|
|
578
|
+
this.produceWithPatches = function (base, recipe) {
|
|
579
|
+
if (typeof base === "function") {
|
|
580
|
+
return function (state) {
|
|
581
|
+
for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
|
582
|
+
args[_key3 - 1] = arguments[_key3];
|
|
583
|
+
}
|
|
584
|
+
return _this.produceWithPatches(state, function (draft) {
|
|
585
|
+
return base.apply(void 0, [draft].concat(args));
|
|
586
|
+
});
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
var patches, inversePatches;
|
|
590
|
+
var result = _this.produce(base, recipe, function (p, ip) {
|
|
591
|
+
patches = p;
|
|
592
|
+
inversePatches = ip;
|
|
593
|
+
});
|
|
594
|
+
return [result, patches, inversePatches];
|
|
595
|
+
};
|
|
596
|
+
if (typeof (config === null || config === void 0 ? void 0 : config.autoFreeze) === "boolean") this.setAutoFreeze(config.autoFreeze);
|
|
597
|
+
if (typeof (config === null || config === void 0 ? void 0 : config.useStrictShallowCopy) === "boolean") this.setUseStrictShallowCopy(config.useStrictShallowCopy);
|
|
598
|
+
}
|
|
599
|
+
return _createClass(Immer2, [{
|
|
600
|
+
key: "createDraft",
|
|
601
|
+
value: function createDraft(base) {
|
|
602
|
+
if (!isDraftable(base)) die(8);
|
|
603
|
+
if (isDraft(base)) base = current(base);
|
|
604
|
+
var scope = enterScope(this);
|
|
605
|
+
var proxy = createProxy(base, void 0);
|
|
606
|
+
proxy[DRAFT_STATE].isManual_ = true;
|
|
607
|
+
leaveScope(scope);
|
|
608
|
+
return proxy;
|
|
609
|
+
}
|
|
610
|
+
}, {
|
|
611
|
+
key: "finishDraft",
|
|
612
|
+
value: function finishDraft(draft, patchListener) {
|
|
613
|
+
var state = draft && draft[DRAFT_STATE];
|
|
614
|
+
if (!state || !state.isManual_) die(9);
|
|
615
|
+
var scope = state.scope_;
|
|
616
|
+
usePatchesInScope(scope, patchListener);
|
|
617
|
+
return processResult(void 0, scope);
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Pass true to automatically freeze all copies created by Immer.
|
|
621
|
+
*
|
|
622
|
+
* By default, auto-freezing is enabled.
|
|
623
|
+
*/
|
|
624
|
+
}, {
|
|
625
|
+
key: "setAutoFreeze",
|
|
626
|
+
value: function setAutoFreeze(value) {
|
|
627
|
+
this.autoFreeze_ = value;
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Pass true to enable strict shallow copy.
|
|
631
|
+
*
|
|
632
|
+
* By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.
|
|
633
|
+
*/
|
|
634
|
+
}, {
|
|
635
|
+
key: "setUseStrictShallowCopy",
|
|
636
|
+
value: function setUseStrictShallowCopy(value) {
|
|
637
|
+
this.useStrictShallowCopy_ = value;
|
|
638
|
+
}
|
|
639
|
+
}, {
|
|
640
|
+
key: "applyPatches",
|
|
641
|
+
value: function applyPatches(base, patches) {
|
|
642
|
+
var i;
|
|
643
|
+
for (i = patches.length - 1; i >= 0; i--) {
|
|
644
|
+
var patch = patches[i];
|
|
645
|
+
if (patch.path.length === 0 && patch.op === "replace") {
|
|
646
|
+
base = patch.value;
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
if (i > -1) {
|
|
651
|
+
patches = patches.slice(i + 1);
|
|
652
|
+
}
|
|
653
|
+
var applyPatchesImpl = getPlugin("Patches").applyPatches_;
|
|
654
|
+
if (isDraft(base)) {
|
|
655
|
+
return applyPatchesImpl(base, patches);
|
|
656
|
+
}
|
|
657
|
+
return this.produce(base, function (draft) {
|
|
658
|
+
return applyPatchesImpl(draft, patches);
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
}]);
|
|
662
|
+
}();
|
|
663
|
+
function createProxy(value, parent) {
|
|
664
|
+
var draft = isMap(value) ? getPlugin("MapSet").proxyMap_(value, parent) : isSet(value) ? getPlugin("MapSet").proxySet_(value, parent) : createProxyProxy(value, parent);
|
|
665
|
+
var scope = parent ? parent.scope_ : getCurrentScope();
|
|
666
|
+
scope.drafts_.push(draft);
|
|
667
|
+
return draft;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// src/core/current.ts
|
|
671
|
+
function current(value) {
|
|
672
|
+
if (!isDraft(value)) die(10, value);
|
|
673
|
+
return currentImpl(value);
|
|
674
|
+
}
|
|
675
|
+
function currentImpl(value) {
|
|
676
|
+
if (!isDraftable(value) || isFrozen(value)) return value;
|
|
677
|
+
var state = value[DRAFT_STATE];
|
|
678
|
+
var copy;
|
|
679
|
+
if (state) {
|
|
680
|
+
if (!state.modified_) return state.base_;
|
|
681
|
+
state.finalized_ = true;
|
|
682
|
+
copy = shallowCopy(value, state.scope_.immer_.useStrictShallowCopy_);
|
|
683
|
+
} else {
|
|
684
|
+
copy = shallowCopy(value, true);
|
|
685
|
+
}
|
|
686
|
+
each(copy, function (key, childValue) {
|
|
687
|
+
set(copy, key, currentImpl(childValue));
|
|
688
|
+
});
|
|
689
|
+
if (state) {
|
|
690
|
+
state.finalized_ = false;
|
|
691
|
+
}
|
|
692
|
+
return copy;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
// src/plugins/patches.ts
|
|
696
|
+
function enablePatches() {
|
|
697
|
+
var errorOffset = 16;
|
|
698
|
+
if (process.env.NODE_ENV !== "production") {
|
|
699
|
+
errors.push('Sets cannot have "replace" patches.', function (op) {
|
|
700
|
+
return "Unsupported patch operation: " + op;
|
|
701
|
+
}, function (path) {
|
|
702
|
+
return "Cannot apply patch, path doesn't resolve: " + path;
|
|
703
|
+
}, "Patching reserved attributes like __proto__, prototype and constructor is not allowed");
|
|
704
|
+
}
|
|
705
|
+
var REPLACE = "replace";
|
|
706
|
+
var ADD = "add";
|
|
707
|
+
var REMOVE = "remove";
|
|
708
|
+
function generatePatches_(state, basePath, patches, inversePatches) {
|
|
709
|
+
switch (state.type_) {
|
|
710
|
+
case 0 /* Object */:
|
|
711
|
+
case 2 /* Map */:
|
|
712
|
+
return generatePatchesFromAssigned(state, basePath, patches, inversePatches);
|
|
713
|
+
case 1 /* Array */:
|
|
714
|
+
return generateArrayPatches(state, basePath, patches, inversePatches);
|
|
715
|
+
case 3 /* Set */:
|
|
716
|
+
return generateSetPatches(state, basePath, patches, inversePatches);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
function generateArrayPatches(state, basePath, patches, inversePatches) {
|
|
720
|
+
var base_ = state.base_,
|
|
721
|
+
assigned_ = state.assigned_;
|
|
722
|
+
var copy_ = state.copy_;
|
|
723
|
+
if (copy_.length < base_.length) {
|
|
724
|
+
;
|
|
725
|
+
var _ref3 = [copy_, base_];
|
|
726
|
+
base_ = _ref3[0];
|
|
727
|
+
copy_ = _ref3[1];
|
|
728
|
+
var _ref4 = [inversePatches, patches];
|
|
729
|
+
patches = _ref4[0];
|
|
730
|
+
inversePatches = _ref4[1];
|
|
731
|
+
}
|
|
732
|
+
for (var i = 0; i < base_.length; i++) {
|
|
733
|
+
if (assigned_[i] && copy_[i] !== base_[i]) {
|
|
734
|
+
var path = basePath.concat([i]);
|
|
735
|
+
patches.push({
|
|
736
|
+
op: REPLACE,
|
|
737
|
+
path: path,
|
|
738
|
+
// Need to maybe clone it, as it can in fact be the original value
|
|
739
|
+
// due to the base/copy inversion at the start of this function
|
|
740
|
+
value: clonePatchValueIfNeeded(copy_[i])
|
|
741
|
+
});
|
|
742
|
+
inversePatches.push({
|
|
743
|
+
op: REPLACE,
|
|
744
|
+
path: path,
|
|
745
|
+
value: clonePatchValueIfNeeded(base_[i])
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
for (var _i = base_.length; _i < copy_.length; _i++) {
|
|
750
|
+
var _path = basePath.concat([_i]);
|
|
751
|
+
patches.push({
|
|
752
|
+
op: ADD,
|
|
753
|
+
path: _path,
|
|
754
|
+
// Need to maybe clone it, as it can in fact be the original value
|
|
755
|
+
// due to the base/copy inversion at the start of this function
|
|
756
|
+
value: clonePatchValueIfNeeded(copy_[_i])
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
for (var _i2 = copy_.length - 1; base_.length <= _i2; --_i2) {
|
|
760
|
+
var _path2 = basePath.concat([_i2]);
|
|
761
|
+
inversePatches.push({
|
|
762
|
+
op: REMOVE,
|
|
763
|
+
path: _path2
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
function generatePatchesFromAssigned(state, basePath, patches, inversePatches) {
|
|
768
|
+
var base_ = state.base_,
|
|
769
|
+
copy_ = state.copy_;
|
|
770
|
+
each(state.assigned_, function (key, assignedValue) {
|
|
771
|
+
var origValue = get(base_, key);
|
|
772
|
+
var value = get(copy_, key);
|
|
773
|
+
var op = !assignedValue ? REMOVE : has(base_, key) ? REPLACE : ADD;
|
|
774
|
+
if (origValue === value && op === REPLACE) return;
|
|
775
|
+
var path = basePath.concat(key);
|
|
776
|
+
patches.push(op === REMOVE ? {
|
|
777
|
+
op: op,
|
|
778
|
+
path: path
|
|
779
|
+
} : {
|
|
780
|
+
op: op,
|
|
781
|
+
path: path,
|
|
782
|
+
value: value
|
|
783
|
+
});
|
|
784
|
+
inversePatches.push(op === ADD ? {
|
|
785
|
+
op: REMOVE,
|
|
786
|
+
path: path
|
|
787
|
+
} : op === REMOVE ? {
|
|
788
|
+
op: ADD,
|
|
789
|
+
path: path,
|
|
790
|
+
value: clonePatchValueIfNeeded(origValue)
|
|
791
|
+
} : {
|
|
792
|
+
op: REPLACE,
|
|
793
|
+
path: path,
|
|
794
|
+
value: clonePatchValueIfNeeded(origValue)
|
|
795
|
+
});
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
function generateSetPatches(state, basePath, patches, inversePatches) {
|
|
799
|
+
var base_ = state.base_,
|
|
800
|
+
copy_ = state.copy_;
|
|
801
|
+
var i = 0;
|
|
802
|
+
base_.forEach(function (value) {
|
|
803
|
+
if (!copy_.has(value)) {
|
|
804
|
+
var path = basePath.concat([i]);
|
|
805
|
+
patches.push({
|
|
806
|
+
op: REMOVE,
|
|
807
|
+
path: path,
|
|
808
|
+
value: value
|
|
809
|
+
});
|
|
810
|
+
inversePatches.unshift({
|
|
811
|
+
op: ADD,
|
|
812
|
+
path: path,
|
|
813
|
+
value: value
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
i++;
|
|
817
|
+
});
|
|
818
|
+
i = 0;
|
|
819
|
+
copy_.forEach(function (value) {
|
|
820
|
+
if (!base_.has(value)) {
|
|
821
|
+
var path = basePath.concat([i]);
|
|
822
|
+
patches.push({
|
|
823
|
+
op: ADD,
|
|
824
|
+
path: path,
|
|
825
|
+
value: value
|
|
826
|
+
});
|
|
827
|
+
inversePatches.unshift({
|
|
828
|
+
op: REMOVE,
|
|
829
|
+
path: path,
|
|
830
|
+
value: value
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
i++;
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
function generateReplacementPatches_(baseValue, replacement, patches, inversePatches) {
|
|
837
|
+
patches.push({
|
|
838
|
+
op: REPLACE,
|
|
839
|
+
path: [],
|
|
840
|
+
value: replacement === NOTHING ? void 0 : replacement
|
|
841
|
+
});
|
|
842
|
+
inversePatches.push({
|
|
843
|
+
op: REPLACE,
|
|
844
|
+
path: [],
|
|
845
|
+
value: baseValue
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
function applyPatches_(draft, patches) {
|
|
849
|
+
patches.forEach(function (patch) {
|
|
850
|
+
var path = patch.path,
|
|
851
|
+
op = patch.op;
|
|
852
|
+
var base = draft;
|
|
853
|
+
for (var i = 0; i < path.length - 1; i++) {
|
|
854
|
+
var parentType = getArchtype(base);
|
|
855
|
+
var p = path[i];
|
|
856
|
+
if (typeof p !== "string" && typeof p !== "number") {
|
|
857
|
+
p = "" + p;
|
|
858
|
+
}
|
|
859
|
+
if ((parentType === 0 /* Object */ || parentType === 1 /* Array */) && (p === "__proto__" || p === "constructor")) die(errorOffset + 3);
|
|
860
|
+
if (typeof base === "function" && p === "prototype") die(errorOffset + 3);
|
|
861
|
+
base = get(base, p);
|
|
862
|
+
if (_typeof(base) !== "object") die(errorOffset + 2, path.join("/"));
|
|
863
|
+
}
|
|
864
|
+
var type = getArchtype(base);
|
|
865
|
+
var value = deepClonePatchValue(patch.value);
|
|
866
|
+
var key = path[path.length - 1];
|
|
867
|
+
switch (op) {
|
|
868
|
+
case REPLACE:
|
|
869
|
+
switch (type) {
|
|
870
|
+
case 2 /* Map */:
|
|
871
|
+
return base.set(key, value);
|
|
872
|
+
case 3 /* Set */:
|
|
873
|
+
die(errorOffset);
|
|
874
|
+
default:
|
|
875
|
+
return base[key] = value;
|
|
876
|
+
}
|
|
877
|
+
case ADD:
|
|
878
|
+
switch (type) {
|
|
879
|
+
case 1 /* Array */:
|
|
880
|
+
return key === "-" ? base.push(value) : base.splice(key, 0, value);
|
|
881
|
+
case 2 /* Map */:
|
|
882
|
+
return base.set(key, value);
|
|
883
|
+
case 3 /* Set */:
|
|
884
|
+
return base.add(value);
|
|
885
|
+
default:
|
|
886
|
+
return base[key] = value;
|
|
887
|
+
}
|
|
888
|
+
case REMOVE:
|
|
889
|
+
switch (type) {
|
|
890
|
+
case 1 /* Array */:
|
|
891
|
+
return base.splice(key, 1);
|
|
892
|
+
case 2 /* Map */:
|
|
893
|
+
return base["delete"](key);
|
|
894
|
+
case 3 /* Set */:
|
|
895
|
+
return base["delete"](patch.value);
|
|
896
|
+
default:
|
|
897
|
+
return delete base[key];
|
|
898
|
+
}
|
|
899
|
+
default:
|
|
900
|
+
die(errorOffset + 1, op);
|
|
901
|
+
}
|
|
902
|
+
});
|
|
903
|
+
return draft;
|
|
904
|
+
}
|
|
905
|
+
function deepClonePatchValue(obj) {
|
|
906
|
+
if (!isDraftable(obj)) return obj;
|
|
907
|
+
if (Array.isArray(obj)) return obj.map(deepClonePatchValue);
|
|
908
|
+
if (isMap(obj)) return new Map(Array.from(obj.entries()).map(function (_ref5) {
|
|
909
|
+
var _ref6 = _slicedToArray(_ref5, 2),
|
|
910
|
+
k = _ref6[0],
|
|
911
|
+
v = _ref6[1];
|
|
912
|
+
return [k, deepClonePatchValue(v)];
|
|
913
|
+
}));
|
|
914
|
+
if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue));
|
|
915
|
+
var cloned = Object.create(_getPrototypeOf(obj));
|
|
916
|
+
for (var key in obj) cloned[key] = deepClonePatchValue(obj[key]);
|
|
917
|
+
if (has(obj, DRAFTABLE)) cloned[DRAFTABLE] = obj[DRAFTABLE];
|
|
918
|
+
return cloned;
|
|
919
|
+
}
|
|
920
|
+
function clonePatchValueIfNeeded(obj) {
|
|
921
|
+
if (isDraft(obj)) {
|
|
922
|
+
return deepClonePatchValue(obj);
|
|
923
|
+
} else return obj;
|
|
924
|
+
}
|
|
925
|
+
loadPlugin("Patches", {
|
|
926
|
+
applyPatches_: applyPatches_,
|
|
927
|
+
generatePatches_: generatePatches_,
|
|
928
|
+
generateReplacementPatches_: generateReplacementPatches_
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
// src/plugins/mapset.ts
|
|
933
|
+
function enableMapSet() {
|
|
934
|
+
var DraftMap = /*#__PURE__*/function (_Map, _ref7) {
|
|
935
|
+
function DraftMap(target, parent) {
|
|
936
|
+
var _this3;
|
|
937
|
+
_classCallCheck(this, DraftMap);
|
|
938
|
+
_this3 = _callSuper(this, DraftMap);
|
|
939
|
+
_this3[DRAFT_STATE] = {
|
|
940
|
+
type_: 2 /* Map */,
|
|
941
|
+
parent_: parent,
|
|
942
|
+
scope_: parent ? parent.scope_ : getCurrentScope(),
|
|
943
|
+
modified_: false,
|
|
944
|
+
finalized_: false,
|
|
945
|
+
copy_: void 0,
|
|
946
|
+
assigned_: void 0,
|
|
947
|
+
base_: target,
|
|
948
|
+
draft_: _this3,
|
|
949
|
+
isManual_: false,
|
|
950
|
+
revoked_: false
|
|
951
|
+
};
|
|
952
|
+
return _this3;
|
|
953
|
+
}
|
|
954
|
+
_inherits(DraftMap, _Map);
|
|
955
|
+
return _createClass(DraftMap, [{
|
|
956
|
+
key: "size",
|
|
957
|
+
get: function get() {
|
|
958
|
+
return latest(this[DRAFT_STATE]).size;
|
|
959
|
+
}
|
|
960
|
+
}, {
|
|
961
|
+
key: "has",
|
|
962
|
+
value: function has(key) {
|
|
963
|
+
return latest(this[DRAFT_STATE]).has(key);
|
|
964
|
+
}
|
|
965
|
+
}, {
|
|
966
|
+
key: "set",
|
|
967
|
+
value: function set(key, value) {
|
|
968
|
+
var state = this[DRAFT_STATE];
|
|
969
|
+
assertUnrevoked(state);
|
|
970
|
+
if (!latest(state).has(key) || latest(state).get(key) !== value) {
|
|
971
|
+
prepareMapCopy(state);
|
|
972
|
+
markChanged(state);
|
|
973
|
+
state.assigned_.set(key, true);
|
|
974
|
+
state.copy_.set(key, value);
|
|
975
|
+
state.assigned_.set(key, true);
|
|
976
|
+
}
|
|
977
|
+
return this;
|
|
978
|
+
}
|
|
979
|
+
}, {
|
|
980
|
+
key: "delete",
|
|
981
|
+
value: function _delete(key) {
|
|
982
|
+
if (!this.has(key)) {
|
|
983
|
+
return false;
|
|
984
|
+
}
|
|
985
|
+
var state = this[DRAFT_STATE];
|
|
986
|
+
assertUnrevoked(state);
|
|
987
|
+
prepareMapCopy(state);
|
|
988
|
+
markChanged(state);
|
|
989
|
+
if (state.base_.has(key)) {
|
|
990
|
+
state.assigned_.set(key, false);
|
|
991
|
+
} else {
|
|
992
|
+
state.assigned_["delete"](key);
|
|
993
|
+
}
|
|
994
|
+
state.copy_["delete"](key);
|
|
995
|
+
return true;
|
|
996
|
+
}
|
|
997
|
+
}, {
|
|
998
|
+
key: "clear",
|
|
999
|
+
value: function clear() {
|
|
1000
|
+
var state = this[DRAFT_STATE];
|
|
1001
|
+
assertUnrevoked(state);
|
|
1002
|
+
if (latest(state).size) {
|
|
1003
|
+
prepareMapCopy(state);
|
|
1004
|
+
markChanged(state);
|
|
1005
|
+
state.assigned_ = /* @__PURE__ */new Map();
|
|
1006
|
+
each(state.base_, function (key) {
|
|
1007
|
+
state.assigned_.set(key, false);
|
|
1008
|
+
});
|
|
1009
|
+
state.copy_.clear();
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
}, {
|
|
1013
|
+
key: "forEach",
|
|
1014
|
+
value: function forEach(cb, thisArg) {
|
|
1015
|
+
var _this4 = this;
|
|
1016
|
+
var state = this[DRAFT_STATE];
|
|
1017
|
+
latest(state).forEach(function (_value, key, _map) {
|
|
1018
|
+
cb.call(thisArg, _this4.get(key), key, _this4);
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
}, {
|
|
1022
|
+
key: "get",
|
|
1023
|
+
value: function get(key) {
|
|
1024
|
+
var state = this[DRAFT_STATE];
|
|
1025
|
+
assertUnrevoked(state);
|
|
1026
|
+
var value = latest(state).get(key);
|
|
1027
|
+
if (state.finalized_ || !isDraftable(value)) {
|
|
1028
|
+
return value;
|
|
1029
|
+
}
|
|
1030
|
+
if (value !== state.base_.get(key)) {
|
|
1031
|
+
return value;
|
|
1032
|
+
}
|
|
1033
|
+
var draft = createProxy(value, state);
|
|
1034
|
+
prepareMapCopy(state);
|
|
1035
|
+
state.copy_.set(key, draft);
|
|
1036
|
+
return draft;
|
|
1037
|
+
}
|
|
1038
|
+
}, {
|
|
1039
|
+
key: "keys",
|
|
1040
|
+
value: function keys() {
|
|
1041
|
+
return latest(this[DRAFT_STATE]).keys();
|
|
1042
|
+
}
|
|
1043
|
+
}, {
|
|
1044
|
+
key: "values",
|
|
1045
|
+
value: function values() {
|
|
1046
|
+
var _this5 = this;
|
|
1047
|
+
var iterator = this.keys();
|
|
1048
|
+
return _defineProperty(_defineProperty({}, Symbol.iterator, function () {
|
|
1049
|
+
return _this5.values();
|
|
1050
|
+
}), "next", function next() {
|
|
1051
|
+
var r = iterator.next();
|
|
1052
|
+
if (r.done) return r;
|
|
1053
|
+
var value = _this5.get(r.value);
|
|
1054
|
+
return {
|
|
1055
|
+
done: false,
|
|
1056
|
+
value: value
|
|
1057
|
+
};
|
|
1058
|
+
});
|
|
1059
|
+
}
|
|
1060
|
+
}, {
|
|
1061
|
+
key: "entries",
|
|
1062
|
+
value: function entries() {
|
|
1063
|
+
var _this6 = this;
|
|
1064
|
+
var iterator = this.keys();
|
|
1065
|
+
return _defineProperty(_defineProperty({}, Symbol.iterator, function () {
|
|
1066
|
+
return _this6.entries();
|
|
1067
|
+
}), "next", function next() {
|
|
1068
|
+
var r = iterator.next();
|
|
1069
|
+
if (r.done) return r;
|
|
1070
|
+
var value = _this6.get(r.value);
|
|
1071
|
+
return {
|
|
1072
|
+
done: false,
|
|
1073
|
+
value: [r.value, value]
|
|
1074
|
+
};
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
}, {
|
|
1078
|
+
key: _ref7,
|
|
1079
|
+
value: function value() {
|
|
1080
|
+
return this.entries();
|
|
1081
|
+
}
|
|
1082
|
+
}]);
|
|
1083
|
+
}(/*#__PURE__*/_wrapNativeSuper(Map), (DRAFT_STATE, Symbol.iterator));
|
|
1084
|
+
function proxyMap_(target, parent) {
|
|
1085
|
+
return new DraftMap(target, parent);
|
|
1086
|
+
}
|
|
1087
|
+
function prepareMapCopy(state) {
|
|
1088
|
+
if (!state.copy_) {
|
|
1089
|
+
state.assigned_ = /* @__PURE__ */new Map();
|
|
1090
|
+
state.copy_ = new Map(state.base_);
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
var DraftSet = /*#__PURE__*/function (_Set, _ref0) {
|
|
1094
|
+
function DraftSet(target, parent) {
|
|
1095
|
+
var _this7;
|
|
1096
|
+
_classCallCheck(this, DraftSet);
|
|
1097
|
+
_this7 = _callSuper(this, DraftSet);
|
|
1098
|
+
_this7[DRAFT_STATE] = {
|
|
1099
|
+
type_: 3 /* Set */,
|
|
1100
|
+
parent_: parent,
|
|
1101
|
+
scope_: parent ? parent.scope_ : getCurrentScope(),
|
|
1102
|
+
modified_: false,
|
|
1103
|
+
finalized_: false,
|
|
1104
|
+
copy_: void 0,
|
|
1105
|
+
base_: target,
|
|
1106
|
+
draft_: _this7,
|
|
1107
|
+
drafts_: /* @__PURE__ */new Map(),
|
|
1108
|
+
revoked_: false,
|
|
1109
|
+
isManual_: false
|
|
1110
|
+
};
|
|
1111
|
+
return _this7;
|
|
1112
|
+
}
|
|
1113
|
+
_inherits(DraftSet, _Set);
|
|
1114
|
+
return _createClass(DraftSet, [{
|
|
1115
|
+
key: "size",
|
|
1116
|
+
get: function get() {
|
|
1117
|
+
return latest(this[DRAFT_STATE]).size;
|
|
1118
|
+
}
|
|
1119
|
+
}, {
|
|
1120
|
+
key: "has",
|
|
1121
|
+
value: function has(value) {
|
|
1122
|
+
var state = this[DRAFT_STATE];
|
|
1123
|
+
assertUnrevoked(state);
|
|
1124
|
+
if (!state.copy_) {
|
|
1125
|
+
return state.base_.has(value);
|
|
1126
|
+
}
|
|
1127
|
+
if (state.copy_.has(value)) return true;
|
|
1128
|
+
if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value))) return true;
|
|
1129
|
+
return false;
|
|
1130
|
+
}
|
|
1131
|
+
}, {
|
|
1132
|
+
key: "add",
|
|
1133
|
+
value: function add(value) {
|
|
1134
|
+
var state = this[DRAFT_STATE];
|
|
1135
|
+
assertUnrevoked(state);
|
|
1136
|
+
if (!this.has(value)) {
|
|
1137
|
+
prepareSetCopy(state);
|
|
1138
|
+
markChanged(state);
|
|
1139
|
+
state.copy_.add(value);
|
|
1140
|
+
}
|
|
1141
|
+
return this;
|
|
1142
|
+
}
|
|
1143
|
+
}, {
|
|
1144
|
+
key: "delete",
|
|
1145
|
+
value: function _delete(value) {
|
|
1146
|
+
if (!this.has(value)) {
|
|
1147
|
+
return false;
|
|
1148
|
+
}
|
|
1149
|
+
var state = this[DRAFT_STATE];
|
|
1150
|
+
assertUnrevoked(state);
|
|
1151
|
+
prepareSetCopy(state);
|
|
1152
|
+
markChanged(state);
|
|
1153
|
+
return state.copy_["delete"](value) || (state.drafts_.has(value) ? state.copy_["delete"](state.drafts_.get(value)) : (/* istanbul ignore next */
|
|
1154
|
+
false));
|
|
1155
|
+
}
|
|
1156
|
+
}, {
|
|
1157
|
+
key: "clear",
|
|
1158
|
+
value: function clear() {
|
|
1159
|
+
var state = this[DRAFT_STATE];
|
|
1160
|
+
assertUnrevoked(state);
|
|
1161
|
+
if (latest(state).size) {
|
|
1162
|
+
prepareSetCopy(state);
|
|
1163
|
+
markChanged(state);
|
|
1164
|
+
state.copy_.clear();
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
}, {
|
|
1168
|
+
key: "values",
|
|
1169
|
+
value: function values() {
|
|
1170
|
+
var state = this[DRAFT_STATE];
|
|
1171
|
+
assertUnrevoked(state);
|
|
1172
|
+
prepareSetCopy(state);
|
|
1173
|
+
return state.copy_.values();
|
|
1174
|
+
}
|
|
1175
|
+
}, {
|
|
1176
|
+
key: "entries",
|
|
1177
|
+
value: function entries() {
|
|
1178
|
+
var state = this[DRAFT_STATE];
|
|
1179
|
+
assertUnrevoked(state);
|
|
1180
|
+
prepareSetCopy(state);
|
|
1181
|
+
return state.copy_.entries();
|
|
1182
|
+
}
|
|
1183
|
+
}, {
|
|
1184
|
+
key: "keys",
|
|
1185
|
+
value: function keys() {
|
|
1186
|
+
return this.values();
|
|
1187
|
+
}
|
|
1188
|
+
}, {
|
|
1189
|
+
key: _ref0,
|
|
1190
|
+
value: function value() {
|
|
1191
|
+
return this.values();
|
|
1192
|
+
}
|
|
1193
|
+
}, {
|
|
1194
|
+
key: "forEach",
|
|
1195
|
+
value: function forEach(cb, thisArg) {
|
|
1196
|
+
var iterator = this.values();
|
|
1197
|
+
var result = iterator.next();
|
|
1198
|
+
while (!result.done) {
|
|
1199
|
+
cb.call(thisArg, result.value, result.value, this);
|
|
1200
|
+
result = iterator.next();
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
}]);
|
|
1204
|
+
}(/*#__PURE__*/_wrapNativeSuper(Set), (DRAFT_STATE, Symbol.iterator));
|
|
1205
|
+
function proxySet_(target, parent) {
|
|
1206
|
+
return new DraftSet(target, parent);
|
|
1207
|
+
}
|
|
1208
|
+
function prepareSetCopy(state) {
|
|
1209
|
+
if (!state.copy_) {
|
|
1210
|
+
state.copy_ = /* @__PURE__ */new Set();
|
|
1211
|
+
state.base_.forEach(function (value) {
|
|
1212
|
+
if (isDraftable(value)) {
|
|
1213
|
+
var draft = createProxy(value, state);
|
|
1214
|
+
state.drafts_.set(value, draft);
|
|
1215
|
+
state.copy_.add(draft);
|
|
1216
|
+
} else {
|
|
1217
|
+
state.copy_.add(value);
|
|
1218
|
+
}
|
|
1219
|
+
});
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
function assertUnrevoked(state) {
|
|
1223
|
+
if (state.revoked_) die(3, JSON.stringify(latest(state)));
|
|
1224
|
+
}
|
|
1225
|
+
loadPlugin("MapSet", {
|
|
1226
|
+
proxyMap_: proxyMap_,
|
|
1227
|
+
proxySet_: proxySet_
|
|
1228
|
+
});
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
// src/immer.ts
|
|
1232
|
+
var immer = new Immer2();
|
|
1233
|
+
var produce = immer.produce;
|
|
1234
|
+
var produceWithPatches = immer.produceWithPatches.bind(immer);
|
|
1235
|
+
var setAutoFreeze = immer.setAutoFreeze.bind(immer);
|
|
1236
|
+
var setUseStrictShallowCopy = immer.setUseStrictShallowCopy.bind(immer);
|
|
1237
|
+
var applyPatches = immer.applyPatches.bind(immer);
|
|
1238
|
+
var createDraft = immer.createDraft.bind(immer);
|
|
1239
|
+
var finishDraft = immer.finishDraft.bind(immer);
|
|
1240
|
+
function castDraft(value) {
|
|
1241
|
+
return value;
|
|
1242
|
+
}
|
|
1243
|
+
function castImmutable(value) {
|
|
1244
|
+
return value;
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
enablePatches();
|
|
1248
|
+
var ReactiveState = /*#__PURE__*/function () {
|
|
1249
|
+
function ReactiveState() {
|
|
1250
|
+
_classCallCheck(this, ReactiveState);
|
|
1251
|
+
_defineProperty(this, "subscribers", /* @__PURE__ */new Set());
|
|
1252
|
+
_defineProperty(this, "pendingChanges", []);
|
|
1253
|
+
_defineProperty(this, "notificationScheduled", false);
|
|
1254
|
+
_defineProperty(this, "pathSubscribers", /* @__PURE__ */new Map());
|
|
1255
|
+
}
|
|
1256
|
+
return _createClass(ReactiveState, [{
|
|
1257
|
+
key: "initialize",
|
|
1258
|
+
value: function initialize(initialState) {
|
|
1259
|
+
this.currentState = Object.freeze(initialState);
|
|
1260
|
+
}
|
|
1261
|
+
}, {
|
|
1262
|
+
key: "setState",
|
|
1263
|
+
value: function setState(updater, paths) {
|
|
1264
|
+
var _produceWithPatches = produceWithPatches(this.currentState, updater),
|
|
1265
|
+
_produceWithPatches2 = _slicedToArray(_produceWithPatches, 2),
|
|
1266
|
+
nextState = _produceWithPatches2[0],
|
|
1267
|
+
patches = _produceWithPatches2[1];
|
|
1268
|
+
var changes = paths || patches.filter(function (p) {
|
|
1269
|
+
return ["replace", "add", "remove"].includes(p.op);
|
|
1270
|
+
}).map(function (p) {
|
|
1271
|
+
return p.path.join(".");
|
|
1272
|
+
});
|
|
1273
|
+
if (changes.length > 0) {
|
|
1274
|
+
var _this$pendingChanges;
|
|
1275
|
+
(_this$pendingChanges = this.pendingChanges).push.apply(_this$pendingChanges, _toConsumableArray(changes));
|
|
1276
|
+
this.currentState = Object.freeze(nextState);
|
|
1277
|
+
this.scheduleNotification();
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
}, {
|
|
1281
|
+
key: "getState",
|
|
1282
|
+
value: function getState() {
|
|
1283
|
+
var cloned = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
1284
|
+
return cloned ? structuredClone(this.currentState) : this.currentState;
|
|
1285
|
+
}
|
|
1286
|
+
}, {
|
|
1287
|
+
key: "subscribe",
|
|
1288
|
+
value: function subscribe(subscriber, paths) {
|
|
1289
|
+
var _this = this;
|
|
1290
|
+
var subscription = {
|
|
1291
|
+
handler: subscriber,
|
|
1292
|
+
paths: paths
|
|
1293
|
+
};
|
|
1294
|
+
this.subscribers.add(subscription);
|
|
1295
|
+
paths === null || paths === void 0 || paths.forEach(function (path) {
|
|
1296
|
+
var _this$pathSubscribers;
|
|
1297
|
+
if (!_this.pathSubscribers.has(path)) {
|
|
1298
|
+
_this.pathSubscribers.set(path, /* @__PURE__ */new Set());
|
|
1299
|
+
}
|
|
1300
|
+
(_this$pathSubscribers = _this.pathSubscribers.get(path)) === null || _this$pathSubscribers === void 0 || _this$pathSubscribers.add(subscription);
|
|
1301
|
+
});
|
|
1302
|
+
return function () {
|
|
1303
|
+
_this.subscribers["delete"](subscription);
|
|
1304
|
+
paths === null || paths === void 0 || paths.forEach(function (path) {
|
|
1305
|
+
var _this$pathSubscribers2;
|
|
1306
|
+
(_this$pathSubscribers2 = _this.pathSubscribers.get(path)) === null || _this$pathSubscribers2 === void 0 || _this$pathSubscribers2["delete"](subscription);
|
|
1307
|
+
});
|
|
1308
|
+
};
|
|
1309
|
+
}
|
|
1310
|
+
}, {
|
|
1311
|
+
key: "scheduleNotification",
|
|
1312
|
+
value: function scheduleNotification() {
|
|
1313
|
+
var _this2 = this;
|
|
1314
|
+
if (this.notificationScheduled) return;
|
|
1315
|
+
this.notificationScheduled = true;
|
|
1316
|
+
queueMicrotask(function () {
|
|
1317
|
+
var changedPaths = _toConsumableArray(new Set(_this2.pendingChanges));
|
|
1318
|
+
_this2.pendingChanges = [];
|
|
1319
|
+
_this2.notificationScheduled = false;
|
|
1320
|
+
var frozenState = Object.freeze(_this2.currentState);
|
|
1321
|
+
var frozenPaths = Object.freeze(changedPaths);
|
|
1322
|
+
_this2.subscribers.forEach(function (_ref) {
|
|
1323
|
+
var handler = _ref.handler,
|
|
1324
|
+
paths = _ref.paths;
|
|
1325
|
+
try {
|
|
1326
|
+
if (!paths || frozenPaths.some(function (p) {
|
|
1327
|
+
return paths.some(function (target) {
|
|
1328
|
+
var targetParts = target.split(".");
|
|
1329
|
+
var pathParts = p.split(".");
|
|
1330
|
+
return targetParts.every(function (part, i) {
|
|
1331
|
+
return pathParts[i] === part;
|
|
1332
|
+
});
|
|
1333
|
+
});
|
|
1334
|
+
})) {
|
|
1335
|
+
handler(frozenState, frozenPaths);
|
|
1336
|
+
}
|
|
1337
|
+
} catch (error) {
|
|
1338
|
+
console.error("Subscriber error:", error);
|
|
1339
|
+
}
|
|
1340
|
+
});
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
}, {
|
|
1344
|
+
key: "debug",
|
|
1345
|
+
value: function debug() {
|
|
1346
|
+
var label = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "State";
|
|
1347
|
+
this.subscribe(function (state, paths) {
|
|
1348
|
+
console.groupCollapsed("%c".concat(label, " Update"), "color: #4CAF50; font-weight: bold;");
|
|
1349
|
+
console.log("Changed Paths:", paths);
|
|
1350
|
+
console.log("New State:", state);
|
|
1351
|
+
console.groupEnd();
|
|
1352
|
+
});
|
|
1353
|
+
return this;
|
|
1354
|
+
}
|
|
1355
|
+
}]);
|
|
1356
|
+
}();
|
|
1357
|
+
|
|
1358
|
+
export { ReactiveState as default };
|
|
1359
|
+
//# sourceMappingURL=reactiveState.js.map
|