Skip to content

Instantly share code, notes, and snippets.

@chaosmonster
chaosmonster / signal-http.service.ts
Created November 8, 2023 10:18
signals and http requests in Angular????
export class SignalHttpService {
constructor(private httpClient: HttpClient) {}
getSomeData() {
const result = signal(null);
this.httpClient.get(url).subscribe(data => result.set(data));
return result;
}
@chaosmonster
chaosmonster / dependency-injection.js
Created June 21, 2023 15:29
minimal dependency injection example in JavaScript
// minimal working dependency injection
// the dependency injection works with a property based injection
const clazzMap = Symbol('clazzMap');
globalThis[clazzMap] = new Map(); // <clazz, instance>
const clazzes = Symbol('clazzes')
globalThis[clazzes] = [];
function register(clazz) {
globalThis[clazzes].push(clazz);
@chaosmonster
chaosmonster / app.compontent.ts
Last active May 11, 2023 18:23
rough spreadsheet example with signals
import { Component, computed, Signal, signal, WritableSignal } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { NgFor } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [
NgFor,
// belongs to SpecialModule which is a generic lib, every App needs to provide their implementation of UserService.
// The lib uses this special implementation as UserService internally
abstract class UserService {
}
@Injectable()
class SpecialService {
constructer(private userService: UserService) {}
}
@customElement('my-element')
export class MyElement extends LitElement {
@property({
hasChanged(newVal: number, oldVal: number) {
const hasChanged: boolean = newVal !== oldVal;
console.log(`${newVal}, ${oldVal}, ${hasChanged}`);
return hasChanged;
},
})
<ng-template *ngIf="stream$ | async as result">
<div>{{result}} can be an empty string</div>
</ng-template>
// simple German normalize
function normalizeIt(str) {
const normalized = str.normalize('NFC')
.toLocaleUpperCase()
.trim()
.replace(/Ä/g, 'AE' )
.replace(/Ö/g, 'OE' )
.replace(/Ü/g, 'UE' )
.replace(/ß/g, 'SS' )
/// <reference lib="webworker" />
interface IMessageEvent<T> {
name: string;
payload: T;
}
function PostMessage(eventName: string) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
function booleanArrayToHex(arr) {
const binaryString = arr.map(it => it ? '1' : '0').join('');
return parseInt(binaryString, 2).toString(16).toUpperCase();
}
function hexToBooleanArray(hex, arrayLength = 16){
const binaryString = (parseInt(hex, 16).toString(2)).padStart(arrayLength, '0');
return binaryString.split('').map(it => it === '1');
}
@chaosmonster
chaosmonster / barcode-example.html
Created December 10, 2020 07:51
example for testing the BarcodeDetector API in Chromium based browsers
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>QR native test</title>
<style>
#scanner {
height: 400px;
border: 1px solid red;
}