|
2 | 2 |
|
3 | 3 | import { STATE_SYMBOL } from '../constants.js';
|
4 | 4 | import { untrack } from '../runtime.js';
|
5 |
| -import { get_descriptors } from '../utils.js'; |
6 | 5 |
|
7 | 6 | /** @type {Record<string, Array<{ start: Location, end: Location, component: Function }>>} */
|
8 | 7 | const boundaries = {};
|
@@ -92,107 +91,49 @@ export function mark_module_end() {
|
92 | 91 | }
|
93 | 92 | }
|
94 | 93 |
|
95 |
| -let add_owner_visited = new Set(); |
96 |
| - |
97 | 94 | /**
|
98 | 95 | *
|
99 | 96 | * @param {any} object
|
100 | 97 | * @param {any} owner
|
101 | 98 | */
|
102 | 99 | export function add_owner(object, owner) {
|
103 |
| - // Needed because ownership addition can invoke getters on a proxy, |
104 |
| - // calling add_owner anew, so just keeping the set as part of |
105 |
| - // add_owner_to_object would not be enough. |
106 |
| - const prev = add_owner_visited; |
107 |
| - try { |
108 |
| - add_owner_visited = new Set(add_owner_visited); |
109 |
| - untrack(() => { |
110 |
| - add_owner_to_object(object, owner, add_owner_visited); |
111 |
| - }); |
112 |
| - } finally { |
113 |
| - add_owner_visited = prev; |
114 |
| - } |
| 100 | + untrack(() => { |
| 101 | + add_owner_to_object(object, owner); |
| 102 | + }); |
115 | 103 | }
|
116 | 104 |
|
117 | 105 | /**
|
118 | 106 | * @param {any} object
|
119 | 107 | * @param {Function} owner
|
120 |
| - * @param {Set<any>} visited |
121 | 108 | */
|
122 |
| -function add_owner_to_object(object, owner, visited) { |
123 |
| - if (visited.has(object)) return; |
124 |
| - visited.add(object); |
125 |
| - |
| 109 | +function add_owner_to_object(object, owner) { |
126 | 110 | if (object?.[STATE_SYMBOL]?.o && !object[STATE_SYMBOL].o.has(owner)) {
|
127 | 111 | object[STATE_SYMBOL].o.add(owner);
|
| 112 | + |
| 113 | + for (const key in object) { |
| 114 | + add_owner_to_object(object[key], owner); |
| 115 | + } |
128 | 116 | }
|
129 |
| - // Not inside previous if-block; there could be normal objects in-between |
130 |
| - traverse_for_owners(object, (nested) => add_owner_to_object(nested, owner, visited)); |
131 | 117 | }
|
132 | 118 |
|
133 |
| -let strip_owner_visited = new Set(); |
134 |
| - |
135 | 119 | /**
|
136 | 120 | * @param {any} object
|
137 | 121 | */
|
138 | 122 | export function strip_owner(object) {
|
139 |
| - // Needed because ownership stripping can invoke getters on a proxy, |
140 |
| - // calling strip_owner anew, so just keeping the set as part of |
141 |
| - // strip_owner_from_object would not be enough. |
142 |
| - const prev = strip_owner_visited; |
143 |
| - try { |
144 |
| - untrack(() => { |
145 |
| - strip_owner_from_object(object, strip_owner_visited); |
146 |
| - }); |
147 |
| - } finally { |
148 |
| - strip_owner_visited = prev; |
149 |
| - } |
| 123 | + untrack(() => { |
| 124 | + strip_owner_from_object(object); |
| 125 | + }); |
150 | 126 | }
|
151 | 127 |
|
152 | 128 | /**
|
153 | 129 | * @param {any} object
|
154 |
| - * @param {Set<any>} visited |
155 | 130 | */
|
156 |
| -function strip_owner_from_object(object, visited) { |
157 |
| - if (visited.has(object)) return; |
158 |
| - visited.add(object); |
159 |
| - |
| 131 | +function strip_owner_from_object(object) { |
160 | 132 | if (object?.[STATE_SYMBOL]?.o) {
|
161 | 133 | object[STATE_SYMBOL].o = null;
|
162 |
| - } |
163 |
| - // Not inside previous if-block; there could be normal objects in-between |
164 |
| - traverse_for_owners(object, (nested) => strip_owner_from_object(nested, visited)); |
165 |
| -} |
166 | 134 |
|
167 |
| -/** |
168 |
| - * @param {any} object |
169 |
| - * @param {(obj: any) => void} cb |
170 |
| - */ |
171 |
| -function traverse_for_owners(object, cb) { |
172 |
| - if (typeof object === 'object' && object !== null && !(object instanceof EventTarget)) { |
173 | 135 | for (const key in object) {
|
174 |
| - cb(object[key]); |
175 |
| - } |
176 |
| - // deal with state on classes |
177 |
| - const proto = Object.getPrototypeOf(object); |
178 |
| - if ( |
179 |
| - proto !== Object.prototype && |
180 |
| - proto !== Array.prototype && |
181 |
| - proto !== Map.prototype && |
182 |
| - proto !== Set.prototype && |
183 |
| - proto !== Date.prototype |
184 |
| - ) { |
185 |
| - const descriptors = get_descriptors(proto); |
186 |
| - for (let key in descriptors) { |
187 |
| - const get = descriptors[key].get; |
188 |
| - if (get) { |
189 |
| - try { |
190 |
| - cb(object[key]); |
191 |
| - } catch (e) { |
192 |
| - // continue |
193 |
| - } |
194 |
| - } |
195 |
| - } |
| 136 | + strip_owner(object[key]); |
196 | 137 | }
|
197 | 138 | }
|
198 | 139 | }
|
|
0 commit comments