Skip to content

Instantly share code, notes, and snippets.

@efossas
efossas / domain.component.ts
Last active August 23, 2018 14:32
domain.component.ts
constructor(private store: Store<any>) {}
someComponentFunction(value: number): void {
this.store.dispatch({
type: 'SET_FIELD_ONE',
payload: value
});
}
@efossas
efossas / domain.component.spec.ts
Created August 23, 2018 14:33
domain.component.spec.ts
/*
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', () => {
@efossas
efossas / domain.reducer.ts
Last active August 24, 2018 19:23
domain.reducer.ts
function reducer(state = 'default value', action) {
switch(action.type) {
case 'SET_FIELD_ONE':
return {
...state,
field-one: action.payload
};
default:
return state;
}
@efossas
efossas / domain.module.ts
Last active August 24, 2018 19:37
domain.module.ts
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
}
*/
@efossas
efossas / subscribe.ts
Created August 24, 2018 19:49
subscribe.ts
this.store.pipe(select('domain')).subscribe(
domain => {
// use the value of domain here
}
);
@efossas
efossas / notification.js
Last active November 21, 2018 18:56
Notification API
// 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
@efossas
efossas / sseClient.js
Created November 26, 2018 04:52
Server Side Event Client
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);
};
@efossas
efossas / sseServer.js
Created November 26, 2018 22:02
Server Side Events Server
// 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'
@efossas
efossas / wsClient.js
Created November 26, 2018 22:05
Web Socket Client
/*
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) {
@efossas
efossas / wsServer.js
Created November 26, 2018 22:06
Web Socket Server
// import the SocketIO library
const socketio = require('socket.io');
// SocketIO requires an http server. If we pass in a port number, it uses NodeJS' default http library
const io = socketio(80);
io.on('connection', function(socket) {
// emit an event to the client, the object can contain anything, it can be any type too
io.emit('MyWebSocketServerEvent', { info: 'got your connection, thanks' });