Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI Any To Main
name: CI
on:
push:
branches:
Expand Down Expand Up @@ -32,12 +32,13 @@ jobs:
with:
install-command: yarn --frozen-lockfile --silent

- name: Build
run: yarn build

- name: Test
run: yarn test
# - name: Lint
# run: yarn lint
- name: Build
run: yarn build

testAndUploadCoverall:
needs: multi_build_and_test
Expand All @@ -56,9 +57,12 @@ jobs:
uses: bahmutov/npm-install@v1
with:
install-command: yarn --frozen-lockfile --silent

- name: Build all package
run: yarn build

- name: Test
run: yarn test:ci
run: yarn test:coverage

- name: Coveralls - ${{ env.package_array }}
uses: coverallsapp/github-action@master
Expand All @@ -71,6 +75,17 @@ jobs:
parallel: true
flag-name: ${{ env.package_array }}

- name: Coveralls - ${{ env.package_array }}
uses: coverallsapp/github-action@master
env:
package_array: "util"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: packages/${{ env.package_array }}/coverage/lcov.info
base-path: packages/${{ env.package_array }}
parallel: true
flag-name: ${{ env.package_array }}

buildAndPublish:
needs: multi_build_and_test
if: ${{ github.event_name == 'push' }}
Expand All @@ -94,13 +109,16 @@ jobs:
install-command: yarn --frozen-lockfile --silent

- name: Build
run: yarn build:ci
run: yarn build

- name: Monorepo publish
id: publish
run: yarn lerna publish from-git --yes
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Build example
run: yarn lerna run build:example --scope=netcodejs

- name: Deploy example 🚀
uses: JamesIves/github-pages-deploy-action@4.1.4
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
"lerna": "^4.0.0",
"lint-staged": "^11.1.2",
"prettier": "^2.2.1",
"rimraf": "^3.0.2"
"rimraf": "^3.0.2",
"typescript": "^4.3.5"
},
"scripts": {
"build": "lerna run build",
"build:ci": "lerna run build:ci",
"build:fast": "lerna run build:fast",
"test": "lerna run test",
"test:ci": "lerna run test:ci",
"test:coverage": "lerna run test:coverage",
"test:main": "lerna run test --scope=netcodejs --stream",
"test:main:coverage": "lerna run test:coverage --scope=netcodejs --stream",
"prepare": "lerna bootstrap && husky install"
},
"lint-staged": {
Expand Down
2 changes: 1 addition & 1 deletion packages/netcode/example/mock-net.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Deferred } from "../src";
import { Deferred } from "@netcodejs/util";

export interface Receiver {
receive(data: any): void;
Expand Down
17 changes: 11 additions & 6 deletions packages/netcode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
"author": "littlemoi",
"license": "GPL-3.0-or-later",
"scripts": {
"build": "node esbuild.js",
"build:fast": "node esbuild.js",
"build": "yarn build:fast && tsc --emitDeclarationOnly --outdir dist",

"test": "jest",
"test:coverage": "jest --ci --coverage",

"build:nojit": "cross-env ENSURE_JIT=false yarn build",
"build:jit": "cross-env ENSURE_JIT=true yarn build",
"build:example": "node ./esbuild.example.js",
"build:ci": "yarn build && yarn build:example && tsc --emitDeclarationOnly --outdir dist",
"test": "jest",
"test:ci": "jest --ci --coverage"
"build:example": "node ./esbuild.example.js"
},
"files": [
"dist",
Expand All @@ -34,5 +36,8 @@
"path": "dist/netcodejs.esm.js",
"limit": "10 KB"
}
]
],
"dependencies": {
"@netcodejs/util": "^0.0.0"
}
}
6 changes: 6 additions & 0 deletions packages/netcode/src/comp-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export abstract class IComp {
get $comps() {
return this._entity!.$comps;
}
get logicTime() {
return this.domain!.logicTime;
}
get renderTime() {
return this.domain!.renderTime;
}

get<T extends IComp>(ctr: { new (): T }): T | null {
return this._entity!.get(ctr);
Expand Down
6 changes: 3 additions & 3 deletions packages/netcode/src/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class Domain<T extends SupportNetDataType = any> {
public readonly readonlyInternalMsgMng!: MessageManager<T>;

private _fixedSecAccumulator = 0;
public readonly time: Entity;
public readonly singleton: Entity;
public readonly logicTime: LogicTimeComp;
public readonly renderTime: RenderTimeComp;

Expand All @@ -123,10 +123,10 @@ export class Domain<T extends SupportNetDataType = any> {

this.logicTime = new LogicTimeComp();
this.renderTime = new RenderTimeComp();
this.time = new Entity(this.logicTime, this.renderTime);
this.singleton = new Entity(this.logicTime, this.renderTime);
this.logicTime.delta = this.option.fixedTimeSec;

this.reg(this.time);
this.reg(this.singleton);
}

//#region public methods
Expand Down
3 changes: 2 additions & 1 deletion packages/netcode/src/message-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { composeVersion, decomposeVersion, Deferred } from "./misc";
import { composeVersion, decomposeVersion } from "./misc";
import {
IDataBufferReader,
IDataBufferWriter,
Expand All @@ -8,6 +8,7 @@ import {
import { DataTypeVoid, ISchema } from "./comp-schema";
import { RPC_MAX_UUID } from "./builtin";
import { IComp } from "./comp-interface";
import { Deferred } from "@netcodejs/util";

export enum MessageType {
UPDATE_COMPONENT,
Expand Down
62 changes: 0 additions & 62 deletions packages/netcode/src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,65 +32,3 @@ export function assert(
}

export type ProtoOf<T> = Pick<T, keyof T>;

export class Deferred<T = unknown> {
public promise: Promise<T>;

private fate: "resolved" | "unresolved";
private state: "pending" | "fulfilled" | "rejected";

private _resolve!: (value: T | PromiseLike<T>) => void;
private _reject!: (reason?: any) => void;
private _value: any;
public get value() {
return this._value;
}

constructor() {
this.state = "pending";
this.fate = "unresolved";
this.promise = new Promise((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
});
this.promise.then(
(res) => {
this.state = "fulfilled";
this._value = res;
},
() => (this.state = "rejected")
);
}

resolve(value: T | PromiseLike<T>) {
if (this.fate === "resolved") {
throw "Deferred cannot be resolved twice";
}
this.fate = "resolved";
this._resolve(value);
}

reject(reason?: any) {
if (this.fate === "resolved") {
throw "Deferred cannot be resolved twice";
}
this.fate = "resolved";
this._reject(reason);
}

isResolved() {
return this.fate === "resolved";
}

isPending() {
return this.state === "pending";
}

isFulfilled() {
return this.state === "fulfilled";
}

isRejected() {
return this.state === "rejected";
}
}
13 changes: 10 additions & 3 deletions packages/netcode/test/blash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ describe("entity-componrnt", () => {
expect(ent.mget(LogicComponent)).toStrictEqual([logic, newLogic]);
expect(view.get(ViewComponentNoDecoration)).toBeNull();
expect(ent.mget(ViewComponentNoDecoration)).toStrictEqual([]);
expect(ent.mget(LengthArrComp)).toStrictEqual([]);
expect(view.get(ArrComp)).toBeNull();
expect(ent.has(ViewComponentNoDecoration)).toBeFalsy();
expect(view.$comps.view === view);
Expand All @@ -169,6 +170,11 @@ describe("entity-componrnt", () => {
expect(hasView).toBeTruthy();
// expect(getView).not.toBeTruthy();
expect(getView).toBeTruthy();

expect(ent.indexOf(view)).toBe(1);
expect(ent.indexOf(newLogic)).toBe(3);
expect(ent.indexOf(new ArrComp())).toBeLessThan(0);
expect(ent.indexOf(null!)).toBeLessThan(0);
});
test("addComp-white-no-decoration", () => {
expect(() => {
Expand Down Expand Up @@ -363,12 +369,12 @@ describe("Serable", () => {
class LifecycleTestComp extends IComp {
logicUpdate() {
hasLogicUpdate = true;
logicTime = this.domain.logicTime.duration;
logicTime = this.logicTime.duration;
}

renderUpdate() {
hasRenderUpdate = true;
renderTime = this.domain.renderTime.duration;
renderTime = this.renderTime.duration;
}

init() {
Expand All @@ -385,7 +391,8 @@ describe("Serable", () => {
"default",
new StringDomainOption(RpcType.SERVER)
);
const e = new Entity(new LifecycleTestComp());
const lifecycle = new LifecycleTestComp();
const e = new Entity(lifecycle);
d.reg(e);
d.update(1);
d.unreg(e);
Expand Down
54 changes: 1 addition & 53 deletions packages/netcode/test/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { asSerable, assert, Deferred, isPrimitive } from "../src";
import { wait } from "./util";
import { asSerable, assert, isPrimitive } from "../src";

test("isPrimitive", () => {
expect(isPrimitive(true)).toBe(true);
Expand All @@ -17,57 +16,6 @@ test("asset", () => {
expect(() => assert(true, Error)).not.toThrowError(Error);
});

describe("Deferred", () => {
test("basic", () => {
const defer = new Deferred<void>();
expect(defer.promise).toBeTruthy();
expect(defer.value).toBeFalsy();
expect(defer.isFulfilled()).toBe(false);
expect(defer.isPending()).toBe(true);
expect(defer.isResolved()).toBe(false);
expect(defer.isRejected()).toBe(false);
});

test("resolved", async () => {
let outResult = 0;
const inResult = 1;
const defer = new Deferred<number>();
defer.resolve(inResult);
expect(() => defer.resolve(inResult)).toThrow(
"Deferred cannot be resolved twice"
);
outResult = await defer.promise;

expect(outResult).toBe(inResult);
expect(defer.value).toBe(inResult);
expect(defer.isFulfilled()).toBe(true);
expect(defer.isPending()).toBe(false);
expect(defer.isResolved()).toBe(true);
expect(defer.isRejected()).toBe(false);
});

test("rejected", async () => {
const inReason = 1;
let outReason = 0;
const defer = new Deferred<number>();
defer.promise.then(undefined, (r) => {
outReason = r;
});
defer.reject(inReason);
expect(() => defer.reject(inReason)).toThrow(
"Deferred cannot be resolved twice"
);
await wait();

expect(outReason).toBe(inReason);
expect(defer.value).toBeFalsy;
expect(defer.isFulfilled()).toBe(false);
expect(defer.isPending()).toBe(false);
expect(defer.isResolved()).toBe(true);
expect(defer.isRejected()).toBe(true);
});
});

test("asSerable", () => {
class S {
ser() {}
Expand Down
13 changes: 10 additions & 3 deletions packages/netcode/test/rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ describe("rpc-return-type", () => {
class Console extends IComp {
@NetVar(DataType.F32)
callCount = 0;
@NetVar(DataType.DOUBLE)
num: number = 0;

@Rpc(Role.AUTHORITY, DataType.BOOL)
log(@RpcVar(DataType.INT) value: number) {
log(num: number, @RpcVar(DataType.INT) value: number) {
this.num = num;
if (value <= 0) return Promise.resolve(false);
console.log(`[${this.domain.name}]I am number: ${value}`);
return Promise.resolve(true);
Expand All @@ -109,22 +112,26 @@ describe("rpc-return-type", () => {
const clientLogic = client.get(serverEnt.id)!.get(Console)!;
let check = false;

clientLogic.log(1).then((result) => {
clientLogic.log(123, 1).then((result) => {
check = result;
});
server.setData(client.asData());
await wait();
expect(serverLogic.num).toBe(123);
client.setData(server.asData());
await wait();
expect(check).toEqual(true);
expect(clientLogic.num).toBe(123);

clientLogic.log(0).then((result) => {
clientLogic.log(124, 0).then((result) => {
check = result;
});
server.setData(client.asData());
await wait();
expect(serverLogic.num).toBe(124);
client.setData(server.asData());
await wait();
expect(check).toEqual(false);
expect(clientLogic.num).toBe(124);
});
});
Loading