@li3/use 0.1.6 → 0.1.7
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/dist/src/async-state.d.ts +14 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/query.d.ts +14 -0
- package/dist/src/vue.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +1 -1
- package/src/async-state.ts +33 -0
- package/src/index.ts +2 -0
- package/src/query.ts +25 -0
- package/src/vue.ts +61 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @example
|
|
3
|
+
* import { useAsyncState } from '@li3/use/async-state';
|
|
4
|
+
*
|
|
5
|
+
* const { data, error, loading, empty, update } = useAsyncState();
|
|
6
|
+
* update();
|
|
7
|
+
*/
|
|
8
|
+
export declare function useAsyncState<T>(effect?: (...args: any[]) => Promise<T>): {
|
|
9
|
+
data: any;
|
|
10
|
+
error: any;
|
|
11
|
+
loading: any;
|
|
12
|
+
empty: any;
|
|
13
|
+
update: (...args: any[]) => Promise<void>;
|
|
14
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @example
|
|
3
|
+
* import { useQuery } from '@li3/use/async-state';
|
|
4
|
+
*
|
|
5
|
+
* const listItems = useQuery('li');
|
|
6
|
+
*
|
|
7
|
+
* for (const li of listItems.all) {
|
|
8
|
+
* // ...
|
|
9
|
+
* }
|
|
10
|
+
*/
|
|
11
|
+
export declare function useQuery(selector: string): {
|
|
12
|
+
readonly one: any;
|
|
13
|
+
readonly all: any;
|
|
14
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useVue(): void;
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ref as e,getElement as r}from"@li3/web";function t(r=fetch){const t=e(""),l=e(!1),a=e(!1),n=e(null);return{data:n,error:t,loading:l,empty:a,update:async(...e)=>{l.value=!0,a.value=!1;try{const t=await r(...e);n.value=t,a.value=Array.isArray(t)?!t.length:!t}catch(e){a.value=!1,t.value=e}finally{l.value=!1}}}}function l(e){const t=r();return{get one(){return t.querySelector(e)},get all(){return t.querySelectorAll(e)}}}export{t as useAsyncState,l as useQuery};
|
package/package.json
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ref } from "@li3/web";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @example
|
|
5
|
+
* import { useAsyncState } from '@li3/use/async-state';
|
|
6
|
+
*
|
|
7
|
+
* const { data, error, loading, empty, update } = useAsyncState();
|
|
8
|
+
* update();
|
|
9
|
+
*/
|
|
10
|
+
export function useAsyncState<T>(effect: (...args: any[]) => Promise<T> = fetch as any) {
|
|
11
|
+
const error = ref("");
|
|
12
|
+
const loading = ref(false);
|
|
13
|
+
const empty = ref(false);
|
|
14
|
+
const data = ref(null);
|
|
15
|
+
|
|
16
|
+
const update = async (...args: any[]) => {
|
|
17
|
+
loading.value = true;
|
|
18
|
+
empty.value = false;
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const v = await effect(...args);
|
|
22
|
+
data.value = v;
|
|
23
|
+
empty.value = Array.isArray(v) ? !v.length : !v;
|
|
24
|
+
} catch (e) {
|
|
25
|
+
empty.value = false;
|
|
26
|
+
error.value = e;
|
|
27
|
+
} finally {
|
|
28
|
+
loading.value = false;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
return { data, error, loading, empty, update };
|
|
33
|
+
}
|
package/src/index.ts
ADDED
package/src/query.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { getElement } from "@li3/web";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @example
|
|
5
|
+
* import { useQuery } from '@li3/use/async-state';
|
|
6
|
+
*
|
|
7
|
+
* const listItems = useQuery('li');
|
|
8
|
+
*
|
|
9
|
+
* for (const li of listItems.all) {
|
|
10
|
+
* // ...
|
|
11
|
+
* }
|
|
12
|
+
*/
|
|
13
|
+
export function useQuery(selector: string) {
|
|
14
|
+
const element = getElement();
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
get one() {
|
|
18
|
+
return element.querySelector(selector);
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
get all() {
|
|
22
|
+
return element.querySelectorAll(selector);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
package/src/vue.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { TemplateFor, TemplateIf, SetProperty, AddEventListener, use } from '@li3/web';
|
|
2
|
+
|
|
3
|
+
export function useVue() {
|
|
4
|
+
const vFor = 'v-for';
|
|
5
|
+
const vIf = 'v-if';
|
|
6
|
+
|
|
7
|
+
class VueTemplateFor extends TemplateFor {
|
|
8
|
+
match(_node, name) {
|
|
9
|
+
return name === vFor;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
exec(node, _name, value, context) {
|
|
13
|
+
const t = document.createElement('template');
|
|
14
|
+
node.replaceWith(t);
|
|
15
|
+
node.removeAttribute(vFor);
|
|
16
|
+
t.content.append(node);
|
|
17
|
+
|
|
18
|
+
super.exec(t, 'for', value, context);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class VueTemplateIf extends TemplateIf {
|
|
23
|
+
match(_node, name) {
|
|
24
|
+
return name === vIf;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
exec(node, _name, value, context) {
|
|
28
|
+
const t = document.createElement('template');
|
|
29
|
+
node.replaceWith(t);
|
|
30
|
+
node.removeAttribute(vFor);
|
|
31
|
+
t.content.append(node);
|
|
32
|
+
|
|
33
|
+
super.exec(t, 'if', value, context);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
class VueSetProperty extends SetProperty {
|
|
38
|
+
match(_node, name) {
|
|
39
|
+
return name.at(0) === ':';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
exec(node, name, value, context) {
|
|
43
|
+
return super.exec(node, 'bind-' + name.slice(1), value, context);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class VueEventListener extends AddEventListener {
|
|
48
|
+
match(_node, name) {
|
|
49
|
+
return name.at(0) === '@';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
exec(node, name, value, context) {
|
|
53
|
+
return super.exec(node, 'on-' + name.slice(1), value, context);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
use(new VueSetProperty());
|
|
58
|
+
use(new VueTemplateFor());
|
|
59
|
+
use(new VueTemplateIf());
|
|
60
|
+
use(new VueEventListener());
|
|
61
|
+
}
|