Skip to content

Instantly share code, notes, and snippets.

chunkQueue$
.pipe(mergeMap((data) => data, null, maxConnections))
.subscribe();
@glendaviesnz
glendaviesnz / component-state.service.ts
Last active December 14, 2018 02:58
NgRX State Machine - component state service
// the component uses an injected componentStateService to register its state transitions
this._componentStateService.addComponentStates(componentStates);
// the component state service is reasonably simple and just maintains a lookup object of all the registered
// state transitions, and also has methods for cleaning these up when components are destroyed
@Injectable()
export class ComponentStateService {
public componentStates: any = {};
@glendaviesnz
glendaviesnz / component-state.builder.ts
Last active December 14, 2018 02:58
NgRX State Machine - fluent component state builder
// Instead of this ....
const componentStates = {
name: 'userApproval',
states: {
[actionTypes.loadUnapprovedUsers]: {
[ComponentStates.Idle]: { to: ComponentStates.Processing, action: PassthroughAction }
},
[actionTypes.loadUnapprovedUsersSuccess]: {
[ComponentStates.Processing]: { to: ComponentStates.Idle, action: PassthroughAction }
},
@glendaviesnz
glendaviesnz / rxjs-chunker.ts
Created July 7, 2018 00:45
RxJs document chunker and uploader. This class takes a document and breaks it into 1Mb chunks and uploads those chunks sequentially to the server.
export class ChunkingService {
public chunkSize = 1024 * 1024;
private _chunkQueue$ = new Subject();
private _maxConnections = 3;
constructor(
private _apiService: apiService,
private _store: Store<any>) {
Guard.notNothing(_apiService, '_apiService');
Guard.notNothing(_store, '_store');
@glendaviesnz
glendaviesnz / rxjs-chunker.ts
Created July 7, 2018 00:42
RxJs document chunker and uploader
export class ChunkingService {
public chunkSize = 1024 * 1024;
private _chunkQueue$ = new Subject();
private _maxConnections = 3;
constructor(
private _apiService: apiService,
private _store: Store<any>) {
Guard.notNothing(_apiService, '_apiService');
Guard.notNothing(_store, '_store');
@glendaviesnz
glendaviesnz / component-state.js
Last active December 14, 2018 01:41
Object literal declaration of component state
const componentStates = {
name: 'userApproval',
states: {
[actionTypes.loadUnapprovedUsers]: {
[ComponentStates.Idle]: { to: ComponentStates.Processing, action: PassthroughAction }
},
[actionTypes.loadUnapprovedUsersSuccess]: {
[ComponentStates.Processing]: { to: ComponentStates.Idle, action: PassthroughAction }
},
[actionTypes.loadUnapprovedUsersError]: {
@glendaviesnz
glendaviesnz / add-validation.jsx
Last active June 4, 2018 03:04
Add validation component to validated field
// where the form is declared a simple string is used to indicated validation type
<input name="firstName" validation="required" />
// an object map is used in the form library to map this string name to a validation method and message
export const validationTypes = {
required: { validate: validateRequired, message: 'Field is required' },
email: { validate: validateEmail, message: 'Valid email address required' }
}
@glendaviesnz
glendaviesnz / state-machine.ts
Last active June 1, 2018 02:33
Extension of NgRx ActionsSubject to manage Component State
@Injectable()
export class ComponentStateMachine extends ActionsSubject {
constructor() {
super();
}
public next(action: any) {
// add all your state machine magic here ....
if (isValidAction) {
super.next(action);
@glendaviesnz
glendaviesnz / RecursiveRefs.jsx
Created May 26, 2018 06:52
Recursive method to add refs to nested form elements in props.children
class EasyForm extends React.Component {
fieldRefs = []
fieldTypes = ['input', 'button', 'textarea'];
addRefsToChildren(children) {
let childPropsChildren;
const newChildren = React.Children.map(children, child => {
if (child.props && child.props.children ) {
childPropsChildren = this.addRefsToChildren(child.props.children);
@glendaviesnz
glendaviesnz / DeclarativeFormStyle.jsx
Last active May 26, 2018 02:46
Rxjs based React forms blog post
<EasyForm onsubmit="this.onFormSubmit">
<label>
First name
<input name="firstName" />
</label>
<label>
Last name
<input name="lastName" />
</label>
<input name="newsletterSub" type="checkbox" />