Skip to content

Instantly share code, notes, and snippets.

View ghetolay's full-sized avatar

Ghet ghetolay

  • Imbasoft
  • /Earth/Europe/France/Paris
View GitHub Profile
@ghetolay
ghetolay / auditMap.ts
Created January 30, 2018 10:26
Audit Map operator for rxjs
// project signature should be (value: T, index: number) => ObservableInput<I>
// ObservableInput instead of Observable but I don't care about handling promise and array like.
export function auditMap<T, I>(
this: Observable<T>,
project: (value: T, index: number) => Observable<I>): Observable<I>;
export function auditMap<T, I, R>(
this: Observable<T>,
project: (value: T, index: number) => Observable<I>,
resultSelector: ((outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R)): Observable<I | R>
@ghetolay
ghetolay / utils.ts
Created January 30, 2018 10:04
Ensure Singleton Provider (AOT compatible)
export function singletonFactory<T>(token: InjectionToken<T>, factory: Function, deps: any[], fnToken = new InjectionToken('')) {
return [
{
provide: fnToken,
useValue: factory
},
{
provide: token,
deps: [[new SkipSelf(), new Optional(), token], fnToken, ...(deps ? deps : [])],
useFactory: function(parent: T | null, factoryFn: Function, ...args) {
@ghetolay
ghetolay / code.ts
Created December 3, 2017 23:35
Using DI for inputs
@Component({
selector: 'some-parent',
providers: [
{ provide: INPUT1, deps: [SomeSmartParent], useFactory: (cmp) => cmp.input1$ }
]
})
export class SomeSmartParent {
input1$ = this.store.select(...).map(...);
}
@Component({
@ghetolay
ghetolay / proposal.md
Last active December 1, 2017 23:18
flatMap proposal for v6

From that @dorus's comment and after talking a while with him about it, I think we can merge the implementation of all the flatten map operators: mergeMap, concatMap, switchMap, exhaustMap, debounce/audiMap (see #1777) into a single operator flexible enough to cover all scenarios. Then each of those operators would just be an alias of that main operator. Plus it may open new possibilities like the debounce/auditMap that doesn't exist yet.

I'll use flatMap and Queue because it's hard to find new meaningful names and it's not worth spending too much time on it at this stage.

Signature

flatMap<T>(project: (value: T, index: number) => ObservableInput<R>, queue: Queue): OperatorFunction<T, R>;
@ghetolay
ghetolay / index.ts
Created October 6, 2017 14:00
Ngrx reducer as map
const IncrementAction = ...;
const DecrementAction = ...;
const reducer1 = {
[]: () => 0,
[IncrementAction.type]: (state, action) => state + 1,
[DecrementAction.type]: (state, action) => state - 1
};
const reducers: { [type: string]: ReduceFn[] } = merge(reducer1, reducer2, ...);
@ghetolay
ghetolay / imbaTemplateOutlet.ts
Last active August 8, 2017 11:10
Custom template outlet to avoid view recreation on context changes
interface Context {
$implicit: any
}
@Directive({
selector: '[imbaTemplateOutlet]'
})
export class ImbaTemplateOutlet {
@Input()
@ghetolay
ghetolay / viewsync.ts
Last active July 8, 2017 15:38
Proposal for ViewSync
export class LoopContext<T> {
index: number;
count: number;
constructor() {}
get first(): boolean { return this.index === 0; }
get last(): boolean { return this.index === this.count - 1; }
@ghetolay
ghetolay / question.md
Last active April 16, 2017 22:09
Question about Angular Forms and reusable components

Problem

At the moment it's not really easy to create reusable part of a form as a component like an address component for example. You either have to create a fake input by implementing ControlValueAccessor or pass the form down to the component using @Input and use a form directive on the component (kinda creating a sub-form).

Expected behavior

It'll be nice if we could make that easier. Like we would just have to create a component with some form directives in it. It'll then register to the parent form like for direct child form directive.
This plunker is a working example except we had to use some tricks with providers in order to make it work that way.

Cause

All form directives, like FormControlName, FormGroupName, NgModel etc..., are injecting the ControlContainer that way :

@ghetolay
ghetolay / proposal.md
Last active April 16, 2017 21:47
@until() injection decorator proposal for Angular

Problem / Need

When you inject a token on a Component, the injection system will use a recursive pattern to inject the token an stops either once it has successfully found the token or once it has reach top injector. In some case we would like to create some kind of encapsulation and set a different boundary that the top injector.

Current solution

@Self() and @Host() were created in order to create that kind of encapsulation, this allows injection to stop at respectively the current component or the host component.

Problem

@Injectable()
export class NgrxStoreService {
ondestroy$: Observable<void> = new Subject<void>();
constructor(protected store: Store<State>, protected changeDetector: ChangeDetectorRef) { }
select<R>(mapFunc: (state: State) => R): Observable<R> {
return this.store.select(mapFunc).pipe(
takeUntil(this.ondestroy$),