Skip to content

Commit 7a9a3c1

Browse files
committed
refactor: cache
1 parent 027de10 commit 7a9a3c1

File tree

5 files changed

+70
-107
lines changed

5 files changed

+70
-107
lines changed

dist/vue-create-api.esm.js

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@ function parseEvents(events) {
129129
var instances = [];
130130

131131
function add(component) {
132+
var ins = void 0;
132133
var alreadyIn = false;
133-
for (var i = 0; i < instances.length; i += 1) {
134-
var ins = instances[i];
134+
var len = instances.length;
135+
for (var i = 0; i < len; i += 1) {
136+
ins = instances[i];
135137
if (ins === component) {
136138
alreadyIn = true;
137139
break;
@@ -143,11 +145,12 @@ function add(component) {
143145
}
144146

145147
function remove(component) {
146-
for (var i = 0; i < instances.length; i += 1) {
147-
var ins = instances[i];
148+
var ins = void 0;
149+
var len = instances.length;
150+
for (var i = 0; i < len; i += 1) {
151+
ins = instances[i];
148152
if (ins === component) {
149153
instances.splice(i, 1);
150-
return;
151154
}
152155
}
153156
}
@@ -159,21 +162,11 @@ function batchDestroy(filter) {
159162
if (!isArray(_instances)) {
160163
return;
161164
}
162-
if (hasFilter) {
163-
_instances.forEach(function (ins) {
164-
if (ins && isFunction(ins.remove)) {
165-
ins.remove();
166-
remove(ins);
167-
}
168-
});
169-
} else {
170-
_instances.forEach(function (ins) {
171-
if (ins && isFunction(ins.remove)) {
172-
ins.remove();
173-
}
174-
});
175-
instances.length = 0;
176-
}
165+
_instances.forEach(function (ins) {
166+
if (ins && isFunction(ins.remove)) {
167+
ins.remove();
168+
}
169+
});
177170
}
178171

179172
var eventBeforeDestroy = 'hook:beforeDestroy';
@@ -186,7 +179,7 @@ function apiCreator(Component) {
186179
var singleMap = {};
187180
var beforeHooks = [];
188181

189-
function createComponent(renderData, renderFn, options, single) {
182+
function createComponent(renderData, renderFn, options, single, ownerInstance) {
190183
beforeHooks.forEach(function (before) {
191184
before(renderData, renderFn, single);
192185
});
@@ -204,8 +197,12 @@ function apiCreator(Component) {
204197
var component = instantiateComponent(Vue, Component, renderData, renderFn, options);
205198
var instance = component.$parent;
206199
var originRemove = component.remove;
200+
var isInVueInstance = !!ownerInstance.$on;
207201

208202
component.remove = function () {
203+
if (isInVueInstance) {
204+
cancelWatchProps(ownerInstance);
205+
}
209206
if (single) {
210207
if (!singleMap[ownerInsUid]) {
211208
return;
@@ -214,6 +211,7 @@ function apiCreator(Component) {
214211
}
215212
originRemove && originRemove.apply(this, arguments);
216213
instance.destroy();
214+
remove(component);
217215
};
218216

219217
var originShow = component.show;
@@ -338,25 +336,14 @@ function apiCreator(Component) {
338336
processEvents(renderData, ownerInstance);
339337
process$(renderData);
340338

341-
var _ref2 = singleMap[options.parent ? options.parent._uid : -1] || {},
342-
comp = _ref2.comp;
343-
344-
var firstCreation = !_single || !comp; // 非单例,或者单例第一次创建
345-
346-
component = createComponent(renderData, renderFn, options, _single);
347-
348-
function beforeDestroy() {
349-
cancelWatchProps(ownerInstance);
350-
component.remove();
351-
component = null;
352-
}
339+
component = createComponent(renderData, renderFn, options, _single, ownerInstance);
353340

354341
if (isInVueInstance) {
355-
ownerInstance.$on(eventBeforeDestroy, beforeDestroy);
356-
} else if (firstCreation) {
357-
add(component);
342+
ownerInstance.$on(eventBeforeDestroy, component.remove.bind(component));
358343
}
359344

345+
add(component);
346+
360347
return component;
361348
}
362349
};

dist/vue-create-api.js

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@
135135
var instances = [];
136136

137137
function add(component) {
138+
var ins = void 0;
138139
var alreadyIn = false;
139-
for (var i = 0; i < instances.length; i += 1) {
140-
var ins = instances[i];
140+
var len = instances.length;
141+
for (var i = 0; i < len; i += 1) {
142+
ins = instances[i];
141143
if (ins === component) {
142144
alreadyIn = true;
143145
break;
@@ -149,11 +151,12 @@
149151
}
150152

151153
function remove(component) {
152-
for (var i = 0; i < instances.length; i += 1) {
153-
var ins = instances[i];
154+
var ins = void 0;
155+
var len = instances.length;
156+
for (var i = 0; i < len; i += 1) {
157+
ins = instances[i];
154158
if (ins === component) {
155159
instances.splice(i, 1);
156-
return;
157160
}
158161
}
159162
}
@@ -165,21 +168,11 @@
165168
if (!isArray(_instances)) {
166169
return;
167170
}
168-
if (hasFilter) {
169-
_instances.forEach(function (ins) {
170-
if (ins && isFunction(ins.remove)) {
171-
ins.remove();
172-
remove(ins);
173-
}
174-
});
175-
} else {
176-
_instances.forEach(function (ins) {
177-
if (ins && isFunction(ins.remove)) {
178-
ins.remove();
179-
}
180-
});
181-
instances.length = 0;
182-
}
171+
_instances.forEach(function (ins) {
172+
if (ins && isFunction(ins.remove)) {
173+
ins.remove();
174+
}
175+
});
183176
}
184177

185178
var eventBeforeDestroy = 'hook:beforeDestroy';
@@ -192,7 +185,7 @@
192185
var singleMap = {};
193186
var beforeHooks = [];
194187

195-
function createComponent(renderData, renderFn, options, single) {
188+
function createComponent(renderData, renderFn, options, single, ownerInstance) {
196189
beforeHooks.forEach(function (before) {
197190
before(renderData, renderFn, single);
198191
});
@@ -210,8 +203,12 @@
210203
var component = instantiateComponent(Vue, Component, renderData, renderFn, options);
211204
var instance = component.$parent;
212205
var originRemove = component.remove;
206+
var isInVueInstance = !!ownerInstance.$on;
213207

214208
component.remove = function () {
209+
if (isInVueInstance) {
210+
cancelWatchProps(ownerInstance);
211+
}
215212
if (single) {
216213
if (!singleMap[ownerInsUid]) {
217214
return;
@@ -220,6 +217,7 @@
220217
}
221218
originRemove && originRemove.apply(this, arguments);
222219
instance.destroy();
220+
remove(component);
223221
};
224222

225223
var originShow = component.show;
@@ -344,25 +342,14 @@
344342
processEvents(renderData, ownerInstance);
345343
process$(renderData);
346344

347-
var _ref2 = singleMap[options.parent ? options.parent._uid : -1] || {},
348-
comp = _ref2.comp;
349-
350-
var firstCreation = !_single || !comp; // 非单例,或者单例第一次创建
351-
352-
component = createComponent(renderData, renderFn, options, _single);
353-
354-
function beforeDestroy() {
355-
cancelWatchProps(ownerInstance);
356-
component.remove();
357-
component = null;
358-
}
345+
component = createComponent(renderData, renderFn, options, _single, ownerInstance);
359346

360347
if (isInVueInstance) {
361-
ownerInstance.$on(eventBeforeDestroy, beforeDestroy);
362-
} else if (firstCreation) {
363-
add(component);
348+
ownerInstance.$on(eventBeforeDestroy, component.remove.bind(component));
364349
}
365350

351+
add(component);
352+
366353
return component;
367354
}
368355
};

dist/vue-create-api.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cache.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { isFunction, isArray } from './util'
33
const instances = []
44

55
export function add(component) {
6+
let ins
67
let alreadyIn = false
7-
for (let i = 0; i < instances.length; i += 1) {
8-
const ins = instances[i]
8+
const len = instances.length
9+
for (let i = 0; i < len; i += 1) {
10+
ins = instances[i]
911
if (ins === component) {
1012
alreadyIn = true
1113
break
@@ -17,11 +19,12 @@ export function add(component) {
1719
}
1820

1921
export function remove(component) {
20-
for (let i = 0; i < instances.length; i += 1) {
21-
const ins = instances[i]
22+
let ins
23+
const len = instances.length
24+
for (let i = 0; i < len; i += 1) {
25+
ins = instances[i]
2226
if (ins === component) {
2327
instances.splice(i, 1)
24-
return
2528
}
2629
}
2730
}
@@ -33,19 +36,9 @@ export function batchDestroy(filter) {
3336
if (!isArray(_instances)) {
3437
return
3538
}
36-
if (hasFilter) {
37-
_instances.forEach(ins => {
38-
if (ins && isFunction(ins.remove)) {
39-
ins.remove()
40-
remove(ins)
41-
}
42-
})
43-
} else {
44-
_instances.forEach(ins => {
45-
if (ins && isFunction(ins.remove)) {
46-
ins.remove()
47-
}
48-
})
49-
instances.length = 0
50-
}
39+
_instances.forEach(ins => {
40+
if (ins && isFunction(ins.remove)) {
41+
ins.remove()
42+
}
43+
})
5144
}

src/creator.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default function apiCreator(Component, events = [], single = false) {
1010
let singleMap = {}
1111
const beforeHooks = []
1212

13-
function createComponent(renderData, renderFn, options, single) {
13+
function createComponent(renderData, renderFn, options, single, ownerInstance) {
1414
beforeHooks.forEach((before) => {
1515
before(renderData, renderFn, single)
1616
})
@@ -24,8 +24,12 @@ export default function apiCreator(Component, events = [], single = false) {
2424
const component = instantiateComponent(Vue, Component, renderData, renderFn, options)
2525
const instance = component.$parent
2626
const originRemove = component.remove
27+
const isInVueInstance = !!ownerInstance.$on
2728

2829
component.remove = function () {
30+
if (isInVueInstance) {
31+
cancelWatchProps(ownerInstance)
32+
}
2933
if (single) {
3034
if (!singleMap[ownerInsUid]) {
3135
return
@@ -34,6 +38,7 @@ export default function apiCreator(Component, events = [], single = false) {
3438
}
3539
originRemove && originRemove.apply(this, arguments)
3640
instance.destroy()
41+
cache.remove(component)
3742
}
3843

3944
const originShow = component.show
@@ -159,23 +164,14 @@ export default function apiCreator(Component, events = [], single = false) {
159164
processEvents(renderData, ownerInstance)
160165
process$(renderData)
161166

162-
const { comp } = singleMap[options.parent ? options.parent._uid : -1] || {}
163-
const firstCreation = !_single || !comp // 非单例,或者单例第一次创建
164-
165-
component = createComponent(renderData, renderFn, options, _single)
166-
167-
function beforeDestroy() {
168-
cancelWatchProps(ownerInstance)
169-
component.remove()
170-
component = null
171-
}
167+
component = createComponent(renderData, renderFn, options, _single, ownerInstance)
172168

173169
if (isInVueInstance) {
174-
ownerInstance.$on(eventBeforeDestroy, beforeDestroy)
175-
} else if (firstCreation) {
176-
cache.add(component)
170+
ownerInstance.$on(eventBeforeDestroy, component.remove.bind(component))
177171
}
178172

173+
cache.add(component)
174+
179175
return component
180176
}
181177
}

0 commit comments

Comments
 (0)