refactor: mixin

This commit is contained in:
cha0s 2024-01-05 19:25:20 -06:00
parent b3407b2d3f
commit 2c48d5b808
36 changed files with 65 additions and 52 deletions

View File

@ -31,7 +31,7 @@ export default (flecks) => class Behaved extends decorate(Trait) {
({
currentRoutine: this.$$currentRoutine,
} = this.constructor.defaultState());
const {Script} = flecks.get('$avocado/resource.resources');
const {Script} = flecks.avocado.Resources;
this.$$context = Script.createContext();
}
@ -79,7 +79,7 @@ export default (flecks) => class Behaved extends decorate(Trait) {
static async loadScripts(scripts, context) {
return mapValuesAsync(scripts, async (codeOrUri) => {
const {Script} = flecks.get('$avocado/resource.resources');
const {Script} = flecks.avocado.Resources;
const script = await Script.load(codeOrUri);
script.context = context;
return script;
@ -88,7 +88,7 @@ export default (flecks) => class Behaved extends decorate(Trait) {
async load(json) {
await super.load(json);
const {Script} = flecks.get('$avocado/resource.resources');
const {Script} = flecks.avocado.Resources;
this.$$context = Script.createContext({
entity: this.entity,
});

View File

@ -14,7 +14,7 @@ export const hooks = {
const dialogFunctions = flecks.invokeMerge('@avocado/dialog.functions');
const resolver = (type) => (dialogFunctions[type] ? dialogFunctions[type] : (i) => i);
flecks.set('$avocado/dialog.parser', async (text) => {
const {Entity} = flecks.get('$avocado/resource.resources');
const {Entity} = flecks.avocado.Resources;
const letters = await Promise.all(
parse(text, resolver)
.map((letter) => Entity.load({traits: letter})),

View File

@ -38,7 +38,7 @@ export default (Trait, flecks) => class DialogInitiator extends Trait {
openDialog: async (text) => {
if ('web' === process.env.FLECKS_CORE_BUILD_TARGET) {
const {Entity} = flecks.get('$avocado/resource.resources');
const {Entity} = flecks.avocado.Resources;
if (!this.entity.list) {
return;
}

View File

@ -1,5 +1,5 @@
const createWord = async (letters, flecks) => {
const {Entity} = flecks.get('$avocado/resource.resources');
const {Entity} = flecks.avocado.Resources;
const word = await Entity.load({
traits: {
DomNode: {

View File

@ -1,5 +1,5 @@
export default async (flecks) => {
const {Entity} = flecks.get('$avocado/resource.resources');
const {Entity} = flecks.avocado.Resources;
const Traits = flecks.get('$avocado/traits.traits');
const AllTraits = Object.fromEntries(
Object.entries(Traits)

View File

@ -25,7 +25,7 @@ const EntityComponent = ({
uri,
}) => {
const flecks = useFlecks();
const {Entity, EntityList} = flecks.get('$avocado/resource.resources');
const {Entity, EntityList} = flecks.avocado.Resources;
const [entity, setEntity] = useState();
const dep = uri ? join(uri, path) : path;
useEffect(() => {

View File

@ -1,7 +1,7 @@
import {JsonController} from '@avocado/resource/persea';
export default (buffer, flecks) => {
const {Entity} = flecks.get('$avocado/resource.resources');
const {Entity} = flecks.avocado.Resources;
const json = Entity.withDefaults(JsonController.fromBuffer(buffer));
return {
...json,

View File

@ -1,7 +1,7 @@
import {JsonController} from '@avocado/resource/persea';
export default (resource, flecks) => {
const {Entity} = flecks.get('$avocado/resource.resources');
const {Entity} = flecks.avocado.Resources;
const json = Entity.withoutDefaults(resource);
return JsonController.toBuffer({
...json,

View File

@ -26,7 +26,7 @@ export default (flecks) => {
await super.acceptPacket(packet);
const {s13nType} = packet;
if ('create' === s13nType) {
const {Entity} = flecks.get('$avocado/resource.resources');
const {Entity} = flecks.avocado.Resources;
const {id} = packet.data.synchronized;
const entity = this.synchronized(Entity.resourceId, id);
if (entity) {
@ -85,7 +85,7 @@ export default (flecks) => {
return;
}
await this.destroyEntities();
const {Entity} = flecks.get('$avocado/resource.resources');
const {Entity} = flecks.avocado.Resources;
const entityInstances = await Promise.all(json.map((entity) => Entity.load(entity)));
for (let i = 0; i < entityInstances.length; i++) {
this.addEntity(entityInstances[i]);
@ -105,7 +105,7 @@ export default (flecks) => {
packetsFor(informed) {
const packets = [];
const {Entity} = flecks.get('$avocado/resource.resources');
const {Entity} = flecks.avocado.Resources;
// Visible entities.
const {areaToInform} = informed;
const previousVisibleEntities = this.$$informedEntities.get(informed);
@ -202,7 +202,7 @@ export default (flecks) => {
}
toJSON() {
const {Entity} = flecks.get('$avocado/resource.resources');
const {Entity} = flecks.avocado.Resources;
const json = [];
for (let i = 0; i < this.$$flatEntities.length; i++) {
const entity = this.$$flatEntities[i];

View File

@ -192,7 +192,7 @@ export default (flecks) => {
}
createContext(locals = {}) {
const {Script} = flecks.get('$avocado/resource.resources');
const {Script} = flecks.avocado.Resources;
return Script.createContext({
...this.contextOrDefault,
...locals,

View File

@ -68,7 +68,7 @@ export default (flecks) => class Alive extends decorate(Trait) {
async die() {
this.entity.emit('startedDying');
const {Script} = flecks.get('$avocado/resource.resources');
const {Script} = flecks.avocado.Resources;
const deathScript = await Script.load(this.params.deathScript, this.entity.contextOrDefault);
await this.entity.addTickingPromise(deathScript.tickingPromise());
const died = this.entity.invokeHookFlat('died');
@ -117,7 +117,7 @@ export default (flecks) => class Alive extends decorate(Trait) {
async load(json) {
await super.load(json);
const {Script} = flecks.get('$avocado/resource.resources');
const {Script} = flecks.avocado.Resources;
this.$$deathCheck = await Script.load(this.params.deathCheck, this.entity.contextOrDefault);
}

View File

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

View File

@ -19,7 +19,7 @@ beforeEach(async () => {
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
({Entity} = flecks.avocado.Resources);
});
afterEach(() => {
flecks.destroy();

View File

@ -18,7 +18,7 @@ beforeEach(async () => {
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
({Entity} = flecks.avocado.Resources);
});
afterEach(() => {
flecks.destroy();

View File

@ -17,7 +17,7 @@ beforeEach(async () => {
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
({Entity} = flecks.avocado.Resources);
});
afterEach(() => {
flecks.destroy();

View File

@ -17,7 +17,7 @@ beforeEach(async () => {
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
({Entity} = flecks.avocado.Resources);
});
afterEach(() => {
flecks.destroy();

View File

@ -17,7 +17,7 @@ beforeEach(async () => {
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
({Entity} = flecks.avocado.Resources);
});
afterEach(() => {
flecks.destroy();

View File

@ -17,7 +17,7 @@ beforeEach(async () => {
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
({Entity} = flecks.avocado.Resources);
});
afterEach(() => {
flecks.destroy();

View File

@ -17,7 +17,7 @@ beforeEach(async () => {
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity} = flecks.get('$avocado/resource.resources'));
({Entity} = flecks.avocado.Resources);
});
afterEach(() => {
flecks.destroy();

View File

@ -19,7 +19,7 @@ beforeEach(async () => {
},
});
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));
({Entity, EntityList} = flecks.get('$avocado/resource.resources'));
({Entity, EntityList} = flecks.avocado.Resources);
});
afterEach(() => {
flecks.destroy();

View File

@ -31,7 +31,7 @@ export default (flecks) => class Interactive extends decorate(Trait) {
return {
interact: async (initiator) => {
const {Script} = flecks.get('$avocado/resource.resources');
const {Script} = flecks.avocado.Resources;
const script = await Script.load(this.params.interactScript, {
entity: this.entity,
initiator,

View File

@ -20,7 +20,7 @@ const Particle = ({
}) => {
const flecks = useFlecks();
const patch = useJsonPatcher();
const {EntityList} = flecks.get('$avocado/resource.resources');
const {EntityList} = flecks.avocado.Resources;
const entityList = new EntityList();
const entityListView = new EntityListView(entityList);
entityList.addEntity(entity);

View File

@ -145,7 +145,7 @@ export default (flecks) => class Collider extends decorate(Trait) {
}
async pushCollisionTickingPromise(codeOrUri, other, incident) {
const {Script} = flecks.get('$avocado/resource.resources');
const {Script} = flecks.avocado.Resources;
this.entity.addTickingPromise(Script.loadTickingPromise(codeOrUri, {
...this.entity.contextOrDefault,
incident,

View File

@ -108,7 +108,7 @@ export default (flecks) => class Emitted extends decorate(Trait) {
if (!this.params.onDead) {
return;
}
const {Script} = flecks.get('$avocado/resource.resources');
const {Script} = flecks.avocado.Resources;
this.entity.addTickingPromise(Script.loadTickingPromise(this.params.onDead, {
...this.entity.contextOrDefault,
}));

View File

@ -59,7 +59,7 @@ export default (flecks) => class Emitter extends decorate(Trait) {
}
async emitParticleJson(json) {
const {Entity} = flecks.get('$avocado/resource.resources');
const {Entity} = flecks.avocado.Resources;
let entity;
if (this.$$pool.length > 0) {
entity = this.$$pool.pop();
@ -282,7 +282,7 @@ export default (flecks) => class Emitter extends decorate(Trait) {
}
static withDefaults(json = {}) {
const {Entity} = flecks.get('$avocado/resource.resources');
const {Entity} = flecks.avocado.Resources;
return {
params: {
...this.defaultParamsWith(json.params || {}),
@ -297,7 +297,7 @@ export default (flecks) => class Emitter extends decorate(Trait) {
}
static withoutDefaults(json) {
const {Entity} = flecks.get('$avocado/resource.resources');
const {Entity} = flecks.avocado.Resources;
return {
params: {
...this.defaultParamsWith(json.params || {}),

View File

@ -10,13 +10,26 @@ export const hooks = {
resourceCache.reset();
}
},
'@flecks/core.mixin': (Flecks) => (
class FlecksWithAvocadoResource extends Flecks {
constructor(...args) {
super(...args);
if (!this.avocado) {
this.avocado = {};
}
this.avocado.Resources = {};
}
}
),
'@flecks/core.starting': (flecks) => {
flecks.set('$avocado/resource.resources', flecks.gather(
flecks.avocado.Resources = flecks.gather(
'@avocado/resource.resources',
{
idProperty: 'resourceId',
typeProperty: 'resourceType',
},
));
);
},
};

View File

@ -3,7 +3,7 @@ import SynchronizedPacket from './synchronized';
export default (flecks) => class SynchronizedUpdatePacket extends SynchronizedPacket {
static pack(data) {
const {Bundle} = flecks.get('$flecks/socket.packets');
const {Bundle} = flecks.socket.Packets;
data.packets = Bundle.encode(data.packets);
return data;
}
@ -19,7 +19,7 @@ export default (flecks) => class SynchronizedUpdatePacket extends SynchronizedPa
}
static unpack(data) {
const {Bundle} = flecks.get('$flecks/socket.packets');
const {Bundle} = flecks.socket.Packets;
data.packets = Bundle.decode(data.packets);
return data;
}

View File

@ -30,7 +30,7 @@ export default (flecks) => (Resource) => (
}
async createSynchronized(type, id, json) {
const {[type]: Resource} = flecks.get('$avocado/resource.resources');
const {[type]: Resource} = flecks.avocado.Resources;
if (!(type in this.$$synchronized)) {
this.$$synchronized[type] = {};
}

View File

@ -18,7 +18,7 @@ import {
const AnimationVisualization = ({json}) => {
const flecks = useFlecks();
// const {uuid} = useParams();
const {Animation} = flecks.get('$avocado/resource.resources');
const {Animation} = flecks.avocado.Resources;
const [animations, setAnimations] = useState([]);
const [container] = useState(new Container());
useEffect(() => {

View File

@ -29,7 +29,7 @@ const Animation = ({
if (!fullJson) {
return;
}
const {Animation} = flecks.get('$avocado/resource.resources');
const {Animation} = flecks.avocado.Resources;
const loadJson = async () => {
const animationView = new AnimationView(await Animation.load(fullJson));
const drawCross = (primitives, origin, color) => {

View File

@ -192,7 +192,7 @@ export default (flecks) => class Animated extends decorate(Trait) {
async load(json) {
await super.load(json);
if (Object.keys(this.params.animations).length > 0) {
const {Animation} = flecks.get('$avocado/resource.resources');
const {Animation} = flecks.avocado.Resources;
const animations = await mapValuesAsync(
this.params.animations,
(json) => Animation.load(json),

View File

@ -41,7 +41,7 @@ const RoomComponent = ({
const {uri, uuid} = useContext(Context);
const dispatch = useDispatch();
const flecks = useFlecks();
const {Room, Tiles} = flecks.get('$avocado/resource.resources');
const {Room, Tiles} = flecks.avocado.Resources;
const previousResource = usePrevious(resource);
const [events, setEvents] = useState();
const [scale, setScale] = useState(3);

View File

@ -448,7 +448,7 @@ function TilesPage({
<VectorComponent
disabled={!resource.tiles[currentLayer]}
onChange={(event, newArea) => {
const {Tiles} = flecks.get('$avocado/resource.resources');
const {Tiles} = flecks.avocado.Resources;
// Create new tiles.
const newTiles = new Tiles();
newTiles.area = newArea;

View File

@ -24,7 +24,7 @@ export default (flecks) => {
constructor() {
super();
this.$$s13nId = s13nId++;
const {EntityList} = flecks.get('$avocado/resource.resources');
const {EntityList} = flecks.avocado.Resources;
this.entityList = new EntityList();
this.entityList.on('entityAdded', this.onEntityAdded, this);
this.entityList.on('entityRemoved', this.onEntityRemoved, this);
@ -66,7 +66,7 @@ export default (flecks) => {
}
async loadTiles(tiles) {
const {Tiles} = flecks.get('$avocado/resource.resources');
const {Tiles} = flecks.avocado.Resources;
this.removeAllTiles();
(await Promise.all(tiles.map((json, i) => Tiles.load({s13nId: i, ...json}))))
.forEach((tiles) => {

View File

@ -13,7 +13,7 @@
// await Promise.all(flecks.invokeFlat('@flecks/core/starting'));
// });
// it('has sane defaults', async () => {
// const {Layer} = flecks.get('$avocado/resource.resources');
// const {Layer} = flecks.avocado.Resources;
// const layer = new Layer();
// expect(Object.keys(layer.entities).length).to.equal(0);
// expect(layer.tiles.size).to.deep.equal([0, 0]);

View File

@ -11,13 +11,13 @@
// await Promise.all(flecks.invokeFlat('@flecks/core/starting'));
// });
// it("has sane defaults", async () => {
// const {Tiles} = flecks.get('$avocado/resource.resources');
// const {Tiles} = flecks.avocado.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 {Tiles} = flecks.get('$avocado/resource.resources');
// const {Tiles} = flecks.avocado.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);
@ -30,7 +30,7 @@
// expect(tiles.data).to.deep.equal([69, 420, 311, 42]);
// });
// it("can get a data slice", async () => {
// const {Tiles} = flecks.get('$avocado/resource.resources');
// const {Tiles} = flecks.avocado.Resources;
// const tiles = new Tiles(
// {
// data: [
@ -50,7 +50,7 @@
// expect(tiles.slice([1, 1, 0, 0])).to.deep.equal([]);
// });
// it("can get convex hulls around indices", () => {
// const {Tiles} = flecks.get('$avocado/resource.resources');
// const {Tiles} = flecks.avocado.Resources;
// const hulls = [
// [
// [2, 0],
@ -81,7 +81,7 @@
// expect(tiles.indexHulls(new Set([1]))).to.deep.equal(hulls);
// });
// it("can stamp at", async () => {
// const {Tiles} = flecks.get('$avocado/resource.resources');
// const {Tiles} = flecks.avocado.Resources;
// const tiles = new Tiles(
// {
// data: [
@ -112,7 +112,7 @@
// ]);
// });
// it("can handle pathological hulls", () => {
// const {Tiles} = flecks.get('$avocado/resource.resources');
// const {Tiles} = flecks.avocado.Resources;
// const hulls = [
// [
// [ 0, 0 ],