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
function reducer(state = 'default value', action) { | |
switch(action.type) { | |
case 'SET_FIELD_ONE': | |
return { | |
...state, | |
field-one: action.payload | |
}; | |
default: | |
return state; | |
} |
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
import { NgModule } from '@angular/core'; | |
import { StoreModule } from '@ngrx/store'; | |
import { reducer } from './state/domain.reducer'; | |
/* | |
The following import sets the store like this: | |
store : { | |
domain: // whatever the default value in the reducer function is | |
} | |
*/ |
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
constructor(private store: Store<any>) {} | |
someComponentFunction(value: number): void { | |
this.store.dispatch({ | |
type: 'SET_FIELD_ONE', | |
payload: value | |
}); | |
} | |
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
/* | |
more documentation here: https://github.com/ngrx/platform/blob/master/docs/store/testing.md | |
*/ | |
import { StoreModule, Store, combineReducers } from '@ngrx/store'; | |
import * as fromRoot from '???/state/'; // path to any root app reducers | |
import * as fromFeature from '???/state/'; // path to any reducers used by this component | |
describe('Component', () => { |
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.store.pipe(select('domain')).subscribe( | |
domain => { | |
// use the value of domain here | |
} | |
); |
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
import { NgModule } from '@angular/core'; | |
import { StoreModule } from '@ngrx/store'; | |
import { StoreDevtoolsModule } from '@ngrx/store-devtools'; // import this for redux devtools | |
@NgModule({ | |
imports: [ | |
StoreModule.forRoot({}), | |
!environment.production ? StoreDevtoolsModule.instrument({ | |
name: 'Some Arbitrary Name', | |
maxAge: 30 // the number of states to store |
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
// simple function to create a notification | |
function notify(title, body) { | |
new Notification(title, { | |
body: body, | |
icon: '/img/favicon.png' | |
}); | |
}; | |
switch(Notification.permission) { | |
// user previously denied permission |
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
let sse = new EventSource("http://web.docker.localhost/sse"); | |
sse.onopen = function() { | |
// connection has been opened, server can start sending events to client | |
}; | |
sse.onmessage = function(e) { | |
// message received from server, do something | |
console.log(e.data); | |
}; |
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 is the ExpressJS function you would put at /sse for setting up Server Side Events | |
async function(request, response) { | |
// for simplicity, we'll use a timestamp as an object key and for the first server event id (see below) | |
let timestamp = (+ new Date()); | |
// store the response so you can use it later in other parts of the code | |
request.app.get('connections')[timestamp] = response; | |
// server side events require mime type 'text/event-stream' |
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
/* | |
It's dead simple to start a web socket with socketIO. | |
If needed, io() takes 2 arguments: | |
url - a specific url to handle web sockets (defaults to whatever domain you're on) | |
options - some powerful configuration options | |
*/ | |
let socket = io(); | |
// listen for this server event | |
socket.on('MyWebSocketServerEvent', function(msg) { |