From ae9deee25ff3000a161f4178eb39b00d8d7d6481 Mon Sep 17 00:00:00 2001
From: Xiao Gui <xgui3783@gmail.com>
Date: Tue, 28 Jun 2022 18:55:17 +0200
Subject: [PATCH] bugfix: tests

---
 src/plugin/const.ts                               |  4 ++++
 src/plugin/pluginPortal/pluginPortal.component.ts |  7 +++----
 src/plugin/service.ts                             |  5 ++++-
 src/widget/constants.ts                           |  2 ++
 src/widget/service.ts                             | 10 +++++++++-
 src/widget/widgetPortal/widgetPortal.component.ts |  8 ++++----
 6 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/src/plugin/const.ts b/src/plugin/const.ts
index 68104badf..474bc36bf 100644
--- a/src/plugin/const.ts
+++ b/src/plugin/const.ts
@@ -1,3 +1,5 @@
+import { InjectionToken } from "@angular/core"
+
 const PLUGIN_SRC_KEY = "x-plugin-portal-src"
 
 export function setPluginSrc(src: string, record: Record<string, unknown> = {}){
@@ -10,3 +12,5 @@ export function setPluginSrc(src: string, record: Record<string, unknown> = {}){
 export function getPluginSrc(record: Record<string, string> = {}){
   return record[PLUGIN_SRC_KEY]
 }
+
+export const SET_PLUGIN_NAME = new InjectionToken('SET_PLUGIN_NAME')
diff --git a/src/plugin/pluginPortal/pluginPortal.component.ts b/src/plugin/pluginPortal/pluginPortal.component.ts
index 7d5b4d421..58bd58993 100644
--- a/src/plugin/pluginPortal/pluginPortal.component.ts
+++ b/src/plugin/pluginPortal/pluginPortal.component.ts
@@ -5,8 +5,7 @@ import { BoothVisitor, JRPCRequest, JRPCSuccessResp, ListenerChannel } from "src
 import { ApiBoothEvents, ApiService, BroadCastingApiEvents, HeartbeatEvents, namespace } from "src/api/service";
 import { getUuid } from "src/util/fn";
 import { WIDGET_PORTAL_TOKEN } from "src/widget/constants";
-import { getPluginSrc } from "../const";
-import { PluginService } from "../service";
+import { getPluginSrc, SET_PLUGIN_NAME } from "../const";
 
 @Component({
   selector: 'sxplr-plugin-portal',
@@ -44,8 +43,8 @@ export class PluginPortal implements AfterViewInit, OnDestroy, ListenerChannel{
 
   constructor(
     private apiService: ApiService,
-    private pluginSvc: PluginService,
     public vcr: ViewContainerRef,
+    @Optional() @Inject(SET_PLUGIN_NAME) private setPluginName: (inst: unknown, pluginName: string) => void,
     @Optional() @Inject(WIDGET_PORTAL_TOKEN) portalData: Record<string, string>
   ){
     if (portalData){
@@ -91,7 +90,7 @@ export class PluginPortal implements AfterViewInit, OnDestroy, ListenerChannel{
             const data = event.data as JRPCSuccessResp<HeartbeatEvents['init']['response']>
 
             this.srcName = data.result.name || 'Untitled Pluging'
-            this.pluginSvc.setPluginName(this, this.srcName)
+            this.setPluginName(this, this.srcName)
             
             while (this.handshakeSub.length > 0) this.handshakeSub.pop().unsubscribe()
 
diff --git a/src/plugin/service.ts b/src/plugin/service.ts
index 4f5a5e74f..19f183189 100644
--- a/src/plugin/service.ts
+++ b/src/plugin/service.ts
@@ -3,7 +3,7 @@ import { Injectable, Injector, NgZone } from "@angular/core";
 import { WIDGET_PORTAL_TOKEN } from "src/widget/constants";
 import { WidgetService } from "src/widget/service";
 import { WidgetPortal } from "src/widget/widgetPortal/widgetPortal.component";
-import { setPluginSrc } from "./const";
+import { setPluginSrc, SET_PLUGIN_NAME } from "./const";
 import { PluginPortal } from "./pluginPortal/pluginPortal.component";
 import { environment } from "src/environments/environment"
 
@@ -33,6 +33,9 @@ export class PluginService {
       providers: [{
         provide: WIDGET_PORTAL_TOKEN,
         useValue: setPluginSrc(htmlSrc, {})
+      }, {
+        provide: SET_PLUGIN_NAME,
+        useValue: (inst: PluginPortal, pluginName: string) => this.setPluginName(inst, pluginName)
       }],
       parent: this.injector
     })
diff --git a/src/widget/constants.ts b/src/widget/constants.ts
index 412bccd40..a779b079a 100644
--- a/src/widget/constants.ts
+++ b/src/widget/constants.ts
@@ -21,3 +21,5 @@ interface TypeActionWidgetReturnVal<T>{
 export type TypeActionToWidget<T> = (type: EnumActionToWidget, obj: T, option: IActionWidgetOption) => TypeActionWidgetReturnVal<T>
 
 export const WIDGET_PORTAL_TOKEN = new InjectionToken<Record<string, unknown>>("WIDGET_PORTAL_TOKEN")
+
+export const RM_WIDGET = new InjectionToken('RM_WIDGET')
\ No newline at end of file
diff --git a/src/widget/service.ts b/src/widget/service.ts
index 75be50886..b20ee1f27 100644
--- a/src/widget/service.ts
+++ b/src/widget/service.ts
@@ -1,5 +1,6 @@
 import { ComponentPortal } from "@angular/cdk/portal";
 import { ComponentFactory, ComponentFactoryResolver, ComponentRef, Injectable, Injector, ViewContainerRef } from "@angular/core";
+import { RM_WIDGET } from "./constants";
 import { WidgetPortal } from "./widgetPortal/widgetPortal.component";
 
 @Injectable({
@@ -19,7 +20,14 @@ export class WidgetService {
 
   public addNewWidget<T>(Component: new (...arg: any) => T, injector: Injector): WidgetPortal<T> {
     const widgetPortal = this.vcr.createComponent(this.cf, 0, injector) as ComponentRef<WidgetPortal<T>>
-    const cmpPortal = new ComponentPortal<T>(Component, this.vcr, injector)
+    const inj = Injector.create({
+      providers: [{
+        provide: RM_WIDGET,
+        useValue: (cmp: WidgetPortal<T>) => this.rmWidget(cmp)
+      }],
+      parent: injector
+    })
+    const cmpPortal = new ComponentPortal<T>(Component, this.vcr, inj)
     
     this.viewRefMap.set(widgetPortal.instance, widgetPortal)
 
diff --git a/src/widget/widgetPortal/widgetPortal.component.ts b/src/widget/widgetPortal/widgetPortal.component.ts
index a1351e15e..5e5cc05fa 100644
--- a/src/widget/widgetPortal/widgetPortal.component.ts
+++ b/src/widget/widgetPortal/widgetPortal.component.ts
@@ -1,6 +1,6 @@
 import { ComponentPortal } from "@angular/cdk/portal";
-import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from "@angular/core";
-import { WidgetService } from "../service";
+import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Inject, Optional } from "@angular/core";
+import { RM_WIDGET } from "../constants";
 
 @Component({
   selector: 'sxplr-widget-portal',
@@ -30,12 +30,12 @@ export class WidgetPortal<T>{
   }
 
   constructor(
-    private wSvc: WidgetService,
     private cdr: ChangeDetectorRef,
+    @Optional() @Inject(RM_WIDGET) private rmWidget: (inst: unknown) => void
   ){
     
   }
   exit(){
-    this.wSvc.rmWidget(this)
+    if (this.rmWidget) this.rmWidget(this)
   }
 }
-- 
GitLab