From 975109b75cb4d44ec164c7d484d521782e4b0741 Mon Sep 17 00:00:00 2001 From: Janne Savolainen Date: Thu, 19 May 2022 08:01:17 +0300 Subject: [PATCH] Introduce injection token for channels to allow injecting all of them at once Co-authored-by: Mikko Aspiala Signed-off-by: Janne Savolainen --- .../channel/{channel.ts => channel-injection-token.ts} | 6 ++++++ src/common/channel/channel-listener-injection-token.ts | 6 +++--- src/common/channel/channel.test.ts | 2 +- .../channel/enlist-channel-listener-injection-token.ts | 2 +- .../channel/send-to-agnostic-channel-injection-token.ts | 2 +- .../sync-box/sync-box-channel-listener.injectable.ts | 7 +++---- src/common/sync-box/sync-box-channel.injectable.ts | 5 ++++- 7 files changed, 19 insertions(+), 11 deletions(-) rename src/common/channel/{channel.ts => channel-injection-token.ts} (59%) diff --git a/src/common/channel/channel.ts b/src/common/channel/channel-injection-token.ts similarity index 59% rename from src/common/channel/channel.ts rename to src/common/channel/channel-injection-token.ts index 53b6768c7e..eb5ec40167 100644 --- a/src/common/channel/channel.ts +++ b/src/common/channel/channel-injection-token.ts @@ -3,7 +3,13 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ +import { getInjectionToken } from "@ogre-tools/injectable"; + export interface Channel { id: string; _template?: TInstance; } + +export const channelInjectionToken = getInjectionToken>({ + id: "channel", +}); diff --git a/src/common/channel/channel-listener-injection-token.ts b/src/common/channel/channel-listener-injection-token.ts index f2725f6c59..29307be1db 100644 --- a/src/common/channel/channel-listener-injection-token.ts +++ b/src/common/channel/channel-listener-injection-token.ts @@ -3,14 +3,14 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectionToken } from "@ogre-tools/injectable"; -import type { Channel } from "./channel"; +import type { Channel } from "./channel-injection-token"; -export interface ChannelListener> { +export interface ChannelListener> { channel: TChannel; handler: (value: TChannel["_template"]) => void; } -export const channelListenerInjectionToken = getInjectionToken>>( +export const channelListenerInjectionToken = getInjectionToken>>( { id: "channel-listener", }, diff --git a/src/common/channel/channel.test.ts b/src/common/channel/channel.test.ts index 608e0cf73e..fc40c70347 100644 --- a/src/common/channel/channel.test.ts +++ b/src/common/channel/channel.test.ts @@ -11,7 +11,7 @@ import { sendToAgnosticChannelInjectionToken } from "./send-to-agnostic-channel- import { getApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder"; import { channelListenerInjectionToken } from "./channel-listener-injection-token"; import createLensWindowInjectable from "../../main/start-main-application/lens-window/application-window/create-lens-window.injectable"; -import type { Channel } from "./channel"; +import type { Channel } from "./channel-injection-token"; type TestChannel = Channel; diff --git a/src/common/channel/enlist-channel-listener-injection-token.ts b/src/common/channel/enlist-channel-listener-injection-token.ts index 113f32069a..6b72ca4ac1 100644 --- a/src/common/channel/enlist-channel-listener-injection-token.ts +++ b/src/common/channel/enlist-channel-listener-injection-token.ts @@ -3,7 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectionToken } from "@ogre-tools/injectable"; -import type { Channel } from "./channel"; +import type { Channel } from "./channel-injection-token"; export type EnlistChannelListener = >( channel: TChannel, diff --git a/src/common/channel/send-to-agnostic-channel-injection-token.ts b/src/common/channel/send-to-agnostic-channel-injection-token.ts index fd96051f42..901e51b94b 100644 --- a/src/common/channel/send-to-agnostic-channel-injection-token.ts +++ b/src/common/channel/send-to-agnostic-channel-injection-token.ts @@ -3,7 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectionToken } from "@ogre-tools/injectable"; -import type { Channel } from "./channel"; +import type { Channel } from "./channel-injection-token"; export type SendToAgnosticChannel = >( channel: TChannel, diff --git a/src/common/sync-box/sync-box-channel-listener.injectable.ts b/src/common/sync-box/sync-box-channel-listener.injectable.ts index 8019f93046..9310718185 100644 --- a/src/common/sync-box/sync-box-channel-listener.injectable.ts +++ b/src/common/sync-box/sync-box-channel-listener.injectable.ts @@ -3,20 +3,19 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import type { SyncBoxChannel } from "./sync-box-channel.injectable"; import syncBoxChannelInjectable from "./sync-box-channel.injectable"; -import type { ChannelListener } from "../channel/channel-listener-injection-token"; import { channelListenerInjectionToken } from "../channel/channel-listener-injection-token"; import syncBoxStateInjectable from "./sync-box-state.injectable"; const syncBoxChannelListenerInjectable = getInjectable({ id: "sync-box-channel-listener", - instantiate: (di): ChannelListener => { + instantiate: (di) => { const getSyncBoxState = (id: string) => di.inject(syncBoxStateInjectable, id); + const channel = di.inject(syncBoxChannelInjectable); return { - channel: di.inject(syncBoxChannelInjectable), + channel, handler: ({ id, value }) => { const target = getSyncBoxState(id); diff --git a/src/common/sync-box/sync-box-channel.injectable.ts b/src/common/sync-box/sync-box-channel.injectable.ts index d5b5101d7c..7b9262f607 100644 --- a/src/common/sync-box/sync-box-channel.injectable.ts +++ b/src/common/sync-box/sync-box-channel.injectable.ts @@ -3,7 +3,8 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import type { Channel } from "../channel/channel"; +import type { Channel } from "../channel/channel-injection-token"; +import { channelInjectionToken } from "../channel/channel-injection-token"; export type SyncBoxChannel = Channel<{ id: string; value: unknown }>; @@ -13,6 +14,8 @@ const syncBoxChannelInjectable = getInjectable({ instantiate: (): SyncBoxChannel => ({ id: "sync-box-channel", }), + + injectionToken: channelInjectionToken, }); export default syncBoxChannelInjectable;