@nemigo/svelte 2.9.0 → 2.10.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/heap.d.ts +26 -0
- package/dist/heap.js +37 -0
- package/dist/list.d.ts +33 -0
- package/dist/list.js +70 -0
- package/dist/record.d.ts +22 -0
- package/dist/record.js +42 -0
- package/dist/tree.d.ts +51 -0
- package/dist/tree.js +132 -0
- package/package.json +18 -2
package/dist/heap.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Emitter } from "@nemigo/helpers/emitter";
|
|
2
|
+
import { SvelteMap } from "svelte/reactivity";
|
|
3
|
+
import type { ISvelteState } from "./index.svelte.js";
|
|
4
|
+
export interface HeapRecord {
|
|
5
|
+
id: string;
|
|
6
|
+
}
|
|
7
|
+
export interface HeapItem<R extends HeapRecord> {
|
|
8
|
+
record: ISvelteState<R>;
|
|
9
|
+
}
|
|
10
|
+
export declare abstract class SvelteHeap<R extends HeapRecord, Item extends HeapItem<R> = HeapItem<R>> extends Emitter<{
|
|
11
|
+
"before-create": Item;
|
|
12
|
+
"after-create": Item;
|
|
13
|
+
"before-update": Item;
|
|
14
|
+
"after-update": Item;
|
|
15
|
+
"before-delete": Item;
|
|
16
|
+
"after-delete": Item;
|
|
17
|
+
}> {
|
|
18
|
+
map: SvelteMap<string, Item>;
|
|
19
|
+
abstract toHeapItem(record: R): Item;
|
|
20
|
+
hooks: {
|
|
21
|
+
create: (record: R) => void;
|
|
22
|
+
update: (record: R, mods: any) => void;
|
|
23
|
+
delete: (id: string) => void;
|
|
24
|
+
};
|
|
25
|
+
__build(records: R[]): void;
|
|
26
|
+
}
|
package/dist/heap.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { isDataEqual } from "@nemigo/helpers";
|
|
2
|
+
import { Emitter } from "@nemigo/helpers/emitter";
|
|
3
|
+
import { SvelteMap } from "svelte/reactivity";
|
|
4
|
+
export class SvelteHeap extends Emitter {
|
|
5
|
+
map = new SvelteMap();
|
|
6
|
+
hooks = {
|
|
7
|
+
create: (record) => {
|
|
8
|
+
const item = this.toHeapItem(record);
|
|
9
|
+
this.dispatch("before-create", item);
|
|
10
|
+
this.map.set(record.id, item);
|
|
11
|
+
this.dispatch("after-create", item);
|
|
12
|
+
},
|
|
13
|
+
update: (record, mods) => {
|
|
14
|
+
const item = this.map.get(record.id);
|
|
15
|
+
if (!item)
|
|
16
|
+
return;
|
|
17
|
+
const result = { ...item.record.signal, ...mods };
|
|
18
|
+
if (isDataEqual(record, result))
|
|
19
|
+
return;
|
|
20
|
+
this.dispatch("before-update", item);
|
|
21
|
+
item.record.signal = result;
|
|
22
|
+
this.dispatch("after-update", item);
|
|
23
|
+
},
|
|
24
|
+
delete: (id) => {
|
|
25
|
+
const item = this.map.get(id);
|
|
26
|
+
if (!item)
|
|
27
|
+
return;
|
|
28
|
+
this.dispatch("before-update", item);
|
|
29
|
+
this.map.delete(id);
|
|
30
|
+
this.dispatch("after-update", item);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
__build(records) {
|
|
34
|
+
for (const r of records)
|
|
35
|
+
this.map.set(r.id, this.toHeapItem(r));
|
|
36
|
+
}
|
|
37
|
+
}
|
package/dist/list.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Emitter } from "@nemigo/helpers/emitter";
|
|
2
|
+
import type { ISvelteState } from "./index.svelte.js";
|
|
3
|
+
export interface ListRecord {
|
|
4
|
+
id: string;
|
|
5
|
+
}
|
|
6
|
+
export interface ListItem<R extends ListRecord> {
|
|
7
|
+
record: ISvelteState<R>;
|
|
8
|
+
}
|
|
9
|
+
export declare abstract class SvelteList<R extends ListRecord, Item extends ListItem<R> = ListItem<R>> extends Emitter<{
|
|
10
|
+
"before-create": Item;
|
|
11
|
+
"after-create": Item;
|
|
12
|
+
"before-update": Item;
|
|
13
|
+
"after-update": Item;
|
|
14
|
+
"before-delete": Item;
|
|
15
|
+
"after-delete": Item;
|
|
16
|
+
}> {
|
|
17
|
+
items: ISvelteState<Item[]>;
|
|
18
|
+
map: Map<string, Item>;
|
|
19
|
+
size: ISvelteState<number>;
|
|
20
|
+
abstract toListItem(record: R): Item;
|
|
21
|
+
abstract sort(a: Item, b: Item): number;
|
|
22
|
+
hooks: {
|
|
23
|
+
create: (record: R) => void;
|
|
24
|
+
update: (record: R, mods: any) => void;
|
|
25
|
+
delete: (id: string) => void;
|
|
26
|
+
};
|
|
27
|
+
__sort(): void;
|
|
28
|
+
__push(item: Item): void;
|
|
29
|
+
__splice(idx: number): void;
|
|
30
|
+
__delete(item: Item): void;
|
|
31
|
+
clear(): void;
|
|
32
|
+
__build(records: R[]): void;
|
|
33
|
+
}
|
package/dist/list.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { isDataEqual } from "@nemigo/helpers";
|
|
2
|
+
import { Emitter } from "@nemigo/helpers/emitter";
|
|
3
|
+
import { createSvelteRawState } from "./index.svelte.js";
|
|
4
|
+
export class SvelteList extends Emitter {
|
|
5
|
+
items = createSvelteRawState([]);
|
|
6
|
+
map = new Map();
|
|
7
|
+
size = createSvelteRawState(0);
|
|
8
|
+
hooks = {
|
|
9
|
+
create: (record) => {
|
|
10
|
+
const item = this.toListItem(record);
|
|
11
|
+
this.dispatch("before-create", item);
|
|
12
|
+
this.map.set(record.id, item);
|
|
13
|
+
this.__push(item);
|
|
14
|
+
this.size.signal = this.map.size;
|
|
15
|
+
this.dispatch("after-create", item);
|
|
16
|
+
},
|
|
17
|
+
update: (record, mods) => {
|
|
18
|
+
const item = this.map.get(record.id);
|
|
19
|
+
if (!item)
|
|
20
|
+
return;
|
|
21
|
+
const result = { ...item.record.signal, ...mods };
|
|
22
|
+
if (isDataEqual(record, result))
|
|
23
|
+
return;
|
|
24
|
+
this.dispatch("before-update", item);
|
|
25
|
+
item.record.signal = result;
|
|
26
|
+
this.__sort();
|
|
27
|
+
this.dispatch("after-update", item);
|
|
28
|
+
},
|
|
29
|
+
delete: (id) => {
|
|
30
|
+
const item = this.map.get(id);
|
|
31
|
+
if (!item)
|
|
32
|
+
return;
|
|
33
|
+
this.dispatch("before-delete", item);
|
|
34
|
+
this.map.delete(id);
|
|
35
|
+
this.__delete(item);
|
|
36
|
+
this.size.signal = this.map.size;
|
|
37
|
+
this.dispatch("after-delete", item);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
__sort() {
|
|
41
|
+
this.items.signal = this.items.signal.toSorted((a, b) => this.sort(a, b));
|
|
42
|
+
}
|
|
43
|
+
__push(item) {
|
|
44
|
+
this.items.signal.push(item);
|
|
45
|
+
this.__sort();
|
|
46
|
+
}
|
|
47
|
+
__splice(idx) {
|
|
48
|
+
this.items.signal.splice(idx, 1);
|
|
49
|
+
this.items.signal = this.items.signal.slice();
|
|
50
|
+
}
|
|
51
|
+
__delete(item) {
|
|
52
|
+
const idx = this.items.signal.indexOf(item);
|
|
53
|
+
if (idx !== -1)
|
|
54
|
+
this.__splice(idx);
|
|
55
|
+
}
|
|
56
|
+
clear() {
|
|
57
|
+
this.items.signal = [];
|
|
58
|
+
this.map.clear();
|
|
59
|
+
this.size.signal = 0;
|
|
60
|
+
}
|
|
61
|
+
__build(records) {
|
|
62
|
+
for (const r of records) {
|
|
63
|
+
const item = this.toListItem(r);
|
|
64
|
+
this.map.set(r.id, item);
|
|
65
|
+
this.items.signal.push(item);
|
|
66
|
+
}
|
|
67
|
+
this.__sort();
|
|
68
|
+
this.size.signal = this.map.size;
|
|
69
|
+
}
|
|
70
|
+
}
|
package/dist/record.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Emitter } from "@nemigo/helpers/emitter";
|
|
2
|
+
import type { ISvelteState } from "./index.svelte.js";
|
|
3
|
+
export declare abstract class SvelteRecord<R extends {
|
|
4
|
+
id: string;
|
|
5
|
+
}> extends Emitter<{
|
|
6
|
+
"before-create": R;
|
|
7
|
+
"after-create": R;
|
|
8
|
+
"before-update": R;
|
|
9
|
+
"after-update": R;
|
|
10
|
+
"before-delete": R;
|
|
11
|
+
"after-delete": R;
|
|
12
|
+
}> {
|
|
13
|
+
id: string;
|
|
14
|
+
record: ISvelteState<R | null>;
|
|
15
|
+
constructor(id: string);
|
|
16
|
+
hooks: {
|
|
17
|
+
create: (record: R) => void;
|
|
18
|
+
update: (record: R, mods: any) => void;
|
|
19
|
+
delete: (id: string) => void;
|
|
20
|
+
};
|
|
21
|
+
__build(record: R | null): void;
|
|
22
|
+
}
|
package/dist/record.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { isDataEqual } from "@nemigo/helpers";
|
|
2
|
+
import { Emitter } from "@nemigo/helpers/emitter";
|
|
3
|
+
import { createSvelteRawState } from "./index.svelte.js";
|
|
4
|
+
export class SvelteRecord extends Emitter {
|
|
5
|
+
id;
|
|
6
|
+
record = createSvelteRawState(null);
|
|
7
|
+
constructor(id) {
|
|
8
|
+
super();
|
|
9
|
+
this.id = id;
|
|
10
|
+
}
|
|
11
|
+
hooks = {
|
|
12
|
+
create: (record) => {
|
|
13
|
+
if (record.id !== this.id)
|
|
14
|
+
return;
|
|
15
|
+
this.dispatch("before-create", record);
|
|
16
|
+
this.record.signal = record;
|
|
17
|
+
this.dispatch("after-create", record);
|
|
18
|
+
},
|
|
19
|
+
update: (record, mods) => {
|
|
20
|
+
const current = this.record.signal;
|
|
21
|
+
if (!current || current.id !== record.id)
|
|
22
|
+
return;
|
|
23
|
+
const result = { ...current, ...mods };
|
|
24
|
+
if (isDataEqual(current, result))
|
|
25
|
+
return;
|
|
26
|
+
this.dispatch("before-update", result);
|
|
27
|
+
this.record.signal = result;
|
|
28
|
+
this.dispatch("after-update", result);
|
|
29
|
+
},
|
|
30
|
+
delete: (id) => {
|
|
31
|
+
const current = this.record.signal;
|
|
32
|
+
if (!current || current.id !== id)
|
|
33
|
+
return;
|
|
34
|
+
this.dispatch("before-delete", current);
|
|
35
|
+
this.record.signal = null;
|
|
36
|
+
this.dispatch("after-delete", current);
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
__build(record) {
|
|
40
|
+
this.record.signal = record;
|
|
41
|
+
}
|
|
42
|
+
}
|
package/dist/tree.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Emitter } from "@nemigo/helpers/emitter";
|
|
2
|
+
import type { ISvelteState } from "./index.svelte.js";
|
|
3
|
+
export interface TreeRecord {
|
|
4
|
+
id: string;
|
|
5
|
+
parent_id: string | null;
|
|
6
|
+
}
|
|
7
|
+
export interface TreeItem<R extends TreeRecord, Item extends TreeItem<R, any>> {
|
|
8
|
+
child: ISvelteState<Item[]>;
|
|
9
|
+
record: ISvelteState<R>;
|
|
10
|
+
}
|
|
11
|
+
export interface BeforeParentUpdateTreeEvent<R extends TreeRecord, Item extends TreeItem<R, any>> {
|
|
12
|
+
prev: string | null;
|
|
13
|
+
item: Item;
|
|
14
|
+
preview: R;
|
|
15
|
+
mods: any;
|
|
16
|
+
}
|
|
17
|
+
export interface AfterParentUpdateTreeEvent<Item extends TreeItem<TreeRecord, any>> {
|
|
18
|
+
prev: string | null;
|
|
19
|
+
item: Item;
|
|
20
|
+
mods: any;
|
|
21
|
+
}
|
|
22
|
+
export declare abstract class SvelteTree<R extends TreeRecord, Item extends TreeItem<R, any> = TreeItem<R, any>> extends Emitter<{
|
|
23
|
+
"before-create": Item;
|
|
24
|
+
"after-create": Item;
|
|
25
|
+
"before-update": Item;
|
|
26
|
+
"after-update": Item;
|
|
27
|
+
"before-parent-update": BeforeParentUpdateTreeEvent<R, Item>;
|
|
28
|
+
"after-parent-update": AfterParentUpdateTreeEvent<Item>;
|
|
29
|
+
"after-delete-child": Item;
|
|
30
|
+
"before-delete-child": Item;
|
|
31
|
+
"before-delete": Item;
|
|
32
|
+
"after-delete": Item;
|
|
33
|
+
}> {
|
|
34
|
+
items: ISvelteState<Item[]>;
|
|
35
|
+
map: Map<string, Item>;
|
|
36
|
+
size: ISvelteState<number>;
|
|
37
|
+
abstract toTreeItem(record: R): Item;
|
|
38
|
+
abstract sort(a: Item, b: Item): number;
|
|
39
|
+
hooks: {
|
|
40
|
+
create: (record: R) => void;
|
|
41
|
+
update: (record: R, mods: any) => void;
|
|
42
|
+
delete: (id: string) => void;
|
|
43
|
+
};
|
|
44
|
+
__sort(items: ISvelteState<Item[]>): void;
|
|
45
|
+
__push(items: ISvelteState<Item[]>, item: Item): void;
|
|
46
|
+
__splice(items: ISvelteState<Item[]>, idx: number): void;
|
|
47
|
+
__delete(items: ISvelteState<Item[]>, item: Item): void;
|
|
48
|
+
__parent(item: Item, call: (items: ISvelteState<Item[]>) => void): void;
|
|
49
|
+
clear(): void;
|
|
50
|
+
__build(records: R[]): void;
|
|
51
|
+
}
|
package/dist/tree.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { isDataEqual } from "@nemigo/helpers";
|
|
2
|
+
import { Emitter } from "@nemigo/helpers/emitter";
|
|
3
|
+
import { createSvelteRawState } from "./index.svelte.js";
|
|
4
|
+
export class SvelteTree extends Emitter {
|
|
5
|
+
items = createSvelteRawState([]);
|
|
6
|
+
map = new Map();
|
|
7
|
+
size = createSvelteRawState(0);
|
|
8
|
+
hooks = {
|
|
9
|
+
create: (record) => {
|
|
10
|
+
const item = this.toTreeItem(record);
|
|
11
|
+
this.dispatch("before-create", item);
|
|
12
|
+
this.map.set(record.id, item);
|
|
13
|
+
this.__parent(item, (items) => {
|
|
14
|
+
this.__push(items, item);
|
|
15
|
+
});
|
|
16
|
+
this.size.signal = this.map.size;
|
|
17
|
+
this.dispatch("after-create", item);
|
|
18
|
+
},
|
|
19
|
+
update: (record, mods) => {
|
|
20
|
+
const item = this.map.get(record.id);
|
|
21
|
+
if (!item)
|
|
22
|
+
return;
|
|
23
|
+
const preview = { ...item.record.signal, ...mods };
|
|
24
|
+
if (isDataEqual(record, preview))
|
|
25
|
+
return;
|
|
26
|
+
const prev_parent_id = record.parent_id;
|
|
27
|
+
const mod_parent_id = mods.parent_id;
|
|
28
|
+
const isParentChange = mod_parent_id !== undefined && mod_parent_id !== prev_parent_id;
|
|
29
|
+
if (isParentChange) {
|
|
30
|
+
this.dispatch("before-parent-update", {
|
|
31
|
+
prev: prev_parent_id,
|
|
32
|
+
item,
|
|
33
|
+
preview,
|
|
34
|
+
mods,
|
|
35
|
+
});
|
|
36
|
+
this.__parent(item, (items) => {
|
|
37
|
+
this.__delete(items, item);
|
|
38
|
+
});
|
|
39
|
+
item.record.signal = preview;
|
|
40
|
+
this.__parent(item, (items) => {
|
|
41
|
+
this.__push(items, item);
|
|
42
|
+
});
|
|
43
|
+
this.dispatch("after-parent-update", {
|
|
44
|
+
prev: prev_parent_id,
|
|
45
|
+
item,
|
|
46
|
+
mods,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
this.dispatch("before-update", item);
|
|
51
|
+
item.record.signal = preview;
|
|
52
|
+
this.__parent(item, (items) => {
|
|
53
|
+
this.__sort(items);
|
|
54
|
+
});
|
|
55
|
+
this.dispatch("after-update", item);
|
|
56
|
+
}
|
|
57
|
+
this.size.signal = this.map.size;
|
|
58
|
+
},
|
|
59
|
+
delete: (id) => {
|
|
60
|
+
const item = this.map.get(id);
|
|
61
|
+
if (!item)
|
|
62
|
+
return;
|
|
63
|
+
this.dispatch("before-delete", item);
|
|
64
|
+
const delete_child = (it) => {
|
|
65
|
+
this.dispatch("before-delete-child", it);
|
|
66
|
+
for (const c of it.child.signal)
|
|
67
|
+
delete_child(c);
|
|
68
|
+
this.map.delete(it.record.signal.id);
|
|
69
|
+
this.dispatch("after-delete-child", it);
|
|
70
|
+
};
|
|
71
|
+
delete_child(item);
|
|
72
|
+
this.__parent(item, (items) => {
|
|
73
|
+
this.__delete(items, item);
|
|
74
|
+
});
|
|
75
|
+
this.size.signal = this.map.size;
|
|
76
|
+
this.dispatch("after-delete", item);
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
__sort(items) {
|
|
80
|
+
items.signal = items.signal.toSorted((a, b) => this.sort(a, b)); // Триггерем эффекты
|
|
81
|
+
}
|
|
82
|
+
__push(items, item) {
|
|
83
|
+
items.signal.push(item);
|
|
84
|
+
this.__sort(items); // Триггерем эффекты
|
|
85
|
+
}
|
|
86
|
+
__splice(items, idx) {
|
|
87
|
+
items.signal.splice(idx, 1);
|
|
88
|
+
items.signal = items.signal.slice(); // Триггерем эффекты
|
|
89
|
+
}
|
|
90
|
+
__delete(items, item) {
|
|
91
|
+
const idx = items.signal.indexOf(item);
|
|
92
|
+
if (idx !== -1)
|
|
93
|
+
this.__splice(items, idx);
|
|
94
|
+
}
|
|
95
|
+
__parent(item, call) {
|
|
96
|
+
const parent_id = item.record.signal.parent_id;
|
|
97
|
+
if (parent_id) {
|
|
98
|
+
const parent = this.map.get(parent_id);
|
|
99
|
+
if (parent)
|
|
100
|
+
call(parent.child);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
call(this.items);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
clear() {
|
|
107
|
+
this.items.signal = [];
|
|
108
|
+
this.map.clear();
|
|
109
|
+
this.size.signal = 0;
|
|
110
|
+
}
|
|
111
|
+
__build(records) {
|
|
112
|
+
// Первый проход: создаём все элементы и строим Map
|
|
113
|
+
for (const r of records)
|
|
114
|
+
this.map.set(r.id, this.toTreeItem(r));
|
|
115
|
+
// Второй проход: строим дерево
|
|
116
|
+
for (const r of records) {
|
|
117
|
+
const item = this.map.get(r.id);
|
|
118
|
+
const parent_id = r.parent_id;
|
|
119
|
+
if (parent_id)
|
|
120
|
+
this.map.get(parent_id)?.child.signal.push(item);
|
|
121
|
+
else
|
|
122
|
+
this.items.signal.push(item);
|
|
123
|
+
}
|
|
124
|
+
// Третий проход: всё сортируем
|
|
125
|
+
for (const r of records) {
|
|
126
|
+
const item = this.map.get(r.id);
|
|
127
|
+
this.__sort(item.child);
|
|
128
|
+
}
|
|
129
|
+
this.__sort(this.items);
|
|
130
|
+
this.size.signal = this.map.size;
|
|
131
|
+
}
|
|
132
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nemigo/svelte",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.10.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "MPL-2.0",
|
|
6
6
|
"author": {
|
|
@@ -26,21 +26,37 @@
|
|
|
26
26
|
"types": "./dist/kit/navigation.d.ts",
|
|
27
27
|
"svelte": "./dist/kit/navigation.js"
|
|
28
28
|
},
|
|
29
|
+
"./heap": {
|
|
30
|
+
"types": "./dist/heap.d.ts",
|
|
31
|
+
"svelte": "./dist/heap.js"
|
|
32
|
+
},
|
|
33
|
+
"./list": {
|
|
34
|
+
"types": "./dist/list.d.ts",
|
|
35
|
+
"svelte": "./dist/list.js"
|
|
36
|
+
},
|
|
29
37
|
"./loader": {
|
|
30
38
|
"types": "./dist/loader.d.ts",
|
|
31
39
|
"svelte": "./dist/loader.js"
|
|
32
40
|
},
|
|
41
|
+
"./record": {
|
|
42
|
+
"types": "./dist/record.d.ts",
|
|
43
|
+
"svelte": "./dist/record.js"
|
|
44
|
+
},
|
|
33
45
|
"./transitions": {
|
|
34
46
|
"types": "./dist/transitions.d.ts",
|
|
35
47
|
"default": "./dist/transitions.js"
|
|
36
48
|
},
|
|
49
|
+
"./tree": {
|
|
50
|
+
"types": "./dist/tree.d.ts",
|
|
51
|
+
"default": "./dist/tree.js"
|
|
52
|
+
},
|
|
37
53
|
"./types": {
|
|
38
54
|
"types": "./dist/types.d.ts",
|
|
39
55
|
"default": "./dist/types.js"
|
|
40
56
|
}
|
|
41
57
|
},
|
|
42
58
|
"peerDependencies": {
|
|
43
|
-
"@nemigo/helpers": ">=2.
|
|
59
|
+
"@nemigo/helpers": ">=2.10.0",
|
|
44
60
|
"@sveltejs/kit": ">=2.49.0",
|
|
45
61
|
"svelte": ">=5.46.0"
|
|
46
62
|
},
|