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
[labs/redux] Reactive controller for subscribing to a Redux store #4586
Open
augustjk
wants to merge
13
commits into
main
Choose a base branch
from
redux-controller
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3725dd3
Initial labs/redux setup
augustjk 7361a99
Add redux example project
augustjk eb76c9a
Rename Selector to Connector and allow selector to be optional
augustjk e49e8cf
Add select and dispatch decorators
augustjk d579ea1
WIP testing
augustjk 7e6718e
Tests for equality and connector
augustjk 7b4e4d4
Add decorator tests
augustjk 1c65493
Add withStoreType test
augustjk 7c99e75
Add jsdoc for exported members
augustjk cda3564
Update readme
augustjk 745ff0d
Add changeset and ready for publish
augustjk 4b8e786
Add build and test script to monorepo root
augustjk a6ab3c8
Add missing wireit deps
augustjk File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@lit-labs/redux': minor | ||
--- | ||
|
||
Initial release of `@lit-labs/redux`, a package containing bindings for Redux for use in Lit elements. |
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,2 @@ | ||
/node_modules/ | ||
/dist/ | ||
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,3 @@ | ||
# Lit Labs Redux example | ||
|
||
Adaptation of the [Redux Counter Example App](https://redux.js.org/tutorials/essentials/part-2-app-structure#the-counter-example-app) built with Lit, using `@lit-labs/redux`. |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,12 @@ | ||
<head> | ||
<title>Lit Labs Redux</title> | ||
<script type="module" src="./src/index.ts"></script> | ||
<style> | ||
:root { | ||
font-family: sans-serif; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<my-app></my-app> | ||
</body> |
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,35 @@ | ||
{ | ||
"name": "@lit-examples/redux", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "wireit", | ||
"build": "wireit", | ||
"test": "wireit" | ||
}, | ||
"dependencies": { | ||
"@lit-labs/redux": "^0.0.0", | ||
"@reduxjs/toolkit": "^2.1.0", | ||
"lit": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"vite": "^5.1.6" | ||
}, | ||
"wireit": { | ||
"dev": { | ||
"command": "vite", | ||
"service": true, | ||
"files": [ | ||
"./src/", | ||
"./index.html" | ||
] | ||
}, | ||
"build": { | ||
"command": "tsc && vite build" | ||
}, | ||
"test": { | ||
"command": "echo \"TODO\"" | ||
} | ||
} | ||
} |
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,14 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import {Connector} from '@lit-labs/redux'; | ||
import type {AppStore} from './store.js'; | ||
|
||
// Create a Selector controller providing the AppStore type for better type | ||
// checks on selector functions and returned values. | ||
// Similar idea as: | ||
// https://redux.js.org/tutorials/typescript-quick-start#define-typed-hooks | ||
export const AppConnector = Connector.withStoreType<AppStore>(); |
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,20 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import {html, LitElement} from 'lit'; | ||
import {customElement} from 'lit/decorators.js'; | ||
import {select} from '@lit-labs/redux'; | ||
import type {RootState} from './store.js'; | ||
|
||
@customElement('count-display') | ||
export class CountDisplay extends LitElement { | ||
@select((state: RootState) => state.counter.value) | ||
_count!: number; | ||
|
||
render() { | ||
return html`<p>The count is: ${this._count}</p>`; | ||
} | ||
} |
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,58 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import {html, css, LitElement} from 'lit'; | ||
import {customElement, state} from 'lit/decorators.js'; | ||
import {incrementByAmount} from './counter-slice.js'; | ||
import {dispatch} from '@lit-labs/redux'; | ||
import type {AppDispatch} from './store.js'; | ||
|
||
@customElement('count-incrementor') | ||
export class CountIncrementor extends LitElement { | ||
@dispatch() | ||
_dispatch!: AppDispatch; | ||
|
||
@state() | ||
_incrementAmount = '2'; | ||
|
||
_handleInput(e: InputEvent) { | ||
this._incrementAmount = (e.target as HTMLInputElement).value; | ||
} | ||
|
||
_incrementCountByAmount() { | ||
this._dispatch(incrementByAmount(Number(this._incrementAmount) || 0)); | ||
} | ||
|
||
render() { | ||
return html` | ||
<div class="row"> | ||
<input @input=${this._handleInput} .value=${this._incrementAmount} /> | ||
<button @click=${this._incrementCountByAmount}>Add Amount</button> | ||
</div> | ||
`; | ||
} | ||
|
||
static styles = css` | ||
.row { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
gap: 1rem; | ||
} | ||
|
||
button { | ||
font-size: 1.5rem; | ||
padding: 10px; | ||
} | ||
|
||
input { | ||
font-size: 1.5rem; | ||
padding: 10px; | ||
width: 100px; | ||
text-align: center; | ||
} | ||
`; | ||
} |
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,35 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import {type PayloadAction, createSlice} from '@reduxjs/toolkit'; | ||
|
||
export type CounterState = { | ||
value: number; | ||
}; | ||
|
||
const initialState: CounterState = { | ||
value: 0, | ||
}; | ||
|
||
export const counterSlice = createSlice({ | ||
name: 'counter', | ||
initialState, | ||
reducers: { | ||
increment: (state) => { | ||
state.value += 1; | ||
}, | ||
decrement: (state) => { | ||
state.value -= 1; | ||
}, | ||
incrementByAmount: (state, action: PayloadAction<number>) => { | ||
state.value += action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
export const {increment, decrement, incrementByAmount} = counterSlice.actions; | ||
|
||
export default counterSlice.reducer; |
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,54 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import {html, css, LitElement} from 'lit'; | ||
import {customElement} from 'lit/decorators.js'; | ||
import {provide, storeContext} from '@lit-labs/redux'; | ||
import {store} from './store.js'; | ||
import './my-counter.js'; | ||
import './count-incrementor.js'; | ||
import './count-display.js'; | ||
|
||
@customElement('my-app') | ||
export class MyApp extends LitElement { | ||
// Provide the Redux store in a context to any children of this component. | ||
@provide({ | ||
context: storeContext, | ||
}) | ||
_store = store; | ||
|
||
render() { | ||
return html` | ||
<main> | ||
<div> | ||
<img alt="Lit Logo" src="/assets/lit.svg" /> | ||
<img alt="Redux Logo" src="/assets/redux.svg" /> | ||
</div> | ||
<my-counter></my-counter> | ||
<count-incrementor></count-incrementor> | ||
<count-display></count-display> | ||
</main> | ||
`; | ||
} | ||
|
||
static styles = css` | ||
:host { | ||
font-size: 2rem; | ||
} | ||
|
||
main { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
gap: 2rem; | ||
} | ||
|
||
img { | ||
height: 150px; | ||
width: 150px; | ||
} | ||
`; | ||
} |
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,50 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import {html, css, LitElement} from 'lit'; | ||
import {customElement} from 'lit/decorators.js'; | ||
import {AppConnector} from './app-connector.js'; | ||
import {increment, decrement} from './counter-slice.js'; | ||
|
||
@customElement('my-counter') | ||
export class MyCounter extends LitElement { | ||
// Select the counter value from the Redux store from parent context. | ||
_connector = new AppConnector(this, { | ||
selector: (state) => state.counter.value, | ||
}); | ||
|
||
_incrementCount() { | ||
this._connector.dispatch(increment()); | ||
} | ||
|
||
_decrementCount() { | ||
this._connector.dispatch(decrement()); | ||
} | ||
|
||
render() { | ||
return html` | ||
<div> | ||
<button @click=${this._incrementCount}>+</button> | ||
<span>${this._connector.selected}</span> | ||
<button @click=${this._decrementCount}>−</button> | ||
</div> | ||
`; | ||
} | ||
|
||
static styles = css` | ||
div { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
gap: 1rem; | ||
} | ||
|
||
button { | ||
font-size: 1.5rem; | ||
padding: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import {configureStore} from '@reduxjs/toolkit'; | ||
import counterReducer from './counter-slice.js'; | ||
|
||
export const store = configureStore({ | ||
reducer: { | ||
counter: counterReducer, | ||
}, | ||
}); | ||
|
||
export type AppStore = typeof store; | ||
export type RootState = ReturnType<typeof store.getState>; | ||
export type AppDispatch = typeof store.dispatch; |
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,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2021", | ||
"experimentalDecorators": true, | ||
"useDefineForClassFields": false, | ||
"module": "ESNext", | ||
"lib": ["ES2021", "DOM", "DOM.Iterable"], | ||
"skipLibCheck": true, | ||
|
||
/* Bundler mode */ | ||
"moduleResolution": "bundler", | ||
"allowImportingTsExtensions": true, | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"noEmit": true, | ||
|
||
/* Linting */ | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noFallthroughCasesInSwitch": true | ||
}, | ||
"include": ["src"] | ||
} |
Oops, something went wrong.
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.
who's writing to
/dist/
, Vite? Leave a comment?