Skip to content

Commit

Permalink
Helpers (#13)
Browse files Browse the repository at this point in the history
* Created ItemIsPending helper.

* Created items stutuses resolver.

* Created wait for store changes async helper.

* Reexported all helpers.

* Fixed naming and reexported helpers.

* Fixed naming.

* Fixed import.

* Updated typescript.

* Fixed ts errors.

* Fixed naming.

* Fixed Dispatcher class naming.

* Fixed ts errors.

* Fixed naming.

* Fixed ts error.
  • Loading branch information
GiedriusGrabauskas authored Sep 26, 2017
1 parent 3294623 commit 8a1ffa7
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 48 deletions.
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-change";
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

0 comments on commit 8a1ffa7

Please sign in to comment.