Skip to content

Fix lifecycle method usage #14

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

Merged
merged 2 commits into from
Oct 9, 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
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ After you have submitted your pull request, we'll try to get back to you as soon

Thank you for contributing!


# Cutting a release

If you are a maintainer and want to cut a release, follow these steps:
Expand Down
8 changes: 2 additions & 6 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,15 @@
"outputPath": "dist/angular-redux-injector-demo",
"index": "projects/angular-redux-injector-demo/src/index.html",
"browser": "projects/angular-redux-injector-demo/src/main.ts",
"polyfills": [
"zone.js"
],
"polyfills": ["zone.js"],
"tsConfig": "projects/angular-redux-injector-demo/tsconfig.app.json",
"assets": [
{
"glob": "**/*",
"input": "projects/angular-redux-injector-demo/public"
}
],
"styles": [
"projects/angular-redux-injector-demo/src/styles.css"
],
"styles": ["projects/angular-redux-injector-demo/src/styles.css"],
"scripts": []
},
"configurations": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export const asyncRunInInjectionContext = <TReturn>(
});
};

export type RunInInjectionContextProps<
T extends object,
> = T & {
export type RunInInjectionContextProps<T extends object> = T & {
injector: EnvironmentInjector;
};
20 changes: 10 additions & 10 deletions projects/angular-redux-injector-demo/src/index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AngularReduxInjectorDemo</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>
<head>
<meta charset="utf-8" />
<title>AngularReduxInjectorDemo</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>
5 changes: 3 additions & 2 deletions projects/angular-redux-injector-demo/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ 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));
bootstrapApplication(AppComponent, appConfig).catch((err) =>
console.error(err),
);
8 changes: 2 additions & 6 deletions projects/angular-redux-injector-demo/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
"outDir": "../../out-tsc/app",
"types": []
},
"files": [
"src/main.ts"
],
"include": [
"src/**/*.d.ts"
]
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"]
}
22 changes: 11 additions & 11 deletions projects/angular-redux/src/lib/inject-selector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EqualityFn } from './types';
import {
assertInInjectionContext,
DestroyRef,
effect,
inject,
Signal,
Expand Down Expand Up @@ -80,6 +81,7 @@ export function createSelectorInjection(): InjectSelector {
): Signal<Selected> => {
assertInInjectionContext(injectSelector);
const reduxContext = inject(ReduxProvider);
const destroyRef = inject(DestroyRef);

const { equalityFn = refEquality } =
typeof equalityFnOrOptions === 'function'
Expand All @@ -90,19 +92,17 @@ export function createSelectorInjection(): InjectSelector {

const selectedState = signal(selector(store.getState()));

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

selectedState.set(data);
});
selectedState.set(data);
});

onCleanup(() => {
unsubscribe();
});
destroyRef.onDestroy(() => {
unsubscribe();
});

return selectedState;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component } from '@angular/core';
import { render } from '@testing-library/angular';
import { render, waitFor } from '@testing-library/angular';
import '@testing-library/jest-dom';
import { configureStore, createSlice } from '@reduxjs/toolkit';
import { provideRedux, injectDispatch, injectSelector } from '../public-api';
import { userEvent } from '@testing-library/user-event';
import { createStore } from 'redux';

const user = userEvent.setup();

Expand Down Expand Up @@ -70,3 +71,32 @@ it('injectSelector should work with reactivity', async () => {

expect(getByText('Count: 1')).toBeInTheDocument();
});

it('should show a value dispatched during ngOnInit', async () => {
const store = configureStore({
reducer: {
counter: counterSlice.reducer,
},
});

@Component({
selector: 'app-root',
standalone: true,
template: '<p>Count: {{count()}}</p>',
})
class Comp {
count = injectSelector((state: any) => state.counter.value);
dispatch = injectDispatch();
increment = counterSlice.actions.increment;

ngOnInit() {
this.dispatch(this.increment());
}
}

const { getByText } = await render(Comp, {
providers: [provideRedux({ store })],
});

await waitFor(() => expect(getByText('Count: 1')).toBeInTheDocument());
});