|
| 1 | +import type { ActorContextHandle, ConnHandle, CoreRuntime } from "./runtime"; |
| 2 | +import type { NativeValidationConfig } from "./native-validation"; |
| 3 | +import { callNativeSync, NativeConnAdapter } from "./native"; |
| 4 | + |
| 5 | +export class ConnectionMap implements ReadonlyMap<string, NativeConnAdapter> { |
| 6 | + #runtime: CoreRuntime; |
| 7 | + #ctx: ActorContextHandle; |
| 8 | + #schemas: NativeValidationConfig; |
| 9 | + |
| 10 | + constructor( |
| 11 | + runtime: CoreRuntime, |
| 12 | + ctx: ActorContextHandle, |
| 13 | + schemas: NativeValidationConfig, |
| 14 | + ) { |
| 15 | + this.#runtime = runtime; |
| 16 | + this.#ctx = ctx; |
| 17 | + this.#schemas = schemas; |
| 18 | + } |
| 19 | + |
| 20 | + #connToAdapter(conn: ConnHandle): NativeConnAdapter { |
| 21 | + return new NativeConnAdapter( |
| 22 | + this.#runtime, |
| 23 | + conn, |
| 24 | + this.#schemas, |
| 25 | + this.#ctx, |
| 26 | + (connId) => |
| 27 | + callNativeSync(() => |
| 28 | + this.#runtime.actorQueueHibernationRemoval( |
| 29 | + this.#ctx, |
| 30 | + connId, |
| 31 | + ), |
| 32 | + ), |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + get size(): number { |
| 37 | + return callNativeSync(() => this.#runtime.actorConns(this.#ctx)).length; |
| 38 | + } |
| 39 | + |
| 40 | + get(key: string): NativeConnAdapter | undefined { |
| 41 | + const conns = callNativeSync(() => this.#runtime.actorConns(this.#ctx)); |
| 42 | + const conn = conns.find( |
| 43 | + (c) => this.#runtime.connId(c) === key, |
| 44 | + ); |
| 45 | + if (!conn) return undefined; |
| 46 | + return this.#connToAdapter(conn); |
| 47 | + } |
| 48 | + |
| 49 | + has(key: string): boolean { |
| 50 | + const conns = callNativeSync(() => this.#runtime.actorConns(this.#ctx)); |
| 51 | + return conns.some((c) => this.#runtime.connId(c) === key); |
| 52 | + } |
| 53 | + |
| 54 | + keys(): MapIterator<string> { |
| 55 | + const conns = callNativeSync(() => this.#runtime.actorConns(this.#ctx)); |
| 56 | + return conns.map((c) => this.#runtime.connId(c))[Symbol.iterator]() as MapIterator<string>; |
| 57 | + } |
| 58 | + |
| 59 | + values(): MapIterator<NativeConnAdapter> { |
| 60 | + const conns = callNativeSync(() => this.#runtime.actorConns(this.#ctx)); |
| 61 | + return conns.map((c) => this.#connToAdapter(c))[Symbol.iterator]() as MapIterator<NativeConnAdapter>; |
| 62 | + } |
| 63 | + |
| 64 | + entries(): MapIterator<[string, NativeConnAdapter]> { |
| 65 | + const conns = callNativeSync(() => this.#runtime.actorConns(this.#ctx)); |
| 66 | + return conns.map( |
| 67 | + (c) => [this.#runtime.connId(c), this.#connToAdapter(c)] as [string, NativeConnAdapter], |
| 68 | + )[Symbol.iterator]() as MapIterator<[string, NativeConnAdapter]>; |
| 69 | + } |
| 70 | + |
| 71 | + forEach( |
| 72 | + callback: (value: NativeConnAdapter, key: string, map: ReadonlyMap<string, NativeConnAdapter>) => void, |
| 73 | + thisArg?: unknown, |
| 74 | + ): void { |
| 75 | + const conns = callNativeSync(() => this.#runtime.actorConns(this.#ctx)); |
| 76 | + for (const conn of conns) { |
| 77 | + const id = this.#runtime.connId(conn); |
| 78 | + callback.call(thisArg, this.#connToAdapter(conn), id, this); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + [Symbol.iterator](): MapIterator<[string, NativeConnAdapter]> { |
| 83 | + return this.entries(); |
| 84 | + } |
| 85 | + |
| 86 | + get [Symbol.toStringTag](): string { |
| 87 | + return "ConnectionMap"; |
| 88 | + } |
| 89 | +} |
0 commit comments