add more types to function parameters and return type

This commit is contained in:
Eddie Lau 3dd13
2016-01-19 16:44:46 +08:00
parent 2d10593dce
commit 07953672f5
14 changed files with 148 additions and 114 deletions

View File

@ -19,7 +19,7 @@ export class IonicApp {
private _scrollTime: number = 0; private _scrollTime: number = 0;
// Our component registry map // Our component registry map
private components: any = {}; private components: {[id: string] : any} = {};
constructor( constructor(
private _config: Config, private _config: Config,
@ -31,7 +31,7 @@ export class IonicApp {
* Sets the document title. * Sets the document title.
* @param {string} val Value to set the document title to. * @param {string} val Value to set the document title to.
*/ */
setTitle(val) { setTitle(val: string) {
let self = this; let self = this;
if (val !== self._title) { if (val !== self._title) {
self._title = val; self._title = val;
@ -56,7 +56,7 @@ export class IonicApp {
* it will automatically enable the app again. It's basically a fallback incase * it will automatically enable the app again. It's basically a fallback incase
* something goes wrong during a transition and the app wasn't re-enabled correctly. * something goes wrong during a transition and the app wasn't re-enabled correctly.
*/ */
setEnabled(isEnabled, duration=700) { setEnabled(isEnabled: boolean, duration: number=700) {
this._disTime = (isEnabled ? 0 : Date.now() + duration); this._disTime = (isEnabled ? 0 : Date.now() + duration);
if (duration > 32 || isEnabled) { if (duration > 32 || isEnabled) {
@ -70,7 +70,7 @@ export class IonicApp {
* Boolean if the app is actively enabled or not. * Boolean if the app is actively enabled or not.
* @return {bool} * @return {bool}
*/ */
isEnabled() { isEnabled(): boolean {
return (this._disTime < Date.now()); return (this._disTime < Date.now());
} }
@ -86,17 +86,17 @@ export class IonicApp {
* Boolean if the app is actively scrolling or not. * Boolean if the app is actively scrolling or not.
* @return {bool} * @return {bool}
*/ */
isScrolling() { isScrolling(): boolean {
return (this._scrollTime + 64 > Date.now()); return (this._scrollTime + 64 > Date.now());
} }
/** /**
* @private * @private
* Register a known component with a key, for easy lookups later. * Register a known component with a key, for easy lookups later.
* @param {TODO} id The id to use to register the component * @param {string} id The id to use to register the component
* @param {TODO} component The component to register * @param {Object} component The component to register
*/ */
register(id, component) { register(id: string, component: any) {
if (this.components[id] && this.components[id] !== component) { if (this.components[id] && this.components[id] !== component) {
//console.error('Component id "' + id + '" already registered.'); //console.error('Component id "' + id + '" already registered.');
} }
@ -106,9 +106,9 @@ export class IonicApp {
/** /**
* @private * @private
* Unregister a known component with a key. * Unregister a known component with a key.
* @param {TODO} id The id to use to unregister * @param {string} id The id to use to unregister
*/ */
unregister(id) { unregister(id: string) {
delete this.components[id]; delete this.components[id];
} }
@ -116,11 +116,12 @@ export class IonicApp {
* @private * @private
* Get a registered component with the given type (returns the first) * Get a registered component with the given type (returns the first)
* @param {Object} cls the type to search for * @param {Object} cls the type to search for
* @return the matching component, or undefined if none was found * @return {Object} the matching component, or undefined if none was found
*/ */
getRegisteredComponent(cls) { getRegisteredComponent(cls: any): any {
for(let component of this.components) { for (let key in this.components) {
if(component instanceof cls) { const component = this.components[key];
if (component instanceof cls) {
return component; return component;
} }
} }
@ -129,10 +130,10 @@ export class IonicApp {
/** /**
* @private * @private
* Get the component for the given key. * Get the component for the given key.
* @param {TODO} key TODO * @param {string} id TODO
* @return {TODO} TODO * @return {Object} TODO
*/ */
getComponent(id) { getComponent(id: string): any {
return this.components[id]; return this.components[id];
} }

View File

@ -80,11 +80,11 @@ export class Icon {
} }
@Input() @Input()
get name() { get name(): string {
return this._name; return this._name;
} }
set name(val) { set name(val: string) {
if (!(/^md-|^ios-|^logo-/.test(val))) { if (!(/^md-|^ios-|^logo-/.test(val))) {
// this does not have one of the defaults // this does not have one of the defaults
// so lets auto add in the mode prefix for them // so lets auto add in the mode prefix for them
@ -115,11 +115,11 @@ export class Icon {
} }
@Input() @Input()
get isActive() { get isActive(): boolean {
return (this._isActive === undefined || this._isActive === true || this._isActive === 'true'); return (this._isActive === undefined || this._isActive === true || this._isActive === 'true');
} }
set isActive(val) { set isActive(val: boolean) {
this._isActive = val; this._isActive = val;
this.update(); this.update();
} }
@ -158,8 +158,9 @@ export class Icon {
/** /**
* @private * @private
* @param {string} add class name
*/ */
addClass(className) { addClass(className: string) {
this._renderer.setElementClass(this._elementRef, className, true); this._renderer.setElementClass(this._elementRef, className, true);
} }

View File

@ -45,14 +45,15 @@ export class Label {
} }
} }
get text() { get text(): string {
return this._elementRef.nativeElement.textContent; return this._elementRef.nativeElement.textContent;
} }
/** /**
* @private * @private
* @param {string} add class name
*/ */
addClass(className) { addClass(className: string) {
this._renderer.setElementClass(this._elementRef, className, true); this._renderer.setElementClass(this._elementRef, className, true);
} }

View File

@ -0,0 +1,10 @@
import {App} from 'ionic/ionic';
@App({
templateUrl: 'main.html'
})
class E2EApp {
constructor() {
}
}

View File

@ -0,0 +1,13 @@
<ion-toolbar>
<ion-title>Icons</ion-title>
</ion-toolbar>
<ion-content>
<ion-input>
<ion-label>Username</ion-label>
<input type="text" value="">
</ion-input>
</ion-content>

View File

@ -26,7 +26,7 @@ export class List extends Ion {
private _virtualScrollingManager: ListVirtualScroll; private _virtualScrollingManager: ListVirtualScroll;
ele: HTMLElement; ele: HTMLElement;
itemTemplate; itemTemplate: any;
slidingGesture: ItemSlidingGesture; slidingGesture: ItemSlidingGesture;
@Input() items; @Input() items;
@ -72,7 +72,7 @@ export class List extends Ion {
/** /**
* @private * @private
*/ */
setItemTemplate(item) { setItemTemplate(item: any) {
this.itemTemplate = item; this.itemTemplate = item;
} }
@ -92,7 +92,7 @@ export class List extends Ion {
* ``` * ```
* @param {Boolean} shouldEnable whether the item-sliding should be enabled or not * @param {Boolean} shouldEnable whether the item-sliding should be enabled or not
*/ */
enableSlidingItems(shouldEnable) { enableSlidingItems(shouldEnable: boolean) {
if (this._enableSliding !== shouldEnable) { if (this._enableSliding !== shouldEnable) {
this._enableSliding = shouldEnable; this._enableSliding = shouldEnable;
@ -147,11 +147,11 @@ export class ListHeader {
this._id = id; this._id = id;
} }
public get id() { public get id(): string {
return this._id; return this._id;
} }
public set id(val) { public set id(val: string) {
this._id = val; this._id = val;
this._renderer.setElementAttribute(this._elementRef, 'id', val); this._renderer.setElementAttribute(this._elementRef, 'id', val);
} }

View File

@ -39,7 +39,7 @@ export class DisplayWhen {
} }
orientation() { orientation(): boolean {
for (let i = 0; i < this.conditions.length; i++) { for (let i = 0; i < this.conditions.length; i++) {
if (this.conditions[i] == 'portrait') { if (this.conditions[i] == 'portrait') {
@ -87,7 +87,7 @@ export class ShowWhen extends DisplayWhen {
/** /**
* @private * @private
*/ */
get hidden() { get hidden(): boolean {
return !this.isMatch; return !this.isMatch;
} }
@ -124,7 +124,7 @@ export class HideWhen extends DisplayWhen {
/** /**
* @private * @private
*/ */
get hidden() { get hidden(): boolean {
return this.isMatch; return this.isMatch;
} }

View File

@ -555,28 +555,28 @@ export class Slides extends Ion {
/** /**
* @private * @private
*/ */
getIndex() { getIndex(): number {
return this.slider.activeIndex; return this.slider.activeIndex;
} }
/** /**
* @private * @private
*/ */
getNumSlides() { getNumSlides(): number {
return this.slider.slides.length; return this.slider.slides.length;
} }
/** /**
* @private * @private
*/ */
isAtEnd() { isAtEnd(): boolean {
return this.slider.isEnd; return this.slider.isEnd;
} }
/** /**
* @private * @private
*/ */
isAtBeginning() { isAtBeginning(): boolean {
return this.slider.isBeginning; return this.slider.isBeginning;
} }

View File

@ -1,6 +1,6 @@
import {getQuerystring, assign} from '../util/util'; import {getQuerystring, assign} from '../util/util';
import {ready, windowDimensions, flushDimensionCache} from '../util/dom'; import {ready, windowDimensions, flushDimensionCache} from '../util/dom';
import {Config} from '../config/config';
/** /**
* @name Platform * @name Platform
@ -31,7 +31,7 @@ export class Platform {
private _ua: string; private _ua: string;
private _bPlt: string; private _bPlt: string;
private _onResizes: Array<any>=[]; private _onResizes: Array<any>=[];
private _readyPromise: any; private _readyPromise: Promise<any>;
private _readyResolve: any; private _readyResolve: any;
private _engineReady: any; private _engineReady: any;
private _resizeTimer: any; private _resizeTimer: any;
@ -65,7 +65,7 @@ export class Platform {
* } * }
* ``` * ```
*/ */
is(platformName) { is(platformName: string): boolean {
return (this._platforms.indexOf(platformName) > -1); return (this._platforms.indexOf(platformName) > -1);
} }
@ -88,7 +88,7 @@ export class Platform {
* } * }
* ``` * ```
*/ */
platforms() { platforms(): Array<string> {
// get the array of active platforms, which also knows the hierarchy, // get the array of active platforms, which also knows the hierarchy,
// with the last one the most important // with the last one the most important
return this._platforms; return this._platforms;
@ -112,7 +112,7 @@ export class Platform {
* @returns {object} An object with various platform info * @returns {object} An object with various platform info
* *
*/ */
versions(platformName) { versions(platformName: string): any {
if (arguments.length) { if (arguments.length) {
// get a specific platform's version // get a specific platform's version
return this._versions[platformName]; return this._versions[platformName];
@ -125,7 +125,7 @@ export class Platform {
/** /**
* @private * @private
*/ */
version() { version(): any {
for (let platformName in this._versions) { for (let platformName in this._versions) {
if (this._versions[platformName]) { if (this._versions[platformName]) {
return this._versions[platformName]; return this._versions[platformName];
@ -151,14 +151,14 @@ export class Platform {
* ``` * ```
* @returns {promise} Returns a promsie when device ready has fired * @returns {promise} Returns a promsie when device ready has fired
*/ */
ready() { ready(): Promise<any> {
return this._readyPromise; return this._readyPromise;
} }
/** /**
* @private * @private
*/ */
prepareReady(config) { prepareReady(config: Config) {
let self = this; let self = this;
function resolve() { function resolve() {
@ -185,7 +185,7 @@ export class Platform {
* [W3C: Structural markup and right-to-left text in HTML](http://www.w3.org/International/questions/qa-html-dir) * [W3C: Structural markup and right-to-left text in HTML](http://www.w3.org/International/questions/qa-html-dir)
* @param {string} dir Examples: `rtl`, `ltr` * @param {string} dir Examples: `rtl`, `ltr`
*/ */
setDir(dir, updateDocument) { setDir(dir: string, updateDocument: boolean) {
this._dir = (dir || '').toLowerCase(); this._dir = (dir || '').toLowerCase();
if (updateDocument !== false) { if (updateDocument !== false) {
document.documentElement.setAttribute('dir', dir); document.documentElement.setAttribute('dir', dir);
@ -199,7 +199,7 @@ export class Platform {
* [W3C: Structural markup and right-to-left text in HTML](http://www.w3.org/International/questions/qa-html-dir) * [W3C: Structural markup and right-to-left text in HTML](http://www.w3.org/International/questions/qa-html-dir)
* @returns {string} * @returns {string}
*/ */
dir() { dir(): string {
return this._dir; return this._dir;
} }
@ -210,7 +210,7 @@ export class Platform {
* [W3C: Structural markup and right-to-left text in HTML](http://www.w3.org/International/questions/qa-html-dir) * [W3C: Structural markup and right-to-left text in HTML](http://www.w3.org/International/questions/qa-html-dir)
* @returns {boolean} * @returns {boolean}
*/ */
isRTL() { isRTL(): boolean {
return (this._dir === 'rtl'); return (this._dir === 'rtl');
} }
@ -223,7 +223,7 @@ export class Platform {
* [W3C: Declaring language in HTML](http://www.w3.org/International/questions/qa-html-language-declarations) * [W3C: Declaring language in HTML](http://www.w3.org/International/questions/qa-html-language-declarations)
* @param {string} language Examples: `en-US`, `en-GB`, `ar`, `de`, `zh`, `es-MX` * @param {string} language Examples: `en-US`, `en-GB`, `ar`, `de`, `zh`, `es-MX`
*/ */
setLang(language, updateDocument) { setLang(language: string, updateDocument: boolean) {
this._lang = language; this._lang = language;
if (updateDocument !== false) { if (updateDocument !== false) {
document.documentElement.setAttribute('lang', language); document.documentElement.setAttribute('lang', language);
@ -237,7 +237,7 @@ export class Platform {
* [W3C: Declaring language in HTML](http://www.w3.org/International/questions/qa-html-language-declarations) * [W3C: Declaring language in HTML](http://www.w3.org/International/questions/qa-html-language-declarations)
* @returns {string} * @returns {string}
*/ */
lang() { lang(): string {
return this._lang; return this._lang;
} }
@ -277,7 +277,7 @@ export class Platform {
/** /**
* @private * @private
*/ */
setUrl(url) { setUrl(url: string) {
this._url = url; this._url = url;
this._qs = getQuerystring(url); this._qs = getQuerystring(url);
} }
@ -285,70 +285,70 @@ export class Platform {
/** /**
* @private * @private
*/ */
url(val) { url(): string {
return this._url; return this._url;
} }
/** /**
* @private * @private
*/ */
query(key) { query(key: string): string {
return (this._qs || {})[key]; return (this._qs || {})[key];
} }
/** /**
* @private * @private
*/ */
setUserAgent(userAgent) { setUserAgent(userAgent: string) {
this._ua = userAgent; this._ua = userAgent;
} }
/** /**
* @private * @private
*/ */
userAgent(val) { userAgent(): string {
return this._ua || ''; return this._ua || '';
} }
/** /**
* @private * @private
*/ */
setNavigatorPlatform(navigatorPlatform) { setNavigatorPlatform(navigatorPlatform: string) {
this._bPlt = navigatorPlatform; this._bPlt = navigatorPlatform;
} }
/** /**
* @private * @private
*/ */
navigatorPlatform(val) { navigatorPlatform(): string {
return this._bPlt || ''; return this._bPlt || '';
} }
/** /**
* @private * @private
*/ */
width() { width(): number {
return windowDimensions().width; return windowDimensions().width;
} }
/** /**
* @private * @private
*/ */
height() { height(): number {
return windowDimensions().height; return windowDimensions().height;
} }
/** /**
* @private * @private
*/ */
isPortrait() { isPortrait(): boolean {
return this.width() < this.height(); return this.width() < this.height();
} }
/** /**
* @private * @private
*/ */
isLandscape() { isLandscape(): boolean {
return !this.isPortrait(); return !this.isPortrait();
} }
@ -375,7 +375,7 @@ export class Platform {
/** /**
* @private * @private
*/ */
onResize(cb) { onResize(cb: Function) {
this._onResizes.push(cb); this._onResizes.push(cb);
} }
@ -393,28 +393,28 @@ export class Platform {
/** /**
* @private * @private
*/ */
static registry() { static registry(): any {
return platformRegistry; return platformRegistry;
} }
/** /**
* @private * @private
*/ */
static get(platformName) { static get(platformName: string): any {
return platformRegistry[platformName] || {}; return platformRegistry[platformName] || {};
} }
/** /**
* @private * @private
*/ */
static setDefault(platformName) { static setDefault(platformName: string) {
platformDefault = platformName; platformDefault = platformName;
} }
/** /**
* @private * @private
*/ */
testQuery(queryValue, queryTestValue) { testQuery(queryValue: string, queryTestValue: string): boolean {
let valueSplit = queryValue.toLowerCase().split(';'); let valueSplit = queryValue.toLowerCase().split(';');
return valueSplit.indexOf(queryTestValue) > -1; return valueSplit.indexOf(queryTestValue) > -1;
} }
@ -422,7 +422,7 @@ export class Platform {
/** /**
* @private * @private
*/ */
testUserAgent(userAgentExpression) { testUserAgent(userAgentExpression): boolean {
let rgx = new RegExp(userAgentExpression, 'i'); let rgx = new RegExp(userAgentExpression, 'i');
return rgx.test(this._ua || ''); return rgx.test(this._ua || '');
} }
@ -430,7 +430,7 @@ export class Platform {
/** /**
* @private * @private
*/ */
testNavigatorPlatform(navigatorPlatformExpression) { testNavigatorPlatform(navigatorPlatformExpression: string): boolean {
let rgx = new RegExp(navigatorPlatformExpression, 'i'); let rgx = new RegExp(navigatorPlatformExpression, 'i');
return rgx.test(this._bPlt); return rgx.test(this._bPlt);
} }
@ -438,7 +438,7 @@ export class Platform {
/** /**
* @private * @private
*/ */
matchUserAgentVersion(userAgentExpression) { matchUserAgentVersion(userAgentExpression: RegExp): any {
if (this._ua && userAgentExpression) { if (this._ua && userAgentExpression) {
let val = this._ua.match(userAgentExpression); let val = this._ua.match(userAgentExpression);
if (val) { if (val) {
@ -453,7 +453,7 @@ export class Platform {
/** /**
* @private * @private
*/ */
isPlatform(queryTestValue, userAgentExpression) { isPlatform(queryTestValue: string, userAgentExpression: string): boolean {
if (!userAgentExpression) { if (!userAgentExpression) {
userAgentExpression = queryTestValue; userAgentExpression = queryTestValue;
} }
@ -469,7 +469,7 @@ export class Platform {
/** /**
* @private * @private
*/ */
load(platformOverride?) { load(platformOverride?: string) {
let rootPlatformNode = null; let rootPlatformNode = null;
let engineNode = null; let engineNode = null;
let self = this; let self = this;
@ -561,7 +561,7 @@ export class Platform {
/** /**
* @private * @private
*/ */
matchPlatform(platformName) { matchPlatform(platformName: string): any {
// build a PlatformNode and assign config data to it // build a PlatformNode and assign config data to it
// use it's getRoot method to build up its hierarchy // use it's getRoot method to build up its hierarchy
// depending on which platforms match // depending on which platforms match
@ -581,7 +581,7 @@ export class Platform {
} }
function insertSuperset(platformNode) { function insertSuperset(platformNode: PlatformNode) {
let supersetPlaformName = platformNode.superset(); let supersetPlaformName = platformNode.superset();
if (supersetPlaformName) { if (supersetPlaformName) {
// add a platform in between two exist platforms // add a platform in between two exist platforms
@ -602,8 +602,9 @@ class PlatformNode {
public parent: PlatformNode; public parent: PlatformNode;
public child: PlatformNode; public child: PlatformNode;
public isEngine: boolean; public isEngine: boolean;
public depth: number;
constructor(platformName) { constructor(platformName: string) {
this.c = Platform.get(platformName); this.c = Platform.get(platformName);
this.isEngine = this.c.isEngine; this.isEngine = this.c.isEngine;
} }
@ -612,19 +613,19 @@ class PlatformNode {
return this.c.name; return this.c.name;
} }
settings() { settings(): any {
return this.c.settings || {}; return this.c.settings || {};
} }
superset() { superset(): any {
return this.c.superset; return this.c.superset;
} }
methods() { methods(): any {
return this.c.methods || {}; return this.c.methods || {};
} }
isMatch(p): boolean { isMatch(p: Platform): boolean {
if (p.platformOverride && !this.isEngine) { if (p.platformOverride && !this.isEngine) {
return (p.platformOverride === this.c.name); return (p.platformOverride === this.c.name);
@ -635,7 +636,7 @@ class PlatformNode {
return this.c.isMatch(p); return this.c.isMatch(p);
} }
version(p) { version(p: Platform): any {
if (this.c.versionParser) { if (this.c.versionParser) {
let v = this.c.versionParser(p); let v = this.c.versionParser(p);
if (v) { if (v) {
@ -650,7 +651,7 @@ class PlatformNode {
} }
} }
getRoot(p) { getRoot(p: Platform): PlatformNode {
if (this.isMatch(p)) { if (this.isMatch(p)) {
let parents = this.getSubsetParents(this.name()); let parents = this.getSubsetParents(this.name());
@ -677,7 +678,7 @@ class PlatformNode {
return null; return null;
} }
getSubsetParents(subsetPlatformName) { getSubsetParents(subsetPlatformName: string): Array<string> {
let platformRegistry = Platform.registry(); let platformRegistry = Platform.registry();
let parentPlatformNames = []; let parentPlatformNames = [];

View File

@ -50,7 +50,7 @@ Platform.register({
'tablet' 'tablet'
], ],
settings: { settings: {
activator: function(p) { activator: function(p: Platform): string {
// md mode defaults to use ripple activator // md mode defaults to use ripple activator
// however, under-powered devices shouldn't use ripple // however, under-powered devices shouldn't use ripple
// if this a linux device, and is using Android Chrome v36 (Android 5.0) // if this a linux device, and is using Android Chrome v36 (Android 5.0)
@ -75,10 +75,10 @@ Platform.register({
mode: 'md', mode: 'md',
scrollAssist: true, scrollAssist: true,
}, },
isMatch(p) { isMatch(p: Platform): boolean {
return p.isPlatform('android', 'android|silk'); return p.isPlatform('android', 'android|silk');
}, },
versionParser(p) { versionParser(p: Platform): any {
return p.matchUserAgentVersion(/Android (\d+).(\d+)?/); return p.matchUserAgentVersion(/Android (\d+).(\d+)?/);
} }
}); });
@ -102,10 +102,10 @@ Platform.register({
swipeBackThreshold: 40, swipeBackThreshold: 40,
tapPolyfill: isIOSDevice, tapPolyfill: isIOSDevice,
}, },
isMatch(p) { isMatch(p: Platform): boolean {
return p.isPlatform('ios', 'iphone|ipad|ipod'); return p.isPlatform('ios', 'iphone|ipad|ipod');
}, },
versionParser(p) { versionParser(p: Platform): any {
return p.matchUserAgentVersion(/OS (\d+)_(\d+)?/); return p.matchUserAgentVersion(/OS (\d+)_(\d+)?/);
} }
}); });
@ -117,8 +117,8 @@ Platform.register({
settings: { settings: {
keyboardHeight: 500, keyboardHeight: 500,
}, },
isMatch(p) { isMatch(p: Platform): boolean {
return p.isPlatform('ipad'); return p.isPlatform('ios', 'ipad');
} }
}); });
@ -128,8 +128,8 @@ Platform.register({
subsets: [ subsets: [
'phablet' 'phablet'
], ],
isMatch(p) { isMatch(p: Platform): boolean {
return p.isPlatform('iphone'); return p.isPlatform('ios', 'iphone');
} }
}); });
@ -144,10 +144,10 @@ Platform.register({
settings: { settings: {
mode: 'md', mode: 'md',
}, },
isMatch(p) { isMatch(p: Platform): boolean {
return p.isPlatform('windowsphone', 'windows phone'); return p.isPlatform('windowsphone', 'windows phone');
}, },
versionParser(p) { versionParser(p: Platform): any {
return p.matchUserAgentVersion(/Windows Phone (\d+).(\d+)?/); return p.matchUserAgentVersion(/Windows Phone (\d+).(\d+)?/);
} }
}); });
@ -167,13 +167,13 @@ Platform.register({
}); });
} }
}, },
isMatch() { isMatch(): boolean {
return !!(win.cordova || win.PhoneGap || win.phonegap); return !!(win.cordova || win.PhoneGap || win.phonegap);
} }
}); });
function isIOSDevice(p) { function isIOSDevice(p: Platform) {
// shortcut function to be reused internally // shortcut function to be reused internally
// checks navigator.platform to see if it's an actual iOS device // checks navigator.platform to see if it's an actual iOS device
// this does not use the user-agent string because it is often spoofed // this does not use the user-agent string because it is often spoofed

View File

@ -38,7 +38,7 @@ export class LocalStorage extends StorageEngine {
* Get the value of a key in LocalStorage * Get the value of a key in LocalStorage
* @param {String} key the key you want to lookup in LocalStorage * @param {String} key the key you want to lookup in LocalStorage
*/ */
get(key) { get(key: string): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
let value = window.localStorage.getItem(key); let value = window.localStorage.getItem(key);
@ -54,7 +54,7 @@ export class LocalStorage extends StorageEngine {
* @param {String} key the key you want to save to LocalStorage * @param {String} key the key you want to save to LocalStorage
* @param {Any} value the value of the key you're saving * @param {Any} value the value of the key you're saving
*/ */
set(key, value) { set(key: string, value: string): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
window.localStorage.setItem(key, value); window.localStorage.setItem(key, value);
@ -69,7 +69,7 @@ export class LocalStorage extends StorageEngine {
* Remove a key from LocalStorage * Remove a key from LocalStorage
* @param {String} key the key you want to remove from LocalStorage * @param {String} key the key you want to remove from LocalStorage
*/ */
remove(key) { remove(key: string): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
window.localStorage.removeItem(key); window.localStorage.removeItem(key);

View File

@ -68,7 +68,7 @@ export class SqlStorage extends StorageEngine {
this._tryInit(); this._tryInit();
} }
_getBackupLocation(dbFlag) { _getBackupLocation(dbFlag: number) {
switch(dbFlag) { switch(dbFlag) {
case SqlStorage.BACKUP_LOCAL: case SqlStorage.BACKUP_LOCAL:
return 2; return 2;
@ -100,7 +100,7 @@ export class SqlStorage extends StorageEngine {
* @param {array} params the additional params to use for query placeholders * @param {array} params the additional params to use for query placeholders
* @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)} * @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)}
*/ */
query(query, params=[]) { query(query, params=[]): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
this._db.transaction((tx) => { this._db.transaction((tx) => {
@ -129,7 +129,7 @@ export class SqlStorage extends StorageEngine {
* @param {string} key the key * @param {string} key the key
* @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)} * @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)}
*/ */
get(key) { get(key: string): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
@ -162,7 +162,7 @@ export class SqlStorage extends StorageEngine {
* @param {string} value The value (as a string) * @param {string} value The value (as a string)
* @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)} * @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)}
*/ */
set(key, value) { set(key: string, value: string): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
this._db.transaction(tx => { this._db.transaction(tx => {
@ -189,7 +189,7 @@ export class SqlStorage extends StorageEngine {
* @param {string} value The value (as a string) * @param {string} value The value (as a string)
* @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)} * @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)}
*/ */
remove(key) { remove(key: string): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
this._db.transaction(tx => { this._db.transaction(tx => {

View File

@ -13,13 +13,13 @@
export class Storage { export class Storage {
private _strategy: any; private _strategy: any;
constructor(strategyCls: any, options) { constructor(strategyCls: IStorageEngine, options: any) {
this._strategy = new strategyCls(options); this._strategy = new strategyCls(options);
} }
get(key) { get(key: string): any {
return this._strategy.get(key); return this._strategy.get(key);
} }
getJson(key) { getJson(key: string): any {
try { try {
return JSON.parse(this._strategy.get(key)); return JSON.parse(this._strategy.get(key));
} catch(e) { } catch(e) {
@ -27,21 +27,28 @@ export class Storage {
return null; return null;
} }
} }
set(key, value) { set(key: string, value: any) {
return this._strategy.set(key, value); return this._strategy.set(key, value);
} }
remove(key) { remove(key: string) {
return this._strategy.remove(key); return this._strategy.remove(key);
} }
query(query, params) { query(query: string, params: any) {
return this._strategy.query(query, params); return this._strategy.query(query, params);
} }
} }
export interface IStorageEngine {
new(options: any): StorageEngine;
}
/** /**
* @private * @private
*/ */
export class StorageEngine { export class StorageEngine {
constructor(options={}) {
throw Error("constructor(options={}) not implemented for this storage engine");
}
get(key, value) { get(key, value) {
throw Error("get() not implemented for this storage engine"); throw Error("get() not implemented for this storage engine");
} }