FileSystem API promisified.

This commit is contained in:
atanasovg
2014-04-30 16:32:35 +03:00
parent b604ef8b82
commit e84f305b6d
8 changed files with 145 additions and 129 deletions

View File

@ -46,7 +46,7 @@ class iOSApplication {
public init() { public init() {
UIKit.UIResponder.extends({/*TODO: Empty parameter here, needs API improvement*/}, { UIKit.UIResponder.extends({/*TODO: Empty parameter here, needs API improvement*/}, {
name: "KimeraAppDelegate", name: "TNSAppDelegate",
}).implements({ }).implements({
protocol: "UIApplicationDelegate", protocol: "UIApplicationDelegate",
implementation: { implementation: {

View File

@ -1,4 +1,6 @@
export declare class FileSystemEntity { import promises = require("promises/promises");
export declare class FileSystemEntity {
/** /**
* Gets the Date object specifying the last time this entity was modified. * Gets the Date object specifying the last time this entity was modified.
*/ */
@ -17,17 +19,17 @@
/** /**
* Gets the Folder object representing the parent of this entity. Will be null for a root folder like Documents or Temporary. * Gets the Folder object representing the parent of this entity. Will be null for a root folder like Documents or Temporary.
*/ */
public getParent(onError?: (error: any) => any): Folder; public getParent(): Folder;
/** /**
* Removes (deletes) the current Entity from the file system. * Removes (deletes) the current Entity from the file system.
*/ */
public remove(onSuccess?: () => any, onError?: (error: any) => any); public remove(): promises.Promise<any>;
/** /**
* Renames the current entity using the specified name. * Renames the current entity using the specified name.
*/ */
public rename(newName: string, onSuccess?: () => any, onError?: (error) => any); public rename(newName: string): promises.Promise<any>;
} }
export declare class File extends FileSystemEntity { export declare class File extends FileSystemEntity {
@ -49,17 +51,17 @@ export declare class File extends FileSystemEntity {
/** /**
* Gets or creates a File entity at the specified path. * Gets or creates a File entity at the specified path.
*/ */
public static fromPath(path: string, onError?: (error: any) => any): File; public static fromPath(path: string): File;
/** /**
* Reads the content of the file as a string using the specified encoding (defaults to UTF-8). * Reads the content of the file as a string using the specified encoding (defaults to UTF-8).
*/ */
public readText(onSuccess: (content: string) => any, onError?: (error: any) => any, encoding?: string); public readText(encoding?: string): promises.Promise<string>;
/** /**
* Writes the provided string to the file, using the specified encoding (defaults to UTF-8). * Writes the provided string to the file, using the specified encoding (defaults to UTF-8).
*/ */
public writeText(content: string, onSuccess?: () => any, onError?: (error: any) => any, encoding?: string); public writeText(content: string, encoding?: string): promises.Promise<any>;
} }
export declare class Folder extends FileSystemEntity { export declare class Folder extends FileSystemEntity {
@ -71,7 +73,7 @@ export declare class Folder extends FileSystemEntity {
/** /**
* Gets or creates a Folder entity at the specified path. * Gets or creates a Folder entity at the specified path.
*/ */
public static fromPath(path: string, onError?: (error: any) => any): Folder; public static fromPath(path: string): Folder;
/** /**
* Checks whether a Folder with the specified path already exists. * Checks whether a Folder with the specified path already exists.
@ -87,29 +89,29 @@ export declare class Folder extends FileSystemEntity {
/** /**
* Deletes all the files and folders (recursively), contained within this Folder. * Deletes all the files and folders (recursively), contained within this Folder.
*/ */
public clear(onSuccess?: () => any, onError?: (error: any) => any); public clear(): promises.Promise<any>;
/** /**
* Gets or creates a File entity with the specified name within this Folder. * Gets or creates a File entity with the specified name within this Folder.
*/ */
public getFile(name: string, onError?: (error: any) => any): File; public getFile(name: string): File;
/** /**
* Gets or creates a Folder entity with the specified name within this Folder. * Gets or creates a Folder entity with the specified name within this Folder.
*/ */
public getFolder(name: string, onError?: (error: any) => any): Folder; public getFolder(name: string): Folder;
/** /**
* Gets all the top-level entities residing within this folder. * Gets all the top-level entities residing within this folder.
*/ */
public getEntities(onSuccess: (entities: Array<FileSystemEntity>) => any, onError?: (error) => any); public getEntities(): promises.Promise<Array<FileSystemEntity>>;
/** /**
Enumerates all the top-level FileSystem entities residing within this folder. Enumerates all the top-level FileSystem entities residing within this folder.
The first parameter is a callback that receives the current entity. The first parameter is a callback that receives the current entity.
If the callback returns false this will mean for the iteration to stop. If the callback returns false this will mean for the iteration to stop.
*/ */
public eachEntity(onEntity: (entity: FileSystemEntity) => boolean, onError?: (error: any) => any); public eachEntity(onEntity: (entity: FileSystemEntity) => boolean);
} }
/** /**
@ -126,40 +128,3 @@ export declare module knownFolders {
*/ */
export function temp(): Folder; export function temp(): Folder;
} }
///**
// * Base class for FileReader and FileWriter APIs.
// */
//export declare class FileAccess {
// constructor(file: File);
// /**
// * Unlocks the file and allows other operations over it.
// */
// public release();
// /**
// * Gets the underlying File instance.
// */
// file: File;
//}
///**
// * Enables reading the content of a File entity.
// */
//export declare class FileReader extends FileAccess {
///**
//* Reads the content of the underlying File as a UTF8 encoded string.
//*/
//public readText(onSuccess: (content: string) => any, onError?: (error: any) => any);
//}
///**
// * Enables saving data to a File entity.
// */
//export declare class FileWriter extends FileAccess {
// /**
// * Enables saving string to a File entity.
// */
// public writeText(content: string, onSuccess?: () => any, onError?: (error: any) => any);
//}

View File

@ -1,4 +1,5 @@
import file_access_module = require("FileSystem/file_system_access"); import file_access_module = require("FileSystem/file_system_access");
import promises = require("promises/promises");
// The FileSystemAccess implementation, used through all the APIs. // The FileSystemAccess implementation, used through all the APIs.
var fileAccess; var fileAccess;
@ -54,7 +55,11 @@ export class FileSystemEntity {
/** /**
* Gets the Folder object representing the parent of this entity. Will be null for a root folder like Documents or Temporary. * Gets the Folder object representing the parent of this entity. Will be null for a root folder like Documents or Temporary.
*/ */
public getParent(onError?: (error: any) => any): Folder { public getParent(): Folder {
var onError = function (error) {
throw error;
}
var folderInfo = getFileAccess().getParent(this.path, onError); var folderInfo = getFileAccess().getParent(this.path, onError);
if (!folderInfo) { if (!folderInfo) {
return undefined; return undefined;
@ -66,35 +71,43 @@ export class FileSystemEntity {
/** /**
* Removes the current entity from the file system. * Removes the current entity from the file system.
*/ */
public remove(onSuccess?: () => any, onError?: (error: any) => any) { public remove(): promises.Promise<any> {
if (this instanceof File) { var fileAccess = getFileAccess();
getFileAccess().deleteFile(this.path, onSuccess, onError); var promise = promises.defer<any>();
} else if (this instanceof Folder) {
getFileAccess().deleteFolder(this.path, this[isKnownProperty], onSuccess, onError); var localSucces = function () {
promise.resolve();
} }
var localError = function (error: any) {
promise.reject(error);
}
if (this instanceof File) {
fileAccess.deleteFile(this.path, localSucces, localError);
} else if (this instanceof Folder) {
fileAccess.deleteFolder(this.path, this[isKnownProperty], localSucces, localError);
}
return promise.promise();
} }
/** /**
* Renames the current entity using the specified name. * Renames the current entity using the specified name.
*/ */
public rename(newName: string, onSuccess?: () => any, onError?: (error: any) => any) { public rename(newName: string): promises.Promise<any> {
var deferred = promises.defer<any>();
if (this instanceof Folder) { if (this instanceof Folder) {
if (this[isKnownProperty]) { if (this[isKnownProperty]) {
if (onError) { deferred.reject(new Error("Cannot rename known folder."));
onError(new Error("Cannot rename known folder.")); return deferred.promise();
}
return;
} }
} }
var parentFolder = this.getParent(); var parentFolder = this.getParent();
if (!parentFolder) { if (!parentFolder) {
if (onError) { deferred.reject(new Error("No parent folder."));
onError(new Error("No parent folder.")); return deferred.promise();
}
return;
} }
var fileAccess = getFileAccess(); var fileAccess = getFileAccess();
@ -110,12 +123,16 @@ export class FileSystemEntity {
that[extensionProperty] = fileAccess.getFileExtension(newPath); that[extensionProperty] = fileAccess.getFileExtension(newPath);
} }
if (onSuccess) { deferred.resolve();
onSuccess();
}
} }
fileAccess.rename(this.path, newPath, localSucceess, onError); var localError = function (error) {
deferred.reject(error);
}
fileAccess.rename(this.path, newPath, localSucceess, localError);
return deferred.promise();
} }
/** /**
@ -152,7 +169,11 @@ export class File extends FileSystemEntity {
/** /**
* Gets the File instance associated with the specified path. * Gets the File instance associated with the specified path.
*/ */
public static fromPath(path: string, onError?: (error: any) => any) { public static fromPath(path: string) {
var onError = function (error) {
throw error;
}
var fileInfo = getFileAccess().getFile(path, onError); var fileInfo = getFileAccess().getFile(path, onError);
if (!fileInfo) { if (!fileInfo) {
return undefined; return undefined;
@ -185,55 +206,53 @@ export class File extends FileSystemEntity {
/** /**
* Reads the content of the file as a string using the specified encoding (defaults to UTF-8). * Reads the content of the file as a string using the specified encoding (defaults to UTF-8).
*/ */
public readText(onSuccess: (content: string) => any, onError?: (error: any) => any, encoding?: string) { public readText(encoding?: string): promises.Promise<string> {
if (!onSuccess) {
return;
}
this.checkAccess(); this.checkAccess();
var deferred = promises.defer<string>();
this[fileLockedProperty] = true; this[fileLockedProperty] = true;
var that = this; var that = this;
var localSuccess = function (content: string) { var localSuccess = function (content: string) {
that[fileLockedProperty] = false; that[fileLockedProperty] = false;
onSuccess(content); deferred.resolve(content);
} }
var localError = function (error) { var localError = function (error) {
that[fileLockedProperty] = false; that[fileLockedProperty] = false;
if (onError) { deferred.reject(error);
onError(error);
}
} }
// TODO: Asyncronous // TODO: Asyncronous
getFileAccess().readText(this.path, localSuccess, localError, encoding); getFileAccess().readText(this.path, localSuccess, localError, encoding);
return deferred.promise();
} }
/** /**
* Writes the provided string to the file, using the specified encoding. Any previous content will be overwritten. * Writes the provided string to the file, using the specified encoding. Any previous content will be overwritten.
*/ */
public writeText(content: string, onSuccess?: () => any, onError?: (error: any) => any, encoding?: string) { public writeText(content: string, encoding?: string): promises.Promise<any> {
this.checkAccess(); this.checkAccess();
var deferred = promises.defer<string>();
this[fileLockedProperty] = true; this[fileLockedProperty] = true;
var that = this; var that = this;
var localSuccess = function () { var localSuccess = function () {
that[fileLockedProperty] = false; that[fileLockedProperty] = false;
if (onSuccess) { deferred.resolve();
onSuccess();
}
}; };
var localError = function (error) { var localError = function (error) {
that[fileLockedProperty] = false; that[fileLockedProperty] = false;
if (onError) { deferred.reject(error);
onError(error);
}
}; };
// TODO: Asyncronous // TODO: Asyncronous
getFileAccess().writeText(this.path, content, localSuccess, localError, encoding); getFileAccess().writeText(this.path, content, localSuccess, localError, encoding);
return deferred.promise();
} }
private checkAccess() { private checkAccess() {
@ -250,7 +269,11 @@ export class Folder extends FileSystemEntity {
/** /**
* Attempts to access a Folder at the specified path and creates a new Folder if there is no existing one. * Attempts to access a Folder at the specified path and creates a new Folder if there is no existing one.
*/ */
public static fromPath(path: string, onSuccess: (folder: Folder) => any, onError?: (error: any) => any) { public static fromPath(path: string): Folder {
var onError = function (error) {
throw error;
}
var folderInfo = getFileAccess().getFolder(path, onError); var folderInfo = getFileAccess().getFolder(path, onError);
if (!folderInfo) { if (!folderInfo) {
return undefined; return undefined;
@ -283,8 +306,19 @@ export class Folder extends FileSystemEntity {
/** /**
* Removes all the files and folders (recursively), contained within this Folder. * Removes all the files and folders (recursively), contained within this Folder.
*/ */
public clear(onSuccess?: () => any, onError?: (error: any) => any) { public clear(): promises.Promise<any> {
var deferred = promises.defer<any>();
var onSuccess = function () {
deferred.resolve();
}
var onError = function (error) {
deferred.reject(error);
}
getFileAccess().emptyFolder(this.path, onSuccess, onError); getFileAccess().emptyFolder(this.path, onSuccess, onError);
return deferred.promise();
} }
/** /**
@ -297,10 +331,14 @@ export class Folder extends FileSystemEntity {
/** /**
* Attempts to open a File with the specified name within this Folder and optionally creates a new File if there is no existing one. * Attempts to open a File with the specified name within this Folder and optionally creates a new File if there is no existing one.
*/ */
public getFile(name: string, onError?: (error: any) => any): File { public getFile(name: string): File {
var fileAccess = getFileAccess(); var fileAccess = getFileAccess();
var path = fileAccess.concatPath(this.path, name); var path = fileAccess.concatPath(this.path, name);
var onError = function (error) {
throw error;
}
var fileInfo = fileAccess.getFile(path, onError); var fileInfo = fileAccess.getFile(path, onError);
if (!fileInfo) { if (!fileInfo) {
return undefined; return undefined;
@ -312,10 +350,14 @@ export class Folder extends FileSystemEntity {
/** /**
* Attempts to open a Folder with the specified name within this Folder and optionally creates a new Folder if there is no existing one. * Attempts to open a Folder with the specified name within this Folder and optionally creates a new Folder if there is no existing one.
*/ */
public getFolder(name: string, onError?: (error: any) => any): Folder { public getFolder(name: string): Folder {
var fileAccess = getFileAccess(); var fileAccess = getFileAccess();
var path = fileAccess.concatPath(this.path, name); var path = fileAccess.concatPath(this.path, name);
var onError = function (error) {
throw error;
}
var folderInfo = fileAccess.getFolder(path, onError); var folderInfo = fileAccess.getFolder(path, onError);
if (!folderInfo) { if (!folderInfo) {
return undefined; return undefined;
@ -327,26 +369,33 @@ export class Folder extends FileSystemEntity {
/** /**
* Gets all the top-level FileSystem entities residing within this Folder. * Gets all the top-level FileSystem entities residing within this Folder.
*/ */
public getEntities(onSuccess: (files: Array<FileSystemEntity>) => any, onError?: (error: any) => any) { public getEntities(): promises.Promise<Array<FileSystemEntity>> {
var localSuccess = function (fileInfos: Array<{ path: string; name: string; extension: string }>) { var deferred = promises.defer<Array<FileSystemEntity>>();
if (onSuccess) {
var entities = new Array<FileSystemEntity>();
var i,
path: string,
entity: FileSystemEntity;
for (i = 0; i < fileInfos.length; i++) { var onSuccess = function (fileInfos: Array<{ path: string; name: string; extension: string }>) {
if (fileInfos[i].extension) { var entities = new Array<FileSystemEntity>();
entities.push(createFile(fileInfos[i])); var i,
} else { path: string,
entities.push(createFolder(fileInfos[i])); entity: FileSystemEntity;
}
for (i = 0; i < fileInfos.length; i++) {
if (fileInfos[i].extension) {
entities.push(createFile(fileInfos[i]));
} else {
entities.push(createFolder(fileInfos[i]));
} }
onSuccess(entities);
} }
deferred.resolve(entities);
} }
getFileAccess().getEntities(this.path, localSuccess, onError);
var onError = function (error) {
throw error;
}
getFileAccess().getEntities(this.path, onSuccess, onError);
return deferred.promise();
} }
/** /**
@ -354,12 +403,12 @@ export class Folder extends FileSystemEntity {
The first parameter is a callback that receives the current entity. The first parameter is a callback that receives the current entity.
If the callback returns false this will mean for the iteration to stop. If the callback returns false this will mean for the iteration to stop.
*/ */
public eachEntity(onEntity: (entity: FileSystemEntity) => boolean, onError?: (error: any) => any) { public eachEntity(onEntity: (entity: FileSystemEntity) => boolean) {
if (!onEntity) { if (!onEntity) {
return; return;
} }
var localSuccess = function (fileInfo: { path: string; name: string; extension: string }): boolean { var onSuccess = function (fileInfo: { path: string; name: string; extension: string }): boolean {
var entity; var entity;
if (fileInfo.extension) { if (fileInfo.extension) {
entity = createFile(fileInfo); entity = createFile(fileInfo);
@ -369,7 +418,12 @@ export class Folder extends FileSystemEntity {
return onEntity(entity); return onEntity(entity);
} }
getFileAccess().eachEntity(this.path, localSuccess, onError);
var onError = function (error) {
throw error;
}
getFileAccess().eachEntity(this.path, onSuccess, onError);
} }
} }

View File

@ -1,10 +1,9 @@
import types = require("Location/location_types"); import types = require("Location/location_types");
import app_module = require("Application/application"); import app_module = require("Application/application");
// merge types // merge the exports of the types module with the exports of this file
declare var exports; declare var exports;
exports.Location = types.Location; require("Utils/module_merge").merge(types, exports);
exports.Accuracy = types.Accuracy;
export class LocationManager { export class LocationManager {
// in meters // in meters

View File

@ -4,12 +4,13 @@
HIGH, HIGH,
} }
export declare class LocationRegion { // For future usage
public latitude: number; //export declare class LocationRegion {
public longitude: number; // public latitude: number;
// public longitude: number;
public raduis: number; // radius in meters // public raduis: number; // radius in meters
} //}
export declare class Location { export declare class Location {
latitude: number; latitude: number;

View File

@ -1,11 +1,8 @@
 import types = require("Location/location_types");
import types = require("Location/location_types");
// merge types // merge the exports of the types module with the exports of this file
declare var exports; declare var exports;
exports.Location = types.Location; require("Utils/module_merge").merge(types, exports);
exports.Accuracy = types.Accuracy;
exports.Options = types.Options;
export class LocationManager { export class LocationManager {

View File

@ -23,7 +23,7 @@ export class Location {
public ios: any; // iOS native location public ios: any; // iOS native location
} }
export declare class Options { export class Options {
/** /**
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH * Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
*/ */

View File

@ -169,7 +169,7 @@ export interface Deferred<Value> extends PromiseState<Value> {
promise(): Promise<Value>; promise(): Promise<Value>;
/// Resolve the promise. /// Resolve the promise.
resolve(result: Value): Deferred<Value>; resolve(result?: Value): Deferred<Value>;
/// Reject the promise. /// Reject the promise.
reject(err: Rejection): Deferred<Value>; reject(err: Rejection): Deferred<Value>;