squishjs 0.7.66 → 1.0.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/package.json +1 -1
- package/src/BaseNode.js +5 -2
- package/src/Squisher.js +31 -3
- package/src/util/listenable.js +7 -1
- package/test/Layers.test.js +3 -0
- package/test/main.test.js +58 -1
- package/metest.js +0 -26
package/package.json
CHANGED
package/src/BaseNode.js
CHANGED
|
@@ -91,11 +91,14 @@ class BaseNode {
|
|
|
91
91
|
this.node.clearChildren(excludedNodeIds);
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
|
|
95
94
|
clone() {
|
|
96
95
|
throw new Error("Clone not implemented");
|
|
97
96
|
}
|
|
97
|
+
|
|
98
|
+
free() {
|
|
99
|
+
this.node.free();
|
|
100
|
+
}
|
|
98
101
|
}
|
|
99
102
|
|
|
100
103
|
|
|
101
|
-
module.exports = {gameNode, BaseNode};
|
|
104
|
+
module.exports = {gameNode, BaseNode};
|
package/src/Squisher.js
CHANGED
|
@@ -10,6 +10,7 @@ class Squisher {
|
|
|
10
10
|
this.ids = new Set();
|
|
11
11
|
|
|
12
12
|
this.game = game;
|
|
13
|
+
this.gameMetadata = game.constructor.metadata && game.constructor.metadata();
|
|
13
14
|
this.assets = {};
|
|
14
15
|
|
|
15
16
|
this.customBottomLayer = customBottomLayer;
|
|
@@ -113,12 +114,21 @@ class Squisher {
|
|
|
113
114
|
}
|
|
114
115
|
|
|
115
116
|
if (playerIdFilter.size > 0) {
|
|
117
|
+
|
|
118
|
+
let playerIdsToRemove = new Set();
|
|
116
119
|
for (let playerId of playerIdFilter) {
|
|
117
120
|
if (!playerMap[playerId]) {
|
|
118
121
|
playerMap[Number(playerId)] = [];
|
|
119
122
|
}
|
|
120
123
|
|
|
121
|
-
|
|
124
|
+
if (node.node.playerIds.length === 0 || node.node.playerIds.findIndex(i => Number(i) === Number(playerId)) >= 0) {
|
|
125
|
+
playerMap[playerId].push(squished);
|
|
126
|
+
} else {
|
|
127
|
+
playerIdsToRemove.add(playerId);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
for (let id of playerIdsToRemove) {
|
|
131
|
+
playerIdFilter.delete(id);
|
|
122
132
|
}
|
|
123
133
|
} else {
|
|
124
134
|
Object.keys(playerMap).forEach(playerId => {
|
|
@@ -130,12 +140,28 @@ class Squisher {
|
|
|
130
140
|
}
|
|
131
141
|
|
|
132
142
|
for (let i = 0; i < node.node.children.length; i++) {
|
|
143
|
+
// if (node.node.children[i].node.playerIds
|
|
133
144
|
// make a new set so child calls within a single generation arent
|
|
134
145
|
// modifying the same filter set
|
|
135
146
|
const pathFilter = new Set(playerIdFilter);
|
|
136
147
|
this.squishHelper(node.node.children[i], squishedNodes, scale, playerMap, pathFilter);
|
|
137
148
|
}
|
|
138
149
|
|
|
150
|
+
// debug
|
|
151
|
+
for (let k in playerMap) {
|
|
152
|
+
for (let squished in playerMap[k]) {
|
|
153
|
+
const unsquished = unsquish(playerMap[k][squished]);
|
|
154
|
+
if (unsquished.node.playerIds.length > 0 && unsquished.node.playerIds.findIndex(i => Number(i) === Number(k)) < 0) {
|
|
155
|
+
console.log(unsquished.node.playerIds);
|
|
156
|
+
console.log(k);
|
|
157
|
+
console.log(unsquished.node.playerIds.indexOf(k));
|
|
158
|
+
if (k != 254) {
|
|
159
|
+
throw new Error('fuk');
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
139
165
|
}
|
|
140
166
|
|
|
141
167
|
getJson() {
|
|
@@ -237,8 +263,10 @@ class Squisher {
|
|
|
237
263
|
}
|
|
238
264
|
|
|
239
265
|
handleStateChange(node, layerName) {
|
|
240
|
-
|
|
241
|
-
|
|
266
|
+
if (this.listeners.size > 0) {
|
|
267
|
+
this.state = this.squish(this.game.getLayers());
|
|
268
|
+
this.broadcast();
|
|
269
|
+
}
|
|
242
270
|
}
|
|
243
271
|
|
|
244
272
|
broadcast() {
|
package/src/util/listenable.js
CHANGED
|
@@ -15,7 +15,13 @@ const listenable = function(obj, onChange) {
|
|
|
15
15
|
}
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
const revocable = Proxy.revocable(obj, handler);
|
|
19
|
+
|
|
20
|
+
obj.free = () => {
|
|
21
|
+
revocable.revoke();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return revocable.proxy;
|
|
19
25
|
};
|
|
20
26
|
|
|
21
27
|
module.exports = listenable;
|
package/test/Layers.test.js
CHANGED
|
@@ -43,6 +43,9 @@ test("test layer with child added", () => {
|
|
|
43
43
|
]);
|
|
44
44
|
|
|
45
45
|
const squisher = new Squisher({ game });
|
|
46
|
+
|
|
47
|
+
// add listener so squisher cares about update
|
|
48
|
+
squisher.addListener(() => {});
|
|
46
49
|
|
|
47
50
|
const initialState = Array.from(squisher.state);
|
|
48
51
|
assert(initialState.length === 2);
|
package/test/main.test.js
CHANGED
|
@@ -4,8 +4,11 @@ const { COLORS } = require("../src/Colors");
|
|
|
4
4
|
const Shapes = require('../src/Shapes');
|
|
5
5
|
const ShapeUtils = require('../src/util/shapes');
|
|
6
6
|
const { verifyArrayEquality } = require('./utils');
|
|
7
|
+
const Squisher = require('../src/Squisher');
|
|
8
|
+
const Game = require('../src/Game');
|
|
7
9
|
|
|
8
10
|
const assert = require('assert');
|
|
11
|
+
const process = require('process');
|
|
9
12
|
|
|
10
13
|
const hypLength = (x, y) => Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
|
|
11
14
|
|
|
@@ -24,7 +27,7 @@ const compareSquished = (preSquish, unsquished) => {
|
|
|
24
27
|
} else {
|
|
25
28
|
verifyArrayEquality(preSquish.coordinates2d, unsquished.coordinates2d);
|
|
26
29
|
}
|
|
27
|
-
} else if (key === 'input' || key == 'text') {
|
|
30
|
+
} else if (key === 'input' || key == 'text' || key == 'free') {
|
|
28
31
|
// TODO: Handle all of this in a more generic way
|
|
29
32
|
} else if (preSquish[key] === undefined || preSquish[key] === null) {
|
|
30
33
|
assert(unsquished[key] === undefined || unsquished[key] === null);
|
|
@@ -354,3 +357,57 @@ test("big big text", () => {
|
|
|
354
357
|
assert(unsquishedNode.text.size.toFixed(2) === scaledTextSize.toFixed(2));
|
|
355
358
|
});
|
|
356
359
|
|
|
360
|
+
// create one hundred nodes and ensure the memory is freed
|
|
361
|
+
test("allocate a bunch of nodes", () => {
|
|
362
|
+
const initialMemUsage = process.memoryUsage();
|
|
363
|
+
|
|
364
|
+
const nodeCount = Math.pow(10, 2);
|
|
365
|
+
|
|
366
|
+
const root = new GameNode.Text({
|
|
367
|
+
textInfo: {
|
|
368
|
+
text: 'I am root',
|
|
369
|
+
x: 4,
|
|
370
|
+
y: 20,
|
|
371
|
+
size: 5,
|
|
372
|
+
align: 'center',
|
|
373
|
+
color: COLORS.RED
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
const sizeEstimate = squish(root).length;
|
|
378
|
+
|
|
379
|
+
const fakeGame = new Game();
|
|
380
|
+
fakeGame.getLayers = () => {
|
|
381
|
+
return [
|
|
382
|
+
{
|
|
383
|
+
root
|
|
384
|
+
}
|
|
385
|
+
]
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
const squisher = new Squisher({game: fakeGame});
|
|
389
|
+
squisher.addListener(() => {
|
|
390
|
+
// necessary to hold the reference to the proxy that we should be revoking when we call delete
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
for (let i = 0; i < nodeCount; i++) {
|
|
394
|
+
const gameNode = new GameNode.Text({
|
|
395
|
+
textInfo: {
|
|
396
|
+
text: 'I am going to write a whoooooole lot of text like more than 64 characters which is quite a bit. Not like a tweet long but still long, you know?',
|
|
397
|
+
x: 4,
|
|
398
|
+
y: 20,
|
|
399
|
+
size: 5,
|
|
400
|
+
align: 'center',
|
|
401
|
+
color: COLORS.RED
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
root.addChild(gameNode);
|
|
406
|
+
root.removeChild(gameNode.node.id);
|
|
407
|
+
gameNode.free();
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const postMemUsage = process.memoryUsage();
|
|
411
|
+
assert(postMemUsage.heapTotal - initialMemUsage.heapTotal <= (.05 * initialMemUsage.heapTotal));
|
|
412
|
+
});
|
|
413
|
+
|
package/metest.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
const { GameNode, squish, unsquish } = require('./');
|
|
2
|
-
|
|
3
|
-
const ting = new GameNode.Shape({
|
|
4
|
-
coordinates2d: [
|
|
5
|
-
[0, 0],
|
|
6
|
-
[0, 0],
|
|
7
|
-
[0, 0],
|
|
8
|
-
[0, 0]
|
|
9
|
-
],
|
|
10
|
-
shapeType: 1
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
ting.node.id = 529025047768;
|
|
14
|
-
ting.id = 529025047768;
|
|
15
|
-
|
|
16
|
-
const squished = squish(ting);
|
|
17
|
-
|
|
18
|
-
const unsquished = unsquish(squished);
|
|
19
|
-
|
|
20
|
-
if (unsquished.node.id != Number(529025047768)) {
|
|
21
|
-
console.log('fuck man');
|
|
22
|
-
console.log(unsquished.node.id);
|
|
23
|
-
console.log(Number(ting.node.id));
|
|
24
|
-
} else {
|
|
25
|
-
console.log('um what');
|
|
26
|
-
}
|