refactor: gather

This commit is contained in:
cha0s 2021-01-22 17:58:45 -06:00
parent 6eb66403cf
commit d13a48365f
32 changed files with 65 additions and 98 deletions

View File

@ -1,4 +1,3 @@
import {resource} from '@avocado/resource';
import {Latus} from '@latus/core';
import {expect} from 'chai';
@ -21,7 +20,7 @@ beforeEach(async () => {
'@avocado/traits': require('@avocado/traits'),
});
await Promise.all(latus.invokeFlat('@latus/core/starting'));
({fromResourceType: {Entity}} = resource(latus));
({Entity} = latus.get('%resources'));
});
describe('Behaved', () => {
let entity;

View File

@ -1,9 +1,9 @@
import {packets, Packet} from '@latus/socket';
import {Packet} from '@latus/socket';
export default (latus) => class EntityListUpdateEntityPacket extends Packet {
static pack(data) {
const {fromType: {Bundle}} = packets(latus);
const {Bundle} = latus.get('%packets');
for (let i = 0; i < data.length; i++) {
// eslint-disable-next-line no-param-reassign
data[i].packets = Bundle.encode(data[i].packets);
@ -21,7 +21,7 @@ export default (latus) => class EntityListUpdateEntityPacket extends Packet {
}
static unpack(data) {
const {fromType: {Bundle}} = packets(latus);
const {Bundle} = latus.get('%packets');
for (let i = 0; i < data.length; i++) {
// eslint-disable-next-line no-param-reassign
data[i].packets = Bundle.decode(data[i].packets);

View File

@ -1,14 +1,12 @@
import {SynchronizedUpdatePacket} from '@avocado/s13n';
import {traits} from '@avocado/traits';
import {packets} from '@latus/socket';
export default (latus) => class EntityUpdateTraitPacket extends SynchronizedUpdatePacket {
static pack(data) {
const {fromType: {Bundle}} = packets(latus);
const {fromType} = traits(latus);
const {Bundle} = latus.get('%packets');
const Traits = latus.get('%traits');
for (let i = 0; i < data.traits.length; i++) {
const Trait = fromType[data.traits[i].type];
const {[data.traits[i].type]: Trait} = Traits;
// eslint-disable-next-line no-param-reassign
data.traits[i] = {
type: Trait.id,
@ -30,10 +28,10 @@ export default (latus) => class EntityUpdateTraitPacket extends SynchronizedUpda
}
static unpack(data) {
const {fromType: {Bundle}} = packets(latus);
const {fromId} = traits(latus);
const {Bundle} = latus.get('%packets');
const Traits = latus.get('%traits');
for (let i = 0; i < data.traits.length; i++) {
const Trait = fromId[data.traits[i].type];
const {[data.traits[i].type]: Trait} = Traits;
// eslint-disable-next-line no-param-reassign
data.traits[i] = {
type: Trait.type,

View File

@ -45,7 +45,7 @@ export default (latus) => class EntityList extends decorate(JsonResource) {
switch (s13nType) {
case 'create': {
const uuid = packet.data.synchronized.id;
const {Entity} = latus.get('%resources.fromResourceType');
const {Entity} = latus.get('%resources');
this.#serializer.create(uuid, Entity.load(packet.data.spec));
this.#serializer.later(uuid, (entity) => {
this.addEntity(entity);
@ -105,7 +105,7 @@ export default (latus) => class EntityList extends decorate(JsonResource) {
async load(json = []) {
await super.load(json);
const {Entity} = latus.get('%resources.fromResourceType');
const {Entity} = latus.get('%resources');
const entityInstances = await Promise.all(json.map((entity) => Entity.load(entity)));
for (let i = 0; i < entityInstances.length; i++) {
this.addEntity(entityInstances[i]);

View File

@ -4,7 +4,6 @@ import without from 'lodash.without';
import {fastApply, mergeDiff} from '@avocado/core';
import {Synchronized} from '@avocado/s13n';
import {JsonResource} from '@avocado/resource';
import {traits} from '@avocado/traits';
import {compose, EventEmitter} from '@latus/core';
import {defineTraitAccessors, enumerateTraitAccessorKeys} from '../accessors';
@ -76,7 +75,7 @@ export default (latus) => class Entity extends decorate(JsonResource) {
}
async addTrait(type, json = {}) {
const {fromType: {[type]: Trait}} = traits(latus);
const {[type]: Trait} = latus.get('%traits');
if (!Trait) {
debug(`Tried to add trait "${type}" which isn't registered!`);
return undefined;
@ -202,14 +201,14 @@ export default (latus) => class Entity extends decorate(JsonResource) {
if (!this.uri || !this.#originalJson || 0 === Object.keys(this.#originalJson).length) {
return json;
}
const {fromType} = latus.get('%traits');
const Traits = latus.get('%traits');
const traits = Object.fromEntries(
Object.entries(this.#originalJson.traits)
.map(([type, json]) => fromType[type] && [
.map(([type, json]) => Traits[type] && [
type,
{
params: fromType[type].defaultParamsWith(json.params),
state: fromType[type].defaultStateWith(json.state),
params: Traits[type].defaultParamsWith(json.params),
state: Traits[type].defaultStateWith(json.state),
},
])
.filter((e) => !!e),
@ -277,7 +276,7 @@ export default (latus) => class Entity extends decorate(JsonResource) {
const implementation = this.#hooks[hook].find(({type: hookType}) => hookType === type);
this.#hooks[hook].splice(this.#hooks[hook].indexOf(implementation), 1);
}
const {fromType: {[type]: Trait}} = traits(latus);
const {[type]: Trait} = latus.get('%traits');
const properties = enumerateTraitAccessorKeys(Trait.prototype);
for (let i = 0; i < properties.length; ++i) {
const property = properties[i];

View File

@ -265,7 +265,7 @@ export default (latus) => class Spawner extends decorate(Trait) {
const childIndex = this.#children.length;
this.#children.push(null);
const list = this.destinationEntityList();
const {Entity} = latus.get('%resources.fromResourceType');
const {Entity} = latus.get('%resources');
const child = await Entity.load(json);
this.#children[childIndex] = child;
// Listen for destroy event.

View File

@ -3,7 +3,6 @@ import {
buildExpression,
buildInvoke,
} from '@avocado/behavior';
import {resource} from '@avocado/resource';
import {Latus} from '@latus/core';
import {normalize} from '@latus/socket';
import {expect} from 'chai';
@ -19,7 +18,7 @@ beforeEach(async () => {
'@latus/socket': require('@latus/socket'),
});
await Promise.all(latus.invokeFlat('@latus/core/starting'));
({fromResourceType: {Entity}} = resource(latus));
({Entity} = latus.get('%resources'));
});
describe('Alive', () => {
let entity;

View File

@ -1,4 +1,3 @@
import {resource} from '@avocado/resource';
import {Latus} from '@latus/core';
import {normalize} from '@latus/socket';
import {expect} from 'chai';
@ -13,7 +12,7 @@ beforeEach(async () => {
'@latus/socket': require('@latus/socket'),
});
await Promise.all(latus.invokeFlat('@latus/core/starting'));
({fromResourceType: {Entity}} = resource(latus));
({Entity} = latus.get('%resources'));
});
describe('Directional', () => {
let entity;

View File

@ -1,4 +1,3 @@
import {resource} from '@avocado/resource';
import {Trait, traits} from '@avocado/traits';
import {Latus} from '@latus/core';
import {expect} from 'chai';
@ -12,7 +11,7 @@ beforeEach(async () => {
'@avocado/traits': require('@avocado/traits'),
});
await Promise.all(latus.invokeFlat('@latus/core/starting'));
({fromResourceType: {Entity}} = resource(latus));
({Entity} = latus.get('%resources'));
});
it('has sane defaults', () => {
const entity = new Entity();
@ -28,7 +27,7 @@ it('can add and remove traits', async () => {
}
};
traits(latus).fromType['TestTrait'] = TestTrait;
latus.set('%traits.TestTrait', TestTrait);
await entity.addTrait('TestTrait');
expect(entity.is('TestTrait')).to.be.true;
entity.removeTrait('TestTrait');
@ -45,7 +44,7 @@ it('can add traits asynchronously', async () => {
}
}
traits(latus).fromType['Async'] = AsyncTrait;
latus.set('%traits.Async', AsyncTrait);
let start = Date.now();
const entity = await Entity.load({
traits: {
@ -85,8 +84,8 @@ it('can invoke hooks', async () => {
}
}
latus.set('%traits.fromType.AnotherTrait', AnotherTrait);
latus.set('%traits.fromType.YetAnotherTrait', YetAnotherTrait);
latus.set('%traits.AnotherTrait', AnotherTrait);
latus.set('%traits.YetAnotherTrait', YetAnotherTrait);
const entity = new Entity();
await entity.addTrait('AnotherTrait');
await entity.addTrait('YetAnotherTrait');

View File

@ -1,4 +1,3 @@
import {resource} from '@avocado/resource';
import {Trait} from '@avocado/traits';
import {Latus} from '@latus/core';
import {expect} from 'chai';
@ -12,7 +11,7 @@ beforeEach(async () => {
'@avocado/traits': require('@avocado/traits'),
});
await Promise.all(latus.invokeFlat('@latus/core/starting'));
({fromResourceType: {Entity}} = resource(latus));
({Entity} = latus.get('%resources'));
});
describe('Existent', () => {
let entity;
@ -74,7 +73,7 @@ describe('Existent', () => {
}
}
latus.get('%traits.fromType')['Waiter'] = Waiter;
latus.set('%traits.Waiter', Waiter);
await entity.addTrait('Waiter');
const promise = Promise.all([
new Promise((resolve) => {

View File

@ -1,4 +1,3 @@
import {resource} from '@avocado/resource';
import {Latus} from '@latus/core';
import {expect} from 'chai';
@ -12,7 +11,7 @@ beforeEach(async () => {
'@avocado/traits': require('@avocado/traits'),
});
await Promise.all(latus.invokeFlat('@latus/core/starting'));
({fromResourceType: {Entity, EntityList}} = resource(latus));
({Entity, EntityList} = latus.get('%resources'));
});
describe('Listed', () => {
let entity;

View File

@ -1,4 +1,3 @@
import {resource} from '@avocado/resource';
import {Latus} from '@latus/core';
import {expect} from 'chai';
@ -12,7 +11,7 @@ beforeEach(async () => {
'@avocado/traits': require('@avocado/traits'),
});
await Promise.all(latus.invokeFlat('@latus/core/starting'));
({fromResourceType: {Entity, EntityList}} = resource(latus));
({Entity, EntityList} = latus.get('%resources'));
});
describe('Mobile', () => {
let entity;

View File

@ -1,4 +1,3 @@
import {resource} from '@avocado/resource';
import {Latus} from '@latus/core';
import {expect} from 'chai';
@ -12,7 +11,7 @@ beforeEach(async () => {
'@avocado/traits': require('@avocado/traits'),
});
await Promise.all(latus.invokeFlat('@latus/core/starting'));
({fromResourceType: {Entity, EntityList}} = resource(latus));
({Entity, EntityList} = latus.get('%resources'));
});
describe('Perishable', () => {
let entity;

View File

@ -1,4 +1,3 @@
import {resource} from '@avocado/resource';
import {Latus} from '@latus/core';
import {normalize} from '@latus/socket';
import {expect} from 'chai';
@ -14,7 +13,7 @@ beforeEach(async () => {
'@latus/socket': require('@latus/socket'),
});
await Promise.all(latus.invokeFlat('@latus/core/starting'));
({fromResourceType: {Entity, EntityList}} = resource(latus));
({Entity, EntityList} = latus.get('%resources'));
});
describe('Positioned', () => {
let entity;

View File

@ -1,4 +1,3 @@
import {resource} from '@avocado/resource';
import {Latus} from '@latus/core';
import {expect} from 'chai';
@ -13,7 +12,7 @@ beforeEach(async () => {
'@avocado/traits': require('@avocado/traits'),
});
await Promise.all(latus.invokeFlat('@latus/core/starting'));
({fromResourceType: {Entity, EntityList}} = resource(latus));
({Entity, EntityList} = latus.get('%resources'));
});
describe('Spawner', () => {
let entity;

View File

@ -1,5 +1,4 @@
import {mapValuesAsync} from '@avocado/core';
import {resource} from '@avocado/resource';
import {StateProperty, Trait} from '@avocado/traits';
import {Rectangle, Vector} from '@avocado/math';
import {compose} from '@latus/core';
@ -149,7 +148,7 @@ export default (latus) => class Pictured extends decorate(Trait) {
await super.load(json);
this.#currentImage = this.state.currentImage;
if (Object.keys(this.params.images).length > 0) {
const {fromResourceType: {Image}} = resource(latus);
const {Image} = latus.get('%resources');
const sprites = await mapValuesAsync(
this.params.images,
async (json) => new Sprite(await Image.load(json.uri)),

View File

@ -92,7 +92,7 @@ export default (latus) => class Emitter extends decorate(Trait) {
}
async emitParticleJson(json) {
const {Entity} = latus.get('%resources.fromResourceType');
const {Entity} = latus.get('%resources');
return this.entity.emitParticleEntity(await Entity.load(json));
}

View File

@ -1,20 +1,19 @@
import {gather} from '@latus/core';
export {default as Resource, resource} from './resource';
export {default as Resource} from './resource';
export {default as JsonResource} from './json-resource';
export default {
hooks: {
'@latus/core/starting': (latus) => {
// eslint-disable-next-line no-param-reassign
latus.config['%resources'] = gather(
latus.set('%resources', gather(
latus,
{
type: '@avocado/resource/resources',
idAttribute: 'resourceId',
typeAttribute: 'resourceType',
},
);
));
},
},
};

View File

@ -8,8 +8,6 @@ const decorate = compose(
Property('uri'),
);
export const resource = ({config}) => config['%resources'];
export default class Resource extends decorate(Class) {
constructor(...args) {

View File

@ -1,4 +1,3 @@
import {resource} from '@avocado/resource';
import {Class, compose, EventEmitter} from '@latus/core';
import Serializer from './serializer';
@ -53,7 +52,7 @@ export default class ReceiverSynchronizer extends decorate(Class) {
}
async createSynchronized(type, id, json) {
const {fromResourceId: {[type]: Resource}} = resource(this.latus);
const {[type]: Resource} = this.latus.get('%resources');
if (!(type in this.#synchronized)) {
this.#synchronized[type] = {};
}

View File

@ -1,4 +1,3 @@
import {resource} from '@avocado/resource';
import {Trait} from '@avocado/traits';
export default (latus) => class Audible extends Trait {
@ -53,7 +52,7 @@ export default (latus) => class Audible extends Trait {
async extendJson(json) {
const extended = await super.extendJson(json);
if (this.params.sounds && 'client' === process.env.SIDE) {
const {fromResourceType: {Sound}} = resource(latus);
const {Sound} = latus.get('%resources');
extended.sounds = Object.fromEntries(
await Promise.all(
Object.entries(this.params.sounds)

View File

@ -1,5 +1,4 @@
import {mapValuesAsync} from '@avocado/core';
import {resource} from '@avocado/resource';
import {StateProperty, Trait} from '@avocado/traits';
import {Rectangle, Vector} from '@avocado/math';
import {compose} from '@latus/core';
@ -202,7 +201,7 @@ export default (latus) => class Animated extends decorate(Trait) {
async load(json) {
await super.load(json);
if (Object.keys(json.params.animations).length > 0) {
const {fromResourceType: {Animation}} = resource(latus);
const {Animation} = latus.get('%resources');
const animations = await mapValuesAsync(
json.params.animations,
(json) => Animation.load(json),

View File

@ -1,9 +1,9 @@
import {packets, Packet} from '@latus/socket';
import {Packet} from '@latus/socket';
export default (latus) => class LayersUpdateLayerPacket extends Packet {
static pack(data) {
const {fromType: {Bundle}} = packets(latus);
const {Bundle} = latus.get('%packets');
for (let i = 0; i < data.length; i++) {
// eslint-disable-next-line no-param-reassign
data[i].layerPackets = Bundle.encode(data[i].layerPackets);
@ -21,7 +21,7 @@ export default (latus) => class LayersUpdateLayerPacket extends Packet {
}
static unpack(data) {
const {fromType: {Bundle}} = packets(latus);
const {Bundle} = latus.get('%packets');
for (let i = 0; i < data.length; i++) {
// eslint-disable-next-line no-param-reassign
data[i].layerPackets = Bundle.decode(data[i].layerPackets);

View File

@ -1,10 +1,9 @@
import {packets} from '@latus/socket';
import {SynchronizedUpdatePacket} from '@avocado/s13n';
export default (latus) => class RoomUpdateLayers extends SynchronizedUpdatePacket {
static pack(data) {
const {fromType: {Bundle}} = packets(latus);
const {Bundle} = latus.get('%packets');
// eslint-disable-next-line no-param-reassign
data.layersPackets = Bundle.encode(data.layersPackets);
return data;
@ -17,7 +16,7 @@ export default (latus) => class RoomUpdateLayers extends SynchronizedUpdatePacke
}
static unpack(data) {
const {fromType: {Bundle}} = packets(latus);
const {Bundle} = latus.get('%packets');
// eslint-disable-next-line no-param-reassign
data.layersPackets = Bundle.decode(data.layersPackets);
return data;

View File

@ -1,5 +1,5 @@
import {Property} from '@avocado/core';
import {JsonResource, resource} from '@avocado/resource';
import {JsonResource} from '@avocado/resource';
import {compose, EventEmitter} from '@latus/core';
const decorate = compose(
@ -15,7 +15,7 @@ export default (latus) => class Layer extends decorate(JsonResource) {
super();
this.tileEntities = {};
this.tileGeometry = [];
const {fromResourceType: {EntityList, Tiles}} = resource(latus);
const {EntityList, Tiles} = latus.get('%resources');
this.setEntityList(new EntityList());
this.setTiles(new Tiles());
}
@ -98,7 +98,7 @@ export default (latus) => class Layer extends decorate(JsonResource) {
tilesetUri,
world,
} = json;
const {fromResourceType: {EntityList, Tiles, Tileset}} = resource(latus);
const {EntityList, Tiles, Tileset} = latus.get('%resources');
this.setTiles(new Tiles(tiles));
this.tileset = tilesetUri
? await Tileset.load({extends: tilesetUri})

View File

@ -1,8 +1,5 @@
import {compose, EventEmitter} from '@latus/core';
import {
JsonResource,
resource,
} from '@avocado/resource';
import {JsonResource} from '@avocado/resource';
const decorate = compose(
EventEmitter,
@ -84,7 +81,7 @@ export default (latus) => class Layers extends decorate(JsonResource) {
async load(json = []) {
await super.load(json);
this.removeAllLayers();
const {fromResourceType: {Layer}} = resource(latus);
const {Layer} = latus.get('%resources');
const layers = await Promise.all(json.map((layer) => Layer.load(layer)));
for (let i = 0; i < layers.length; i++) {
this.addLayer(layers[i]);

View File

@ -1,8 +1,5 @@
import {Vector} from '@avocado/math';
import {
JsonResource,
resource,
} from '@avocado/resource';
import {JsonResource} from '@avocado/resource';
import {Synchronized} from '@avocado/s13n';
import {compose, EventEmitter} from '@latus/core';
@ -21,7 +18,7 @@ export default (latus) => class Room extends decorate(JsonResource) {
constructor() {
super();
this._s13nId = s13nId++;
const {fromResourceType: {Layers}} = resource(latus);
const {Layers} = latus.get('%resources');
this.setLayers(new Layers());
}
@ -69,7 +66,7 @@ export default (latus) => class Room extends decorate(JsonResource) {
if (size) {
this.size = size;
}
const {fromResourceType: {Layers}} = resource(latus);
const {Layers} = latus.get('%resources');
this.setLayers(
layers
? await Layers.load(layers)

View File

@ -1,4 +1,3 @@
import {resource} from '@avocado/resource';
import {Latus} from '@latus/core';
import {expect} from 'chai';
@ -14,7 +13,7 @@ describe('Layer', () => {
await Promise.all(latus.invokeFlat('@latus/core/starting'));
});
it('has sane defaults', async () => {
const {fromResourceType: {Layer}} = resource(latus);
const {Layer} = latus.get('%resources');
const layer = new Layer();
expect(Object.keys(layer.entities).length).to.equal(0);
expect(layer.tiles.size).to.deep.equal([0, 0]);
@ -23,7 +22,7 @@ describe('Layer', () => {
let entity;
let layer;
beforeEach(async () => {
const {fromResourceType: {Entity, EntityList, Layer}} = resource(latus);
const {Entity, EntityList, Layer} = latus.get('%resources');
entity = await Entity.load({
traits: {
Existent: {},

View File

@ -1,4 +1,3 @@
import {resource} from '@avocado/resource';
import {Latus} from '@latus/core';
import {expect} from 'chai';
@ -12,13 +11,13 @@ describe('Tiles', () => {
await Promise.all(latus.invokeFlat('@latus/core/starting'));
});
it("has sane defaults", async () => {
const {fromResourceType: {Tiles}} = resource(latus);
const {Tiles} = latus.get('%resources');
const tiles = new Tiles();
expect(tiles.size).to.deep.equal([0, 0]);
expect(tiles.data.length).to.equal(0);
});
it("gets and sets tile data", async () => {
const {fromResourceType: {Tiles}} = resource(latus);
const {Tiles} = latus.get('%resources');
const tiles = new Tiles({data: [0, 1, 2, 3], size: [2, 2]});
expect(tiles.tileAt([0, 0])).to.equal(0);
expect(tiles.tileAt([1, 0])).to.equal(1);
@ -31,7 +30,7 @@ describe('Tiles', () => {
expect(tiles.data).to.deep.equal([69, 420, 311, 42]);
});
it("can get a data slice", async () => {
const {fromResourceType: {Tiles}} = resource(latus);
const {Tiles} = latus.get('%resources');
const tiles = new Tiles(
{
data: [

View File

@ -1,5 +1,4 @@
import {Image} from '@avocado/graphics';
import {resource} from '@avocado/resource';
import {Latus} from '@latus/core';
import {expect} from 'chai';
@ -13,17 +12,17 @@ describe('Tileset', () => {
'@avocado/topdown': require('../src'),
});
await Promise.all(latus.invokeFlat('@latus/core/starting'));
const {fromResourceType: {Tileset}} = resource(latus);
const {Tileset} = latus.get('%resources');
Tileset.root = 'test/fixtures';
});
it("has sane defaults", async () => {
const {fromResourceType: {Tileset}} = resource(latus);
const {Tileset} = latus.get('%resources');
const tileset = new Tileset();
expect(tileset.tileSize).to.deep.equal([0, 0]);
expect(tileset.image).to.equal(undefined);
});
it("can load", async () => {
const {fromResourceType: {Tileset}} = resource(latus);
const {Tileset} = latus.get('%resources');
const tileset = await Tileset.load({extends: '/test.tileset.json'});
expect(tileset.tileSize).to.deep.equal([4, 4]);
expect(tileset.image.size).to.deep.equal([16, 16]);

View File

@ -1,18 +1,17 @@
import {gather} from '@latus/core';
export {default as Trait, traits} from './trait';
export {default as Trait} from './trait';
export {default as StateProperty} from './state-property';
export default {
hooks: {
'@latus/core/starting': (latus) => {
// eslint-disable-next-line no-param-reassign
latus.config['%traits'] = gather(
latus.set('%traits', gather(
latus,
{
type: '@avocado/traits',
},
);
));
},
},
};

View File

@ -6,8 +6,6 @@ const decorate = compose(
Synchronized,
);
export const traits = (latus) => latus.config['%traits'];
export default class Trait extends decorate(JsonResource) {
#markedAsDirty = true;