From d513fcc89e8247732ca553187875a0aaeda17919 Mon Sep 17 00:00:00 2001 From: benStre Date: Thu, 18 Jan 2024 18:17:22 +0100 Subject: [PATCH 1/6] add ignoreHEllo --- network/client.ts | 14 +++++++++++++- runtime/pointers.ts | 1 + runtime/runtime.ts | 4 ++-- threads/worker-com-interface.ts | 1 + types/addressing.ts | 4 ++++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/network/client.ts b/network/client.ts index 0b04e23a..b7a80334 100644 --- a/network/client.ts +++ b/network/client.ts @@ -32,6 +32,7 @@ export interface ComInterface { endpoint?: Endpoint // connected directly to a single endpoint endpoints?: Set // multiple endpoints is_bidirectional_hub?: boolean, // allow the same block to go in and out eg a -> this interface -> this runtime -> this interface again -> b + immediate?: boolean // can send immediately (eg. for local interfaces, workers) isEqualSource?:(source: Partial, to: Endpoint) => boolean in: boolean // can receive data out: boolean // can send data @@ -162,6 +163,7 @@ export abstract class CommonInterface implements Co public in = true public out = true public global = true + public immediate = false public get endpoint() { return this._endpoint @@ -197,7 +199,17 @@ export abstract class CommonInterface implements Co this.initial_arguments = args; this.connected = await this.connect(); - if (this.connected) this.updateEndpoint(); + console.log("connect",this) + if (this.connected) { + this.updateEndpoint(); + // immediately consider endpoint as online + if (this.endpoint && this.immediate) { + console.warn("immediate online " + endpoint,this) + this.endpoint.setOnline(true) + // don't trigger online state change (to offline) once first HELLO message is received + this.endpoint.ignoreHello = true; + } + } return this.connected; } diff --git a/runtime/pointers.ts b/runtime/pointers.ts index f8ab7b3d..8ac24c98 100644 --- a/runtime/pointers.ts +++ b/runtime/pointers.ts @@ -3073,6 +3073,7 @@ export class Pointer extends Ref { * Removes all subscriptions for an endpoint */ public static clearEndpointSubscriptions(endpoint: Endpoint) { + console.warn("cler frpr " + endpoint) let removeCount = 0; for (const pointer of Pointer.#endpoint_subscriptions.get(endpoint) ?? []) { diff --git a/runtime/runtime.ts b/runtime/runtime.ts index fda166ec..b7d8d4c1 100644 --- a/runtime/runtime.ts +++ b/runtime/runtime.ts @@ -1824,9 +1824,9 @@ export class Runtime { } // other message, assume sender endpoint is online now else { + // HELLO message received, regard as new login to network, reset previous subscriptions + if (header.type == ProtocolDataType.HELLO && !header.sender.ignoreHello) Pointer.clearEndpointSubscriptions(header.sender) header.sender.setOnline(true) - // new login to network, reset previous subscriptions - if (header.type == ProtocolDataType.HELLO) Pointer.clearEndpointSubscriptions(header.sender) } } } diff --git a/threads/worker-com-interface.ts b/threads/worker-com-interface.ts index 83fcd9ed..74cf804b 100644 --- a/threads/worker-com-interface.ts +++ b/threads/worker-com-interface.ts @@ -12,6 +12,7 @@ export class WorkerCommunicationInterface extends CommonInterface<[Worker]> { override global = false; override authorization_required = false; // don't connect with public keys override type = "worker"; + override immediate = true; protected connect() { diff --git a/types/addressing.ts b/types/addressing.ts index 5d2396b1..5223ced7 100644 --- a/types/addressing.ts +++ b/types/addressing.ts @@ -473,6 +473,10 @@ export class Endpoint extends Target { setTimeout(() => this.#online=undefined, this.#current_online ? Endpoint.cache_life_online : Endpoint.cache_life_offline); } + /** + * Ignore HELLO messages from this endpoint in regards to online state + */ + public ignoreHello = false; // get endpoint from string public static fromString(string:string) { From 2a0fa18cbbe5916811fb528e3a3f5f7e665e7c80 Mon Sep 17 00:00:00 2001 From: benStre Date: Thu, 18 Jan 2024 18:20:23 +0100 Subject: [PATCH 2/6] update comments --- network/client.ts | 2 +- types/addressing.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/network/client.ts b/network/client.ts index b7a80334..348b503b 100644 --- a/network/client.ts +++ b/network/client.ts @@ -206,7 +206,7 @@ export abstract class CommonInterface implements Co if (this.endpoint && this.immediate) { console.warn("immediate online " + endpoint,this) this.endpoint.setOnline(true) - // don't trigger online state change (to offline) once first HELLO message is received + // don't trigger subscription cleanup once first HELLO message is received this.endpoint.ignoreHello = true; } } diff --git a/types/addressing.ts b/types/addressing.ts index 5223ced7..e31efd42 100644 --- a/types/addressing.ts +++ b/types/addressing.ts @@ -474,7 +474,7 @@ export class Endpoint extends Target { } /** - * Ignore HELLO messages from this endpoint in regards to online state + * Ignore HELLO messages from this endpoint (don't clean up subscriptions) */ public ignoreHello = false; From 71250f9e82b8583cc54e8bbdf77021974a71f89e Mon Sep 17 00:00:00 2001 From: benStre Date: Thu, 18 Jan 2024 18:24:37 +0100 Subject: [PATCH 3/6] remove debug logs --- network/client.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/network/client.ts b/network/client.ts index 348b503b..d5da3576 100644 --- a/network/client.ts +++ b/network/client.ts @@ -199,12 +199,10 @@ export abstract class CommonInterface implements Co this.initial_arguments = args; this.connected = await this.connect(); - console.log("connect",this) if (this.connected) { this.updateEndpoint(); // immediately consider endpoint as online if (this.endpoint && this.immediate) { - console.warn("immediate online " + endpoint,this) this.endpoint.setOnline(true) // don't trigger subscription cleanup once first HELLO message is received this.endpoint.ignoreHello = true; From 521ef9bc34e36783a4471bedc64f243691692710 Mon Sep 17 00:00:00 2001 From: benStre Date: Thu, 18 Jan 2024 18:25:49 +0100 Subject: [PATCH 4/6] remove debug logs --- runtime/pointers.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/runtime/pointers.ts b/runtime/pointers.ts index 8ac24c98..f8ab7b3d 100644 --- a/runtime/pointers.ts +++ b/runtime/pointers.ts @@ -3073,7 +3073,6 @@ export class Pointer extends Ref { * Removes all subscriptions for an endpoint */ public static clearEndpointSubscriptions(endpoint: Endpoint) { - console.warn("cler frpr " + endpoint) let removeCount = 0; for (const pointer of Pointer.#endpoint_subscriptions.get(endpoint) ?? []) { From 3b9d341f849bcb001dde6314496ff8e063f4ae86 Mon Sep 17 00:00:00 2001 From: benStre Date: Thu, 18 Jan 2024 18:28:37 +0100 Subject: [PATCH 5/6] updated thread worker log --- threads/thread-worker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/threads/thread-worker.ts b/threads/thread-worker.ts index a4840aa7..a6615985 100644 --- a/threads/thread-worker.ts +++ b/threads/thread-worker.ts @@ -3,7 +3,7 @@ import type { Datex as DatexType } from "../mod.ts"; const isServiceWorker = 'registration' in globalThis && (globalThis as any).registration instanceof ServiceWorkerRegistration; -console.log("initialized thread worker", {isServiceWorker}) +console.log("spawned new thread worker") if (isServiceWorker) { // https://developer.mozilla.org/en-US/docs/Web/API/Clients/claim From f8c50281d22503899f40d3589e855d34d94107b0 Mon Sep 17 00:00:00 2001 From: benStre Date: Thu, 18 Jan 2024 18:37:49 +0100 Subject: [PATCH 6/6] add dark/light theme inheritance for worker loggers --- threads/thread-worker.ts | 3 +++ threads/threads.ts | 7 ++++--- utils/logger.ts | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/threads/thread-worker.ts b/threads/thread-worker.ts index a6615985..9f3245b4 100644 --- a/threads/thread-worker.ts +++ b/threads/thread-worker.ts @@ -58,6 +58,9 @@ addEventListener("message", async function (event) { // await import("https://ga.jspm.io/npm:es-module-shims@1.8.0/dist/es-module-shims.wasm.js"); // if (data.importMap) importShim.addImportMap(data.importMap); + // inherit theme from parent + (globalThis as any)._override_console_theme = data.theme; + await initDatex(data.datexURL); await initWorkerComInterface(data.comInterfaceURL); await initTsInterfaceGenerator(data.tsInterfaceGeneratorURL); diff --git a/threads/threads.ts b/threads/threads.ts index 6e39ddb0..1ca2705e 100644 --- a/threads/threads.ts +++ b/threads/threads.ts @@ -1,4 +1,4 @@ -import { Logger } from "../utils/logger.ts"; +import { Logger, console_theme } from "../utils/logger.ts"; import "./worker-com-interface.ts"; import { Equals } from "../utils/global_types.ts"; @@ -23,7 +23,7 @@ export type ThreadPool = Recordvoid} export type MessageToWorker = - {type: "INIT", datexURL: string, comInterfaceURL: string, moduleURL: string, tsInterfaceGeneratorURL:string, endpoint: string, importMap:Record} | + {type: "INIT", datexURL: string, comInterfaceURL: string, moduleURL: string, tsInterfaceGeneratorURL:string, endpoint: string, importMap:Record, theme:"dark"|"light"} | {type: "INIT_PORT"} export type MessageFromWorker = @@ -503,7 +503,8 @@ export async function _initWorker(worker: Worker|ServiceWorkerRegistration, modu comInterfaceURL: import.meta.resolve("./worker-com-interface.ts"), tsInterfaceGeneratorURL: import.meta.resolve("../utils/interface-generator.ts"), moduleURL: modulePath ? import.meta.resolve(modulePath.toString()): null, - endpoint: Datex.Runtime.endpoint.toString() + endpoint: Datex.Runtime.endpoint.toString(), + theme: console_theme }); let resolve: Function; diff --git a/utils/logger.ts b/utils/logger.ts index ce4cfd63..ebb2aec7 100644 --- a/utils/logger.ts +++ b/utils/logger.ts @@ -127,7 +127,7 @@ const COLOR = { POINTER: [ESCAPE_SEQUENCES.BLUE, ESCAPE_SEQUENCES.UNYT_POINTER] as COLOR, } as const; -export let console_theme:"dark"|"light" = (client_type=="deno" || (globalThis).matchMedia && (globalThis).matchMedia('(prefers-color-scheme: dark)')?.matches) ? "dark" : "light"; +export let console_theme:"dark"|"light" = (globalThis as any)._override_console_theme ?? ((client_type=="deno" || (globalThis).matchMedia && (globalThis).matchMedia('(prefers-color-scheme: dark)')?.matches) ? "dark" : "light"); try { (globalThis).matchMedia && (globalThis).matchMedia('(prefers-color-scheme: dark)')?.addEventListener("change", (e:any)=>{