@nymphjs/client 1.0.0-alpha.2 → 1.0.0-alpha.23
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/CHANGELOG.md +102 -0
- package/README.md +13 -32
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/lib/Entity.d.ts +6 -3
- package/lib/Entity.js +71 -18
- package/lib/Entity.js.map +1 -1
- package/lib/Entity.types.d.ts +3 -0
- package/lib/EntityWeakCache.d.ts +6 -0
- package/lib/EntityWeakCache.js +31 -0
- package/lib/EntityWeakCache.js.map +1 -0
- package/lib/Nymph.d.ts +44 -35
- package/lib/Nymph.js +69 -46
- package/lib/Nymph.js.map +1 -1
- package/lib/Nymph.types.d.ts +1 -0
- package/lib/PubSub.d.ts +40 -39
- package/lib/PubSub.js +54 -87
- package/lib/PubSub.js.map +1 -1
- package/lib/index.d.ts +0 -3
- package/lib/index.js +1 -4
- package/lib/index.js.map +1 -1
- package/lib/utils.d.ts +2 -1
- package/lib/utils.js +10 -13
- package/lib/utils.js.map +1 -1
- package/package.json +10 -10
- package/src/Entity.ts +120 -22
- package/src/Entity.types.ts +10 -0
- package/src/EntityWeakCache.ts +32 -0
- package/src/Nymph.ts +118 -98
- package/src/Nymph.types.ts +19 -0
- package/src/PubSub.ts +59 -102
- package/src/index.ts +0 -4
- package/src/utils.ts +7 -12
- package/lib/EntitySorter.d.ts +0 -16
- package/lib/EntitySorter.js +0 -134
- package/lib/EntitySorter.js.map +0 -1
- package/src/EntitySorter.ts +0 -186
package/src/EntitySorter.ts
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
export default class EntitySorter<
|
|
2
|
-
Entity extends new () => Object,
|
|
3
|
-
Nymph extends {
|
|
4
|
-
getEntityClass(className: string): Entity;
|
|
5
|
-
}
|
|
6
|
-
> {
|
|
7
|
-
private array: (Entity & { [k: string]: any })[];
|
|
8
|
-
private sortProperty: string | null = null;
|
|
9
|
-
private sortParent: string | null = null;
|
|
10
|
-
private sortCaseSensitive: boolean | null = null;
|
|
11
|
-
|
|
12
|
-
private nymph: Nymph;
|
|
13
|
-
|
|
14
|
-
constructor(array: Entity[], nymph: Nymph) {
|
|
15
|
-
this.array = array as (Entity & { [k: string]: any })[];
|
|
16
|
-
this.nymph = nymph;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
private _arraySortProperty(
|
|
20
|
-
a: Entity & { [k: string]: any },
|
|
21
|
-
b: Entity & { [k: string]: any }
|
|
22
|
-
) {
|
|
23
|
-
let prop = this.sortProperty as string;
|
|
24
|
-
let parent = this.sortParent as string;
|
|
25
|
-
const Entity = this.nymph.getEntityClass('Entity');
|
|
26
|
-
if (
|
|
27
|
-
parent != null &&
|
|
28
|
-
a[parent] instanceof Entity &&
|
|
29
|
-
b[parent] instanceof Entity
|
|
30
|
-
) {
|
|
31
|
-
const aParentProp = a[parent][prop];
|
|
32
|
-
const bParentProp = b[parent][prop];
|
|
33
|
-
if (
|
|
34
|
-
typeof aParentProp !== 'undefined' ||
|
|
35
|
-
typeof bParentProp !== 'undefined'
|
|
36
|
-
) {
|
|
37
|
-
if (
|
|
38
|
-
!this.sortCaseSensitive &&
|
|
39
|
-
typeof aParentProp === 'string' &&
|
|
40
|
-
typeof bParentProp === 'string'
|
|
41
|
-
) {
|
|
42
|
-
const asort = aParentProp.toUpperCase();
|
|
43
|
-
const bsort = bParentProp.toUpperCase();
|
|
44
|
-
if (asort !== bsort) {
|
|
45
|
-
return asort.localeCompare(bsort);
|
|
46
|
-
}
|
|
47
|
-
} else {
|
|
48
|
-
if (aParentProp > bParentProp) {
|
|
49
|
-
return 1;
|
|
50
|
-
}
|
|
51
|
-
if (aParentProp < bParentProp) {
|
|
52
|
-
return -1;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
// If they have the same parent, order them by their own prop.
|
|
58
|
-
const aProp = a[prop];
|
|
59
|
-
const bProp = b[prop];
|
|
60
|
-
if (
|
|
61
|
-
!this.sortCaseSensitive &&
|
|
62
|
-
typeof aProp === 'string' &&
|
|
63
|
-
typeof bProp === 'string'
|
|
64
|
-
) {
|
|
65
|
-
const asort = aProp.toUpperCase();
|
|
66
|
-
const bsort = bProp.toUpperCase();
|
|
67
|
-
return asort.localeCompare(bsort);
|
|
68
|
-
} else {
|
|
69
|
-
if (aProp > bProp) {
|
|
70
|
-
return 1;
|
|
71
|
-
}
|
|
72
|
-
if (aProp < bProp) {
|
|
73
|
-
return -1;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return 0;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public hsort(
|
|
80
|
-
property: string,
|
|
81
|
-
parentProperty: string,
|
|
82
|
-
caseSensitive = false,
|
|
83
|
-
reverse = false
|
|
84
|
-
) {
|
|
85
|
-
// First sort by the requested property.
|
|
86
|
-
this.sort(property, caseSensitive, reverse);
|
|
87
|
-
if (typeof parentProperty === 'undefined' || parentProperty === null) {
|
|
88
|
-
return this.array;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Now sort by children.
|
|
92
|
-
let newArray: (Entity & { [k: string]: any })[] = [];
|
|
93
|
-
// Look for entities ready to go in order.
|
|
94
|
-
let changed: boolean;
|
|
95
|
-
while (this.array.length) {
|
|
96
|
-
changed = false;
|
|
97
|
-
for (let key = 0; key < this.array.length; key++) {
|
|
98
|
-
// Must break after adding one, so any following children don't go in
|
|
99
|
-
// the wrong order.
|
|
100
|
-
if (
|
|
101
|
-
this.array[key][parentProperty] == null ||
|
|
102
|
-
typeof this.array[key][parentProperty].$inArray !== 'function' ||
|
|
103
|
-
!this.array[key][parentProperty].$inArray(newArray.concat(this.array))
|
|
104
|
-
) {
|
|
105
|
-
// If they have no parent (or their parent isn't in the array), they
|
|
106
|
-
// go on the end.
|
|
107
|
-
newArray.push(this.array[key]);
|
|
108
|
-
this.array.splice(key, 1);
|
|
109
|
-
changed = true;
|
|
110
|
-
break;
|
|
111
|
-
} else if (
|
|
112
|
-
typeof this.array[key][parentProperty].$arraySearch === 'function'
|
|
113
|
-
) {
|
|
114
|
-
// Else find the parent.
|
|
115
|
-
const pkey = this.array[key][parentProperty].$arraySearch(newArray);
|
|
116
|
-
if (pkey !== -1) {
|
|
117
|
-
// And insert after the parent.
|
|
118
|
-
// This makes entities go to the end of the child list.
|
|
119
|
-
const ancestry = [this.array[key][parentProperty].guid];
|
|
120
|
-
let newKey = Number(pkey);
|
|
121
|
-
while (
|
|
122
|
-
typeof newArray[newKey + 1] !== 'undefined' &&
|
|
123
|
-
newArray[newKey + 1][parentProperty] != null &&
|
|
124
|
-
ancestry.indexOf(newArray[newKey + 1][parentProperty].guid) !== -1
|
|
125
|
-
) {
|
|
126
|
-
ancestry.push(newArray[newKey + 1].guid);
|
|
127
|
-
newKey += 1;
|
|
128
|
-
}
|
|
129
|
-
// Where to place the entity.
|
|
130
|
-
newKey += 1;
|
|
131
|
-
if (typeof newArray[newKey] !== 'undefined') {
|
|
132
|
-
// If it already exists, we have to splice it in.
|
|
133
|
-
newArray.splice(newKey, 0, this.array[key]);
|
|
134
|
-
} else {
|
|
135
|
-
// Else just add it.
|
|
136
|
-
newArray.push(this.array[key]);
|
|
137
|
-
}
|
|
138
|
-
this.array.splice(key, 1);
|
|
139
|
-
changed = true;
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
if (!changed) {
|
|
145
|
-
// If there are any unexpected errors and the array isn't changed, just
|
|
146
|
-
// stick the rest on the end.
|
|
147
|
-
if (this.array.length) {
|
|
148
|
-
newArray = newArray.concat(this.array);
|
|
149
|
-
this.array.splice(0, this.array.length);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
// Now push the new array out.
|
|
154
|
-
this.array.splice(0, 0, ...newArray);
|
|
155
|
-
return this.array as Entity[];
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
public psort(
|
|
159
|
-
property: string,
|
|
160
|
-
parentProperty: string,
|
|
161
|
-
caseSensitive = false,
|
|
162
|
-
reverse = false
|
|
163
|
-
) {
|
|
164
|
-
// Sort by the requested property.
|
|
165
|
-
this.sortProperty = property;
|
|
166
|
-
this.sortParent = parentProperty;
|
|
167
|
-
this.sortCaseSensitive = !!caseSensitive;
|
|
168
|
-
this.array.sort(this._arraySortProperty.bind(this));
|
|
169
|
-
if (reverse) {
|
|
170
|
-
this.array.reverse();
|
|
171
|
-
}
|
|
172
|
-
return this.array as Entity[];
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
public sort(property: string, caseSensitive = false, reverse = false) {
|
|
176
|
-
// Sort by the requested property.
|
|
177
|
-
this.sortProperty = property;
|
|
178
|
-
this.sortParent = null;
|
|
179
|
-
this.sortCaseSensitive = !!caseSensitive;
|
|
180
|
-
this.array.sort(this._arraySortProperty.bind(this));
|
|
181
|
-
if (reverse) {
|
|
182
|
-
this.array.reverse();
|
|
183
|
-
}
|
|
184
|
-
return this.array as Entity[];
|
|
185
|
-
}
|
|
186
|
-
}
|