Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 1x 4x 4x 7x 4x 4x 3x 3x 3x 3x 2x 4x 1x 4x 3x 2x 3x 4x 2x 2x 1x 1x 1x | import * as React from 'react';
export type ComponentType<P> =
| React.ComponentClass<P>
| React.StatelessComponent<P>;
export type StoreState = Partial<{
hasFixedNavBar: boolean;
hasStickyFooter: boolean;
navBarHeight: number;
footerHeight: number;
}>;
export type StoreListener = (state: StoreState) => any;
export class Store {
private state: StoreState = {};
private listeners: StoreListener[] = [];
public constructor(initialState: StoreState = {}) {
this.state = initialState;
}
public setState = (state: StoreState) => {
for (const key in state) {
/* istanbul ignore else */
if (Object.prototype.hasOwnProperty.call(state, key)) {
this.state[key as keyof StoreState] = state[key as keyof StoreState];
}
}
this.listeners.forEach(listener => {
listener({ ...this.state });
});
};
public getState = () => {
return { ...this.state };
};
public subscribe = (listener: StoreListener) => {
if (this.listeners.indexOf(listener) < 0) {
this.listeners.push(listener);
}
return this.createUnsubscriber(listener);
};
private createUnsubscriber = (listener: StoreListener) => () => {
const index = this.listeners.indexOf(listener);
if (index >= 0) {
this.listeners.splice(index, 1);
}
};
}
export default new Store();
|