Skip to content

Instantly share code, notes, and snippets.

View LironHazan's full-sized avatar
🎸
666 -> 🦀

LironH LironHazan

🎸
666 -> 🦀
View GitHub Profile
function renderImage(ctx, url, width, height): Promise<boolean> {
return new Promise((resolve) => {
fetch(url)
.then((res) => res.blob())
.then((blob) => createImageBitmap(blob))
.then((ibm) => {
ctx.drawImage(ibm, 0, 0);
resolve();
});
});
@LironHazan
LironHazan / bootstrap.ts
Created April 3, 2020 07:27
for blog post
import {Injector, Resolver} from "../src/diy/injector";
import {RendererService} from "./services/renderer.service";
class DiBootstrap {
static run() {
console.log('---------------- | START | -----------------');
// Static injector
@LironHazan
LironHazan / injectable.ts
Created April 3, 2020 06:33
For my blog post
import {Ctr} from "../../../common/types";
type ClazzDecorator<T> = (target: T) => void;
export const Injectable = () : ClazzDecorator<Ctr<any>> => {
return (target: Ctr<any>) => {
// this is needed so the design:paramtypes could be collected
console.log('inside: Injectable decorator');
console.log(target.name, ' is used');
};
@LironHazan
LironHazan / injector.ts
Last active April 3, 2020 08:05
For my blog post about DI
import 'reflect-metadata';
import {Ctr} from "../../../common/types";
export class Injector {
private depInstances: Map<string, Ctr<any>> = new Map<string, Ctr<any>>();
// Not storing an instances map
static resolve<T>(target: Ctr<any>): T {
const tokens = Reflect.getMetadata('design:paramtypes', target) || [];
class FunWithFunctionalTS {
queryEditorState: Readonly<QueryEditorState>;
static updateEditorState(
editorState: Readonly<QueryEditorState>,
sliceToUpdate: Partial<Readonly<QueryEditorState>>
): Readonly<QueryEditorState> {
return { ...editorState, ...sliceToUpdate };
}
@LironHazan
LironHazan / immutable-typescript-example.ts
Last active March 24, 2020 08:22
Get new ref of your entity to achieve immutability (reduces bugs) and pass entity by ref when using onPush when working in Angular e.g.
class updateEditorExample {
private queryEditorState: Readonly<QueryEditorState>;
static updateEditorState (editorState: Readonly<QueryEditorState>, sliceToUpdate: Partial<Readonly<QueryEditorState>>) {
return { ...editorState, ...sliceToUpdate };
}
// Get new state:
doSomthingGood(newText: string) {
@LironHazan
LironHazan / lib.rs
Created March 11, 2020 06:08
gist for a blog pos
#![allow(unused_variables)]
fn main() {
use wasm_bindgen::prelude::*;
// Called by our JS entry point to run the example
#[wasm_bindgen(start)]
pub fn rust_in_peace() -> Result<(), JsValue> {
// Use `web_sys`'s global `window` function to get a handle on the global
// window object.
let window = web_sys::window().expect("no global `window` exists");
@LironHazan
LironHazan / gist:a14a801bb55ca069e982d3b645347126
Created October 11, 2019 19:59
checked state - show hide legend by css only
input[type=checkbox] {
display: none;
}
.toggle-legend {
cursor: pointer;
}
input[type=checkbox]:checked ~ .groups-legend {
display: flex;
}
input:not(:checked) ~ .groups-legend {
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
@Component({
selector: 'toggle-btn',
template: `
<label class='so-slide-toggle'>
<input (change)="onChange($event)" type="checkbox"/>
<span class="back">
<span class="toggle"></span>
<span class="label on" [title]="enabledText">{{ enabledText }}</span>
@LironHazan
LironHazan / silly-btn.ts
Created October 5, 2019 18:11
silly-btn.ts
import {Component, Input, OnInit, ViewEncapsulation} from '@angular/core';
import {SillyButtonService} from './silly-button.service';
@Component({
// tslint:disable-next-line:component-selector
selector: 'ng-silly-button',
template:
`<div class="silly-btn" (click)="onClick($event)">
<div class="content">
<ng-content></ng-content>