Skip to content

Commit

Permalink
feat(): prepare for easier subscription handling for updatelimit and …
Browse files Browse the repository at this point in the history
…changedetection
  • Loading branch information
wzr1337 committed Jan 28, 2017
1 parent 6987ec2 commit 96ccf9c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 29 deletions.
13 changes: 6 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { viwiClientWebSocketMessage } from "./types";
import * as uuid from "uuid";
import * as fs from "fs";
import * as path from "path";
import { Service, Resource } from "./plugins/viwiPlugin";
import { Service, Resource, Element } from "./plugins/viwiPlugin";
import { viwiLogger } from "./log";

const logger = viwiLogger.getInstance('verbose');
Expand Down Expand Up @@ -96,9 +96,8 @@ const elementGET = (service:Service, resource:Resource) => {

// proprietary element fetching
let element = resource.getElement(req.params.id);

if(element){
let data = element.getValue()
let data = element.getValue().data;
// filter the result before responding if needed
if (req.query.hasOwnProperty("$fields")) {
data = filterByKeys(data ,["id", "name", "uri"].concat(req.query["$fields"].split(",")));
Expand Down Expand Up @@ -139,8 +138,8 @@ const resourceGET = (service:Service, resource:Resource) => {
let elements = resource.getResource(parseNumberOrId(req.query.$offset), parseNumberOrId(req.query.$limit));

if(elements) {
let resp = elements.map((value) => {
return value.getValue();
let resp = elements.map((value:BehaviorSubject<Element>) => {
return value.getValue().data;
});
res.status(200);
res.json({
Expand Down Expand Up @@ -269,8 +268,8 @@ const handleWebSocketMessages = (service:Service, resource:Resource, ws:WebSocke
_viwiWebSocket.subscribeAck(msg.event);
element.takeUntil(unsubscriptions.map(topic => {topic === msg.event}))
.subscribe(
(data:any) => {
_viwiWebSocket.data(msg.event, data);
(data:Element) => {
_viwiWebSocket.data(msg.event, data.data);
},
(err:any) => {
_viwiWebSocket.error(500, new Error(err));
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/media/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("Renderers resource", () => {
done();
});

it("should not a renderer", (done:DoneFn) => {
it("should hold a renderer", (done:DoneFn) => {
r.getElement('d6ebfd90-d2c1-11e6-9376-df943f51f0d8').subscribe((element) => {
expect(element).toBeDefined();
});
Expand Down
63 changes: 45 additions & 18 deletions src/plugins/media/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BehaviorSubject, Subject } from '@reactivex/rxjs';
import { Service, Resource } from "../viwiPlugin";
import { Service, Resource, Element } from "../viwiPlugin";

class Media implements Service {
private _resources:Resource[]=[];
Expand Down Expand Up @@ -38,24 +38,32 @@ interface RendererObject {
type?: "track"|"video"|"image";
}

interface RendererElement extends Element {
data: RendererObject
}

class Renderers implements Resource {
private _name:string;
private _renderers:BehaviorSubject<{}>[] = [];
private _renderers:BehaviorSubject<RendererElement>[] = [];
private _change:Subject<string> = new Subject();

constructor(private service:Service) {

const rendererId = "d6ebfd90-d2c1-11e6-9376-df943f51f0d8";//uuid.v1(); // FIXED for now
//const collections = service.resources.map<Collections>(resource => resource.name === "collections");
//const initialCollection = collections.map( element => element.name === "default");
let netfluxRenderer = new BehaviorSubject<RendererObject>({
let netfluxRenderer = new BehaviorSubject<RendererElement>({
lastUpdate: Date.now(),
propertiesChanged: [],
data: {
uri: "/" + this.service.name.toLowerCase() + "/" + this.name.toLowerCase() + "/" + rendererId,
id: rendererId,
name: "Netflux",
state: "idle",
offset: 0,
media: "initialCollection"
});
}
});
this._renderers.push(netfluxRenderer);
this._change.next("add");
}
Expand All @@ -72,16 +80,16 @@ class Renderers implements Resource {
return this._change;
}

getElement(elementId:string):BehaviorSubject<{}> {
getElement(elementId:string):BehaviorSubject<RendererElement> {
// find the element requested by the client
return this._renderers.find((element:BehaviorSubject<{}>) => {
return (<{id:string}>element.getValue()).id === elementId;
return this._renderers.find((element:BehaviorSubject<RendererElement>) => {
return (<{id:string}>element.getValue().data).id === elementId;
});
};

getResource(offset?:string|number, limit?:string|number):BehaviorSubject<{}>[]{
getResource(offset?:string|number, limit?:string|number):BehaviorSubject<RendererElement>[]{
// retriev all element
let resp:BehaviorSubject<{}>[];
let resp:BehaviorSubject<RendererElement>[];

if((typeof offset === "number" && typeof limit === "number") || (typeof limit === "number" && !offset) || (typeof offset === "number" && !limit) || (!offset && !limit)) {
resp = this._renderers.slice(<number>offset, <number>limit);
Expand All @@ -94,20 +102,29 @@ class Renderers implements Resource {
private _interval:NodeJS.Timer; //@TODO has to become per-renderer
updateElement(elementId:string, difference:any):Boolean {
let element = this.getElement(elementId);
let renderer:any = element.getValue();
let renderer:RendererObject = element.getValue().data;
if (difference.hasOwnProperty("state")) {
renderer.state = difference.state;
if (difference.state === "play") {
const speed = 1000;
this._interval = setInterval(() => {
renderer.offset = renderer.hasOwnProperty("offset") ? renderer.offset + speed : 0;
element.next(renderer);
element.next(
{
lastUpdate: Date.now(),
propertiesChanged: ["offset"],
data: renderer
});
}, speed);
}
else {
clearInterval(this._interval);
}
element.next(renderer); // @TODO: check diffs bevor updating without a need
element.next({
lastUpdate: Date.now(),
propertiesChanged: ["state"],
data: renderer
}); // @TODO: check diffs bevor updating without a need
}
else {
return false;
Expand All @@ -123,19 +140,29 @@ interface CollectionObject {
uri: string;
items?: Object[];
}

interface CollectionElement extends Element {
data: CollectionObject
}

class Collections implements Resource {
private _collections:BehaviorSubject<{}>[] = [];
private _collections:BehaviorSubject<CollectionElement>[] = [];
private _change:Subject<string> = new Subject();

constructor(private service:Service) {

const rendererId = "deadbeef-d2c1-11e6-9376-df943f51f0d8";//uuid.v1(); // FIXED for now
let initialCollection = new BehaviorSubject<CollectionObject>({
let initialCollection = new BehaviorSubject<CollectionElement>(
{
lastUpdate: Date.now(),
propertiesChanged: [],
data: {
uri: "/" + this.service.name.toLowerCase() + "/" + this.name.toLowerCase() + "/" + rendererId,
id: rendererId,
name: "default",
items: []
});
}
});
this._collections.push(initialCollection);
this._change.next("add");
}
Expand All @@ -152,16 +179,16 @@ class Collections implements Resource {
return this._change;
}

getElement(elementId:string):BehaviorSubject<{}> {
getElement(elementId:string):BehaviorSubject<CollectionElement> {
// find the element requested by the client
return this._collections.find((element:BehaviorSubject<{}>) => {
return (<{id:string}>element.getValue()).id === elementId;
});
};

getResource(offset?:string|number, limit?:string|number):BehaviorSubject<{}>[]{
getResource(offset?:string|number, limit?:string|number):BehaviorSubject<CollectionElement>[]{
// retriev all element
let resp:BehaviorSubject<{}>[];
let resp:BehaviorSubject<CollectionElement>[];

if((typeof offset === "number" && typeof limit === "number") || (typeof limit === "number" && !offset) || (typeof offset === "number" && !limit) || (!offset && !limit)) {
resp = this._collections.slice(<number>offset, <number>limit);
Expand Down
14 changes: 11 additions & 3 deletions src/plugins/viwiPlugin.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { BehaviorSubject, Subject } from '@reactivex/rxjs';



declare namespace viwiPlugin {

interface Service {
Expand All @@ -8,15 +10,21 @@ declare namespace viwiPlugin {
resources:Resource[];
}

interface Element {
lastUpdate: number;
propertiesChanged: string[];
data: any;
}

interface Resource {
name:string;
change:Subject<"add"|"move"|"remove">;

//@TODO: will returning promises make sense???
getResource?(offset?:string|number, limit?:string|number):BehaviorSubject<any>[]; //GET /<service>/<resource>/
getResource?(offset?:string|number, limit?:string|number):BehaviorSubject<Element>[]; //GET /<service>/<resource>/
createElement?(state:{}):Boolean; //POST /<service>/<resource>/<element>
getElement(elementId:string):BehaviorSubject<any>; //GET /<service>/<resource>/<element>
updateElement?(elementId:string, difference:any):Boolean; //POST /<service>/<resource>/<element>
getElement(elementId:string):BehaviorSubject<Element>; //GET /<service>/<resource>/<element>
updateElement?(elementId:string, difference:any):Boolean; //POST /<service>/<resource>/<element>
deleteElement?(elementId:string):Boolean; //DELETE /<service>/<resource>/<element>

resourceSubscribable?:Boolean; //subscribe /<service>/<resource>/
Expand Down

0 comments on commit 96ccf9c

Please sign in to comment.