@zimi/remote 0.2.1-alpha.7 → 0.2.1-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ import { type AdaptorPackageData } from '../../adaptor';
3
+ interface RemoteChannel {
4
+ onClientEvent(fn: (e: unknown) => void): void;
5
+ }
6
+ /**
7
+ * 这是 [dao3 游戏](https://dao3.fun/) 代码客户端适配器。
8
+ * ```ts
9
+ * const remoteManager = new ClientSideRemoteChannelEventManager(remoteChannel)
10
+ *
11
+ * export const remoteAdaptor = {
12
+ * every: remoteManager.onEvery.bind(remoteManager),
13
+ * off: remoteManager.off.bind(remoteManager),
14
+ * on: remoteManager.on.bind(remoteManager),
15
+ * once: remoteManager.once.bind(remoteManager),
16
+ * emit: (e) => {
17
+ * remoteChannel.sendServerEvent(e)
18
+ * },
19
+ * } satisfies Adaptor
20
+ * ```
21
+ */
22
+ export declare class ClientSideRemoteChannelEventManager extends EventEmitter<{
23
+ [key: string]: [AdaptorPackageData];
24
+ }> {
25
+ constructor(remoteChannel: RemoteChannel);
26
+ onEvery(fn: (args: AdaptorPackageData) => void): void;
27
+ }
28
+ export {};
@@ -0,0 +1,36 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ import { isRemoteAdaptorData } from '../../remote';
3
+ /**
4
+ * 这是 [dao3 游戏](https://dao3.fun/) 代码客户端适配器。
5
+ * ```ts
6
+ * const remoteManager = new ClientSideRemoteChannelEventManager(remoteChannel)
7
+ *
8
+ * export const remoteAdaptor = {
9
+ * every: remoteManager.onEvery.bind(remoteManager),
10
+ * off: remoteManager.off.bind(remoteManager),
11
+ * on: remoteManager.on.bind(remoteManager),
12
+ * once: remoteManager.once.bind(remoteManager),
13
+ * emit: (e) => {
14
+ * remoteChannel.sendServerEvent(e)
15
+ * },
16
+ * } satisfies Adaptor
17
+ * ```
18
+ */
19
+ export class ClientSideRemoteChannelEventManager extends EventEmitter {
20
+ constructor(remoteChannel) {
21
+ super();
22
+ remoteChannel.onClientEvent((e) => {
23
+ if (!isRemoteAdaptorData(e)) {
24
+ return;
25
+ }
26
+ if (!e.name.startsWith('__REMOTE_VALUE_REQ__')) {
27
+ this.emit('__remote_every__', e);
28
+ }
29
+ this.emit(e.name, e);
30
+ });
31
+ }
32
+ onEvery(fn) {
33
+ this.on('__remote_every__', fn);
34
+ }
35
+ }
36
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/adaptors/dao3/client.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,eAAe,CAAA;AACxC,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAA;AAOlD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,mCAAoC,SAAQ,YAEvD;IACA,YAAY,aAA4B;QACtC,KAAK,EAAE,CAAA;QACP,aAAa,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,EAAE;YAChC,IAAI,CAAC,mBAAmB,CAAC,CAAC,CAAC,EAAE,CAAC;gBAC5B,OAAM;YACR,CAAC;YACD,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,sBAAsB,CAAC,EAAE,CAAC;gBAC/C,IAAI,CAAC,IAAI,CAAC,kBAAkB,EAAE,CAAC,CAAC,CAAA;YAClC,CAAC;YACD,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAA;QACtB,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,OAAO,CAAC,EAAsC;QAC5C,IAAI,CAAC,EAAE,CAAC,kBAAkB,EAAE,EAAE,CAAC,CAAA;IACjC,CAAC;CACF"}
@@ -0,0 +1,65 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ import { type AdaptorPackageData } from '../../adaptor';
3
+ interface GamePlayerEntityLike {
4
+ player: {
5
+ userKey: string;
6
+ };
7
+ }
8
+ interface RemoteChannel<T extends GamePlayerEntityLike> {
9
+ onServerEvent(fn: (e: {
10
+ args: unknown;
11
+ entity: T;
12
+ }) => void): void;
13
+ }
14
+ /**
15
+ * 这是 [dao3 游戏](https://dao3.fun/) 代码服务端适配器。
16
+ * ```ts
17
+ * const remoteManager = new ServerSideRemoteChannelEventManager(remoteChannel)
18
+ *
19
+ * world.onPlayerLeave(
20
+ * remoteManager.onPlayerLeave.bind(remoteManager)
21
+ * )
22
+ *
23
+ * export const remoteAdaptor = {
24
+ * every: remoteManager.onEvery.bind(remoteManager),
25
+ * off: remoteManager.off.bind(remoteManager),
26
+ * on: remoteManager.on.bind(remoteManager),
27
+ * once: remoteManager.once.bind(remoteManager),
28
+ * emit: (e) => {
29
+ * const entity = getEntity(e.targetDeviceId)
30
+ * if (!entity) {
31
+ * console.error('entity not found')
32
+ * return
33
+ * }
34
+ * remoteChannel.sendClientEvent(entity, e as unknown as JSONValue)
35
+ * },
36
+ * } satisfies Adaptor
37
+ *
38
+ * ```
39
+ */
40
+ export declare class ServerSideRemoteChannelEventManager<T extends GamePlayerEntityLike> extends EventEmitter<{
41
+ [key: string]: [AdaptorPackageData];
42
+ }> {
43
+ entityMap: [string, T][];
44
+ constructor(remoteChannel: RemoteChannel<T>);
45
+ onPlayerLeave(e: {
46
+ entity: T;
47
+ }): void;
48
+ onEvery(fn: (args: AdaptorPackageData) => void): void;
49
+ waitForRegister(entity: T): Promise<void>;
50
+ getEntity(deviceId: string): T | undefined;
51
+ getIdByEntity(entity: T): string | undefined;
52
+ remoteTo(config: {
53
+ target: T;
54
+ }): {
55
+ targetDeviceId: string;
56
+ };
57
+ remoteTo(config: {
58
+ target: T;
59
+ timeoutMs: number;
60
+ }): {
61
+ targetDeviceId: string;
62
+ timeoutMs: number;
63
+ };
64
+ }
65
+ export {};
@@ -0,0 +1,90 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ import { isRemoteAdaptorData } from '../../remote';
3
+ import { isRemoteValueEvent } from '../../remoteValue/exposeToRemote';
4
+ /**
5
+ * 这是 [dao3 游戏](https://dao3.fun/) 代码服务端适配器。
6
+ * ```ts
7
+ * const remoteManager = new ServerSideRemoteChannelEventManager(remoteChannel)
8
+ *
9
+ * world.onPlayerLeave(
10
+ * remoteManager.onPlayerLeave.bind(remoteManager)
11
+ * )
12
+ *
13
+ * export const remoteAdaptor = {
14
+ * every: remoteManager.onEvery.bind(remoteManager),
15
+ * off: remoteManager.off.bind(remoteManager),
16
+ * on: remoteManager.on.bind(remoteManager),
17
+ * once: remoteManager.once.bind(remoteManager),
18
+ * emit: (e) => {
19
+ * const entity = getEntity(e.targetDeviceId)
20
+ * if (!entity) {
21
+ * console.error('entity not found')
22
+ * return
23
+ * }
24
+ * remoteChannel.sendClientEvent(entity, e as unknown as JSONValue)
25
+ * },
26
+ * } satisfies Adaptor
27
+ *
28
+ * ```
29
+ */
30
+ export class ServerSideRemoteChannelEventManager extends EventEmitter {
31
+ constructor(remoteChannel) {
32
+ super();
33
+ Object.defineProperty(this, "entityMap", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: []
38
+ });
39
+ remoteChannel.onServerEvent((e) => {
40
+ const { args } = e;
41
+ if (!isRemoteAdaptorData(args)) {
42
+ return;
43
+ }
44
+ const prevItem = this.entityMap.find((item) => item[1].player.userKey === e.entity.player.userKey);
45
+ if (prevItem) {
46
+ prevItem[0] = args.deviceId;
47
+ prevItem[1] = e.entity;
48
+ }
49
+ else {
50
+ this.entityMap.push([args.deviceId, e.entity]);
51
+ }
52
+ if (!isRemoteValueEvent(args.name)) {
53
+ this.emit('__remote_every__', args);
54
+ }
55
+ this.emit(args.name, args);
56
+ });
57
+ }
58
+ onPlayerLeave(e) {
59
+ this.entityMap = this.entityMap.filter((item) => item[1].player.userKey !== e.entity.player.userKey);
60
+ }
61
+ onEvery(fn) {
62
+ this.on('__remote_every__', fn);
63
+ }
64
+ waitForRegister(entity) {
65
+ return new Promise((resolve) => {
66
+ if (this.entityMap.some(([_, en]) => en.player.userKey === entity.player.userKey)) {
67
+ resolve();
68
+ return;
69
+ }
70
+ this.once('__remote_every__', () => {
71
+ resolve();
72
+ });
73
+ });
74
+ }
75
+ getEntity(deviceId) {
76
+ var _a;
77
+ return (_a = this.entityMap.find((item) => item[0] === deviceId)) === null || _a === void 0 ? void 0 : _a[1];
78
+ }
79
+ getIdByEntity(entity) {
80
+ var _a;
81
+ return (_a = this.entityMap.find((item) => item[1].player.userKey === entity.player.userKey)) === null || _a === void 0 ? void 0 : _a[0];
82
+ }
83
+ remoteTo({ target, ...rest }) {
84
+ return {
85
+ targetDeviceId: this.getIdByEntity(target),
86
+ ...rest,
87
+ };
88
+ }
89
+ }
90
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/adaptors/dao3/server.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,eAAe,CAAA;AACxC,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAA;AAClD,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAA;AAarE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,OAAO,mCAEX,SAAQ,YAER;IAGA,YAAY,aAA+B;QACzC,KAAK,EAAE,CAAA;QAHT;;;;mBAA2B,EAAE;WAAA;QAI3B,aAAa,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,EAAE;YAChC,MAAM,EAAE,IAAI,EAAE,GAAG,CAAC,CAAA;YAClB,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC/B,OAAM;YACR,CAAC;YACD,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAClC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,KAAK,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAC7D,CAAA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C,kBAAkB,EAAE,GAAG,EAAE;gBACjC,OAAO,EAAE,CAAA;YACX,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,SAAS,CAAC,QAAgB;;QACxB,OAAO,MAAA,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ,CAAC,0CAAG,CAAC,CAAC,CAAA;IACjE,CAAC;IAED,aAAa,CAAC,MAAS;;QACrB,OAAO,MAAA,IAAI,CAAC,SAAS,CAAC,IAAI,CACxB,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,KAAK,MAAM,CAAC,MAAM,CAAC,OAAO,CAC3D,0CAAG,CAAC,CAAC,CAAA;IACR,CAAC;IASD,QAAQ,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI,EAAqC;QAC7D,OAAO;YACL,cAAc,EAAE,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC;YAC1C,GAAG,IAAI;SACR,CAAA;IACH,CAAC;CACF"}
package/dist/index.d.ts CHANGED
@@ -1,8 +1,10 @@
1
- export { Remote, isRemoteAdaptorData } from './remote';
2
- export type { Adaptor, AdaptorCallback, AdaptorPackageData } from './adaptor';
3
- export { RemoteError, RemoteNotFoundError, RemoteTimeoutError, response, } from './response';
4
- export { createIframeAdaptor } from './adaptors/iframe';
5
- export { createHttpAdaptor, remoteEventManager } from './adaptors/http';
1
+ export * from './remote';
2
+ export * from './adaptor';
3
+ export * from './response';
4
+ export * from './remoteValue/remoteValue';
5
+ export * from './remoteValue/exposeToRemote';
6
6
  export type { ToFunc } from './remoteValue/type';
