Skip to content

Commit 640b438

Browse files
Merge pull request #483 from mean-expert-official/development
Development
2 parents 948c2a3 + 7bde747 commit 640b438

File tree

12 files changed

+162
-71
lines changed

12 files changed

+162
-71
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
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`
44

5+
## Release 2.1.0-rc.13.5
6+
7+
- Milestone Details: https://github.com/mean-expert-official/loopback-sdk-builder/milestone/47?closed=1
8+
9+
- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/482
10+
- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/481
11+
- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/480
12+
- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/475
13+
514
## Release 2.1.0-rc.13.4
615

716
- Replaces Release 2.1.0-rc.13.3

lib/angular2/index.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ module.exports = function generate(ctx) {
433433
method.name.match(/(^createMany$|^find)/g) ||
434434
(
435435
typeof method.returns === 'object' &&
436-
(method.returns.type === 'array' || Array.isArray(method.returns.type))
436+
(String(method.returns.type).toLowerCase() === 'array' || Array.isArray(method.returns.type))
437437
)
438438
) type = `${model.name}[]`;
439439
if (method.name.match(/(^create$|upsert|^findBy|^findOne$)/g)) type = model.name;
@@ -776,6 +776,31 @@ module.exports = function generate(ctx) {
776776
function paramIsContext(param) {
777777
return (typeof param.http !== 'undefined' && typeof param.http.source !== 'undefined' && param.http.source === 'context');
778778
}
779+
/**
780+
* @method paramIsBody
781+
* @description
782+
* Testing if the param is a http.body or form
783+
*/
784+
function paramIsBody(param) {
785+
return (typeof param.http !== 'undefined' && typeof param.http.source !== 'undefined' && (param.http.source == 'body' || param.http.source == 'form'));
786+
}
787+
/**
788+
* @method paramIsQuery
789+
* @description
790+
* Testing if the param is a http.query or form
791+
*/
792+
function paramIsQuery(param) {
793+
return (
794+
(
795+
typeof param.http === 'undefined' && // Query is default, if http is not defined we treat it as query param
796+
(param.arg && !param.arg.match(/(^id$|fk|^file$|container)/)) // But only if it is not id, fk, file or container
797+
)
798+
||
799+
(
800+
typeof param.http !== 'undefined' && typeof param.http.source !== 'undefined' && param.http.source == 'query'
801+
)
802+
);
803+
}
779804
/**
780805
* @method buildPostBody
781806
* @description
@@ -786,11 +811,11 @@ module.exports = function generate(ctx) {
786811
if (Array.isArray(postData)) {
787812
postData = postData.filter(param => {
788813
// Filter out route params and function params
789-
if (paramIsRoute(param) || paramIsFunction(param)) {
814+
if (paramIsRoute(param) || paramIsFunction(param) || paramIsContext(param) || paramIsQuery(param)) {
790815
return false
791816
}
792817
// Make sure the param is body or form data
793-
return param.http && (param.http.source == 'body' || param.http.source == 'form')
818+
return paramIsBody(param);
794819
})
795820
if (postData.length > 0) {
796821
output.push('');
@@ -826,11 +851,11 @@ module.exports = function generate(ctx) {
826851
// filter params that should not go over url query string
827852
urlParams = urlParams.filter(param => {
828853
// Filter out route params and function params
829-
if (paramIsRoute(param) || paramIsFunction(param) || paramIsContext(param)) {
854+
if (paramIsRoute(param) || paramIsFunction(param) || paramIsContext(param) || paramIsBody(param)) {
830855
return false
831856
}
832857
// Filter out body params
833-
return (!param.http || param.http.source != 'body')
858+
return paramIsQuery(param);
834859
});
835860
if (model.isUser && methodName === 'logout')
836861
output.push(` _urlParams.access_token = this.auth.getAccessTokenId();`);
@@ -850,7 +875,10 @@ module.exports = function generate(ctx) {
850875
let output = [];
851876
if (routeParams) {
852877
routeParams = routeParams.filter((param) => {
853-
return paramIsRoute(param) && !paramIsFunction(param)
878+
if (paramIsQuery(param) || paramIsFunction(param) || paramIsContext(param) || paramIsBody(param)) {
879+
return false
880+
}
881+
return paramIsRoute(param)
854882
});
855883
if (routeParams.length > 0) {
856884
output.push('');

lib/angular2/shared/models/flref.ts

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export class FireLoopRef<T> {
1818
private instance: any;
1919
// Model Childs
2020
private childs: any = {};
21+
// Disposable Events
22+
private disposable: { [key: string]: any } = {};
2123
/**
2224
* @method constructor
2325
* @param {any} model The model we want to create a reference
@@ -37,8 +39,8 @@ export class FireLoopRef<T> {
3739
private relationship: string = null
3840
) {
3941
this.socket.emit(
40-
`Subscribe.${ !parent ? model.getModelName() : parent.model.getModelName() }`,
41-
{ id : this.id, scope: model.getModelName(), relationship: relationship }
42+
`Subscribe.${!parent ? model.getModelName() : parent.model.getModelName()}`,
43+
{ id: this.id, scope: model.getModelName(), relationship: relationship }
4244
);
4345
return this;
4446
}
@@ -54,9 +56,13 @@ export class FireLoopRef<T> {
5456
* }
5557
**/
5658
public dispose(): void {
57-
this.operation('dispose', {})
58-
.subscribe()
59-
.unsubscribe();
59+
const subscription = this.operation('dispose', {}).subscribe(() => {
60+
Object.keys(this.disposable).forEach((channel: string) => {
61+
this.socket.removeListener(channel, this.disposable[channel]);
62+
this.socket.removeAllListeners(channel);
63+
});
64+
subscription.unsubscribe();
65+
});
6066
}
6167
/**
6268
* @method upsert
@@ -116,9 +122,9 @@ export class FireLoopRef<T> {
116122
public onRemote(method: string): Observable<any> {
117123
let event: string = 'remote';
118124
if (!this.relationship) {
119-
event = `${ this.model.getModelName() }.${event}`;
125+
event = `${this.model.getModelName()}.${event}`;
120126
} else {
121-
event = `${ this.parent.model.getModelName() }.${ this.relationship }.${event}`;
127+
event = `${this.parent.model.getModelName()}.${this.relationship}.${event}`;
122128
}
123129
return this.broadcasts(event, {});
124130
}
@@ -141,10 +147,10 @@ export class FireLoopRef<T> {
141147
}
142148
let request: any;
143149
if (!this.relationship) {
144-
event = `${ this.model.getModelName() }.${event}`;
150+
event = `${this.model.getModelName()}.${event}`;
145151
request = { filter };
146152
} else {
147-
event = `${ this.parent.model.getModelName() }.${ this.relationship }.${event}`;
153+
event = `${this.parent.model.getModelName()}.${this.relationship}.${event}`;
148154
request = { filter, parent: this.parent.instance };
149155
}
150156
if (event.match(/(value|change|stats)/)) {
@@ -182,7 +188,7 @@ export class FireLoopRef<T> {
182188
**/
183189
public make(instance: any): FireLoopRef<T> {
184190
let reference: FireLoopRef<T> = new FireLoopRef<T>(this.model, this.socket);
185-
reference.instance = instance;
191+
reference.instance = instance;
186192
return reference;
187193
}
188194
/**
@@ -200,15 +206,15 @@ export class FireLoopRef<T> {
200206
let settings: any = this.model.getModelDefinition().relations[relationship];
201207
// Verify the relationship actually exists
202208
if (!settings) {
203-
throw new Error(`Invalid model relationship ${ this.model.getModelName() } <-> ${ relationship }, verify your model settings.`);
209+
throw new Error(`Invalid model relationship ${this.model.getModelName()} <-> ${relationship}, verify your model settings.`);
204210
}
205211
// Verify if the relationship model is public
206212
if (settings.model === '') {
207-
throw new Error(`Relationship model is private, cam't use ${ relationship } unless you set your model as public.`);
213+
throw new Error(`Relationship model is private, cam't use ${relationship} unless you set your model as public.`);
208214
}
209215
// Lets get a model reference and add a reference for all of the models
210-
let model: any = this.model.models.get(settings.model);
211-
model.models = this.model.models;
216+
let model: any = this.model.models.get(settings.model);
217+
model.models = this.model.models;
212218
// If everything goes well, we will store a child reference and return it.
213219
this.childs[relationship] = new FireLoopRef<T>(model, this.socket, this, relationship);
214220
return this.childs[relationship];
@@ -224,8 +230,8 @@ export class FireLoopRef<T> {
224230
private pull(event: string, request: any): Observable<T> {
225231
let sbj: Subject<T> = new Subject<T>();
226232
let that: FireLoopRef<T> = this;
227-
let nowEvent: any = `${event}.pull.requested.${ this.id }`;
228-
this.socket.emit(`${event}.pull.request.${ this.id }`, request);
233+
let nowEvent: any = `${event}.pull.requested.${this.id}`;
234+
this.socket.emit(`${event}.pull.request.${this.id}`, request);
229235
function pullNow(data: any) {
230236
if (that.socket.removeListener) {
231237
that.socket.removeListener(nowEvent, pullNow);
@@ -246,12 +252,21 @@ export class FireLoopRef<T> {
246252
**/
247253
private broadcasts(event: string, request: any): Observable<T> {
248254
let sbj: Subject<T> = new Subject<T>();
249-
this.socket.on(
250-
`${event}.broadcast.announce.${ this.id }`,
251-
(res: T) =>
252-
this.socket.emit(`${event}.broadcast.request.${ this.id }`, request)
253-
);
254-
this.socket.on(`${ event }.broadcast.${ this.id }`, (data: any) => sbj.next(data));
255+
let channels: { announce: string, broadcast: string } = {
256+
announce: `${event}.broadcast.announce.${this.id}`,
257+
broadcast: `${event}.broadcast.${this.id}`
258+
};
259+
let that = this;
260+
// Announces Handler
261+
this.disposable[channels.announce] = function (res: T) {
262+
that.socket.emit(`${event}.broadcast.request.${that.id}`, request)
263+
};
264+
// Broadcasts Handler
265+
this.disposable[channels.broadcast] = function (data: any) {
266+
sbj.next(data);
267+
};
268+
this.socket.on(channels.announce, this.disposable[channels.announce]);
269+
this.socket.on(channels.broadcast, this.disposable[channels.broadcast]);
255270
return sbj.asObservable();
256271
}
257272
/**
@@ -264,9 +279,9 @@ export class FireLoopRef<T> {
264279
**/
265280
private operation(event: string, data: any): Observable<T> {
266281
if (!this.relationship) {
267-
event = `${ this.model.getModelName() }.${event}.${ this.id }`;
282+
event = `${this.model.getModelName()}.${event}.${this.id}`;
268283
} else {
269-
event = `${ this.parent.model.getModelName() }.${ this.relationship }.${ event }.${ this.id }`;
284+
event = `${this.parent.model.getModelName()}.${this.relationship}.${event}.${this.id}`;
270285
}
271286
let subject: Subject<T> = new Subject<T>();
272287
let config: { data: any, parent: any } = {
@@ -276,9 +291,9 @@ export class FireLoopRef<T> {
276291
this.socket.emit(event, config);
277292
let resultEvent: string = '';
278293
if (!this.relationship) {
279-
resultEvent = `${ this.model.getModelName()}.value.result.${ this.id }`;
294+
resultEvent = `${this.model.getModelName()}.value.result.${this.id}`;
280295
} else {
281-
resultEvent = `${ this.parent.model.getModelName() }.${ this.relationship }.value.result.${ this.id }`;
296+
resultEvent = `${this.parent.model.getModelName()}.${this.relationship}.value.result.${this.id}`;
282297
}
283298
this.socket.on(resultEvent, (res: any) => {
284299
if (res.error) {
@@ -287,6 +302,9 @@ export class FireLoopRef<T> {
287302
subject.next(res);
288303
}
289304
});
305+
if (event.match('dispose')) {
306+
setTimeout(() => subject.next());
307+
}
290308
// This event listener will be wiped within socket.connections
291309
this.socket.sharedObservables.sharedOnDisconnect.subscribe(() => subject.complete());
292310
return subject.asObservable().catch((error: any) => Observable.throw(error));

lib/angular2/shared/services/core/realtime.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Injectable, Inject } from '@angular/core';
22
import { IO } from './io.service';
33
import { LoopBackAuth } from './auth.service';
4-
import { LoopBackConfig } from '../../lb.config';
54
import { FireLoop } from '../../models/FireLoop';
65
import { SocketConnection } from '../../sockets/socket.connections';
76
import { SDKModels } from '../custom/SDKModels';

lib/angular2/shared/sockets/connections.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,21 @@ export class SocketConnection {
162162
this.socket.off(event, handler);
163163
}
164164
}
165+
/**
166+
* @method removeAllListeners
167+
* @param {string} event Event name
168+
* @param {Function} handler Event listener handler
169+
* @return {void}
170+
* @description
171+
* This method will wrap the original "on" method and run it within the Angular Zone
172+
* Note: off is being used since the nativescript socket io client does not provide
173+
* removeListener method, but only provides with off which is provided in any platform.
174+
**/
175+
public removeAllListeners(event: string): void {
176+
if (typeof this.socket.removeAllListeners === 'function') {
177+
this.socket.removeAllListeners(event);
178+
}
179+
}
165180
/**
166181
* @method disconnect
167182
* @return {void}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mean-expert/loopback-sdk-builder",
3-
"version": "2.1.0-rc.13.4",
3+
"version": "2.1.0-rc.13.5",
44
"description": "Tool for auto-generating Software Development Kits (SDKs) for LoopBack",
55
"bin": {
66
"lb-sdk": "bin/lb-sdk"

0 commit comments

Comments
 (0)