Skip to content
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

fix: dispatch operations are now untracked #20

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,75 @@
"builder": "@angular-devkit/build-angular:extract-i18n"
}
}
},
"angular-redux-auto-dispatch-demo": {
"projectType": "application",
"schematics": {},
"root": "projects/angular-redux-auto-dispatch-demo",
"sourceRoot": "projects/angular-redux-auto-dispatch-demo/src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:application",
"options": {
"outputPath": "dist/angular-redux-auto-dispatch-demo",
"index": "projects/angular-redux-auto-dispatch-demo/src/index.html",
"browser": "projects/angular-redux-auto-dispatch-demo/src/main.ts",
"polyfills": [
"zone.js"
],
"tsConfig": "projects/angular-redux-auto-dispatch-demo/tsconfig.app.json",
"assets": [
{
"glob": "**/*",
"input": "projects/angular-redux-auto-dispatch-demo/public"
}
],
"styles": [
"projects/angular-redux-auto-dispatch-demo/src/styles.css"
],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kB",
"maximumError": "1MB"
},
{
"type": "anyComponentStyle",
"maximumWarning": "4kB",
"maximumError": "8kB"
}
],
"outputHashing": "all"
},
"development": {
"optimization": false,
"extractLicenses": false,
"sourceMap": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"buildTarget": "angular-redux-auto-dispatch-demo:build:production"
},
"development": {
"buildTarget": "angular-redux-auto-dispatch-demo:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n"
}
}
}
}
}
Binary file not shown.
24 changes: 24 additions & 0 deletions projects/angular-redux-auto-dispatch-demo/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Component, effect } from '@angular/core';
import { injectSelector, injectDispatch } from '@reduxjs/angular-redux';
import { decrement, increment } from './store/counter-slice';
import { RootState } from './store';

@Component({
selector: 'app-root',
standalone: true,
template: `
<button (click)="dispatch(increment())">Increment</button>
<span>{{ count() }}</span>
<button (click)="dispatch(decrement())">Decrement</button>
`,
})
export class AppComponent {
count = injectSelector((state: RootState) => state.counter.value);
dispatch = injectDispatch();
increment = increment;
decrement = decrement;

_auto_increment = effect(() => {
this.dispatch(increment());
});
}
10 changes: 10 additions & 0 deletions projects/angular-redux-auto-dispatch-demo/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRedux } from '@reduxjs/angular-redux';
import { store } from './store';

export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRedux({ store }),
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createSlice } from '@reduxjs/toolkit';
import type { PayloadAction } from '@reduxjs/toolkit';

export interface CounterState {
value: number;
}

const initialState: CounterState = {
value: 0,
};

export const counterSlice = createSlice({
name: 'counter',
initialState,
reducers: {
increment: (state) => {
// Redux Toolkit allows us to write "mutating" logic in reducers. It
// doesn't actually mutate the state because it uses the Immer library,
// which detects changes to a "draft state" and produces a brand new
// immutable state based off those changes
state.value += 1;
},
decrement: (state) => {
state.value -= 1;
},
incrementByAmount: (state, action: PayloadAction<number>) => {
state.value += action.payload;
},
},
});

// Action creators are generated for each case reducer function
export const { increment, decrement, incrementByAmount } = counterSlice.actions;

export default counterSlice.reducer;
13 changes: 13 additions & 0 deletions projects/angular-redux-auto-dispatch-demo/src/app/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './counter-slice';

export const store = configureStore({
reducer: {
counter: counterReducer,
},
});

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
// Inferred type: {counter: CounterState}
export type AppDispatch = typeof store.dispatch;
13 changes: 13 additions & 0 deletions projects/angular-redux-auto-dispatch-demo/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>AngularReduxDemo</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
</head>
<body>
<app-root></app-root>
</body>
</html>
7 changes: 7 additions & 0 deletions projects/angular-redux-auto-dispatch-demo/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';

bootstrapApplication(AppComponent, appConfig).catch((err) =>
console.error(err),
);
1 change: 1 addition & 0 deletions projects/angular-redux-auto-dispatch-demo/src/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* You can add global styles to this file, and also import other style files */
15 changes: 15 additions & 0 deletions projects/angular-redux-auto-dispatch-demo/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/app",
"types": []
},
"files": [
"src/main.ts"
],
"include": [
"src/**/*.d.ts"
]
}
7 changes: 3 additions & 4 deletions projects/angular-redux/src/lib/inject-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,12 @@ export function createSelectorInjection(): InjectSelector {

const { store, subscription } = reduxContext;

const selectedState = linkedSignal(() => selector(store.getState()));
const selectedState = linkedSignal(() => selector(store.getState()), {
equal: equalityFn,
});

const unsubscribe = subscription.addNestedSub(() => {
const data = selector(store.getState());
if (equalityFn(selectedState(), data)) {
return;
}

selectedState.set(data);
});
Expand Down
Loading