-
Notifications
You must be signed in to change notification settings - Fork 400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(store): add tree shaking states into integration example #631
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,7 @@ button { | |
margin: 0 auto; | ||
border-top: solid 1px #ddd; | ||
} | ||
|
||
.menu a { | ||
margin-right: 10px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { Routes } from '@angular/router'; | ||
import { MenuComponent } from './menu.component'; | ||
|
||
export const routes: Routes = [ | ||
{ path: '', pathMatch: 'full', redirectTo: '/list' }, | ||
{ path: '', component: MenuComponent }, | ||
{ path: 'list', loadChildren: './list/list.module#ListModule' }, | ||
{ path: 'detail', loadChildren: './detail/detail.module#DetailModule' } | ||
]; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export class DetailFooActions { | ||
public static readonly type: string = '[DetailActions] description'; | ||
constructor(public foo: boolean) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
import { Component } from '@angular/core'; | ||
import { Select, Store } from '@ngxs/store'; | ||
import { DetailFooActions } from './detail.actions'; | ||
import { DetailState, DetailStateModel } from './detail.state'; | ||
import { Observable } from 'rxjs'; | ||
|
||
@Component({ | ||
selector: 'app-detail', | ||
template: ` | ||
<a [routerLink]="['/list']">List</a> | ||
<a [routerLink]="['/list']">List</a> {{ detail$ | async | json }} | ||
<br><br><button (click)="updateFoo()">Update foo</button> | ||
` | ||
}) | ||
export class DetailComponent {} | ||
export class DetailComponent { | ||
@Select(DetailState) | ||
detail$: Observable<DetailStateModel>; | ||
|
||
constructor(private store: Store) {} | ||
|
||
public updateFoo() { | ||
this.store.dispatch(new DetailFooActions(false)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
import { State } from '@ngxs/store'; | ||
import { Action, State, StateContext } from '@ngxs/store'; | ||
import { DetailModule } from './detail.module'; | ||
import { DetailFooActions } from './detail.actions'; | ||
|
||
@State({ | ||
export interface DetailStateModel { | ||
foo: boolean; | ||
} | ||
|
||
@State<DetailStateModel>({ | ||
name: 'detail', | ||
defaults: { foo: true } | ||
defaults: { foo: true }, | ||
providedIn: DetailModule | ||
}) | ||
export class DetailState {} | ||
export class DetailState { | ||
@Action(DetailFooActions) | ||
changeFoo(ctx: StateContext<DetailStateModel>, { foo }: DetailFooActions) { | ||
ctx.setState({ foo }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-menu', | ||
template: ` | ||
<p class="menu"> | ||
<b>Menu</b>: | ||
<a [routerLink]="['/list']">List</a> | ||
<a [routerLink]="['/detail']">Detail</a> | ||
</p> | ||
` | ||
}) | ||
export class MenuComponent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Action, Selector, State, StateContext } from '@ngxs/store'; | ||
|
||
export class AddTodo { | ||
static type = 'AddTodo'; | ||
|
||
constructor(public readonly payload: string) {} | ||
} | ||
|
||
export class RemoveTodo { | ||
static type = 'RemoveTodo'; | ||
|
||
constructor(public readonly payload: number) {} | ||
} | ||
|
||
@State<string[]>({ | ||
name: 'todo', | ||
defaults: [] | ||
}) | ||
export class TodoState { | ||
@Selector() | ||
static pandas(state: string[]) { | ||
return state.filter(s => s.indexOf('panda') > -1); | ||
} | ||
|
||
@Action(AddTodo) | ||
addTodo({ getState, setState }: StateContext<string[]>, { payload }: AddTodo) { | ||
setState([...getState(), payload]); | ||
} | ||
|
||
@Action(RemoveTodo) | ||
removeTodo({ getState, setState }: StateContext<string[]>, { payload }: RemoveTodo) { | ||
setState(getState().filter((_, i) => i !== payload)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/store/src/internal/state-providers/state-providers.interfaces.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Type } from '@angular/core'; | ||
|
||
export type NgxsStateType = Type<unknown>; | ||
|
||
export enum NgxsProvidedIn { | ||
root = 'ngxsRoot', | ||
feature = 'ngxsFeature' | ||
} | ||
|
||
export interface NgxsProvides { | ||
ngxsRoot: NgxsStateType[]; | ||
ngxsFeature: NgxsStateType[]; | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/store/src/internal/state-providers/state-providers.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Injectable, NgModule, Type } from '@angular/core'; | ||
import { NgxsProvidedIn, NgxsProvides, NgxsStateType } from './state-providers.interfaces'; | ||
|
||
@NgModule() | ||
export class NgxsStateProvidersModule { | ||
public static states: NgxsProvides = { | ||
ngxsRoot: [], | ||
ngxsFeature: [] | ||
}; | ||
|
||
public static provideInNgxsModule(target: NgxsStateType, type: Type<unknown> = null): void { | ||
const { ngxsRoot, ngxsFeature } = NgxsStateProvidersModule.states; | ||
const firstInitialize = ![...ngxsRoot, ...ngxsFeature].includes(target); | ||
|
||
if (firstInitialize) { | ||
Injectable({ providedIn: type || NgxsStateProvidersModule })(target); | ||
} | ||
} | ||
|
||
public static defineStatesByProvideIn(provideIn: string | Type<unknown>, states: NgxsStateType[]): void { | ||
const stateSourceKey = typeof provideIn === 'string' ? provideIn : NgxsProvidedIn.feature; | ||
const sources = NgxsStateProvidersModule.states[stateSourceKey] || []; | ||
sources.push(...states); | ||
} | ||
|
||
public static filterWithoutDuplicate(states: NgxsStateType[], sources: NgxsStateType[] = []): NgxsStateType[] { | ||
return states.filter((value, index) => { | ||
const nonDuplicateSource = states.indexOf(value) === index; | ||
const nonDuplicateTarget = !sources.includes(value); | ||
return value && nonDuplicateSource && nonDuplicateTarget; | ||
}); | ||
} | ||
|
||
public static flattenedUniqueStates(entry: NgxsStateType[], compare: NgxsStateType[]) { | ||
return this.filterWithoutDuplicate([...entry, ...compare]); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to
providedIn