refactor: withResolvers
This commit is contained in:
parent
eb6e23f0e1
commit
c224445345
|
@ -1,4 +1,5 @@
|
|||
import {encode} from '@/net/packets/index.js';
|
||||
import {withResolvers} from '@/util/promise.js';
|
||||
|
||||
let connected = false;
|
||||
let socket;
|
||||
|
@ -12,7 +13,7 @@ onmessage = async (event) => {
|
|||
const url = new URL(`wss://${event.data.host}/ws`)
|
||||
socket = new WebSocket(url.href);
|
||||
socket.binaryType = 'arraybuffer';
|
||||
const {promise, resolve} = Promise.withResolvers();
|
||||
const {promise, resolve} = withResolvers();
|
||||
socket.addEventListener('open', resolve);
|
||||
socket.addEventListener('error', () => {
|
||||
postMessage(encode({type: 'ConnectionStatus', payload: 'aborted'}));
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import Client from '@/net/client.js';
|
||||
import {encode} from '@/net/packets/index.js';
|
||||
import {CLIENT_PREDICTION} from '@/util/constants.js';
|
||||
import {withResolvers} from '@/util/promise.js';
|
||||
|
||||
export default class RemoteClient extends Client {
|
||||
constructor() {
|
||||
|
@ -30,7 +31,7 @@ export default class RemoteClient extends Client {
|
|||
const onMessage = (event) => {
|
||||
this.accept(event.data);
|
||||
}
|
||||
const {promise, resolve} = Promise.withResolvers();
|
||||
const {promise, resolve} = withResolvers();
|
||||
this.socket.addEventListener('open', resolve);
|
||||
this.socket.addEventListener('error', () => {
|
||||
this.accept(encode({type: 'ConnectionStatus', payload: 'aborted'}));
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import {Encoder, Decoder} from '@msgpack/msgpack';
|
||||
import {LRUCache} from 'lru-cache';
|
||||
|
||||
import {withResolvers} from '@/util/promise.js';
|
||||
import Script from '@/util/script.js';
|
||||
|
||||
import EntityFactory from './entity-factory.js';
|
||||
|
@ -326,11 +327,7 @@ export default class Ecs {
|
|||
async readJson(uri) {
|
||||
const key = ['$$json', uri].join(':');
|
||||
if (!cache.has(key)) {
|
||||
let promise, resolve, reject;
|
||||
promise = new Promise((res, rej) => {
|
||||
resolve = res;
|
||||
reject = rej;
|
||||
});
|
||||
const {promise, resolve, reject} = withResolvers();
|
||||
cache.set(key, promise);
|
||||
this.readAsset(uri)
|
||||
.then((chars) => {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import {LRUCache} from 'lru-cache';
|
||||
|
||||
import Ecs from '@/ecs/ecs.js';
|
||||
import {withResolvers} from '@/util/promise.js';
|
||||
|
||||
const cache = new LRUCache({
|
||||
max: 128,
|
||||
|
@ -9,11 +10,7 @@ const cache = new LRUCache({
|
|||
export default class ClientEcs extends Ecs {
|
||||
async readAsset(uri) {
|
||||
if (!cache.has(uri)) {
|
||||
let promise, resolve, reject;
|
||||
promise = new Promise((res, rej) => {
|
||||
resolve = res;
|
||||
reject = rej;
|
||||
});
|
||||
const {promise, resolve, reject} = withResolvers();
|
||||
cache.set(uri, promise);
|
||||
fetch(new URL(uri, window.location.origin))
|
||||
.then(async (response) => {
|
||||
|
|
|
@ -7,6 +7,7 @@ import {
|
|||
RESOLUTION,
|
||||
TPS,
|
||||
} from '@/util/constants.js';
|
||||
import {withResolvers} from '@/util/promise.js';
|
||||
|
||||
import createEcs from './create/ecs.js';
|
||||
import createForest from './create/forest.js';
|
||||
|
@ -48,11 +49,7 @@ export default class Engine {
|
|||
}
|
||||
async readAsset(uri) {
|
||||
if (!cache.has(uri)) {
|
||||
let promise, resolve, reject;
|
||||
promise = new Promise((res, rej) => {
|
||||
resolve = res;
|
||||
reject = rej;
|
||||
});
|
||||
const {promise, resolve, reject} = withResolvers();
|
||||
cache.set(uri, promise);
|
||||
server.readAsset(uri)
|
||||
.then(resolve)
|
||||
|
|
|
@ -2,6 +2,7 @@ import {del, get, set} from 'idb-keyval';
|
|||
|
||||
import {encode} from '@/net/packets/index.js';
|
||||
import Server from '@/net/server.js';
|
||||
import {withResolvers} from '@/util/promise.js';
|
||||
|
||||
import createEcs from './create/ecs.js';
|
||||
import './create/forest.js';
|
||||
|
@ -62,27 +63,19 @@ onmessage = async (event) => {
|
|||
})();
|
||||
|
||||
if (import.meta.hot) {
|
||||
const createResolver = () => {
|
||||
let r;
|
||||
const promise = new Promise((resolve) => {
|
||||
r = resolve;
|
||||
});
|
||||
promise.resolve = r;
|
||||
return promise;
|
||||
};
|
||||
const beforeResolver = createResolver();
|
||||
const resolvers = [beforeResolver];
|
||||
const before = withResolvers();
|
||||
const promises = [before.promise];
|
||||
import.meta.hot.on('vite:beforeUpdate', async () => {
|
||||
engine.stop();
|
||||
await engine.disconnectPlayer(0);
|
||||
await engine.saveEcses();
|
||||
beforeResolver.resolve();
|
||||
before.resolve();
|
||||
});
|
||||
import.meta.hot.accept('./engine.js');
|
||||
import.meta.hot.accept('./create/player.js', async ({default: createPlayer}) => {
|
||||
const resolver = createResolver();
|
||||
resolvers.push(resolver);
|
||||
await beforeResolver;
|
||||
const {promise, resolve} = withResolvers();
|
||||
promises.push(promise);
|
||||
await before.promise;
|
||||
const oldPlayer = await engine.loadPlayer(0);
|
||||
const player = await createPlayer(0);
|
||||
// Less jarring
|
||||
|
@ -90,12 +83,12 @@ if (import.meta.hot) {
|
|||
player.Direction = oldPlayer.Direction;
|
||||
player.Position = oldPlayer.Position;
|
||||
await engine.savePlayer(0, player);
|
||||
resolver.resolve();
|
||||
resolve();
|
||||
});
|
||||
import.meta.hot.accept('./create/forest.js', async ({default: createForest}) => {
|
||||
const resolver = createResolver();
|
||||
resolvers.push(resolver);
|
||||
await beforeResolver;
|
||||
const {promise, resolve} = withResolvers();
|
||||
promises.push(promise);
|
||||
await before.promise;
|
||||
delete engine.ecses['forests/0'];
|
||||
await engine.server.removeData('forests/0');
|
||||
const forest = createEcs(engine.Ecs);
|
||||
|
@ -103,12 +96,12 @@ if (import.meta.hot) {
|
|||
await forest.create(entity);
|
||||
}
|
||||
await engine.saveEcs('forests/0', forest);
|
||||
resolver.resolve();
|
||||
resolve();
|
||||
});
|
||||
import.meta.hot.accept('./create/homestead.js', async ({default: createHomestead}) => {
|
||||
const resolver = createResolver();
|
||||
resolvers.push(resolver);
|
||||
await beforeResolver;
|
||||
const {promise, resolve} = withResolvers();
|
||||
promises.push(promise);
|
||||
await before.promise;
|
||||
delete engine.ecses['homesteads/0'];
|
||||
await engine.server.removeData('homesteads/0');
|
||||
const homestead = createEcs(engine.Ecs);
|
||||
|
@ -116,10 +109,10 @@ if (import.meta.hot) {
|
|||
await homestead.create(entity);
|
||||
}
|
||||
await engine.saveEcs('homesteads/0', homestead);
|
||||
resolver.resolve();
|
||||
resolve();
|
||||
});
|
||||
import.meta.hot.on('vite:afterUpdate', async () => {
|
||||
await Promise.all(resolvers);
|
||||
await Promise.all(promises);
|
||||
postMessage(encode({type: 'ConnectionStatus', payload: 'aborted'}));
|
||||
close();
|
||||
});
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
export const CHUNK_SIZE = 32;
|
||||
|
||||
export const CLIENT_LATENCY = 0;
|
||||
export const CLIENT_LATENCY = 100;
|
||||
|
||||
export const CLIENT_PREDICTION = true;
|
||||
|
||||
|
@ -11,6 +11,6 @@ export const RESOLUTION = {
|
|||
y: 450,
|
||||
};
|
||||
|
||||
export const SERVER_LATENCY = 0;
|
||||
export const SERVER_LATENCY = 100;
|
||||
|
||||
export const TPS = 60;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import {withResolvers} from '@/util/promise.js';
|
||||
import TickingPromise from '@/util/ticking-promise.js';
|
||||
|
||||
const Modulators = {
|
||||
|
@ -35,9 +36,7 @@ export default function lfo(object, properties) {
|
|||
}
|
||||
}
|
||||
oscillator.low = oscillator.median - oscillator.magnitude / 2;
|
||||
oscillator.promise = new Promise((resolve) => {
|
||||
oscillator.stop = resolve;
|
||||
});
|
||||
({promise: oscillator.promise, resolve: oscillator.stop} = withResolvers());
|
||||
oscillator.promise.then(() => {
|
||||
delete oscillators[key];
|
||||
});
|
||||
|
|
7
app/util/promise.js
Normal file
7
app/util/promise.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
export function withResolvers() {
|
||||
let resolve, reject, promise = new Promise((res, rej) => {
|
||||
resolve = res;
|
||||
reject = rej;
|
||||
});
|
||||
return {promise, reject, resolve};
|
||||
}
|
Loading…
Reference in New Issue
Block a user