Skip to content

Commit 4900175

Browse files
authored
Merge pull request #2 from leynier/feat/wrapped-all-promises-functions-recursively-recursively-to-the-root-prototype
feat: wrapped all promises functions recursively recursively to the root prototype
2 parents 138cd1d + 29e142b commit 4900175

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nuxt-use-async-data-wrapper",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "A utility to wrap Promise-returning functions with useAsyncData for Nuxt",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

readme.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,26 @@ import { ref } from 'vue';
8888
const id = ref(1);
8989
9090
// Function without arguments
91-
const { data: dataList, pending: listPending, error: listError } = wrappedService.fetchData();
91+
const { data: dataList, status: listStatus, error: listError } = wrappedService.fetchData();
9292
9393
// Function with arguments
94-
const { data: itemData, pending: itemPending, error: itemError } = wrappedService.getItem(() => [id.value]);
94+
const { data: itemData, status: itemStatus, error: itemError } = wrappedService.getItem(() => [id.value]);
9595
9696
// Reactivity: when id.value changes, itemData updates automatically
9797
</script>
9898
9999
<template>
100100
<div>
101101
<h1>Data List</h1>
102-
<div v-if="listPending">Loading...</div>
102+
<div v-if="listStatus === 'pending'">Loading...</div>
103103
<div v-else-if="listError">Error: {{ listError.message }}</div>
104104
<div v-else>
105105
<pre>{{ dataList }}</pre>
106106
</div>
107107
108108
<h1>Item Data (ID: {{ id }})</h1>
109109
<input v-model="id" type="number" min="1" />
110-
<div v-if="itemPending">Loading...</div>
110+
<div v-if="itemStatus === 'pending'">Loading...</div>
111111
<div v-else-if="itemError">Error: {{ itemError.message }}</div>
112112
<div v-else>
113113
<pre>{{ itemData }}</pre>
@@ -160,7 +160,7 @@ async function fetchData() {
160160
const wrappedService = useAsyncDataWrapper({ fetchData });
161161

162162
// Use in a component
163-
const { data, pending, error } = wrappedService.fetchData();
163+
const { data, status, error } = wrappedService.fetchData();
164164
```
165165

166166
### Function With Arguments
@@ -179,7 +179,7 @@ import { ref } from 'vue';
179179

180180
const id = ref(1);
181181

182-
const { data, pending, error } = wrappedService.getItem(() => [id.value]);
182+
const { data, status, error } = wrappedService.getItem(() => [id.value]);
183183

184184
// When id.value changes, data is automatically refreshed
185185
```
@@ -193,7 +193,7 @@ You can pass options to `useAsyncData` through the wrapped functions to control
193193
### Example with Options
194194

195195
```typescript
196-
const { data, pending, error } = wrappedService.fetchData({
196+
const { data, status, error } = wrappedService.fetchData({
197197
lazy: true,
198198
server: false,
199199
default: () => [],

src/index.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ export type AsyncDataWrapper<T> = {
2626
? /**
2727
* Functions without arguments.
2828
* @param options - Optional AsyncDataOptions to configure useAsyncData.
29-
* @returns AsyncDataResult containing the data, pending state, and error.
29+
* @returns AsyncDataResult containing the data, status state, and error.
3030
*/
3131
(options?: AsyncDataOptions<R>) => AsyncDataResult<R>
3232
: /**
3333
* Functions with arguments.
3434
* @param argsSupplier - A function that returns the arguments array for the original function.
3535
* @param options - Optional AsyncDataOptions to configure useAsyncData.
36-
* @returns AsyncDataResult containing the data, pending state, and error.
36+
* @returns AsyncDataResult containing the data, status state, and error.
3737
*/
3838
(
3939
argsSupplier: () => Args,
@@ -65,12 +65,21 @@ export function useAsyncDataWrapper<T extends Record<string, any>>(
6565
obj: T,
6666
): AsyncDataWrapper<T> {
6767
const composable = {} as AsyncDataWrapper<T>;
68-
const proto = Object.getPrototypeOf(obj);
6968

70-
// Get function names from the object's prototype, excluding the constructor
71-
const functionNames = Object.getOwnPropertyNames(proto).filter(
72-
key => key !== 'constructor' && typeof obj[key] === 'function',
73-
);
69+
// Get all function names from the object and its prototypes
70+
const functionNameSet = new Set<string>();
71+
let currentObj = obj;
72+
while (currentObj && currentObj !== Object.prototype) {
73+
const keys = Object.getOwnPropertyNames(currentObj);
74+
for (const key of keys) {
75+
// Exclude constructor and non-function properties
76+
if (key !== 'constructor' && typeof currentObj[key] === 'function') {
77+
functionNameSet.add(key);
78+
}
79+
}
80+
currentObj = Object.getPrototypeOf(currentObj);
81+
}
82+
const functionNames = Array.from(functionNameSet);
7483

7584
for (const key of functionNames) {
7685
// Bind the function to preserve context

0 commit comments

Comments
 (0)