Skip to content

Commit

Permalink
Merge pull request #270 from mean-expert-official/development
Browse files Browse the repository at this point in the history
2.1.0-rc.6
  • Loading branch information
jonathan-casarrubias authored Dec 21, 2016
2 parents aa107d3 + 81405ee commit 6ceca90
Show file tree
Hide file tree
Showing 25 changed files with 383 additions and 132 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

This file is created to keep history of the LoopBack SDK Builder, it does not consider or keeps any history of its parent module `loopback-sdk-angular`.

## Release 2.1.0-rc.6

- Milestone Details: https://github.com/mean-expert-official/loopback-sdk-builder/milestone/28?closed=1

- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/268
- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/266
- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/265
- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/264
- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/262
- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/151

## Release 2.1.0-rc.5

- Milestone Details: https://github.com/mean-expert-official/loopback-sdk-builder/milestone/27?closed=1
Expand Down
15 changes: 12 additions & 3 deletions lib/angular2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ module.exports = function generate(ctx) {
* STORAGE
*/
{
template: './shared/storage/internal.storage.ts',
output: '/storage/internal.storage.ts',
template: './shared/storage/storage.swaps.ts',
output: '/storage/storage.swaps.ts',
params: {}
}
];
Expand All @@ -159,6 +159,11 @@ module.exports = function generate(ctx) {
output: '/storage/cookie.browser.ts',
params: {}
});
schema.push({
template: './shared/storage/storage.browser.ts',
output: '/storage/storage.browser.ts',
params: {}
});
}
// Add Server Specific Code
if (ctx.driver === 'ng2universal') {
Expand Down Expand Up @@ -470,7 +475,7 @@ module.exports = function generate(ctx) {
{ module: 'LoopBackAuth', from: './services/core/auth.service'},
{ module: 'LoggerService', from: './services/custom/logger.service'},
{ module: 'SDKModels', from: './services/custom/SDKModels'},
{ module: 'InternalStorage', from: './storage/internal.storage'},
{ module: 'InternalStorage, SDKStorage', from: './storage/storage.swaps'},
{ module: 'HttpModule', from: '@angular/http'},
{ module: 'CommonModule', from: '@angular/common'},
{ module: 'NgModule, ModuleWithProviders', from: '@angular/core'}
Expand All @@ -479,13 +484,15 @@ module.exports = function generate(ctx) {
switch (driver) {
case 'ng2web':
imports.push({ module: 'CookieBrowser', from: './storage/cookie.browser'});
imports.push({ module: 'StorageBrowser', from: './storage/storage.browser'});
if (isIo === 'enabled') {
imports.push({ module: 'SocketBrowser', from: './sockets/socket.browser'});
}
break;
case 'ng2universal':
imports.push({ module: 'CookieBrowser', from: './storage/cookie.browser'});
imports.push({ module: 'CookieNode', from: './storage/cookie.node'});
imports.push({ module: 'StorageBrowser', from: './storage/storage.browser'});
if (isIo === 'enabled') {
imports.push({ module: 'SocketBrowser', from: './sockets/socket.browser'});
imports.push({ module: 'SocketNode', from: './sockets/socket.node'});
Expand Down Expand Up @@ -528,6 +535,7 @@ module.exports = function generate(ctx) {
case 'browser':
if (driver === 'ng2web' || driver === 'ng2universal') {
imports.push('{ provide: InternalStorage, useClass: CookieBrowser }');
imports.push('{ provide: SDKStorage, useClass: StorageBrowser }');
if (isIo === 'enabled') {
imports.push('{ provide: SocketDriver, useClass: SocketBrowser }');
}
Expand All @@ -544,6 +552,7 @@ module.exports = function generate(ctx) {
case 'nativescript':
if (driver === 'ng2native') {
imports.push('{ provide: InternalStorage, useClass: StorageNative }');
imports.push('{ provide: SDKStorage, useClass: StorageNative }');
if (isIo === 'enabled') {
imports.push('{ provide: SocketDriver, useClass: SocketNative }');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/angular2/shared/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
* // App Root
* import { AppComponent } from './app.component';
* // Feature Modules
* import { SDKModule } from './shared/sdk/sdk.module';
* import { SDK[Browser|Node|Native]Module } from './shared/sdk/sdk.module';
* // Import Routing
* import { routing } from './app.routing';
* @NgModule({
* imports: [
* BrowserModule,
* routing,
* SDKModule.forRoot()
* SDK[Browser|Node|Native]Module.forRoot()
* ],
* declarations: [ AppComponent ],
* bootstrap: [ AppComponent ]
Expand Down
8 changes: 4 additions & 4 deletions lib/angular2/shared/models/flref.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class FireLoopRef<T> {
}
sbj.next(data);
};
this.socket.on(nowEvent, pullNow);
this.socket.onZone(nowEvent, pullNow);
return sbj.asObservable();
}
/**
Expand All @@ -176,12 +176,12 @@ export class FireLoopRef<T> {
**/
private broadcasts(event: string, request: any): Observable<T> {
let sbj: Subject<T> = new Subject<T>();
this.socket.on(
this.socket.onZone(
`${event}.broadcast.announce.${ this.id }`,
(res: T) =>
this.socket.emit(`${event}.broadcast.request.${ this.id }`, request)
);
this.socket.on(`${ event }.broadcast.${ this.id }`, (data) => sbj.next(data));
this.socket.onZone(`${ event }.broadcast.${ this.id }`, (data) => sbj.next(data));
return sbj.asObservable();
}
/**
Expand All @@ -203,7 +203,7 @@ export class FireLoopRef<T> {
parent: this.parent && this.parent.instance ? this.parent.instance : null
};
this.socket.emit(event, config);
this.socket.on(`${ this.model.getModelName() }.value.result.${ this.id }`, (res: any) =>
this.socket.onZone(`${ this.model.getModelName() }.value.result.${ this.id }`, (res: any) =>
subject.next(res.error ? Observable.throw(res.error) : res)
);
return subject.asObservable();
Expand Down
2 changes: 1 addition & 1 deletion lib/angular2/shared/services/core/auth.ejs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
declare var Object: any;
import { Injectable, Inject } from '@angular/core';
import { InternalStorage } from '../../storage/internal.storage';
import { InternalStorage } from '../../storage/storage.swaps';
import { SDKToken, AccessToken } from '../../models/BaseModels';

/**
Expand Down
5 changes: 2 additions & 3 deletions lib/angular2/shared/services/core/base.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,16 @@ export abstract class BaseLoopBackApi {
body = postBody;
}
// Separate filter object from url params
let filter: string = '';
if (urlParams.filter) {
filter = `?filter=${ encodeURI(JSON.stringify(urlParams.filter))}`;
headers.append('filter', JSON.stringify(urlParams.filter));
delete urlParams.filter;
}

this.searchParams.setJSON(urlParams);
let request: Request = new Request({
headers : headers,
method : method,
url : `${requestUrl}${filter}`,
url : requestUrl,
search : Object.keys(urlParams).length > 0
? this.searchParams.getURLSearchParams() : null,
body : body ? JSON.stringify(body) : undefined
Expand Down
19 changes: 14 additions & 5 deletions lib/angular2/shared/services/core/realtime.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ export class RealTime {
@Inject(SDKModels) protected models: SDKModels,
@Inject(LoopBackAuth) protected auth: LoopBackAuth,
@Inject(JSONSearchParams) protected searchParams: JSONSearchParams
) {
this.socket = this.getConnection();
this.IO = new IO(this.socket);
this.FireLoop = new FireLoop(this.socket, models);
) {}

disconnect(): void {
this.connected = false;
this.IO = null;
this.FireLoop = null;
this.connections.disconnect();
}

getConnection(): void {
Expand All @@ -41,11 +44,17 @@ export class RealTime {
if (this.connected) {
subject.next();
} else {
this.socket = this.getConnection();
this.IO = new IO(this.socket);
this.FireLoop = new FireLoop(this.socket, this.models);
this.socket.on('connect', () => {
this.connected = true;
subject.next();
});
this.socket.on('disconnect', () => this.connected = false);
this.socket.on('disconnect', () => {
subject.complete();
this.disconnect();
});
}
return subject.asObservable();
}
Expand Down
45 changes: 36 additions & 9 deletions lib/angular2/shared/sockets/connections.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* tslint:disable */
import { Injectable, Inject } from '@angular/core';
import { Injectable, Inject, NgZone } from '@angular/core';
import { SocketDriver } from './socket.driver';
import { AccessToken } from '../models';
/**
Expand All @@ -14,16 +14,25 @@ import { AccessToken } from '../models';
@Injectable()
export class SocketConnections {
private connections: any = {};
private configured: boolean = false;
constructor(@Inject(SocketDriver) private driver: SocketDriver) {}
private configured: boolean = false;
constructor(
@Inject(SocketDriver) private driver: SocketDriver,
@Inject(NgZone) private zone: NgZone
) { }
getHandler(url: string, token: AccessToken) {
console.log('Getting handler for socket connections');
if (!this.connections[url]) {
console.log('Creating a new connection with: ', url);
let config: any = { log: false, secure: false, forceWebsockets: true };
let config: any = { log: false, secure: false, forceNew: true, forceWebsockets: true };
this.connections[url] = this.driver.connect(url, config);
this.connections[url].onZone = ((event: string, handler: Function) => {
this.connections[url].on(event, (data: any) => {
this.zone.run(() => handler(data));
});
});
this.connections[url].on('connect', () => {
if (!this.configured)
this.setupConnection(url, token, config);
if (!this.configured)
this.setupConnection(url, token, config);
});
let forceConfig: any = setInterval(() => {
if (!this.configured && this.connections[url].connected) {
Expand All @@ -40,17 +49,35 @@ export class SocketConnections {
return this.connections[url];
}

public disconnect() {
Object.keys(this.connections).forEach((connKey) => {
if (this.connections[connKey].connected) {
this.connections[connKey].disconnect();
}
});
this.connections = {};
this.configured = false;
}

private setupConnection(url: string, token: AccessToken, config: any): void {
this.configured = true;
console.log('Connected to %s', url);
if(token.id) {
if (token.id) {
console.log('Emitting authentication', token.id);
this.connections[url].emit('authentication', token);
}
this.connections[url].on('unauthorized', (res: any) => console.error('Unauthenticated', res));
setInterval(() => this.connections[url].emit('lb-ping'), 15000);
let heartbeater: any = setInterval(() => {
if (this.connections[url]) {
this.connections[url].emit('lb-ping');
} else {
clearInterval(heartbeater);
}
}, 15000);
this.connections[url].on('lb-pong', (data: any) => console.info('Heartbeat: ', data));
this.connections[url].on('disconnect', (data: any) => {
console.info('Unexpected disconnection from IO - Socket IO will try to reconnect');
this.disconnect();
console.info('Disconnected from WebSocket server');
});
}
}
2 changes: 1 addition & 1 deletion lib/angular2/shared/storage/cookie.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class CookieBrowser {

set(key: string, value: any, expires?: Date) {
this.cookies[key] = value;
let cookie = `${key}=${value}${expires ? `; expires=${ expires.toUTCString() }` : ''}`;
let cookie = `${key}=${value}; path=/${expires ? `; expires=${ expires.toUTCString() }` : ''}`;
window.document.cookie = cookie;
}

Expand Down
13 changes: 0 additions & 13 deletions lib/angular2/shared/storage/internal.storage.ts

This file was deleted.

37 changes: 37 additions & 0 deletions lib/angular2/shared/storage/storage.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* tslint:disable */
import { Injectable } from '@angular/core';
/**
* @module StorageBrowser
* @author Jonathan Casarrubias
* @license MIT
* @description
* Stand-alone cookie service for browsers
**/
@Injectable()
export class StorageBrowser {
set(key: string, value: any) {
localStorage.setItem(
key,
typeof value === 'object' ? JSON.stringify(value) : value
);
}
get(key: string): any {
let data: string = localStorage.getItem(key);
return this.isJSON(data) ? JSON.parse(data) : data;
}
remove(key: string): any {
if (localStorage[key]) {
localStorage.removeItem(key);
} else {
console.log('Trying to remove unexisting key: ', key);
}
}
private isJSON(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
}
16 changes: 14 additions & 2 deletions lib/angular2/shared/storage/storage.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import { Injectable } from '@angular/core';
@Injectable()
export class StorageNative {
set(key: string, value: any) {
AppSettings.setString(key, String(value));
AppSettings.setString(
key,
String(typeof value === 'object' ? JSON.stringify(value) : value)
);
}
get(key: string): any {
return AppSettings.getString(key);
let data: string = AppSettings.getString(key);
return this.isJSON(data) ? JSON.parse(data) : data;
}
remove(key: string): any {
if (AppSettings.hasKey(key)) {
Expand All @@ -17,4 +21,12 @@ export class StorageNative {
console.log('Trying to remove unexisting key: ', key);
}
}
private isJSON(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
}
Loading

0 comments on commit 6ceca90

Please sign in to comment.