Skip to content

Instantly share code, notes, and snippets.

View mohanramphp's full-sized avatar

Mohan Ram mohanramphp

View GitHub Profile
@mohanramphp
mohanramphp / fibonacci-generator.js
Created July 20, 2018 08:17 — forked from jfairbank/fibonacci-generator.js
Fibonacci ES6 Generator
function *fibonacci(n) {
const infinite = !n && n !== 0;
let current = 0;
let next = 1;
while (infinite || n--) {
yield current;
[current, next] = [next, current + next];
}
}
@mohanramphp
mohanramphp / session.store.ts
Last active September 21, 2018 04:17
Session store declaration
// session.store.ts
import { ID, Store, StoreConfig } from '@datorama/akita';
export interface User {
firstName: string;
lastName: string;
token: string;
}
export interface SessionState {
@mohanramphp
mohanramphp / session.query.ts
Last active September 21, 2018 03:19
Session query declaration
// session.query.ts
import { Query, toBoolean } from '@datorama/akita';
import { filter, map } from 'rxjs/operators';
import { SessionStore, SessionState } from './session.store';
export class SessionQuery extends Query<SessionState> {
isLoggedIn$ = this.select(({ user }) => toBoolean(user));
loggedInUser$ = this.select().pipe(
// student.model.ts
import { ID, guid } from '@datorama/akita';
export interface Student {
id: ID;
name: string;
sex: 'Male' | 'Female';
standard: number;
quarterlyScore: number;
halfyearlyScore: number;
@mohanramphp
mohanramphp / student.store.ts
Last active September 21, 2018 04:19
Student store declaration
// student.store.ts
import { EntityState, EntityStore, StoreConfig } from '@datorama/akita';
import { Student } from './student.model';
export interface StudentState extends EntityState<Student> { }
@StoreConfig({
name: 'students'
})
export class StudentStore extends EntityStore<StudentState, Student> {
@mohanramphp
mohanramphp / student.query.ts
Last active September 21, 2018 05:29
Student query declaration
// student.query.ts
import { QueryEntity } from '@datorama/akita';
export class StudentQuery extends QueryEntity<StudentState, Student> {
studentsGraphData$ = this.selectAll().pipe(
map(this.getStudentGraphData.bind(this))
);
constructor(protected store: StudentStore) {
@mohanramphp
mohanramphp / student-data.service.ts
Last active September 19, 2018 05:23
Student data service declaration
// student-data.service.ts
import { timer, Observable } from 'rxjs';
import { mapTo } from 'rxjs/operators';
import { Student } from './student.model';
import { guid } from '@datorama/akita';
const students: Array<Student> = [
{
id: guid(),
name: 'Mohan Ram',
@mohanramphp
mohanramphp / student.service.ts
Last active September 21, 2018 04:21
Student service declaration which encapsulates the functionalities of Student store and data service.
// student.service.ts
import { noop, ID } from '@datorama/akita';
// importing respective dependencies here
// ...
export class StudentService {
constructor(
private studentDataService: StudentDataService,
private studentStore: StudentStore,
@mohanramphp
mohanramphp / app.module.ts
Last active September 18, 2018 02:59
App component file where akitaDevtools are enabled
// app.module.ts
import { AkitaNgDevtools } from '@datorama/akita-ngdevtools';
import { environment } from '../environments/environment';
@NgModule({
imports: [
environment.production ? [] : AkitaNgDevtools.forRoot()
],
declarations: [AppComponent],
bootstrap: [AppComponent]
@mohanramphp
mohanramphp / types.d.ts
Last active September 16, 2018 11:01
Entity State Interface
/**
* Below codes are from the type declaration of Akita
*/
// An interface to represent objects
export interface HashMap<T> {
[id: string]: T;
}
/**