@usenagi/core 0.3.0 → 0.4.1
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/addons/cue.es.js +1 -1
- package/dist/addons/scheduler.cjs.js +1 -1
- package/dist/addons/scheduler.es.js +26 -27
- package/dist/main.es.js +120 -118
- package/dist/main.umd.js +1 -1
- package/package.json +3 -17
- package/types/addons/cue/index.d.ts +1 -1
- package/types/addons/scheduler/_internal/pending.d.ts +11 -0
- package/types/addons/scheduler/_internal/schedule.d.ts +11 -0
- package/types/addons/scheduler/index.d.ts +10 -2
- package/types/core/_internal/addonRegistry.d.ts +10 -0
- package/types/core/_internal/component.d.ts +24 -0
- package/types/core/_internal/registry.d.ts +5 -0
- package/types/core/addon.d.ts +0 -7
- package/types/core/app.d.ts +2 -3
- package/types/core/component.d.ts +0 -22
- package/types/core/error.d.ts +2 -3
- package/types/core/runtime.d.ts +4 -3
- package/types/hooks/{useDomRef.d.ts → core/useDomRef.d.ts} +1 -1
- package/types/hooks/{useSlot.d.ts → core/useSlot.d.ts} +2 -3
- package/types/main.d.ts +7 -8
- package/types/types.d.ts +10 -8
- package/LICENSE +0 -21
- package/README.ja.md +0 -269
- package/README.md +0 -269
- package/types/addons/scheduler/addon.d.ts +0 -10
- package/types/addons/scheduler/pending.d.ts +0 -11
- package/types/addons/scheduler/scheduler.d.ts +0 -4
- package/types/addons/scheduler/task.d.ts +0 -2
- package/types/core/internal/registry.d.ts +0 -4
- package/types/hooks/domRefs.d.ts +0 -2
- package/types/utils/isAbortError.d.ts +0 -1
- /package/types/{hooks/createContext.d.ts → core/context.d.ts} +0 -0
- /package/types/{props.d.ts → core/props.d.ts} +0 -0
package/dist/addons/cue.es.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});function e(e){return e}function t(
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});function e(e){return e}function t(){let e=new Map;return{add(t){let n=e.get(t);n&&n.abort();let r=new AbortController;return e.set(t,r),{signal:r.signal,complete(){return e.get(t)!==r||r.signal.aborted?!1:(e.delete(t),!0)},abort(){e.get(t)===r&&(r.abort(),e.delete(t))}}},abort(t){let n=e.get(t);n&&(n.abort(),e.delete(t))}}}function n(e){return(e instanceof DOMException||e instanceof Error)&&e.name===`AbortError`}function r(e,t,r){if(r?.aborted)return;let{scheduler:a}=globalThis;if(typeof a?.postTask==`function`){a.postTask(e,{priority:t,signal:r}).catch(e=>{n(e)||queueMicrotask(()=>{throw e})});return}i(e,t,r)}function i(e,t,n){function r(){n?.aborted||e()}function i(e,t){let r=e();n?.addEventListener(`abort`,()=>t(r),{once:!0})}switch(t){case`user-blocking`:queueMicrotask(r);break;case`user-visible`:i(()=>requestAnimationFrame(r),cancelAnimationFrame);break;case`background`:typeof requestIdleCallback==`function`?i(()=>requestIdleCallback(r),cancelIdleCallback):i(()=>setTimeout(r,0),clearTimeout);break}}function a(e={}){let t=e.priority??`user-visible`;return{schedule(e,n={}){r(e,n.priority??t,n.signal)}}}function o(e){return(e instanceof DOMException||e instanceof Error)&&e.name===`AbortError`}function s(n){return e({name:`@usenagi/scheduler`,install(e){let r=a(n),i=t();e.addMountMiddleware((e,t,n)=>(t,a)=>{let s=i.add(t),c=()=>{r.schedule(()=>{s.complete()&&e(t,a)},{priority:n.priority,signal:s.signal})},{when:l}=n;l?l(t,s.signal).then(()=>{s.signal.aborted||c()},e=>{o(e)||(s.abort(),queueMicrotask(()=>{throw e}))}):c()}),e.addUnmountMiddleware(e=>t=>{t.forEach(i.abort),e(t)})}})}exports.schedulerAddon=s;
|
|
@@ -3,13 +3,8 @@ function e(e) {
|
|
|
3
3
|
return e;
|
|
4
4
|
}
|
|
5
5
|
//#endregion
|
|
6
|
-
//#region
|
|
7
|
-
function t(
|
|
8
|
-
return (e instanceof DOMException || e instanceof Error) && e.name === "AbortError";
|
|
9
|
-
}
|
|
10
|
-
//#endregion
|
|
11
|
-
//#region lib/addons/scheduler/pending.ts
|
|
12
|
-
function n() {
|
|
6
|
+
//#region ../addons/scheduler/_internal/pending.ts
|
|
7
|
+
function t() {
|
|
13
8
|
let e = /* @__PURE__ */ new Map();
|
|
14
9
|
return {
|
|
15
10
|
add(t) {
|
|
@@ -33,22 +28,25 @@ function n() {
|
|
|
33
28
|
};
|
|
34
29
|
}
|
|
35
30
|
//#endregion
|
|
36
|
-
//#region
|
|
37
|
-
function
|
|
31
|
+
//#region ../addons/scheduler/_internal/schedule.ts
|
|
32
|
+
function n(e) {
|
|
33
|
+
return (e instanceof DOMException || e instanceof Error) && e.name === "AbortError";
|
|
34
|
+
}
|
|
35
|
+
function r(e, t, r) {
|
|
38
36
|
if (r?.aborted) return;
|
|
39
37
|
let { scheduler: a } = globalThis;
|
|
40
38
|
if (typeof a?.postTask == "function") {
|
|
41
39
|
a.postTask(e, {
|
|
42
|
-
priority:
|
|
40
|
+
priority: t,
|
|
43
41
|
signal: r
|
|
44
42
|
}).catch((e) => {
|
|
45
|
-
|
|
43
|
+
n(e) || queueMicrotask(() => {
|
|
46
44
|
throw e;
|
|
47
45
|
});
|
|
48
46
|
});
|
|
49
47
|
return;
|
|
50
48
|
}
|
|
51
|
-
i(e,
|
|
49
|
+
i(e, t, r);
|
|
52
50
|
}
|
|
53
51
|
function i(e, t, n) {
|
|
54
52
|
function r() {
|
|
@@ -70,8 +68,6 @@ function i(e, t, n) {
|
|
|
70
68
|
break;
|
|
71
69
|
}
|
|
72
70
|
}
|
|
73
|
-
//#endregion
|
|
74
|
-
//#region lib/addons/scheduler/scheduler.ts
|
|
75
71
|
function a(e = {}) {
|
|
76
72
|
let t = e.priority ?? "user-visible";
|
|
77
73
|
return { schedule(e, n = {}) {
|
|
@@ -79,33 +75,36 @@ function a(e = {}) {
|
|
|
79
75
|
} };
|
|
80
76
|
}
|
|
81
77
|
//#endregion
|
|
82
|
-
//#region
|
|
83
|
-
function o(
|
|
78
|
+
//#region ../addons/scheduler/index.ts
|
|
79
|
+
function o(e) {
|
|
80
|
+
return (e instanceof DOMException || e instanceof Error) && e.name === "AbortError";
|
|
81
|
+
}
|
|
82
|
+
function s(n) {
|
|
84
83
|
return e({
|
|
85
84
|
name: "@usenagi/scheduler",
|
|
86
85
|
install(e) {
|
|
87
|
-
let
|
|
88
|
-
e.addMountMiddleware((e,
|
|
89
|
-
let s =
|
|
90
|
-
|
|
91
|
-
s.complete() && e(
|
|
86
|
+
let r = a(n), i = t();
|
|
87
|
+
e.addMountMiddleware((e, t, n) => (t, a) => {
|
|
88
|
+
let s = i.add(t), c = () => {
|
|
89
|
+
r.schedule(() => {
|
|
90
|
+
s.complete() && e(t, a);
|
|
92
91
|
}, {
|
|
93
|
-
priority:
|
|
92
|
+
priority: n.priority,
|
|
94
93
|
signal: s.signal
|
|
95
94
|
});
|
|
96
|
-
}, { when: l } =
|
|
97
|
-
l ? l(
|
|
95
|
+
}, { when: l } = n;
|
|
96
|
+
l ? l(t, s.signal).then(() => {
|
|
98
97
|
s.signal.aborted || c();
|
|
99
98
|
}, (e) => {
|
|
100
|
-
|
|
99
|
+
o(e) || (s.abort(), queueMicrotask(() => {
|
|
101
100
|
throw e;
|
|
102
101
|
}));
|
|
103
102
|
}) : c();
|
|
104
103
|
}), e.addUnmountMiddleware((e) => (t) => {
|
|
105
|
-
t.forEach(
|
|
104
|
+
t.forEach(i.abort), e(t);
|
|
106
105
|
});
|
|
107
106
|
}
|
|
108
107
|
});
|
|
109
108
|
}
|
|
110
109
|
//#endregion
|
|
111
|
-
export {
|
|
110
|
+
export { s as schedulerAddon };
|
package/dist/main.es.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
//#region lib/core/addon.ts
|
|
2
|
-
function e() {
|
|
2
|
+
function e(e) {
|
|
3
|
+
return e;
|
|
4
|
+
}
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region lib/core/_internal/addonRegistry.ts
|
|
7
|
+
function t() {
|
|
3
8
|
let e = /* @__PURE__ */ new Set(), t = [], n = [], r = [], i = {
|
|
4
9
|
get installedAddons() {
|
|
5
10
|
return e;
|
|
@@ -29,9 +34,6 @@ function e() {
|
|
|
29
34
|
};
|
|
30
35
|
return i;
|
|
31
36
|
}
|
|
32
|
-
function t(e) {
|
|
33
|
-
return e;
|
|
34
|
-
}
|
|
35
37
|
//#endregion
|
|
36
38
|
//#region lib/core/error.ts
|
|
37
39
|
function n(e) {
|
|
@@ -62,7 +64,7 @@ function i(e) {
|
|
|
62
64
|
return e instanceof r;
|
|
63
65
|
}
|
|
64
66
|
//#endregion
|
|
65
|
-
//#region lib/core/
|
|
67
|
+
//#region lib/core/_internal/registry.ts
|
|
66
68
|
var a = /* @__PURE__ */ new WeakMap();
|
|
67
69
|
function o(e, t) {
|
|
68
70
|
let n = a.get(e);
|
|
@@ -70,10 +72,10 @@ function o(e, t) {
|
|
|
70
72
|
a.set(e, t);
|
|
71
73
|
}
|
|
72
74
|
//#endregion
|
|
73
|
-
//#region lib/core/component.ts
|
|
75
|
+
//#region lib/core/_internal/component.ts
|
|
74
76
|
var s = /* @__PURE__ */ function(e) {
|
|
75
77
|
return e.MOUNTED = "Mounted", e.UNMOUNTED = "Unmounted", e;
|
|
76
|
-
}({}), c = 0, l = class {
|
|
78
|
+
}(s || {}), c = 0, l = class {
|
|
77
79
|
Mounted = [];
|
|
78
80
|
Unmounted = [];
|
|
79
81
|
parent = null;
|
|
@@ -121,67 +123,93 @@ var s = /* @__PURE__ */ function(e) {
|
|
|
121
123
|
get childElements() {
|
|
122
124
|
return this.#e.map((e) => e.element);
|
|
123
125
|
}
|
|
124
|
-
};
|
|
125
|
-
function
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
//#endregion
|
|
129
|
-
//#region lib/core/runtime.ts
|
|
130
|
-
var d;
|
|
131
|
-
function f(e) {
|
|
132
|
-
if (!d) throw Error(`"${e}" called outside setup() will never be run.`);
|
|
133
|
-
return d;
|
|
126
|
+
}, u;
|
|
127
|
+
function d(e) {
|
|
128
|
+
if (!u) throw Error(`"${e}" called outside setup() will never be run.`);
|
|
129
|
+
return u;
|
|
134
130
|
}
|
|
135
|
-
function
|
|
136
|
-
let a = new l(t, e.name), o =
|
|
137
|
-
|
|
131
|
+
function f(e, t, n = {}) {
|
|
132
|
+
let a = new l(t, e.name), o = u;
|
|
133
|
+
u = a;
|
|
138
134
|
try {
|
|
139
135
|
o && (a.parent = o), a.props = n, a.current = e.setup(t, n) || {};
|
|
140
136
|
} catch (e) {
|
|
141
|
-
throw
|
|
137
|
+
throw u = o, i(e) ? e : r.create("setup", a, e, o, { props: a.props });
|
|
142
138
|
}
|
|
143
|
-
return
|
|
139
|
+
return u = o, a;
|
|
144
140
|
}
|
|
145
141
|
//#endregion
|
|
146
142
|
//#region lib/core/app.ts
|
|
147
|
-
function
|
|
148
|
-
let
|
|
143
|
+
function p() {
|
|
144
|
+
let e = t(), n = (e) => {
|
|
149
145
|
for (let t of e) {
|
|
150
146
|
let e = a.get(t);
|
|
151
147
|
e && (e.onUnmount(), a.delete(t));
|
|
152
148
|
}
|
|
153
149
|
}, r = {
|
|
154
|
-
install(...
|
|
155
|
-
return
|
|
150
|
+
install(...t) {
|
|
151
|
+
return t.forEach(e.install), r;
|
|
156
152
|
},
|
|
157
|
-
component(
|
|
158
|
-
let r =
|
|
159
|
-
let n =
|
|
153
|
+
component(t, n = {}) {
|
|
154
|
+
let r = e.composeComponent(t), i = e.composeMount((e, t) => {
|
|
155
|
+
let n = f(r, e, t);
|
|
160
156
|
return o(e, n), n.onMount(), n;
|
|
161
157
|
}, r, n);
|
|
162
158
|
return (e, t = {}) => i(e, t);
|
|
163
159
|
},
|
|
164
|
-
unmount(
|
|
165
|
-
|
|
160
|
+
unmount(t) {
|
|
161
|
+
e.composeUnmount(n)(t);
|
|
166
162
|
}
|
|
167
163
|
};
|
|
168
164
|
return r;
|
|
169
165
|
}
|
|
170
166
|
//#endregion
|
|
167
|
+
//#region lib/core/component.ts
|
|
168
|
+
function m(e) {
|
|
169
|
+
return e;
|
|
170
|
+
}
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region lib/core/context.ts
|
|
173
|
+
function h() {
|
|
174
|
+
let e = Symbol();
|
|
175
|
+
return [{ _id: e }, () => {
|
|
176
|
+
let t = d("createContext.use");
|
|
177
|
+
for (; t !== null;) {
|
|
178
|
+
if (t.provides.has(e)) return t.provides.get(e);
|
|
179
|
+
t = t.parent;
|
|
180
|
+
}
|
|
181
|
+
throw Error("createContext.use: no provider found");
|
|
182
|
+
}];
|
|
183
|
+
}
|
|
184
|
+
function g(e, t) {
|
|
185
|
+
return (n) => ({
|
|
186
|
+
name: n.name,
|
|
187
|
+
setup(r, i) {
|
|
188
|
+
return d(`withContext.${n.name}`).provides.set(e._id, t), n.setup(r, i);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
//#endregion
|
|
171
193
|
//#region lib/core/lifecycle.ts
|
|
172
|
-
function
|
|
194
|
+
function _(e) {
|
|
173
195
|
return (t) => {
|
|
174
|
-
|
|
196
|
+
d(e)[e].push(t);
|
|
175
197
|
};
|
|
176
198
|
}
|
|
177
|
-
var
|
|
199
|
+
var v = _(s.MOUNTED), y = _(s.UNMOUNTED);
|
|
200
|
+
//#endregion
|
|
201
|
+
//#region lib/core/props.ts
|
|
202
|
+
function b() {}
|
|
203
|
+
//#endregion
|
|
204
|
+
//#region lib/core/reactivity.ts
|
|
205
|
+
var x = Symbol("watch"), S = null, C = class {
|
|
178
206
|
#e;
|
|
179
207
|
#t = /* @__PURE__ */ new Set();
|
|
180
208
|
constructor(e) {
|
|
181
209
|
this.#e = e;
|
|
182
210
|
}
|
|
183
211
|
get value() {
|
|
184
|
-
return
|
|
212
|
+
return S !== null && S.add(this), this.#e;
|
|
185
213
|
}
|
|
186
214
|
set value(e) {
|
|
187
215
|
if (Object.is(e, this.#e)) return;
|
|
@@ -189,12 +217,12 @@ var g = h(s.MOUNTED), _ = h(s.UNMOUNTED), v = Symbol("watch"), y = null, b = cla
|
|
|
189
217
|
this.#e = e;
|
|
190
218
|
for (let n of Array.from(this.#t)) n(e, t);
|
|
191
219
|
}
|
|
192
|
-
[
|
|
220
|
+
[x](e) {
|
|
193
221
|
return this.#t.add(e), () => {
|
|
194
222
|
this.#t.delete(e);
|
|
195
223
|
};
|
|
196
224
|
}
|
|
197
|
-
},
|
|
225
|
+
}, w = (e) => new C(e), T = class {
|
|
198
226
|
#e;
|
|
199
227
|
constructor(e) {
|
|
200
228
|
this.#e = e;
|
|
@@ -202,75 +230,54 @@ var g = h(s.MOUNTED), _ = h(s.UNMOUNTED), v = Symbol("watch"), y = null, b = cla
|
|
|
202
230
|
get value() {
|
|
203
231
|
return this.#e.value;
|
|
204
232
|
}
|
|
205
|
-
[
|
|
206
|
-
return this.#e[
|
|
233
|
+
[x](e) {
|
|
234
|
+
return this.#e[x](e);
|
|
207
235
|
}
|
|
208
|
-
},
|
|
209
|
-
function
|
|
210
|
-
return e[
|
|
236
|
+
}, E = (e) => new T(e);
|
|
237
|
+
function D(e, t) {
|
|
238
|
+
return e[x](t);
|
|
211
239
|
}
|
|
212
|
-
function
|
|
213
|
-
|
|
240
|
+
function O(e, t) {
|
|
241
|
+
y(D(e, t));
|
|
214
242
|
}
|
|
215
|
-
function
|
|
216
|
-
let t =
|
|
243
|
+
function k(e) {
|
|
244
|
+
let t = w(void 0), n = [], r = () => {
|
|
217
245
|
n.forEach((e) => {
|
|
218
246
|
e();
|
|
219
247
|
}), n = [];
|
|
220
248
|
}, i = () => {
|
|
221
249
|
r();
|
|
222
|
-
let a =
|
|
223
|
-
|
|
250
|
+
let a = S, o = /* @__PURE__ */ new Set();
|
|
251
|
+
S = o;
|
|
224
252
|
let s;
|
|
225
253
|
try {
|
|
226
254
|
s = e();
|
|
227
255
|
} finally {
|
|
228
|
-
|
|
256
|
+
S = a;
|
|
229
257
|
}
|
|
230
258
|
t.value = s;
|
|
231
|
-
for (let e of o) n.push(e[
|
|
259
|
+
for (let e of o) n.push(e[x](() => {
|
|
232
260
|
i();
|
|
233
261
|
}));
|
|
234
262
|
};
|
|
235
|
-
return i(),
|
|
236
|
-
}
|
|
237
|
-
//#endregion
|
|
238
|
-
//#region lib/hooks/createContext.ts
|
|
239
|
-
function D() {
|
|
240
|
-
let e = Symbol();
|
|
241
|
-
return [{ _id: e }, () => {
|
|
242
|
-
let t = f("createContext.use");
|
|
243
|
-
for (; t !== null;) {
|
|
244
|
-
if (t.provides.has(e)) return t.provides.get(e);
|
|
245
|
-
t = t.parent;
|
|
246
|
-
}
|
|
247
|
-
throw Error("createContext.use: no provider found");
|
|
248
|
-
}];
|
|
249
|
-
}
|
|
250
|
-
function O(e, t) {
|
|
251
|
-
return (n) => ({
|
|
252
|
-
name: n.name,
|
|
253
|
-
setup(r, i) {
|
|
254
|
-
return f(`withContext.${n.name}`).provides.set(e._id, t), n.setup(r, i);
|
|
255
|
-
}
|
|
256
|
-
});
|
|
263
|
+
return i(), y(r), E(t);
|
|
257
264
|
}
|
|
258
265
|
//#endregion
|
|
259
|
-
//#region lib/hooks/
|
|
260
|
-
function
|
|
266
|
+
//#region lib/hooks/core/useDomRef.ts
|
|
267
|
+
function A(e, t) {
|
|
261
268
|
return t.some((t) => t !== e && t.contains(e));
|
|
262
269
|
}
|
|
263
|
-
function
|
|
264
|
-
let r = `[data-ref="${CSS.escape(e)}"]`, i = Array.from(t.querySelectorAll(r)).filter((e) => !
|
|
270
|
+
function j(e, t, n) {
|
|
271
|
+
let r = `[data-ref="${CSS.escape(e)}"]`, i = Array.from(t.querySelectorAll(r)).filter((e) => !A(e, n));
|
|
265
272
|
return i.length === 0 ? null : i.length === 1 ? i[0] : i;
|
|
266
273
|
}
|
|
267
|
-
function
|
|
274
|
+
function M(e, t) {
|
|
268
275
|
let n = /* @__PURE__ */ new Map();
|
|
269
276
|
return new Proxy({}, {
|
|
270
277
|
get(r, i) {
|
|
271
278
|
if (typeof i == "symbol" || i === "then") return;
|
|
272
279
|
if (n.has(i)) return n.get(i);
|
|
273
|
-
let a =
|
|
280
|
+
let a = j(i, e, t());
|
|
274
281
|
return n.set(i, a), a;
|
|
275
282
|
},
|
|
276
283
|
has(e, t) {
|
|
@@ -288,22 +295,43 @@ function j(e, t) {
|
|
|
288
295
|
}
|
|
289
296
|
});
|
|
290
297
|
}
|
|
298
|
+
function N() {
|
|
299
|
+
let e = d("useDomRef");
|
|
300
|
+
return { refs: M(e.element, () => e.childElements) };
|
|
301
|
+
}
|
|
291
302
|
//#endregion
|
|
292
|
-
//#region lib/hooks/
|
|
293
|
-
function
|
|
294
|
-
let e =
|
|
295
|
-
return {
|
|
303
|
+
//#region lib/hooks/core/useSlot.ts
|
|
304
|
+
function P() {
|
|
305
|
+
let e = d("useSlot");
|
|
306
|
+
return {
|
|
307
|
+
addChild(t, n, r) {
|
|
308
|
+
let i = (t) => {
|
|
309
|
+
let i = f(n, t, r);
|
|
310
|
+
return e.addChild(i), i;
|
|
311
|
+
};
|
|
312
|
+
return Array.isArray(t) ? t.map((e) => i(e)) : [i(t)];
|
|
313
|
+
},
|
|
314
|
+
removeChild(t) {
|
|
315
|
+
t.forEach((t) => {
|
|
316
|
+
try {
|
|
317
|
+
e.removeChild(t);
|
|
318
|
+
} catch (n) {
|
|
319
|
+
console.error("[nagi] removeChild failed", r.create("removeChild", t, n, e));
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
};
|
|
296
324
|
}
|
|
297
325
|
//#endregion
|
|
298
326
|
//#region lib/hooks/useEvent.ts
|
|
299
|
-
function
|
|
300
|
-
|
|
327
|
+
function F(e, t, n, r) {
|
|
328
|
+
v(() => (e.addEventListener(t, n, r), () => {
|
|
301
329
|
e.removeEventListener(t, n, r);
|
|
302
330
|
}));
|
|
303
331
|
}
|
|
304
332
|
//#endregion
|
|
305
333
|
//#region lib/hooks/useIntersectionWatch.ts
|
|
306
|
-
function
|
|
334
|
+
function I(e, t, n = {
|
|
307
335
|
rootMargin: "0px",
|
|
308
336
|
threshold: .1
|
|
309
337
|
}) {
|
|
@@ -313,9 +341,9 @@ function P(e, t, n = {
|
|
|
313
341
|
r.observe(e);
|
|
314
342
|
}) : r.observe(e);
|
|
315
343
|
}
|
|
316
|
-
i(e),
|
|
344
|
+
v(() => (i(e), () => {
|
|
317
345
|
r.disconnect();
|
|
318
|
-
});
|
|
346
|
+
}));
|
|
319
347
|
function a(e) {
|
|
320
348
|
r.unobserve(e);
|
|
321
349
|
}
|
|
@@ -323,40 +351,14 @@ function P(e, t, n = {
|
|
|
323
351
|
}
|
|
324
352
|
//#endregion
|
|
325
353
|
//#region lib/hooks/useMediaQuery.ts
|
|
326
|
-
function
|
|
327
|
-
let n = window.matchMedia(e), r =
|
|
354
|
+
function L(e, t) {
|
|
355
|
+
let n = window.matchMedia(e), r = w(n.matches), i = null;
|
|
328
356
|
function a(e) {
|
|
329
357
|
r.value = e.matches, e.matches ? i = t() : (i?.(), i = null);
|
|
330
358
|
}
|
|
331
|
-
return
|
|
359
|
+
return v(() => (n.addEventListener("change", a), n.matches && (i = t()), () => {
|
|
332
360
|
i?.(), n.removeEventListener("change", a);
|
|
333
|
-
})), { matchesQuery:
|
|
334
|
-
}
|
|
335
|
-
//#endregion
|
|
336
|
-
//#region lib/hooks/useSlot.ts
|
|
337
|
-
function I() {
|
|
338
|
-
let e = f("useSlot");
|
|
339
|
-
return {
|
|
340
|
-
addChild(t, n, r) {
|
|
341
|
-
let i = (t) => {
|
|
342
|
-
let i = p(n, t, r);
|
|
343
|
-
return e.addChild(i), i;
|
|
344
|
-
};
|
|
345
|
-
return Array.isArray(t) ? t.map((e) => i(e)) : [i(t)];
|
|
346
|
-
},
|
|
347
|
-
removeChild(t) {
|
|
348
|
-
t.forEach((t) => {
|
|
349
|
-
try {
|
|
350
|
-
e.removeChild(t);
|
|
351
|
-
} catch (n) {
|
|
352
|
-
console.error("[nagi] removeChild failed", r.create("removeChild", t, n, e));
|
|
353
|
-
}
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
};
|
|
361
|
+
})), { matchesQuery: E(r) };
|
|
357
362
|
}
|
|
358
363
|
//#endregion
|
|
359
|
-
|
|
360
|
-
function L() {}
|
|
361
|
-
//#endregion
|
|
362
|
-
export { r as LifecycleError, m as create, D as createContext, t as defineAddon, u as defineComponent, i as isLifecycleError, L as propTypes, C as readonly, x as signal, E as useComputed, M as useDomRef, N as useEvent, P as useIntersectionWatch, F as useMediaQuery, g as useMount, I as useSlot, _ as useUnmount, T as useWatch, O as withContext };
|
|
364
|
+
export { r as LifecycleError, p as create, h as createContext, e as defineAddon, m as defineComponent, i as isLifecycleError, b as propTypes, E as readonly, w as signal, k as useComputed, N as useDomRef, F as useEvent, I as useIntersectionWatch, L as useMediaQuery, v as useMount, P as useSlot, y as useUnmount, O as useWatch, g as withContext };
|
package/dist/main.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.Nagi={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function t(e){return e}function n(){let e=new Set,t=[],n=[],r=[],i={get installedAddons(){return e},addComponentMiddleware(e){t.push(e)},addMountMiddleware(e){n.push(e)},addUnmountMiddleware(e){r.push(e)},composeComponent(e){return t.reduce((e,t)=>t(e),e)},composeMount(e,t,r){return n.reduce((e,n)=>n(e,t,r),e)},composeUnmount(e){return r.reduce((e,t)=>t(e),e)},install(t){if(e.has(t.name))throw Error(`[nagi] addon "${t.name}" is already installed`);t.install(i),e.add(t.name)}};return i}function r(e){let t=[],n=e;for(;n;)t.unshift(n.name),n=n.parent;return t.join(` > `)}var i=class e extends Error{details;constructor(e){super(`[nagi] Component error in phase "${e.phase}" for "${e.name}"${e.path?` (${e.path})`:``}`,{cause:e.cause}),this.name=`LifecycleError`,this.details=e}static create(t,n,i,a=n.parent,o){return new e({phase:t,name:n.name,uid:n.uid,path:r(n),parentName:a?.name,parentUid:a?.uid,element:n.element,cause:i,...o})}};function a(e){return e instanceof i}var o=new WeakMap;function s(e,t){let n=o.get(e);if(n)throw i.create(`mount`,t,Error(`Component "${n.name}" (${n.uid}) is already mounted on this element`),n);o.set(e,t)}var c=function(e){return e.MOUNTED=`Mounted`,e.UNMOUNTED=`Unmounted`,e}(c||{}),l=0,u=class{Mounted=[];Unmounted=[];parent=null;#e=[];uid;name;current={};props={};element;provides=new Map;constructor(e,t){this.uid=`${t}.${l++}`,this.name=t,this.element=e}onMount=()=>{let e=[];for(let t of this.Mounted)try{let n=t();typeof n==`function`&&e.push(n)}catch(e){console.error(`[nagi] onMount hook failed`,i.create(`mount`,this,e))}this.Unmounted.push(...e)};onUnmount=()=>{for(let e of this.Unmounted)try{e()}catch(e){console.error(`[nagi] onUnmount cleanup failed`,i.create(`unmount`,this,e))}for(let e of this.#e)e.onUnmount()};addChild=e=>{this.#e.push(e),e.parent=this;try{e.onMount()}catch(t){let n=this.#e.indexOf(e);throw n!==-1&&this.#e.splice(n,1),e.parent=null,t}};removeChild=e=>{let t=this.#e.indexOf(e);t!==-1&&(this.#e.splice(t,1),e.parent=null,e.onUnmount())};get childElements(){return this.#e.map(e=>e.element)}},d;function f(e){if(!d)throw Error(`"${e}" called outside setup() will never be run.`);return d}function p(e,t,n={}){let r=new u(t,e.name),o=d;d=r;try{o&&(r.parent=o),r.props=n,r.current=e.setup(t,n)||{}}catch(e){throw d=o,a(e)?e:i.create(`setup`,r,e,o,{props:r.props})}return d=o,r}function m(){let e=n(),t=e=>{for(let t of e){let e=o.get(t);e&&(e.onUnmount(),o.delete(t))}},r={install(...t){return t.forEach(e.install),r},component(t,n={}){let r=e.composeComponent(t),i=e.composeMount((e,t)=>{let n=p(r,e,t);return s(e,n),n.onMount(),n},r,n);return(e,t={})=>i(e,t)},unmount(n){e.composeUnmount(t)(n)}};return r}function h(e){return e}function g(){let e=Symbol();return[{_id:e},()=>{let t=f(`createContext.use`);for(;t!==null;){if(t.provides.has(e))return t.provides.get(e);t=t.parent}throw Error(`createContext.use: no provider found`)}]}function _(e,t){return n=>({name:n.name,setup(r,i){return f(`withContext.${n.name}`).provides.set(e._id,t),n.setup(r,i)}})}function v(e){return t=>{f(e)[e].push(t)}}var y=v(c.MOUNTED),b=v(c.UNMOUNTED);function x(){}var S=Symbol(`watch`),C=null,w=class{#e;#t=new Set;constructor(e){this.#e=e}get value(){return C!==null&&C.add(this),this.#e}set value(e){if(Object.is(e,this.#e))return;let t=this.#e;this.#e=e;for(let n of Array.from(this.#t))n(e,t)}[S](e){return this.#t.add(e),()=>{this.#t.delete(e)}}},T=e=>new w(e),E=class{#e;constructor(e){this.#e=e}get value(){return this.#e.value}[S](e){return this.#e[S](e)}},D=e=>new E(e);function O(e,t){return e[S](t)}function k(e,t){b(O(e,t))}function A(e){let t=T(void 0),n=[],r=()=>{n.forEach(e=>{e()}),n=[]},i=()=>{r();let a=C,o=new Set;C=o;let s;try{s=e()}finally{C=a}t.value=s;for(let e of o)n.push(e[S](()=>{i()}))};return i(),b(r),D(t)}function j(e,t){return t.some(t=>t!==e&&t.contains(e))}function M(e,t,n){let r=`[data-ref="${CSS.escape(e)}"]`,i=Array.from(t.querySelectorAll(r)).filter(e=>!j(e,n));return i.length===0?null:i.length===1?i[0]:i}function N(e,t){let n=new Map;return new Proxy({},{get(r,i){if(typeof i==`symbol`||i===`then`)return;if(n.has(i))return n.get(i);let a=M(i,e,t());return n.set(i,a),a},has(e,t){return typeof t==`string`},ownKeys(){return[]},getOwnPropertyDescriptor(){},set(){return!1},deleteProperty(){return!1}})}function P(){let e=f(`useDomRef`);return{refs:N(e.element,()=>e.childElements)}}function F(){let e=f(`useSlot`);return{addChild(t,n,r){let i=t=>{let i=p(n,t,r);return e.addChild(i),i};return Array.isArray(t)?t.map(e=>i(e)):[i(t)]},removeChild(t){t.forEach(t=>{try{e.removeChild(t)}catch(n){console.error(`[nagi] removeChild failed`,i.create(`removeChild`,t,n,e))}})}}}function I(e,t,n,r){y(()=>(e.addEventListener(t,n,r),()=>{e.removeEventListener(t,n,r)}))}function L(e,t,n={rootMargin:`0px`,threshold:.1}){let r=new IntersectionObserver(t,n);function i(e){Array.isArray(e)?e.forEach(e=>{r.observe(e)}):r.observe(e)}y(()=>(i(e),()=>{r.disconnect()}));function a(e){r.unobserve(e)}return{unwatch:a}}function R(e,t){let n=window.matchMedia(e),r=T(n.matches),i=null;function a(e){r.value=e.matches,e.matches?i=t():(i?.(),i=null)}return y(()=>(n.addEventListener(`change`,a),n.matches&&(i=t()),()=>{i?.(),n.removeEventListener(`change`,a)})),{matchesQuery:D(r)}}e.LifecycleError=i,e.create=m,e.createContext=g,e.defineAddon=t,e.defineComponent=h,e.isLifecycleError=a,e.propTypes=x,e.readonly=D,e.signal=T,e.useComputed=A,e.useDomRef=P,e.useEvent=I,e.useIntersectionWatch=L,e.useMediaQuery=R,e.useMount=y,e.useSlot=F,e.useUnmount=b,e.useWatch=k,e.withContext=_});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usenagi/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Composition-API ergonomics for vanilla DOM. Bring your own mounter.",
|
|
5
5
|
"main": "./dist/main.umd.js",
|
|
6
6
|
"module": "./dist/main.es.js",
|
|
@@ -49,22 +49,8 @@
|
|
|
49
49
|
"access": "public"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
|
-
"build": "NODE_ENV=production tsc && vite build && vite build --config vite.addons.config.ts",
|
|
53
|
-
"watch": "vite build -w"
|
|
54
|
-
"test": "vitest run",
|
|
55
|
-
"test:watch": "vitest",
|
|
56
|
-
"lint": "oxlint ./lib",
|
|
57
|
-
"format": "biome format --write ./lib",
|
|
58
|
-
"fix:biome": "biome check --write ./lib",
|
|
59
|
-
"fix": "npm run fix:biome"
|
|
60
|
-
},
|
|
61
|
-
"devDependencies": {
|
|
62
|
-
"@biomejs/biome": "^2.4.13",
|
|
63
|
-
"happy-dom": "^20.9.0",
|
|
64
|
-
"oxlint": "^1.61.0",
|
|
65
|
-
"typescript": "^6.0.3",
|
|
66
|
-
"vite": "^8.0.10",
|
|
67
|
-
"vitest": "^4.1.5"
|
|
52
|
+
"build": "NODE_ENV=production tsc && tsc -p ../addons/tsconfig.json && vite build && vite build --config vite.addons.config.ts",
|
|
53
|
+
"watch": "vite build -w"
|
|
68
54
|
},
|
|
69
55
|
"type": "module",
|
|
70
56
|
"sideEffects": false
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { RefElement } from "@usenagi/core";
|
|
2
|
+
type PendingMounts = {
|
|
3
|
+
add(el: RefElement): {
|
|
4
|
+
readonly signal: AbortSignal;
|
|
5
|
+
complete(): boolean;
|
|
6
|
+
abort(): void;
|
|
7
|
+
};
|
|
8
|
+
abort(el: RefElement): void;
|
|
9
|
+
};
|
|
10
|
+
declare function createPendingMounts(): PendingMounts;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SchedulePriority } from "@usenagi/core";
|
|
2
|
+
type Scheduler = {
|
|
3
|
+
schedule(task: () => void, options?: {
|
|
4
|
+
priority?: SchedulePriority;
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
}): void;
|
|
7
|
+
};
|
|
8
|
+
declare function createScheduler(opts?: {
|
|
9
|
+
priority?: SchedulePriority;
|
|
10
|
+
}): Scheduler;
|
|
11
|
+
export {};
|
|
@@ -1,2 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { Cue, SchedulePriority } from "@usenagi/core";
|
|
2
|
+
declare module "@usenagi/core" {
|
|
3
|
+
interface MountOptions {
|
|
4
|
+
priority?: SchedulePriority;
|
|
5
|
+
when?: Cue;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export declare function schedulerAddon(opts?: {
|
|
9
|
+
priority?: SchedulePriority;
|
|
10
|
+
}): import("@usenagi/core").Addon;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ComponentSetup } from "../../types";
|
|
2
|
+
import type { Addon, AddonContext, MountFn, MountOptions, UnmountFn } from "../addon";
|
|
3
|
+
type AddonRegistry = AddonContext & {
|
|
4
|
+
composeComponent<S extends ComponentSetup>(setup: S): S;
|
|
5
|
+
composeMount(mountFn: MountFn, setup: ComponentSetup, opts: MountOptions): MountFn;
|
|
6
|
+
composeUnmount(unmountFn: UnmountFn): UnmountFn;
|
|
7
|
+
install(addon: Addon): void;
|
|
8
|
+
};
|
|
9
|
+
declare function createAddonRegistry(): AddonRegistry;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ComponentContext, ComponentSetup, ExposedSetup, RefElement } from "../../types";
|
|
2
|
+
declare enum LifecycleHooks {
|
|
3
|
+
MOUNTED = "Mounted",
|
|
4
|
+
UNMOUNTED = "Unmounted"
|
|
5
|
+
}
|
|
6
|
+
declare class ComponentContextImpl<T = any> implements ComponentContext<ExposedSetup<T>> {
|
|
7
|
+
#private;
|
|
8
|
+
private [LifecycleHooks.MOUNTED];
|
|
9
|
+
private [LifecycleHooks.UNMOUNTED];
|
|
10
|
+
parent: ComponentContextImpl | null;
|
|
11
|
+
readonly uid: string;
|
|
12
|
+
readonly name: ComponentContext["name"];
|
|
13
|
+
current: ExposedSetup<T>;
|
|
14
|
+
props: Parameters<ComponentSetup<T>["setup"]>[1];
|
|
15
|
+
element: ComponentContext["element"];
|
|
16
|
+
provides: Map<symbol, unknown>;
|
|
17
|
+
constructor(element: RefElement, name: string);
|
|
18
|
+
onMount: () => void;
|
|
19
|
+
onUnmount: () => void;
|
|
20
|
+
addChild: (child: ComponentContextImpl) => void;
|
|
21
|
+
removeChild: (child: ComponentContextImpl) => void;
|
|
22
|
+
get childElements(): RefElement[];
|
|
23
|
+
}
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { RefElement } from "../../types";
|
|
2
|
+
import type { ComponentContextImpl } from "./component";
|
|
3
|
+
declare const DOM_COMPONENT_INSTANCE: WeakMap<RefElement, ComponentContextImpl<any>>;
|
|
4
|
+
declare function bindDOMNodeToComponent(el: RefElement, component: ComponentContextImpl): void;
|
|
5
|
+
export {};
|