Skip to content
Snippets Groups Projects
Commit ae9deee2 authored by Xiao Gui's avatar Xiao Gui
Browse files

bugfix: tests

parent 033a43bf
No related branches found
No related tags found
No related merge requests found
import { InjectionToken } from "@angular/core"
const PLUGIN_SRC_KEY = "x-plugin-portal-src" const PLUGIN_SRC_KEY = "x-plugin-portal-src"
export function setPluginSrc(src: string, record: Record<string, unknown> = {}){ export function setPluginSrc(src: string, record: Record<string, unknown> = {}){
...@@ -10,3 +12,5 @@ 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> = {}){ export function getPluginSrc(record: Record<string, string> = {}){
return record[PLUGIN_SRC_KEY] return record[PLUGIN_SRC_KEY]
} }
export const SET_PLUGIN_NAME = new InjectionToken('SET_PLUGIN_NAME')
...@@ -5,8 +5,7 @@ import { BoothVisitor, JRPCRequest, JRPCSuccessResp, ListenerChannel } from "src ...@@ -5,8 +5,7 @@ import { BoothVisitor, JRPCRequest, JRPCSuccessResp, ListenerChannel } from "src
import { ApiBoothEvents, ApiService, BroadCastingApiEvents, HeartbeatEvents, namespace } from "src/api/service"; import { ApiBoothEvents, ApiService, BroadCastingApiEvents, HeartbeatEvents, namespace } from "src/api/service";
import { getUuid } from "src/util/fn"; import { getUuid } from "src/util/fn";
import { WIDGET_PORTAL_TOKEN } from "src/widget/constants"; import { WIDGET_PORTAL_TOKEN } from "src/widget/constants";
import { getPluginSrc } from "../const"; import { getPluginSrc, SET_PLUGIN_NAME } from "../const";
import { PluginService } from "../service";
@Component({ @Component({
selector: 'sxplr-plugin-portal', selector: 'sxplr-plugin-portal',
...@@ -44,8 +43,8 @@ export class PluginPortal implements AfterViewInit, OnDestroy, ListenerChannel{ ...@@ -44,8 +43,8 @@ export class PluginPortal implements AfterViewInit, OnDestroy, ListenerChannel{
constructor( constructor(
private apiService: ApiService, private apiService: ApiService,
private pluginSvc: PluginService,
public vcr: ViewContainerRef, public vcr: ViewContainerRef,
@Optional() @Inject(SET_PLUGIN_NAME) private setPluginName: (inst: unknown, pluginName: string) => void,
@Optional() @Inject(WIDGET_PORTAL_TOKEN) portalData: Record<string, string> @Optional() @Inject(WIDGET_PORTAL_TOKEN) portalData: Record<string, string>
){ ){
if (portalData){ if (portalData){
...@@ -91,7 +90,7 @@ export class PluginPortal implements AfterViewInit, OnDestroy, ListenerChannel{ ...@@ -91,7 +90,7 @@ export class PluginPortal implements AfterViewInit, OnDestroy, ListenerChannel{
const data = event.data as JRPCSuccessResp<HeartbeatEvents['init']['response']> const data = event.data as JRPCSuccessResp<HeartbeatEvents['init']['response']>
this.srcName = data.result.name || 'Untitled Pluging' 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() while (this.handshakeSub.length > 0) this.handshakeSub.pop().unsubscribe()
......
...@@ -3,7 +3,7 @@ import { Injectable, Injector, NgZone } from "@angular/core"; ...@@ -3,7 +3,7 @@ import { Injectable, Injector, NgZone } from "@angular/core";
import { WIDGET_PORTAL_TOKEN } from "src/widget/constants"; import { WIDGET_PORTAL_TOKEN } from "src/widget/constants";
import { WidgetService } from "src/widget/service"; import { WidgetService } from "src/widget/service";
import { WidgetPortal } from "src/widget/widgetPortal/widgetPortal.component"; 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 { PluginPortal } from "./pluginPortal/pluginPortal.component";
import { environment } from "src/environments/environment" import { environment } from "src/environments/environment"
...@@ -33,6 +33,9 @@ export class PluginService { ...@@ -33,6 +33,9 @@ export class PluginService {
providers: [{ providers: [{
provide: WIDGET_PORTAL_TOKEN, provide: WIDGET_PORTAL_TOKEN,
useValue: setPluginSrc(htmlSrc, {}) useValue: setPluginSrc(htmlSrc, {})
}, {
provide: SET_PLUGIN_NAME,
useValue: (inst: PluginPortal, pluginName: string) => this.setPluginName(inst, pluginName)
}], }],
parent: this.injector parent: this.injector
}) })
......
...@@ -21,3 +21,5 @@ interface TypeActionWidgetReturnVal<T>{ ...@@ -21,3 +21,5 @@ interface TypeActionWidgetReturnVal<T>{
export type TypeActionToWidget<T> = (type: EnumActionToWidget, obj: T, option: IActionWidgetOption) => 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 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
import { ComponentPortal } from "@angular/cdk/portal"; import { ComponentPortal } from "@angular/cdk/portal";
import { ComponentFactory, ComponentFactoryResolver, ComponentRef, Injectable, Injector, ViewContainerRef } from "@angular/core"; import { ComponentFactory, ComponentFactoryResolver, ComponentRef, Injectable, Injector, ViewContainerRef } from "@angular/core";
import { RM_WIDGET } from "./constants";
import { WidgetPortal } from "./widgetPortal/widgetPortal.component"; import { WidgetPortal } from "./widgetPortal/widgetPortal.component";
@Injectable({ @Injectable({
...@@ -19,7 +20,14 @@ export class WidgetService { ...@@ -19,7 +20,14 @@ export class WidgetService {
public addNewWidget<T>(Component: new (...arg: any) => T, injector: Injector): WidgetPortal<T> { 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 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) this.viewRefMap.set(widgetPortal.instance, widgetPortal)
......
import { ComponentPortal } from "@angular/cdk/portal"; import { ComponentPortal } from "@angular/cdk/portal";
import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from "@angular/core"; import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Inject, Optional } from "@angular/core";
import { WidgetService } from "../service"; import { RM_WIDGET } from "../constants";
@Component({ @Component({
selector: 'sxplr-widget-portal', selector: 'sxplr-widget-portal',
...@@ -30,12 +30,12 @@ export class WidgetPortal<T>{ ...@@ -30,12 +30,12 @@ export class WidgetPortal<T>{
} }
constructor( constructor(
private wSvc: WidgetService,
private cdr: ChangeDetectorRef, private cdr: ChangeDetectorRef,
@Optional() @Inject(RM_WIDGET) private rmWidget: (inst: unknown) => void
){ ){
} }
exit(){ exit(){
this.wSvc.rmWidget(this) if (this.rmWidget) this.rmWidget(this)
} }
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment