@cajoa/core 0.16.0 → 0.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/dist/index.cjs +94 -16
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +93 -16
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -36,6 +36,7 @@ __export(index_exports, {
|
|
|
36
36
|
createMemo: () => createMemo,
|
|
37
37
|
createResource: () => createResource,
|
|
38
38
|
createRoot: () => createRoot,
|
|
39
|
+
createSelector: () => createSelector,
|
|
39
40
|
createSignal: () => createSignal,
|
|
40
41
|
createStore: () => createStore,
|
|
41
42
|
getRenderer: () => getRenderer,
|
|
@@ -258,6 +259,39 @@ function createMemo(fn) {
|
|
|
258
259
|
return node.value;
|
|
259
260
|
};
|
|
260
261
|
}
|
|
262
|
+
function createSelector(source) {
|
|
263
|
+
const cells = /* @__PURE__ */ new Map();
|
|
264
|
+
let previous = untrack(source);
|
|
265
|
+
createEffect(() => {
|
|
266
|
+
const next = source();
|
|
267
|
+
if (Object.is(next, previous)) return;
|
|
268
|
+
const old = previous;
|
|
269
|
+
previous = next;
|
|
270
|
+
batch(() => {
|
|
271
|
+
for (const key of [old, next]) {
|
|
272
|
+
const cell = cells.get(key);
|
|
273
|
+
if (cell) notify(cell);
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
return (key) => {
|
|
278
|
+
const current2 = untrack(source);
|
|
279
|
+
if (listener) {
|
|
280
|
+
let cell = cells.get(key);
|
|
281
|
+
if (!cell) {
|
|
282
|
+
cell = { observers: /* @__PURE__ */ new Set() };
|
|
283
|
+
cells.set(key, cell);
|
|
284
|
+
}
|
|
285
|
+
track(cell);
|
|
286
|
+
const mine = cell;
|
|
287
|
+
listener.cleanups.push(() => {
|
|
288
|
+
if (mine.observers.size === 0 && cells.get(key) === mine)
|
|
289
|
+
cells.delete(key);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
return Object.is(current2, key);
|
|
293
|
+
};
|
|
294
|
+
}
|
|
261
295
|
function onCleanup(fn) {
|
|
262
296
|
if (!currentOwner) {
|
|
263
297
|
warn(
|
|
@@ -701,6 +735,65 @@ function getRenderer() {
|
|
|
701
735
|
return current;
|
|
702
736
|
}
|
|
703
737
|
|
|
738
|
+
// src/runtime/reconcile.ts
|
|
739
|
+
function longestIncreasingSubsequence(values) {
|
|
740
|
+
const predecessor = new Array(values.length);
|
|
741
|
+
const tails = [];
|
|
742
|
+
for (let i = 0; i < values.length; i++) {
|
|
743
|
+
const v = values[i];
|
|
744
|
+
if (v < 0) continue;
|
|
745
|
+
let lo;
|
|
746
|
+
if (tails.length === 0 || values[tails[tails.length - 1]] < v) {
|
|
747
|
+
lo = tails.length;
|
|
748
|
+
} else {
|
|
749
|
+
lo = 0;
|
|
750
|
+
let hi = tails.length - 1;
|
|
751
|
+
while (lo < hi) {
|
|
752
|
+
const mid = lo + hi >> 1;
|
|
753
|
+
if (values[tails[mid]] < v) lo = mid + 1;
|
|
754
|
+
else hi = mid;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
predecessor[i] = lo > 0 ? tails[lo - 1] : -1;
|
|
758
|
+
tails[lo] = i;
|
|
759
|
+
}
|
|
760
|
+
const result = new Array(tails.length);
|
|
761
|
+
let k = tails.length > 0 ? tails[tails.length - 1] : -1;
|
|
762
|
+
for (let j = tails.length - 1; j >= 0; j--) {
|
|
763
|
+
result[j] = k;
|
|
764
|
+
k = predecessor[k];
|
|
765
|
+
}
|
|
766
|
+
return result;
|
|
767
|
+
}
|
|
768
|
+
function reconcile(start, end, next) {
|
|
769
|
+
const r = getRenderer();
|
|
770
|
+
const keep = new Set(next);
|
|
771
|
+
const oldIndex = /* @__PURE__ */ new Map();
|
|
772
|
+
let count = 0;
|
|
773
|
+
let cur = r.nextSibling(start);
|
|
774
|
+
while (cur && cur !== end) {
|
|
775
|
+
const after = r.nextSibling(cur);
|
|
776
|
+
if (keep.has(cur)) oldIndex.set(cur, count++);
|
|
777
|
+
else r.remove(cur);
|
|
778
|
+
cur = after;
|
|
779
|
+
}
|
|
780
|
+
const parent = r.parentNode(end);
|
|
781
|
+
const n = next.length;
|
|
782
|
+
const positions = new Array(n);
|
|
783
|
+
for (let i = 0; i < n; i++) positions[i] = oldIndex.get(next[i]) ?? -1;
|
|
784
|
+
const stable = new Uint8Array(n);
|
|
785
|
+
for (const i of longestIncreasingSubsequence(positions)) stable[i] = 1;
|
|
786
|
+
const anchors = new Array(n);
|
|
787
|
+
let anchor = end;
|
|
788
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
789
|
+
anchors[i] = anchor;
|
|
790
|
+
if (stable[i]) anchor = next[i];
|
|
791
|
+
}
|
|
792
|
+
for (let i = 0; i < n; i++) {
|
|
793
|
+
if (!stable[i]) r.insert(parent, next[i], anchors[i]);
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
704
797
|
// src/runtime/h.ts
|
|
705
798
|
function h(tag, props, ...children) {
|
|
706
799
|
const r = getRenderer();
|
|
@@ -741,22 +834,6 @@ function toNodes(value) {
|
|
|
741
834
|
}
|
|
742
835
|
return out;
|
|
743
836
|
}
|
|
744
|
-
function reconcile(start, end, next) {
|
|
745
|
-
const r = getRenderer();
|
|
746
|
-
const keep = new Set(next);
|
|
747
|
-
let cur = r.nextSibling(start);
|
|
748
|
-
while (cur && cur !== end) {
|
|
749
|
-
const after = r.nextSibling(cur);
|
|
750
|
-
if (!keep.has(cur)) r.remove(cur);
|
|
751
|
-
cur = after;
|
|
752
|
-
}
|
|
753
|
-
const parent = r.parentNode(end);
|
|
754
|
-
let ref = r.nextSibling(start);
|
|
755
|
-
for (const node of next) {
|
|
756
|
-
if (node === ref) ref = r.nextSibling(ref);
|
|
757
|
-
else r.insert(parent, node, ref);
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
837
|
function appendChildren(parent, children) {
|
|
761
838
|
const r = getRenderer();
|
|
762
839
|
for (const child of children.flat(Infinity)) {
|
|
@@ -1064,6 +1141,7 @@ function lazy(loader) {
|
|
|
1064
1141
|
createMemo,
|
|
1065
1142
|
createResource,
|
|
1066
1143
|
createRoot,
|
|
1144
|
+
createSelector,
|
|
1067
1145
|
createSignal,
|
|
1068
1146
|
createStore,
|
|
1069
1147
|
getRenderer,
|
package/dist/index.d.cts
CHANGED
|
@@ -3,6 +3,7 @@ declare function batch<T>(fn: () => T): T;
|
|
|
3
3
|
declare function createSignal<T>(value: T): readonly [() => T, (next: T | ((prev: T) => T)) => void];
|
|
4
4
|
declare function createEffect(fn: () => void): void;
|
|
5
5
|
declare function createMemo<T>(fn: () => T): () => T;
|
|
6
|
+
declare function createSelector<T>(source: () => T): (key: T) => boolean;
|
|
6
7
|
declare function onCleanup(fn: Cleanup): void;
|
|
7
8
|
declare function createRoot<T>(fn: (dispose: () => void) => T): T;
|
|
8
9
|
declare function onMount(fn: () => void): void;
|
|
@@ -140,5 +141,5 @@ declare function Fragment(props: {
|
|
|
140
141
|
declare function setRenderer(renderer: Renderer): void;
|
|
141
142
|
declare function getRenderer(): Renderer;
|
|
142
143
|
|
|
143
|
-
export { Dynamic, ErrorBoundary, For, Fragment, JSX, Match, Portal, Show, Suspense, Switch, batch, catchError, createContext, createEffect, createMemo, createResource, createRoot, createSignal, createStore, getRenderer, h, jsx, jsxDEV, jsxs, lazy, onCleanup, onMount, provide, render, setRenderer, untrack, unwrap, useContext };
|
|
144
|
+
export { Dynamic, ErrorBoundary, For, Fragment, JSX, Match, Portal, Show, Suspense, Switch, batch, catchError, createContext, createEffect, createMemo, createResource, createRoot, createSelector, createSignal, createStore, getRenderer, h, jsx, jsxDEV, jsxs, lazy, onCleanup, onMount, provide, render, setRenderer, untrack, unwrap, useContext };
|
|
144
145
|
export type { Child, Context, HostNode, MatchDef, Renderer, Resource, ResourceActions, SetStoreFunction };
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ declare function batch<T>(fn: () => T): T;
|
|
|
3
3
|
declare function createSignal<T>(value: T): readonly [() => T, (next: T | ((prev: T) => T)) => void];
|
|
4
4
|
declare function createEffect(fn: () => void): void;
|
|
5
5
|
declare function createMemo<T>(fn: () => T): () => T;
|
|
6
|
+
declare function createSelector<T>(source: () => T): (key: T) => boolean;
|
|
6
7
|
declare function onCleanup(fn: Cleanup): void;
|
|
7
8
|
declare function createRoot<T>(fn: (dispose: () => void) => T): T;
|
|
8
9
|
declare function onMount(fn: () => void): void;
|
|
@@ -140,5 +141,5 @@ declare function Fragment(props: {
|
|
|
140
141
|
declare function setRenderer(renderer: Renderer): void;
|
|
141
142
|
declare function getRenderer(): Renderer;
|
|
142
143
|
|
|
143
|
-
export { Dynamic, ErrorBoundary, For, Fragment, JSX, Match, Portal, Show, Suspense, Switch, batch, catchError, createContext, createEffect, createMemo, createResource, createRoot, createSignal, createStore, getRenderer, h, jsx, jsxDEV, jsxs, lazy, onCleanup, onMount, provide, render, setRenderer, untrack, unwrap, useContext };
|
|
144
|
+
export { Dynamic, ErrorBoundary, For, Fragment, JSX, Match, Portal, Show, Suspense, Switch, batch, catchError, createContext, createEffect, createMemo, createResource, createRoot, createSelector, createSignal, createStore, getRenderer, h, jsx, jsxDEV, jsxs, lazy, onCleanup, onMount, provide, render, setRenderer, untrack, unwrap, useContext };
|
|
144
145
|
export type { Child, Context, HostNode, MatchDef, Renderer, Resource, ResourceActions, SetStoreFunction };
|
package/dist/index.js
CHANGED
|
@@ -201,6 +201,39 @@ function createMemo(fn) {
|
|
|
201
201
|
return node.value;
|
|
202
202
|
};
|
|
203
203
|
}
|
|
204
|
+
function createSelector(source) {
|
|
205
|
+
const cells = /* @__PURE__ */ new Map();
|
|
206
|
+
let previous = untrack(source);
|
|
207
|
+
createEffect(() => {
|
|
208
|
+
const next = source();
|
|
209
|
+
if (Object.is(next, previous)) return;
|
|
210
|
+
const old = previous;
|
|
211
|
+
previous = next;
|
|
212
|
+
batch(() => {
|
|
213
|
+
for (const key of [old, next]) {
|
|
214
|
+
const cell = cells.get(key);
|
|
215
|
+
if (cell) notify(cell);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
return (key) => {
|
|
220
|
+
const current2 = untrack(source);
|
|
221
|
+
if (listener) {
|
|
222
|
+
let cell = cells.get(key);
|
|
223
|
+
if (!cell) {
|
|
224
|
+
cell = { observers: /* @__PURE__ */ new Set() };
|
|
225
|
+
cells.set(key, cell);
|
|
226
|
+
}
|
|
227
|
+
track(cell);
|
|
228
|
+
const mine = cell;
|
|
229
|
+
listener.cleanups.push(() => {
|
|
230
|
+
if (mine.observers.size === 0 && cells.get(key) === mine)
|
|
231
|
+
cells.delete(key);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
return Object.is(current2, key);
|
|
235
|
+
};
|
|
236
|
+
}
|
|
204
237
|
function onCleanup(fn) {
|
|
205
238
|
if (!currentOwner) {
|
|
206
239
|
warn(
|
|
@@ -644,6 +677,65 @@ function getRenderer() {
|
|
|
644
677
|
return current;
|
|
645
678
|
}
|
|
646
679
|
|
|
680
|
+
// src/runtime/reconcile.ts
|
|
681
|
+
function longestIncreasingSubsequence(values) {
|
|
682
|
+
const predecessor = new Array(values.length);
|
|
683
|
+
const tails = [];
|
|
684
|
+
for (let i = 0; i < values.length; i++) {
|
|
685
|
+
const v = values[i];
|
|
686
|
+
if (v < 0) continue;
|
|
687
|
+
let lo;
|
|
688
|
+
if (tails.length === 0 || values[tails[tails.length - 1]] < v) {
|
|
689
|
+
lo = tails.length;
|
|
690
|
+
} else {
|
|
691
|
+
lo = 0;
|
|
692
|
+
let hi = tails.length - 1;
|
|
693
|
+
while (lo < hi) {
|
|
694
|
+
const mid = lo + hi >> 1;
|
|
695
|
+
if (values[tails[mid]] < v) lo = mid + 1;
|
|
696
|
+
else hi = mid;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
predecessor[i] = lo > 0 ? tails[lo - 1] : -1;
|
|
700
|
+
tails[lo] = i;
|
|
701
|
+
}
|
|
702
|
+
const result = new Array(tails.length);
|
|
703
|
+
let k = tails.length > 0 ? tails[tails.length - 1] : -1;
|
|
704
|
+
for (let j = tails.length - 1; j >= 0; j--) {
|
|
705
|
+
result[j] = k;
|
|
706
|
+
k = predecessor[k];
|
|
707
|
+
}
|
|
708
|
+
return result;
|
|
709
|
+
}
|
|
710
|
+
function reconcile(start, end, next) {
|
|
711
|
+
const r = getRenderer();
|
|
712
|
+
const keep = new Set(next);
|
|
713
|
+
const oldIndex = /* @__PURE__ */ new Map();
|
|
714
|
+
let count = 0;
|
|
715
|
+
let cur = r.nextSibling(start);
|
|
716
|
+
while (cur && cur !== end) {
|
|
717
|
+
const after = r.nextSibling(cur);
|
|
718
|
+
if (keep.has(cur)) oldIndex.set(cur, count++);
|
|
719
|
+
else r.remove(cur);
|
|
720
|
+
cur = after;
|
|
721
|
+
}
|
|
722
|
+
const parent = r.parentNode(end);
|
|
723
|
+
const n = next.length;
|
|
724
|
+
const positions = new Array(n);
|
|
725
|
+
for (let i = 0; i < n; i++) positions[i] = oldIndex.get(next[i]) ?? -1;
|
|
726
|
+
const stable = new Uint8Array(n);
|
|
727
|
+
for (const i of longestIncreasingSubsequence(positions)) stable[i] = 1;
|
|
728
|
+
const anchors = new Array(n);
|
|
729
|
+
let anchor = end;
|
|
730
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
731
|
+
anchors[i] = anchor;
|
|
732
|
+
if (stable[i]) anchor = next[i];
|
|
733
|
+
}
|
|
734
|
+
for (let i = 0; i < n; i++) {
|
|
735
|
+
if (!stable[i]) r.insert(parent, next[i], anchors[i]);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
647
739
|
// src/runtime/h.ts
|
|
648
740
|
function h(tag, props, ...children) {
|
|
649
741
|
const r = getRenderer();
|
|
@@ -684,22 +776,6 @@ function toNodes(value) {
|
|
|
684
776
|
}
|
|
685
777
|
return out;
|
|
686
778
|
}
|
|
687
|
-
function reconcile(start, end, next) {
|
|
688
|
-
const r = getRenderer();
|
|
689
|
-
const keep = new Set(next);
|
|
690
|
-
let cur = r.nextSibling(start);
|
|
691
|
-
while (cur && cur !== end) {
|
|
692
|
-
const after = r.nextSibling(cur);
|
|
693
|
-
if (!keep.has(cur)) r.remove(cur);
|
|
694
|
-
cur = after;
|
|
695
|
-
}
|
|
696
|
-
const parent = r.parentNode(end);
|
|
697
|
-
let ref = r.nextSibling(start);
|
|
698
|
-
for (const node of next) {
|
|
699
|
-
if (node === ref) ref = r.nextSibling(ref);
|
|
700
|
-
else r.insert(parent, node, ref);
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
779
|
function appendChildren(parent, children) {
|
|
704
780
|
const r = getRenderer();
|
|
705
781
|
for (const child of children.flat(Infinity)) {
|
|
@@ -1006,6 +1082,7 @@ export {
|
|
|
1006
1082
|
createMemo,
|
|
1007
1083
|
createResource,
|
|
1008
1084
|
createRoot,
|
|
1085
|
+
createSelector,
|
|
1009
1086
|
createSignal,
|
|
1010
1087
|
createStore,
|
|
1011
1088
|
getRenderer,
|