Skip to content

Commit 2016a9d

Browse files
committed
feat($browser): Adding localforage and updating README
Specifying that the project is a WIP on the readme and adding a base for using localforage
1 parent 798356a commit 2016a9d

File tree

10 files changed

+93
-2
lines changed

10 files changed

+93
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# WIP: DO NOT USE YET
2+
13
# ngx-storage
24
[![Build Status](https://travis-ci.org/phillipcurl/ngx-storage.svg?branch=master)](https://travis-ci.org/phillipcurl/ngx-storage)
35
[![npm version](https://badge.fury.io/js/ngx-storage.svg)](http://badge.fury.io/js/ngx-storage)

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@types/chai": "^3.4.30",
4848
"@types/core-js": "^0.9.32",
4949
"@types/jasmine": "^2.2.33",
50+
"@types/localforage": "0.0.32",
5051
"@types/sinon": "^1.16.27",
5152
"@types/sinon-chai": "^2.7.26",
5253
"awesome-typescript-loader": "^2.2.4",
@@ -93,5 +94,8 @@
9394
"commitizen": {
9495
"path": "node_modules/cz-conventional-changelog"
9596
}
97+
},
98+
"dependencies": {
99+
"localforage": "^1.4.3"
96100
}
97101
}

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
export * from './ngxStorage.module';
1+
export * from './services';
2+
export * from './models';
3+
4+
export * from './ngxStorage.module';

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { LocalForageItem } from './localForageItem';

src/models/localForageItem.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface LocalForageItem {
2+
key: string;
3+
value: any;
4+
};

src/ngxStorage.module.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { NgModule, ModuleWithProviders } from '@angular/core';
22
import { CommonModule } from '@angular/common';
3+
import * as localforage from 'localforage';
4+
5+
import { LocalForageService } from './services';
6+
import { LocalForageToken } from './tokens';
7+
38
import { HelloWorldComponent } from './helloWorld.component';
49

10+
export function localforageFactory(): any {
11+
return localforage;
12+
}
13+
514
@NgModule({
615
declarations: [
716
HelloWorldComponent
@@ -13,7 +22,13 @@ export class NgxStorageModule {
1322

1423
static forRoot(): ModuleWithProviders {
1524
return {
16-
ngModule: NgxStorageModule
25+
ngModule: NgxStorageModule,
26+
providers: [{
27+
provide: LocalForageToken,
28+
useFactory: localforageFactory
29+
},
30+
LocalForageService
31+
]
1732
};
1833
}
1934

src/services/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { LocalForageService } from './localForage.service';

src/services/localForage.service.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Injectable, Inject } from '@angular/core';
2+
3+
import { Observable } from 'rxjs/Observable';
4+
import 'rxjs/add/observable/fromPromise';
5+
6+
import { LocalForageItem } from './../models';
7+
import { LocalForageToken } from './../tokens';
8+
9+
@Injectable()
10+
export class LocalForageService {
11+
12+
constructor(@Inject(LocalForageToken) private localForage: any) {}
13+
14+
getItem(key: string): Observable < any > {
15+
let promise: Promise < any > = this.localForage.getItem(key);
16+
17+
return Observable.fromPromise(promise);
18+
}
19+
20+
setItem(item: LocalForageItem): Observable < any > {
21+
let promise: Promise < any > = this.localForage.setItem(item.key, item.value);
22+
23+
return Observable.fromPromise(promise);
24+
}
25+
26+
removeItem(key: string): Observable < any > {
27+
let promise: Promise < any > = this.localForage.removeItem(key);
28+
29+
return Observable.fromPromise(promise);
30+
}
31+
32+
clear(): Observable < any > {
33+
let promise: Promise < any > = this.localForage.clear();
34+
35+
return Observable.fromPromise(promise);
36+
}
37+
38+
length(): Observable < number > {
39+
let promise: Promise < any > = this.localForage.length();
40+
41+
return Observable.fromPromise(promise);
42+
}
43+
44+
key(index: number): Observable < string > {
45+
let promise: Promise < any > = this.localForage.key(index);
46+
47+
return Observable.fromPromise(promise);
48+
}
49+
50+
keys(): Observable < string[] > {
51+
let promise: Promise < any > = this.localForage.keys();
52+
53+
return Observable.fromPromise(promise);
54+
}
55+
}

src/tokens/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { LocalForageToken } from './localForage.token';

src/tokens/localForage.token.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { OpaqueToken } from '@angular/core';
2+
3+
export function LocalForageToken(): OpaqueToken {
4+
return new OpaqueToken('localforage');
5+
}

0 commit comments

Comments
 (0)