Skip to content

Instantly share code, notes, and snippets.

View gparlakov's full-sized avatar

Georgi Parlakov gparlakov

  • Sofia, Bulgaria
View GitHub Profile
@gparlakov
gparlakov / 1. The service.ts
Last active February 11, 2018 11:08
ngOnDestroy service ng-gotchas 1
@Injectable()
export class TimeService implements OnDestroy {
// ... some useful stuff
ngOnDestroy(): void {
// ... some clean up logic
}
}
@gparlakov
gparlakov / 1. The service.ts
Created February 11, 2018 11:13
2. Workaround
@Injectable()
export class TimeService {
// ... some useful stuff
onDestroy(): void {
// ... some clean up logic
}
}
@Injectable()
export class TimeService implements OnDestroy {
// ... some useful stuff
ngOnDestroy(): void {
// ... some clean up logic
}
}
h1: {
color: pink;
}
@gparlakov
gparlakov / ng-gotchas-module-injector.ts
Last active May 5, 2018 19:54
ng-gotchas-module-injector
var injector = {}; //created at app-bootstrap time
injector["UserProvider"] = new EagerUserService(); // at EagerModule evaluation time
injector["UserProvider"] = new UserService(); // at AppModule evaluation time
@gparlakov
gparlakov / ng-gotchas-module-lazy-injector.ts
Last active May 5, 2018 19:53
ng-gotchas-module-lazy-injector
var lazyInjector = {}; // provided at lazy-module start
lazyInjector.prototype = injector; // 'inherit' the root injector
// and then at LazyModule instantiation:
lazyInjector["UserProfile"] = new LazyUserService();
@gparlakov
gparlakov / auth.ts
Last active January 8, 2019 18:35
@Medium article FlushMicrotasks the Service and the Component
export class AuthService {
getUser(): Promise<User> {
// probably make a xhr but we'll just
return Promise.resolve(User.Empty);
}
}
@gparlakov
gparlakov / spec.ts
Created January 8, 2019 18:40
@Medium article flushMicrotasks updated spec.ts
it('should redirect to login if getUser returns the empty user', () => {
// given
// setup the empty user
auth.getUser.and.returnValue(Promise.resolve(User.Empty));
// when
comp.login();
flushMictrotasks(); // forse the promise microtask on the call stack and let it run
// then
expect(router.navigate).toHaveBeenCalledWith('/login');
});
const source = from([1, 2, 3]);
source.subscribe(x => console.log('observable', x), null, () => console.info("completed"));
// observable 1
// observable 2
// observable 3
// completed
source.toPromise().then(x => console.log('toPromise', x));
// toPromise 3
@gparlakov
gparlakov / launch.json
Last active February 19, 2019 12:56
Debug jest current test from vs code (current - where the cursor is at)
{
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Jest for current test file",
"program": "${workspaceFolder}\\node_modules\\jest\\bin\\jest",
"//comment": "--runInBand allows the 'debugger' and breakpoints to be hit",
"//comment1": "--verbosity false due to bug where console.log is not seen on the console",
"//comment2": "${fileBaseName} will give the filename that is being edited in VS Code",