@varlet/use 3.16.1 → 3.17.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.
package/lib/index.cjs CHANGED
@@ -144,7 +144,17 @@ function useChildren(key) {
144
144
  const length = (0, vue.computed)(() => childInstances.length);
145
145
  const sortInstances = () => {
146
146
  const vNodes = flatVNodes(parentInstance.subTree);
147
- childInstances.sort((a, b) => vNodes.indexOf(a.vnode) - vNodes.indexOf(b.vnode));
147
+ const pairs = childInstances.map((instance, index) => ({
148
+ instance,
149
+ provider: childProviders[index]
150
+ }));
151
+ const getVNodeIndex = (instance) => {
152
+ const index = vNodes.indexOf(instance.vnode);
153
+ return index === -1 ? Number.MAX_SAFE_INTEGER : index;
154
+ };
155
+ pairs.sort((a, b) => getVNodeIndex(a.instance) - getVNodeIndex(b.instance));
156
+ childInstances.splice(0, childInstances.length, ...pairs.map(({ instance }) => instance));
157
+ childProviders.splice(0, childProviders.length, ...pairs.map(({ provider }) => provider));
148
158
  };
149
159
  const collect = (childInstance, childProvider) => {
150
160
  childInstances.push(childInstance);
@@ -163,8 +173,10 @@ function useChildren(key) {
163
173
  ...parentProvider
164
174
  });
165
175
  };
176
+ (0, vue.onUpdated)(sortInstances);
166
177
  return {
167
178
  length,
179
+ childInstances,
168
180
  childProviders,
169
181
  bindChildren
170
182
  };
package/lib/index.d.cts CHANGED
@@ -29,6 +29,7 @@ interface UseChildrenBaseProvider<C> {
29
29
  }
30
30
  declare function useChildren<P, C>(key: symbol | string): {
31
31
  length: ComputedRef<number>;
32
+ childInstances: ComponentInternalInstance[];
32
33
  childProviders: C[];
33
34
  bindChildren: (parentProvider: P) => void;
34
35
  };
package/lib/index.d.ts CHANGED
@@ -29,6 +29,7 @@ interface UseChildrenBaseProvider<C> {
29
29
  }
30
30
  declare function useChildren<P, C>(key: symbol | string): {
31
31
  length: ComputedRef<number>;
32
+ childInstances: ComponentInternalInstance[];
32
33
  childProviders: C[];
33
34
  bindChildren: (parentProvider: P) => void;
34
35
  };
package/lib/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { call, getScrollTop, inBrowser, isArray, isFunction, kebabCase, motion, removeItem } from "@varlet/shared";
2
- import { computed, getCurrentInstance, inject, isRef, isVNode, nextTick, onActivated, onBeforeUnmount, onDeactivated, onMounted, onUnmounted, provide, reactive, ref, unref, watch } from "vue";
2
+ import { computed, getCurrentInstance, inject, isRef, isVNode, nextTick, onActivated, onBeforeUnmount, onDeactivated, onMounted, onUnmounted, onUpdated, provide, reactive, ref, unref, watch } from "vue";
3
3
  //#region src/onSmartMounted.ts
4
4
  function onSmartMounted(hook) {
5
5
  let isMounted = false;
@@ -143,7 +143,17 @@ function useChildren(key) {
143
143
  const length = computed(() => childInstances.length);
144
144
  const sortInstances = () => {
145
145
  const vNodes = flatVNodes(parentInstance.subTree);
146
- childInstances.sort((a, b) => vNodes.indexOf(a.vnode) - vNodes.indexOf(b.vnode));
146
+ const pairs = childInstances.map((instance, index) => ({
147
+ instance,
148
+ provider: childProviders[index]
149
+ }));
150
+ const getVNodeIndex = (instance) => {
151
+ const index = vNodes.indexOf(instance.vnode);
152
+ return index === -1 ? Number.MAX_SAFE_INTEGER : index;
153
+ };
154
+ pairs.sort((a, b) => getVNodeIndex(a.instance) - getVNodeIndex(b.instance));
155
+ childInstances.splice(0, childInstances.length, ...pairs.map(({ instance }) => instance));
156
+ childProviders.splice(0, childProviders.length, ...pairs.map(({ provider }) => provider));
147
157
  };
148
158
  const collect = (childInstance, childProvider) => {
149
159
  childInstances.push(childInstance);
@@ -162,8 +172,10 @@ function useChildren(key) {
162
172
  ...parentProvider
163
173
  });
164
174
  };
175
+ onUpdated(sortInstances);
165
176
  return {
166
177
  length,
178
+ childInstances,
167
179
  childProviders,
168
180
  bindChildren
169
181
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/use",
3
- "version": "3.16.1",
3
+ "version": "3.17.0",
4
4
  "description": "composable utils of varlet",
5
5
  "keywords": [
6
6
  "composable",
@@ -32,7 +32,7 @@
32
32
  "lib"
33
33
  ],
34
34
  "dependencies": {
35
- "@varlet/shared": "3.16.1"
35
+ "@varlet/shared": "3.17.0"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/node": "^20.19.0",