Skip to content

Commit

Permalink
Added channel color
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Nappi committed Jul 16, 2024
1 parent 097ad90 commit 61d4b2d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@micro-lc/post-channel",
"version": "0.2.0-rc2",
"version": "0.2.0-rc3",
"type": "module",
"main": "dist/cjs/index.cjs",
"module": "dist/es/index.js",
Expand Down
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ const isPostChannelMessage = (message: MessageEvent): message is MessageEventWit
&& 'content' in data
}

const generateDarkColorHex = () => {
let color = '#'
for (let i = 0; i < 3; i++) { color += (`0${Math.floor(Math.random() * Math.pow(16, 2) / 2).toString(16)}`).slice(-2) }
return color
}

class PostChannel<S extends MessageToSend = MessageToSend> {
private static __generateId = () => {
if (window) {
Expand All @@ -80,6 +86,7 @@ class PostChannel<S extends MessageToSend = MessageToSend> {
private __write$: Observable<string>
private __synack$: Observable<SynAckMessage>
private __user$: Observable<UserMessage>
private __colors: {bg: string; fg: string}

private __connect(period: number) {
/**
Expand Down Expand Up @@ -202,6 +209,12 @@ class PostChannel<S extends MessageToSend = MessageToSend> {
this.__okToWrite = new ReplaySubject(1)
this.__write$ = this.__okToWrite.pipe(take(1))

const color = generateDarkColorHex()
this.__colors = {
bg: `${color}22`,
fg: color,
}

// period cannot be zero!
this.__connect(
opts?.period ? opts.period : 10
Expand Down Expand Up @@ -237,6 +250,10 @@ class PostChannel<S extends MessageToSend = MessageToSend> {
return this.__instance
}

get colors() {
return this.__colors
}

/**
* signals readiness on listening and cannot fail but keeps hanging
* if not resolved
Expand Down

0 comments on commit 61d4b2d

Please sign in to comment.