Modified the http module to remove the cyclic references. Added promises.d.ts.

This commit is contained in:
atanasovg
2014-05-21 16:27:13 +03:00
parent 1dd3a5f9b5
commit db987a361d
19 changed files with 169 additions and 37 deletions

View File

@ -116,11 +116,12 @@
<TypeScriptCompile Include="file-system\file-system.impl.ts">
<DependentUpon>file-system.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="http\http.android.ts">
<DependentUpon>http.d.ts</DependentUpon>
<TypeScriptCompile Include="http\http-request.d.ts" />
<TypeScriptCompile Include="http\http-request.android.ts">
<DependentUpon>http-request.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="http\http.ios.ts">
<DependentUpon>http.d.ts</DependentUpon>
<TypeScriptCompile Include="http\http-request.ios.ts">
<DependentUpon>http-request.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="image-source\image-source-native.android.ts">
<DependentUpon>image-source-native.d.ts</DependentUpon>
@ -135,6 +136,7 @@
</TypeScriptCompile>
<TypeScriptCompile Include="image-source\index.ts" />
<TypeScriptCompile Include="location\location.d.ts" />
<TypeScriptCompile Include="promises\promises.d.ts" />
<TypeScriptCompile Include="Tests\application-tests-common.ts" />
<TypeScriptCompile Include="Tests\application-tests.ios.ts">
<DependentUpon>application-tests-common.ts</DependentUpon>
@ -188,7 +190,9 @@
<DependentUpon>file-system-access.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="application\application-common.ts" />
<TypeScriptCompile Include="http\http-common.ts" />
<TypeScriptCompile Include="http\http.impl.ts">
<DependentUpon>http.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="http\http.d.ts" />
<TypeScriptCompile Include="local-settings\index.ts" />
<TypeScriptCompile Include="local-settings\local-settings.d.ts" />

2
camera/camera.d.ts vendored
View File

@ -1,7 +1,7 @@

