about summary refs log tree commit diff
path: root/assets/src/components/icons/EyeClosedIcon.svelte
blob: 5da240544099b751b2d2f32779e00dd2300fcf0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<script lang="ts">
    export let color: "white" | "black";
</script>

<svg
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 24 24"
    width="16"
    height="16"
>
    <path fill="none" d="M0 0h24v24H0z" />
    <path
        d="M9.342 18.782l-1.931-.518.787-2.939a10.988 10.988 0 0 1-3.237-1.872l-2.153 2.154-1.415-1.415 2.154-2.153a10.957 10.957 0 0 1-2.371-5.07l1.968-.359C3.903 10.812 7.579 14 12 14c4.42 0 8.097-3.188 8.856-7.39l1.968.358a10.957 10.957 0 0 1-2.37 5.071l2.153 2.153-1.415 1.415-2.153-2.154a10.988 10.988 0 0 1-3.237 1.872l.787 2.94-1.931.517-.788-2.94a11.072 11.072 0 0 1-3.74 0l-.788 2.94z"
        fill={color}
    />
</svg>
e.Namespace */ .highlight .nx { color: #DDD } /* Name.Other */ .highlight .py { color: #DDD } /* Name.Property */ .highlight .nt { color: #DDD } /* Name.Tag */ .highlight .nv { color: #EEDD82 } /* Name.Variable */ .highlight .ow { color: #F00 } /* Operator.Word */ .highlight .pm { color: #DDD } /* Punctuation.Marker */ .highlight .w { color: #DDD } /* Text.Whitespace */ .highlight .mb { color: #F0F } /* Literal.Number.Bin */ .highlight .mf { color: #F0F } /* Literal.Number.Float */ .highlight .mh { color: #F0F } /* Literal.Number.Hex */ .highlight .mi { color: #F0F } /* Literal.Number.Integer */ .highlight .mo { color: #F0F } /* Literal.Number.Oct */ .highlight .sa { color: #87CEEB } /* Literal.String.Affix */ .highlight .sb { color: #87CEEB } /* Literal.String.Backtick */ .highlight .sc { color: #87CEEB } /* Literal.String.Char */ .highlight .dl { color: #87CEEB } /* Literal.String.Delimiter */ .highlight .sd { color: #87CEEB } /* Literal.String.Doc */ .highlight .s2 { color: #87CEEB } /* Literal.String.Double */ .highlight .se { color: #87CEEB } /* Literal.String.Escape */ .highlight .sh { color: #87CEEB } /* Literal.String.Heredoc */ .highlight .si { color: #87CEEB } /* Literal.String.Interpol */ .highlight .sx { color: #87CEEB } /* Literal.String.Other */ .highlight .sr { color: #87CEEB } /* Literal.String.Regex */ .highlight .s1 { color: #87CEEB } /* Literal.String.Single */ .highlight .ss { color: #87CEEB } /* Literal.String.Symbol */ .highlight .bp { color: #DDD } /* Name.Builtin.Pseudo */ .highlight .fm { color: #FF0 } /* Name.Function.Magic */ .highlight .vc { color: #EEDD82 } /* Name.Variable.Class */ .highlight .vg { color: #EEDD82 } /* Name.Variable.Global */ .highlight .vi { color: #EEDD82 } /* Name.Variable.Instance */ .highlight .vm { color: #EEDD82 } /* Name.Variable.Magic */ .highlight .il { color: #F0F } /* Literal.Number.Integer.Long */
package cancellablewebsocket

import (
	"context"
	"net/http"
	"time"

	"github.com/gorilla/websocket"
)

type CancellableWebSocket struct {
	conn      *websocket.Conn
	ctx       context.Context
	cancel    context.CancelFunc
	closeCode int // 0 means killed
	errors    chan error
}

func Dial(dialer *websocket.Dialer, ctx context.Context, url string, requestHeader http.Header) (*CancellableWebSocket, error) {
	conn, _, err := dialer.Dial(url, requestHeader)
	if err != nil {
		return nil, err
	}

	childCtx, cancel := context.WithCancel(ctx)

	cws := &CancellableWebSocket{
		conn:      conn,
		ctx:       childCtx,
		cancel:    cancel,
		closeCode: 0,
		errors:    make(chan error),
	}

	go cws.listenForCancel()

	return cws, nil
}

func (cws *CancellableWebSocket) ReadJSON(v interface{}) error {
	err := cws.conn.ReadJSON(v)
	if err != nil {
		// Check if context was not cancelled,
		// if so, return error
		if cws.ctx.Err() == nil {
			return err
		}
	}
	return nil
}

func (cws *CancellableWebSocket) WriteJSON(v interface{}) error {
	err := cws.conn.WriteJSON(v)
	if err != nil {
		if cws.ctx.Err() == nil {
			return err
		}
	}
	return nil
}

func (cws *CancellableWebSocket) Kill() error {
	cws.cancel()
	err, ok := <-cws.errors

	if ok && err != nil {
		return err
	}

	return nil
}

func (cws *CancellableWebSocket) Close(code int) error {
	cws.closeCode = code
	if err := cws.Kill(); err != nil {
		cws.closeCode = 0 // Reset close code
		return err
	}
	return nil
}

func (cws *CancellableWebSocket) OnClose(f func(code int, text string) error) {
	cws.conn.SetCloseHandler(f)
}

func (cws *CancellableWebSocket) listenForCancel() {
	<-cws.ctx.Done()

	var err error
	aLongTimeAgo := time.Unix(0, 0)

	if err = cws.conn.SetReadDeadline(aLongTimeAgo); err != nil {
		cws.errors <- err
	}

	if err = cws.conn.SetWriteDeadline(aLongTimeAgo); err != nil {
		cws.errors <- err
	}

	if cws.closeCode == 0 {
		// Close without sending close.
		if err = cws.conn.Close(); err != nil {
			cws.errors <- err
		}
	} else {
		// Send close with code.
		// NOTE: The SetWriteDeadline above does not affect this. :)
		err = cws.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(cws.closeCode, ""), time.Now().Add(time.Second))
		if err != nil {
			cws.errors <- err
		}
	}

	close(cws.errors)
}