7
- export { remoteValue } from './remoteValue/remoteValue';
8
- export { exposeToRemote } from './remoteValue/exposeToRemote';
7
+ export * from './adaptors/iframe';
8
+ export * from './adaptors/http';
9
+ export * from './adaptors/dao3/client';
10
+ export * from './adaptors/dao3/server';
package/dist/index.js CHANGED
@@ -1,7 +1,10 @@
1
- export { Remote, isRemoteAdaptorData } from './remote';
2
- export { RemoteError, RemoteNotFoundError, RemoteTimeoutError, response, } from './response';
3
- export { createIframeAdaptor } from './adaptors/iframe';
4
- export { createHttpAdaptor, remoteEventManager } from './adaptors/http';
5
- export { remoteValue } from './remoteValue/remoteValue';
6
- export { exposeToRemote } from './remoteValue/exposeToRemote';
1
+ export * from './remote';
2
+ export * from './adaptor';
3
+ export * from './response';
4
+ export * from './remoteValue/remoteValue';
5
+ export * from './remoteValue/exposeToRemote';
6
+ export * from './adaptors/iframe';
7
+ export * from './adaptors/http';
8
+ export * from './adaptors/dao3/client';
9
+ export * from './adaptors/dao3/server';
7
10
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAA;AAEtD,OAAO,EACL,WAAW,EACX,mBAAmB,EACnB,kBAAkB,EAClB,QAAQ,GACT,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AAEvE,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAE1B,cAAc,2BAA2B,CAAA;AACzC,cAAc,8BAA8B,CAAA;AAG5C,cAAc,mBAAmB,CAAA;AACjC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,wBAAwB,CAAA;AACtC,cAAc,wBAAwB,CAAA"}
@@ -11,5 +11,6 @@ interface ExposeProps {
11
11
  */
12
12
  onRequest?: (e: AdaptorPackageData) => void | Promise<void>;
13
13
  }
