Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Commit

Permalink
Finish stores conversion to TS 2.2 mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
maier49 committed Mar 7, 2017
1 parent c1f2fef commit e4e9717
Show file tree
Hide file tree
Showing 33 changed files with 2,211 additions and 2,603 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"test": "grunt test"
},
"peerDependencies": {
"@dojo/compose": "2.0.0-beta.24",
"@dojo/core": "2.0.0-alpha.25",
"@dojo/has": "2.0.0-alpha.8",
"@dojo/shim": "2.0.0-beta.10"
Expand Down
105 changes: 105 additions & 0 deletions src/interfaces.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import Patch from './patch/Patch';
import { Subscribable } from '@dojo/core/Observable';
import Map from '@dojo/shim/Map';
import Promise from '@dojo/shim/Promise';

export interface Query<T> {
apply(data: T[]): T[];
toString(querySerializer?: (query: Query<T>) => string): string;
incremental?: boolean;
queryType?: QueryType;
}

export type Constructor<T> = new (...args: any[]) => T;

export const enum QueryType {
Filter,
Sort,
Range,
Compound
}

export interface CrudOptions {
rejectOverwrite?: boolean;
id?: string;
}

export interface UpdateResults<T> {
currentItems?: T[];
failedData?: CrudArgument<T>[];
successfulData: T[] | string[];
type: StoreOperation;
}

/**
* Adds a then method to the observable for those consumers of the store API who
* only want to know about the end result of an operation, and don't want to deal with
* any recoverable failures.
*/
export type StoreObservable<T, U> = Subscribable<U> & Promise<T[]>

export interface Storage<T, O extends CrudOptions> {
identify(items: T[]|T): string[];
createId(): Promise<string>;
fetch(query?: Query<T>): FetchResult<T>;
get(ids: string[]): Promise<T[]>;
put(items: T[], options?: O): Promise<UpdateResults<T>>;
add(items: T[], options?: O): Promise<UpdateResults<T>>;
delete(ids: string[]): Promise<UpdateResults<T>>;
patch(updates: { id: string; patch: Patch<T, T> }[], options?: O): Promise<UpdateResults<T>>;
}

export interface Store<T, O extends CrudOptions, U extends UpdateResults<T>> {
get(ids: string[]): Promise<T[]>;
get(id: string): Promise<T | undefined>;
get(ids: string | string[]): Promise<T | undefined | T[]>;
identify(items: T[]): string[];
identify(items: T): string;
identify(items: T | T[]): string | string[];
createId(): Promise<string>;
add(items: T[] | T, options?: O): StoreObservable<T, U>;
put(items: T[] | T, options?: O): StoreObservable<T, U>;
patch(updates: PatchArgument<T>, options?: O): StoreObservable<T, U>;
delete(ids: string[] | string): StoreObservable<string, U>;
fetch(query?: Query<T>): FetchResult<T>;
}

export const enum StoreOperation {
Add,
Put,
Patch,
Delete
}

export interface StoreOptions<T, O extends CrudOptions> {
data?: T[];
idProperty?: keyof T;
idFunction?: (item: T) => string;
storage?: Storage<T, O>;
}

export type CrudArgument<T> = T | string | PatchMapEntry<T, T>;

export type BasicPatch<T> = { id: string } & {
[P in keyof T]?: T[P] | BasicPatch<T[P]>;
};

export type PatchArgument<T> = Map<string, Patch<T, T>> |
{ id: string; patch: Patch<T, T> } |
{ id: string; patch: Patch<T, T> }[] |
BasicPatch<T> |
BasicPatch<T>[];

export interface FetchResult<T> extends Promise<T[]> {
/**
* A Promise that resolves to the total number of items in the underlying storage.
*/
totalLength: Promise<number>;
/**
* For a store, this is identical to totalLength. For a QueryTransformResultInterface, this resolves to the number of items
* that match the QueryTransformResultInterface's queries
*/
dataLength: Promise<number>;
}

export type PatchMapEntry<T, U> = { id: string; patch: Patch<T, U> };
2 changes: 0 additions & 2 deletions src/patch/Patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { shouldRecurseInto, isEqual } from '../utils';
import createOperation, { Operation, OperationType } from './createOperation';
import JsonPointer from './JsonPointer';

export type PatchMapEntry<T, U> = { id: string; patch: Patch<T, U> };

function _diff(to: any, from: any, startingPath?: JsonPointer): Operation[] {
if (!shouldRecurseInto(from) || !shouldRecurseInto(to)) {
return [];
Expand Down
2 changes: 1 addition & 1 deletion src/query/CompoundQuery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Query, QueryType } from './interfaces';
import { Query, QueryType } from '../interfaces';

export interface QueryOptions<T> {
query?: Query<T>;
Expand Down
2 changes: 1 addition & 1 deletion src/query/createFilter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JsonPointer, { navigate } from '../patch/JsonPointer';
import { isEqual } from '../utils';
import { Query, QueryType } from './interfaces';
import { Query, QueryType } from '../interfaces';

export type FilterFunction<T> = (data: T[]) => T[];
export type ObjectPointer<T> = JsonPointer | keyof T | '';
Expand Down
2 changes: 1 addition & 1 deletion src/query/createSort.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Query, QueryType } from './interfaces';
import { Query, QueryType } from '../interfaces';
import JsonPointer, { navigate } from '../patch/JsonPointer';

export type SortParameter<T> = ((a: T, b: T) => number) | keyof T | JsonPointer;
Expand Down
2 changes: 1 addition & 1 deletion src/query/createStoreRange.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Query, QueryType } from './interfaces';
import { Query, QueryType } from '../interfaces';
export interface StoreRange<T> extends Query<T> {
readonly start: number;
readonly count: number;
Expand Down
13 changes: 0 additions & 13 deletions src/query/interfaces.d.ts

This file was deleted.

27 changes: 2 additions & 25 deletions src/storage/InMemoryStorage.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
import { Query } from '../query/interfaces';
import { StoreOperation, CrudOptions, StoreOptions, UpdateResults } from '../store/createStore';
import { Query, FetchResult } from '../interfaces';
import { StoreOperation, CrudOptions, StoreOptions, UpdateResults, Storage } from '../interfaces';
import Promise from '@dojo/shim/Promise';
import Map from '@dojo/shim/Map';
import Patch from '../patch/Patch';
import { duplicate } from '@dojo/core/lang';
import uuid from '@dojo/core/uuid';

export interface FetchResult<T> extends Promise<T[]> {
/**
* A Promise that resolves to the total number of items in the underlying storage.
*/
totalLength: Promise<number>;
/**
* For a store, this is identical to totalLength. For a QueryTransformResult, this resolves to the number of items
* that match the QueryTransformResult's queries
*/
dataLength: Promise<number>;
}

export interface Storage<T, O extends CrudOptions> {
identify(items: T[]|T): string[];
createId(): Promise<string>;
fetch(query?: Query<T>): FetchResult<T>;
get(ids: string[]): Promise<T[]>;
put(items: T[], options?: O): Promise<UpdateResults<T>>;
add(items: T[], options?: O): Promise<UpdateResults<T>>;
delete(ids: string[]): Promise<UpdateResults<T>>;
patch(updates: { id: string; patch: Patch<T, T> }[], options?: O): Promise<UpdateResults<T>>;
}

export default class InMemoryStorage<T> implements Storage<T, CrudOptions> {
private idProperty?: keyof T;
private idFunction?: (item: T) => string;
Expand Down
6 changes: 3 additions & 3 deletions src/storage/IndexedDBStorage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import InMemoryStorage, { FetchResult } from './InMemoryStorage';
import { StoreOptions, CrudOptions, StoreOperation, UpdateResults } from '../store/createStore';
import {Query, QueryType} from '../query/interfaces';
import InMemoryStorage from './InMemoryStorage';
import { StoreOptions, CrudOptions, StoreOperation, UpdateResults, FetchResult } from '../interfaces';
import {Query, QueryType} from '../interfaces';
import Set from '@dojo/shim/Set';
import Promise from '@dojo/shim/Promise';
import Patch from '../patch/Patch';
Expand Down
Loading

0 comments on commit e4e9717

Please sign in to comment.