Skip to content
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

Helpers #13

Merged
merged 14 commits into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 40 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"license": "AGPL-3.0",
"devDependencies": {
"ts-loader": "^2.1.0",
"tslint": "^5.4.2",
"tslint": "^5.7.0",
"tslint-loader": "^3.5.3",
"typescript": "^2.3.4",
"typescript": "^2.5.2",
"uglifyjs": "^2.4.11",
"webpack": "^2.6.1"
},
Expand Down
4 changes: 2 additions & 2 deletions src/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface DispatcherMessage<TAction> {
action: TAction;
}

export class DispatcherBuilder extends flux.Dispatcher<DispatcherMessage<any>> {
export class DispatcherClass extends flux.Dispatcher<DispatcherMessage<any>> {
/**
* Dispatches a payload to all registered callbacks.
*
Expand All @@ -26,4 +26,4 @@ export class DispatcherBuilder extends flux.Dispatcher<DispatcherMessage<any>> {
}
}

export const Dispatcher = new DispatcherBuilder();
export const Dispatcher = new DispatcherClass();
File renamed without changes.
3 changes: 3 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { ItemIsPending } from "./helpers/item-is-pending";
export { ItemsStatusResolver } from "./helpers/items-status-resolver";
export { WaitForStoreChange } from "./helpers/wait-for-store-changes";
5 changes: 5 additions & 0 deletions src/helpers/item-is-pending.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ItemStatus } from "../abstractions";

export function ItemIsPending(status: ItemStatus): boolean {
return (status <= ItemStatus.Pending);
}
33 changes: 33 additions & 0 deletions src/helpers/items-status-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ItemStatus } from "../abstractions";

export function ItemsStatusResolver(...statuses: ItemStatus[]): ItemStatus {
let loadedCount: number = 0;
let noDataCount: number = 0;
let pendingCount: number = 0;

for (let i = 0; i < statuses.length; i++) {
const status = statuses[i];
switch (status) {
case ItemStatus.Failed:
return status;
case ItemStatus.Loaded:
loadedCount++;
break;
case ItemStatus.NoData:
noDataCount++;
break;
case ItemStatus.Init:
case ItemStatus.Pending:
pendingCount++;
break;
}
}

if (pendingCount > 0) {
return ItemStatus.Pending;
} else if (loadedCount === 0 && noDataCount > 0) {
return ItemStatus.NoData;
} else {
return ItemStatus.Loaded;
}
}
10 changes: 10 additions & 0 deletions src/helpers/wait-for-store-change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Store } from "flux/utils";

export async function WaitForStoreChange(store: Store<any>): Promise<void> {
return new Promise<void>(resolve => {
const listener = store.addListener(() => {
listener.remove();
resolve();
});
});
}
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ export * from "./dispatcher";
export * from "./stores/map-store";
export * from "./stores/data-store";
export * from "./stores/reduce-store";
export * from "./emmitters/actions-emmitter";
export * from "./emitters/actions-emitter";
import * as Actions from "./actions/actions";
import * as Abstractions from "./abstractions";
import * as Contracts from "./contracts";
import * as Helpers from "./helpers";

export {
Actions,
Abstractions,
Contracts
Contracts,
Helpers
};
4 changes: 3 additions & 1 deletion src/stores/map-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,9 @@ export abstract class MapStore<TValue> extends ReduceStore<Items<TValue>> {
for (let i = 0; i < moveList.length; i++) {
let key = moveList[i];
let item = this.queuesHandler.Get(key);
stateMap.set(key, { ...item });
if (item != null) {
stateMap.set(key, { ...item });
}
this.queuesHandler.Remove(key);
}
});
Expand Down
6 changes: 3 additions & 3 deletions src/stores/reduce-store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Flux from "flux";
import { ReduceStore as FluxReduceStore } from "flux/utils";
import * as Immutable from "immutable";
import { Dispatcher, DispatcherMessage, DispatcherBuilder } from "../dispatcher";
import { Dispatcher, DispatcherMessage, DispatcherClass } from "../dispatcher";

export type ActionHandler<TClass, TState> = (action: TClass, state: TState) => TState | void;

Expand Down Expand Up @@ -138,8 +138,8 @@ export abstract class ReduceStore<TState> extends FluxReduceStore<TState, Dispat
* This method will return the dispatcher for this store.
*
*/
public getDispatcher(): DispatcherBuilder {
return super.getDispatcher();
public getDispatcher(): DispatcherClass {
return super.getDispatcher() as DispatcherClass;
}

/**
Expand Down