Skip to content

Instantly share code, notes, and snippets.

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

LironH LironHazan

🎸
666 -> 🦀
View GitHub Profile
@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 / 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) {
class FunWithFunctionalTS {
queryEditorState: Readonly<QueryEditorState>;
static updateEditorState(
editorState: Readonly<QueryEditorState>,
sliceToUpdate: Partial<Readonly<QueryEditorState>>
): Readonly<QueryEditorState> {
return { ...editorState, ...sliceToUpdate };
}
@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) || [];
@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 / 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
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 / oz_gonen_store.ts
Created October 8, 2020 10:08
rxjs_snippets_for_blog_post
import {BehaviorSubject, Observable} from 'rxjs';
export class StoreService<T> {
private readonly state$: BehaviorSubject<T>;
protected get state(): T {
return this.state$.getValue();
}
constructor(initialState: T) {
this.state$ = new BehaviorSubject<T>(initialState);
}
protected getState(): Observable<T> {
@LironHazan
LironHazan / close-that-stream-if-no-need.ts
Created October 10, 2020 06:14
Irena's stop fetching recipe - for rxjs blog post
private unsubscribeDataFetch: Subject<void> = new Subject<void>();
// polling data till we get what we need and stop
fetchData() {
this.dataService
.pipe(takeUntil(this.unsubscribeDataFetch))
.subscribe((data) => actOnData(data));
}
actOnData(data) {
@LironHazan
LironHazan / registerTransitionEnd.ts
Last active January 26, 2022 07:50
Amir Baraks's registerTransitionEnd used in an Angular directive for rxjs blog post
registerTransitionEnd<T extends {propertyName}>(el: FromEventTarget<T>, propName: string) {
// Returns an Observable from DOM event
fromEvent(el, 'transitionend')
.pipe(
filter(({ propertyName }: T) => propertyName === propName) // Will only emit value for the wanted css prop name e.g. height
)
.subscribe((_) => {
this.transitionEnd.emit();
});
}