Skip to content

Commit

Permalink
✨ feat(angular): provide events as streams (#1024)
Browse files Browse the repository at this point in the history
feat(angular): provide events as streams
  • Loading branch information
hirsch88 committed Aug 17, 2023
1 parent 0af9729 commit 8c9af30
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-dogs-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@baloise/design-system-output-target-angular': minor
---

provide component events as a stream
32 changes: 22 additions & 10 deletions packages/output-targets/angular/angular-component-lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { EventEmitter } from '@angular/core'
/* eslint-disable */
/* tslint:disable */
import { fromEvent } from 'rxjs'

export const proxyInputs = (Cmp: any, inputs: string[]) => {
const Prototype = Cmp.prototype
Expand Down Expand Up @@ -26,20 +28,30 @@ export const proxyMethods = (Cmp: any, methods: string[]) => {
})
}

export const proxyOutputs = (instance: any, _el: HTMLElement, events: string[]) => {
events.forEach(eventName => {
instance[eventName] = new EventEmitter()
})
export const proxyOutputs = (instance: any, el: any, events: string[]) => {
events.forEach(eventName => (instance[eventName] = fromEvent(el, eventName)))
}

export const defineCustomElement = (tagName: string, customElement: any) => {
if (customElement !== undefined && typeof customElements !== 'undefined' && !customElements.get(tagName)) {
customElements.define(tagName, customElement)
}
}

// tslint:disable-next-line: only-arrow-functions
export function ProxyCmp(opts: { inputs?: any; methods?: any }) {
export function ProxyCmp(opts: { defineCustomElementFn?: () => void; inputs?: any; methods?: any }) {
const decorator = function (cls: any) {
if (opts.inputs) {
proxyInputs(cls, opts.inputs)
const { defineCustomElementFn, inputs, methods } = opts

if (defineCustomElementFn !== undefined) {
defineCustomElementFn()
}

if (inputs) {
proxyInputs(cls, inputs)
}
if (opts.methods) {
proxyMethods(cls, opts.methods)
if (methods) {
proxyMethods(cls, methods)
}
return cls
}
Expand Down

0 comments on commit 8c9af30

Please sign in to comment.