14
+ export declare function isRemoteValueEvent(eventName: string): boolean;
14
15
  export declare function exposeToRemote<T extends object>(obj: T, options: ExposeProps): () => void;
15
16
  export {};
@@ -2,6 +2,9 @@ import { RemoteError, response } from '../response';
2
2
  function defaultOnRequest(e) {
3
3
  return e;
4
4
  }
5
+ export function isRemoteValueEvent(eventName) {
6
+ return eventName.startsWith('__REMOTE_VALUE_REQ__');
7
+ }
5
8
  export function exposeToRemote(obj, options) {
6
9
  const { globalName, adaptor, exposeTo, onRequest = defaultOnRequest, } = options;
7
10
  const callback = async (e) => {
@@ -1 +1 @@
1
- {"version":3,"file":"exposeToRemote.js","sourceRoot":"","sources":["../../src/remoteValue/exposeToRemote.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAenD,SAAS,gBAAgB,CAAC,CAAqB;IAC7C,OAAO,CAAC,CAAA;AACV,CAAC;AAED,MAAM,UAAU,cAAc,CAAmB,GAAM,EAAE,OAAoB;IAC3E,MAAM,EACJ,UAAU,EACV,OAAO,EACP,QAAQ,EACR,SAAS,GAAG,gBAAgB,GAC7B,GAAG,OAAO,CAAA;IACX,MAAM,QAAQ,GAAG,KAAK,EAAE,CAAqB,EAAE,EAAE;;QAC/C,IAAI,CAAC;YACH,MAAM,SAAS,CAAC,CAAC,CAAC,CAAA;YAClB,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC9D,MAAM,IAAI,WAAW,CAAC,mBAAmB,CAAC,CAAA;YAC5C,CAAC;YACD,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC,IAA4C,CAAA;YACtE,IAAI,MAAM,GAAG,G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;YACD,OAAO,CAAC,IAAI,CAAC;gBACX,IAAI,EAAE,MAAA,CAAC,CAAC,YAAY,mCAAI,6BAA6B;gBACrD,QAAQ,EAAE,CAAC,CAAC,cAAc;gBAC1B,cAAc,EAAE,CAAC,CAAC,QAAQ;gBAC1B,IAAI,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC;aAC5B,CAAC,CAAA;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,IAAI,CAAC;gBACX,IAAI,EAAE,MAAA,CAAC,CAAC,YAAY,mCAAI,6BAA6B;gBACrD,QAAQ,EAAE,CAAC,CAAC,cAAc;gBAC1B,cAAc,EAAE,CAAC,CAAC,QAAQ;gBAC1B,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,WAAW,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;aACnD,CAAC,CAAA;QACJ,CAAC;IACH,CAAC,CAAA;IACD,OAAO,CAAC,EAAE,CAAC,uBAAuB,UAAU,EAAE,EAAE,QAAQ,CAAC,CAAA;IACzD,OAAO,GAAG,EAAE;QACV,OAAO,CAAC,GAAG,CAAC,uBAAuB,UAAU,EAAE,EAAE,QAAQ,CAAC,CAAA;IAC5D,CAAC,CAAA;AACH,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zimi/remote",
3
3
  "license": "MIT",
4
- "version": "0.2.1-alpha.7",
4
+ "version": "0.2.1-alpha.9",
5
5
  "author": "xiaomingTang",
6
6
  "description": "call remote functions as local",
7
7
  "private": false,
@@ -0,0 +1,44 @@
1
+ import EventEmitter from 'eventemitter3'
2
+ import { isRemoteAdaptorData } from '../../remote'
3
+ import { type AdaptorPackageData } from '../../adaptor'
4
+
5
+ interface RemoteChannel {
6
+ onClientEvent(fn: (e: unknown) => void): void
7
+ }
8
+
9
+ /**
10
+ * 这是 [dao3 游戏](https://dao3.fun/) 代码客户端适配器。
11
+ * ```ts
12
+ * const remoteManager = new ClientSideRemoteChannelEventManager(remoteChannel)
13
+ *
14
+ * export const remoteAdaptor = {
15
+ * every: remoteManager.onEvery.bind(remoteManager),
16
+ * off: remoteManager.off.bind(remoteManager),
17
+ * on: remoteManager.on.bind(remoteManager),
18
+ * once: remoteManager.once.bind(remoteManager),
19
+ * emit: (e) => {
20
+ * remoteChannel.sendServerEvent(e)
21
+ * },
22
+ * } satisfies Adaptor
23
+ * ```
24
+ */
25
+ export class ClientSideRemoteChannelEventManager extends EventEmitter<{
26
+ [key: string]: [AdaptorPackageData]
27
+ }> {
28
+ constructor(remoteChannel: RemoteChannel) {
29
+ super()
30
+ remoteChannel.onClientEvent((e) => {
31
+ if (!isRemoteAdaptorData(e)) {
32
+ return
33
+ }
34
+ if (!e.name.startsWith('__REMOTE_VALUE_REQ__')) {
35
+ this.emit('__remote_every__', e)
36
+ }
37
+ this.emit(e.name, e)
38
+ })
39
+ }
40
+
41
+ onEvery(fn: (args: AdaptorPackageData) => void): void {
42
+ this.on('__remote_every__', fn)
43
+ }
44
+ }
@@ -0,0 +1,121 @@
1
+ import EventEmitter from 'eventemitter3'
2
+ import { isRemoteAdaptorData } from '../../remote'
3
+ import { isRemoteValueEvent } from '../../remoteValue/exposeToRemote'
4
+ import { type AdaptorPackageData } from '../../adaptor'
5
+
6
+ interface GamePlayerEntityLike {
7
+ player: {
8
+ userKey: string
9
+ }
10
+ }
11
+
12
+ interface RemoteChannel<T extends GamePlayerEntityLike> {
13
+ onServerEvent(fn: (e: { args: unknown; entity: T }) => void): void
14
+ }
15
+
16
+ /**
17
+ * 这是 [dao3 游戏](https://dao3.fun/) 代码服务端适配器。
18
+ * ```ts
19
+ * const remoteManager = new ServerSideRemoteChannelEventManager(remoteChannel)
20
+ *
21
+ * world.onPlayerLeave(
22
+ * remoteManager.onPlayerLeave.bind(remoteManager)
23
+ * )
24
+ *
25
+ * export const remoteAdaptor = {
26
+ * every: remoteManager.onEvery.bind(remoteManager),
27
+ * off: remoteManager.off.bind(remoteManager),
28
+ * on: remoteManager.on.bind(remoteManager),
29
+ * once: remoteManager.once.bind(remoteManager),
30
+ * emit: (e) => {
31
+ * const entity = getEntity(e.targetDeviceId)
32
+ * if (!entity) {
33
+ * console.error('entity not found')
34
+ * return
35
+ * }
36
+ * remoteChannel.sendClientEvent(entity, e as unknown as JSONValue)
37
+ * },
38
+ * } satisfies Adaptor
39
+ *
40
+ * ```
41
+ */
42
+ export class ServerSideRemoteChannelEventManager<
43
+ T extends GamePlayerEntityLike,
44
+ > extends EventEmitter<{
45
+ [key: string]: [AdaptorPackageData]
46
+ }> {
47
+ entityMap: [string, T][] = []
48
+
49
+ constructor(remoteChannel: RemoteChannel<T>) {
50
+ super()
51
+ remoteChannel.onServerEvent((e) => {
52
+ const { args } = e
53
+ if (!isRemoteAdaptorData(args)) {
54
+ return
55
+ }
56
+ const prevItem = this.entityMap.find(
57
+ (item) => item[1].player.userKey === e.entity.player.userKey
58
+ )
59
+ if (prevItem) {
60
+ prevItem[0] = args.deviceId
61
+ prevItem[1] = e.entity
62
+ } else {
63
+ this.entityMap.push([args.deviceId, e.entity])
64
+ }
65
+ if (!isRemoteValueEvent(args.name)) {
66
+ this.emit('__remote_every__', args)
67
+ }
68
+ this.emit(args.name, args)
69
+ })
70
+ }
71
+
72
+ onPlayerLeave(e: { entity: T }) {
73
+ this.entityMap = this.entityMap.filter(
74
+ (item) => item[1].player.userKey !== e.entity.player.userKey
75
+ )
76
+ }
77
+
78
+ onEvery(fn: (args: AdaptorPackageData) => void): void {
79
+ this.on('__remote_every__', fn)
80
+ }
81
+
82
+ waitForRegister(entity: T) {
83
+ return new Promise<void>((resolve) => {
84
+ if (
85
+ this.entityMap.some(
86
+ ([_, en]) => en.player.userKey === entity.player.userKey
87
+ )
88
+ ) {
89
+ resolve()
90
+ return
91
+ }
92
+ this.once('__remote_every__', () => {
93
+ resolve()
94
+ })
95
+ })
96
+ }
97
+
98
+ getEntity(deviceId: string): T | undefined {
99
+ return this.entityMap.find((item) => item[0] === deviceId)?.[1]
100
+ }
101
+
102
+ getIdByEntity(entity: T): string | undefined {
103
+ return this.entityMap.find(
104
+ (item) => item[1].player.userKey === entity.player.userKey
105
+ )?.[0]
106
+ }
107
+
108
+ remoteTo(config: { target: T }): {
109
+ targetDeviceId: string
110
+ }
111
+ remoteTo(config: { target: T; timeoutMs: number }): {
112
+ targetDeviceId: string
113
+ timeoutMs: number
114
+ }
115
+ remoteTo({ target, ...rest }: { target: T; timeoutMs?: number }) {
116
+ return {
117
+ targetDeviceId: this.getIdByEntity(target),
118
+ ...rest,
119
+ }
120
+ }
121
+ }
package/src/index.ts CHANGED
@@ -1,13 +1,12 @@
1
- export { Remote, isRemoteAdaptorData } from './remote'
2
- export type { Adaptor, AdaptorCallback, AdaptorPackageData } from './adaptor'
3
- export {
4
- RemoteError,
5
- RemoteNotFoundError,
6
- RemoteTimeoutError,
7
- response,
8
- } from './response'
9
- export { createIframeAdaptor } from './adaptors/iframe'
10
- export { createHttpAdaptor, remoteEventManager } from './adaptors/http'
1
+ export * from './remote'
2
+ export * from './adaptor'
3
+ export * from './response'
4
+
5
+ export * from './remoteValue/remoteValue'
6
+ export * from './remoteValue/exposeToRemote'
11
7
  export type { ToFunc } from './remoteValue/type'
12
- export { remoteValue } from './remoteValue/remoteValue'
13
- export { exposeToRemote } from './remoteValue/exposeToRemote'
8
+
9
+ export * from './adaptors/iframe'
10
+ export * from './adaptors/http'
11
+ export * from './adaptors/dao3/client'
12
+ export * from './adaptors/dao3/server'
@@ -18,6 +18,10 @@ function defaultOnRequest(e: AdaptorPackageData) {
18
18
  return e
19
19
  }
20
20
 
21
+ export function isRemoteValueEvent(eventName: string) {
22
+ return eventName.startsWith('__REMOTE_VALUE_REQ__')
23
+ }
24
+
21
25
  export function exposeToRemote<T extends object>(obj: T, options: ExposeProps) {
22
26
  const {
23
27
  globalName,