diff --git a/app/client/prediction.js b/app/client/prediction.js index af47878..6660074 100644 --- a/app/client/prediction.js +++ b/app/client/prediction.js @@ -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'})); diff --git a/app/client/remote.js b/app/client/remote.js index 18b4535..243081e 100644 --- a/app/client/remote.js +++ b/app/client/remote.js @@ -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'})); diff --git a/app/ecs/ecs.js b/app/ecs/ecs.js index 6fa4fe3..491d815 100644 --- a/app/ecs/ecs.js +++ b/app/ecs/ecs.js @@ -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) => { diff --git a/app/react/components/client-ecs.js b/app/react/components/client-ecs.js index cbb5d93..73c3464 100644 --- a/app/react/components/client-ecs.js +++ b/app/react/components/client-ecs.js @@ -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) => { diff --git a/app/server/engine.js b/app/server/engine.js index 8184b32..b199f1b 100644 --- a/app/server/engine.js +++ b/app/server/engine.js @@ -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) diff --git a/app/server/worker.js b/app/server/worker.js index 875bf28..904b778 100644 --- a/app/server/worker.js +++ b/app/server/worker.js @@ -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(); }); diff --git a/app/util/constants.js b/app/util/constants.js index 2ed20fb..d7e3863 100644 --- a/app/util/constants.js +++ b/app/util/constants.js @@ -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; diff --git a/app/util/lfo.js b/app/util/lfo.js index 39596ee..95c793c 100644 --- a/app/util/lfo.js +++ b/app/util/lfo.js @@ -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]; }); diff --git a/app/util/promise.js b/app/util/promise.js new file mode 100644 index 0000000..51eeece --- /dev/null +++ b/app/util/promise.js @@ -0,0 +1,7 @@ +export function withResolvers() { + let resolve, reject, promise = new Promise((res, rej) => { + resolve = res; + reject = rej; + }); + return {promise, reject, resolve}; +}