declare module "camera" {
import promises = require("promises/promises");
import promises = require("promises");
import imageSource = require("image-source");
enum CameraPosition {

View File

@ -1,4 +1,4 @@
import promises = require("promises/promises");
import promises = require("promises");
import imageSource = require("image-source");
import types = require("camera/camera-types");

View File

@ -1,4 +1,5 @@
export declare var helper_log: (message: string) => void;
//@private
export declare var helper_log: (message: string) => void;
export declare var info: (message: string) => void;
export declare var error: (message: string) => void;
export declare var warn: (message: string) => void;

View File

@ -1,6 +1,4 @@
// TODO: Implement "hidden" notation so that such declarations are not included in the d.ts file we will provide for the users.
//@hidden
//@private
export declare class FileSystemAccess {
getLastModified(path: string): Date;

View File

@ -1,7 +1,7 @@

declare module "file-system" {
import promises = require("promises/promises");
import promises = require("promises");
export class FileSystemEntity {
/**

View File

@ -1,5 +1,5 @@
import file_access_module = require("file-system/file-system-access");
import promises = require("promises/promises");
import promises = require("promises");
// The FileSystemAccess implementation, used through all the APIs.
var fileAccess;

View File

@ -1,11 +1,10 @@
/**
* Android specific http request implementation.
*/
import promises = require("promises/promises");
import http = require("http");
import promises = require("promises");
declare var exports;
require("utils/module-merge").merge(require("http/http-common"), exports);
// this is imported for definition purposes only
import http = require("http");
export function request(options: http.HttpRequestOptions): promises.Promise<http.HttpResponse> {
var d = promises.defer<http.HttpResponse>();

5
http/http-request.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
//@private
import promises = require("promises");
import http = require("http");
export declare var request: (options: http.HttpRequestOptions) => promises.Promise<http.HttpResponse>;

View File

@ -1,12 +1,9 @@
/**
* iOS specific http request implementation.
*/
import promises = require("promises/promises");
import promises = require("promises");
import http = require("http");
declare var exports;
require("utils/module-merge").merge(require("http/http-common"), exports);
export function request(options: http.HttpRequestOptions): promises.Promise<http.HttpResponse> {
var d = promises.defer<http.HttpResponse>();

2
http/http.d.ts vendored
View File

@ -1,7 +1,7 @@

declare module "http" {
import image = require("image-source");
import promises = require("promises/promises");
import promises = require("promises");
function getString(url: string): promises.Promise<string>
function getString(options: HttpRequestOptions): promises.Promise<string>

View File

@ -1,15 +1,18 @@
import image = require("image-source");
import promises = require("promises/promises");
import http = require("http");
import promises = require("promises");
import request = require("http/http-request");
// merge the exports of the request file with the exports of this file
declare var exports;
require("utils/module-merge").merge(request, exports);
/**
* Gets string from url.
*/
export function getString(arg: any): promises.Promise<string> {
var d = promises.defer<string>();
http.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
request.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
.then(r => d.resolve(r.content.toString()))
.fail(e => d.reject(e));
@ -22,7 +25,7 @@ export function getString(arg: any): promises.Promise<string> {
export function getJSON<T>(arg: any): promises.Promise<T> {
var d = promises.defer<T>();
http.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
request.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
.then(r => d.resolve(r.content.toJSON()))
.fail(e => d.reject(e));
@ -36,7 +39,7 @@ export function getJSON<T>(arg: any): promises.Promise<T> {
export function getImage(arg: any): promises.Promise<image.ImageSource> {
var d = promises.defer<image.ImageSource>();
http.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
request.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
.then(r => d.resolve(r.content.toImage()))
.fail(e => d.reject(e));

View File

@ -1,7 +1,9 @@
/**
//@private
/**
* This module is used as a native implementation for each of the underlying platforms.
* Users will not typically require it as it supports the module infrastructure.
*/
//
export declare function fromResource(name: string): any;
export declare function fromFile(path: string): any;
export declare function fromData(data: any): any;

View File

@ -1,6 +1,6 @@

declare module "image-source" {
import promises = require("promises/promises");
import promises = require("promises");
/**
* Defines the recognized image formats.

View File

@ -1,6 +1,6 @@
import app = require("application");
import native = require("image-source/image-source-native");
import promises = require("promises/promises");
import promises = require("promises");
import http = require("http");
// This is used for definition purposes only, it does not generate JavaScript for it.

View File

@ -1,4 +1,4 @@
//@private
export declare class Location {
latitude: number;
longitude: number;

View File

@ -1,5 +1,4 @@

import promises = require("promises/promises");
import promises = require("promises");
export declare enum Accuracy {
// in meters

View File

@ -1,4 +1,4 @@
import promises = require("promises/promises");
import promises = require("promises");
import timer = require("timer/timer");
import types = require("location/location-types");
import locationManagerModule = require("location/location-manager");

124
promises/promises.d.ts vendored Normal file
View File

@ -0,0 +1,124 @@
declare module "promises" {
/**
Module P: Generic Promises for TypeScript
Project, documentation, and license: https://github.com/pragmatrix/Promise
*/
/**
Returns a new "Deferred" value that may be resolved or rejected.
*/
export function defer<Value>(): Deferred<Value>;
/**
Converts a value to a resolved promise.
*/
export function resolve<Value>(v: Value): Promise<Value>;
/**
Returns a rejected promise.
*/
export function reject<Value>(err: Rejection): Promise<Value>;
/**
http://en.wikipedia.org/wiki/Anamorphism
Given a seed value, unfold calls the unspool function, waits for the returned promise to be resolved, and then
calls it again if a next seed value was returned.
All the values of all promise results are collected into the resulting promise which is resolved as soon
the last generated element value is resolved.
*/
export function unfold<Seed, Element>(unspool: (current: Seed) => {
promise: Promise<Element>;
next?: Seed;
}, seed: Seed): Promise<Element[]>;
/**
The status of a Promise. Initially a Promise is Unfulfilled and may
change to Rejected or Resolved.
Once a promise is either Rejected or Resolved, it can not change its
status anymore.
*/
export enum Status {
Unfulfilled = 0,
Rejected = 1,
Resolved = 2,
}
/**
If a promise gets rejected, at least a message that indicates the error or
reason for the rejection must be provided.
*/
export interface Rejection {
message: string;
}
/**
Both Promise<T> and Deferred<T> share these properties.
*/
export interface PromiseState<Value> {
status: Status;
result?: Value;
error?: Rejection;
}
/**
A Promise<Value> supports basic composition and registration of handlers that are called when the
promise is fulfilled.
When multiple handlers are registered with done(), fail(), or always(), they are called in the
same order.
*/
export interface Promise<Value> extends PromiseState<Value> {
/**
Returns a promise that represents a promise chain that consists of this
promise and the promise that is returned by the function provided.
The function receives the value of this promise as soon it is resolved.
If this promise fails, the function is never called and the returned promise
will also fail.
*/
then<T2>(f: (v: Value) => Promise<T2>): Promise<T2>;
then<T2>(f: (v: Value) => T2): Promise<T2>;
done(f: (v: Value) => void): Promise<Value>;
fail(f: (err: Rejection) => void): Promise<Value>;
always(f: (v?: Value, err?: Rejection) => void): Promise<Value>;
}
/**
Deferred<Value> supports the explicit resolving and rejecting of the
promise and the registration of fulfillment handlers.
A Deferred<Value> should be only visible to the function that initially sets up
an asynchronous process. Callers of that function should only see the Promise<Value> that
is returned by promise().
*/
export interface Deferred<Value> extends PromiseState<Value> {
promise(): Promise<Value>;
resolve(result?: Value): Deferred<Value>;
reject(err: Rejection): Deferred<Value>;
done(f: (v: Value) => void): Deferred<Value>;
fail(f: (err: Rejection) => void): Deferred<Value>;
always(f: (v?: Value, err?: Rejection) => void): Deferred<Value>;
}
/**
Creates a promise that gets resolved when all the promises in the argument list get resolved.
As soon one of the arguments gets rejected, the resulting promise gets rejected.
If no promises were provided, the resulting promise is immediately resolved.
*/
export function when(...promises: Promise<any>[]): Promise<any[]>;
/**
Promise Generators and Iterators.
*/
export interface Generator<E> {
(): Iterator<E>;
}
export interface Iterator<E> {
advance(): Promise<boolean>;
current: E;
}
export function generator<E>(g: () => () => Promise<E>): Generator<E>;
export function iterator<E>(f: () => Promise<E>): Iterator<E>;
/**
Iterator functions.
*/
export function each<E>(gen: Generator<E>, f: (e: E) => void): Promise<{}>;
/**
std
*/
export function isUndefined(v: any): boolean;
}