Skip to content

Commit

Permalink
Merge pull request #483 from GrimoireGL/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
kyasbal authored Jun 4, 2017
2 parents ab404b1 + 6d71990 commit a871927
Show file tree
Hide file tree
Showing 54 changed files with 1,407 additions and 1,067 deletions.
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "grimoirejs",
"version": "0.0.0-development",
"version": "0.16.0-beta3",
"description": "A service-oriented WebGL framework.",
"main": "./ref/index.js",
"typings": "./ref/index.d.ts",
Expand All @@ -15,7 +15,7 @@
"dependencies": {
"@types/node": "^7.0.12",
"events": "^1.1.1",
"grimoirejs-cauldron": "^2.3.3"
"grimoirejs-cauldron": "beta"
},
"devDependencies": {
"ava": "^0.18.2",
Expand All @@ -39,20 +39,21 @@
"typescript": "^2.2.2",
"typescript-awaiter": "^1.0.0",
"webpack": "^2.3.2",
"webpack-shell-plugin": "^0.5.0",
"xhr-mock": "^1.7.0",
"xmldom": "^0.1.27",
"yargs": "^8.0.1"
},
"repository": "http://github.com/GrimoireGL/GrimoireJS",
"scripts": {
"test": "tsc --outDir ./lib && babel ./lib --presets es2015,stage-2 --plugins transform-runtime --out-dir ./lib-es5 && babel ./test --presets es2015,stage-2 --plugins transform-runtime --out-dir ./test-es5 && ava ./test-es5/**/*Test.js --verbose --serial",
"test": "tsc && babel ./lib --presets es2015,stage-2 --plugins transform-runtime --out-dir ./lib-es5 && babel ./test --presets es2015,stage-2 --plugins transform-runtime --out-dir ./test-es5 && ava ./test-es5/**/*Test.js --verbose --serial",
"lint": "tslint -c tslint.json ./src/**/*.ts",
"prepublish": "npm run build-production && npm test",
"start": "webpack --progress --watch",
"build": "npm run generate-expose && webpack --progress && npm run generate-reference",
"build": "webpack --progress",
"build-production": "npm run generate-expose && webpack --progress --env.prod && npm run generate-reference",
"generate-expose": "cauldron generate-exposure --src ./src --dest ./src/index.ts --ts --main ./src/main.ts",
"generate-reference": "cauldron generate-reference --src ./src --dest ./src/index.ts --ts --main ./src/main.ts --dts ./ref",
"generate-expose": "cauldron generate-exposure --src ./src --dest ./src/index.ts --ts --main ./src/main.ts --core",
"generate-reference": "cauldron generate-reference --src ./src --dest ./src/index.ts --ts --main ./src/main.ts --dts ./ref --core",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"doc": "typedoc --out ./docs/ --options typedoc.json ./tsconfig.json"
},
Expand Down
76 changes: 39 additions & 37 deletions src/Base/AttributeManager.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import Utility from "./Utility";
import Ensure from "./Ensure";
import NSIdentity from "./NSIdentity";
import Attribute from "../Node/Attribute";
import IdResolver from "../Base/IdResolver";
import Namespace from "../Base/Namespace";
import NSIdentity from "../Base/NSIdentity";
import {Name} from "../Base/Types";

export default class AttributeManager {
private _attributesMap: { [name: string]: Attribute[] } = {};
private _attributesFQNMap: { [fqn: string]: Attribute[] } = {};

private _idResolver: IdResolver = new IdResolver();

private _attrBuffer: { [fqn: string]: any } = {};
private _watchBuffer: { [fqn: string]: (newValue: any, oldValue: any, attr: Attribute) => void } = {};

public constructor(public tag: string) { }

public addAttribute(attr: Attribute): Attribute {
const fqn = attr.name.fqn;
const name = attr.name.name;
if (this._attributesMap[name] !== void 0) {
if (this._idResolver.has(fqn)) {
Utility.w(`attribute ${attr.name} is already exist in ${this.tag}`);
} else {
this._attributesMap[name] = [];
}
if (this._attributesFQNMap[fqn] === void 0) {
this._attributesFQNMap[fqn] = [];
}
this._attributesMap[name].push(attr);
this._attributesFQNMap[fqn].push(attr);
this._idResolver.add(attr.name);

// check buffer value.
const attrBuf = this._attrBuffer[attr.name.fqn];
Expand All @@ -40,21 +41,18 @@ export default class AttributeManager {
return attr;
}

public watch(attrName: string | NSIdentity, watcher: ((newValue: any, oldValue: any, attr: Attribute) => void), immediate = false) {
public watch(attrName: Name, watcher: ((newValue: any, oldValue: any, attr: Attribute) => void), immediate = false) {
if (typeof attrName === "string") {
const attrs = this._attributesMap[attrName];
if (attrs === void 0 || attrs.length === 0) {
let res = this._idResolver.get(Namespace.defineByArray(attrName.split(".")));
if (res.length === 0) {
this._watchBuffer[Ensure.tobeNSIdentity(attrName).fqn] = watcher;
return;
}
const attrFQN = attrs[0].name.fqn;
for (let i = 1; i < attrs.length; i++) {
if (attrFQN !== attrs[i].name.fqn) {
throw new Error(`attribute ${attrName} is ambigious`);
}
if (res.length > 1) {
throw new Error(`attribute ${attrName} is ambigious`);
}
for (let i = 0; i < attrs.length; i++) {
attrs[i].watch(watcher, immediate);
for (let i = 0; i < this._attributesFQNMap[res[0]].length; i++) {
this._attributesFQNMap[res[0]][i].watch(watcher, immediate);
}
} else {
const attrs = this._attributesFQNMap[attrName.fqn];
Expand All @@ -68,21 +66,18 @@ export default class AttributeManager {
}
}

public setAttribute(attrName: string | NSIdentity, value: any): void {
public setAttribute(attrName: Name, value: any): void {
if (typeof attrName === "string") {
const attrs = this._attributesMap[attrName];
if (attrs === void 0 || attrs.length === 0) {
let res = this._idResolver.get(Namespace.defineByArray(attrName.split(".")));
if (res.length === 0) {
this._attrBuffer[Ensure.tobeNSIdentity(attrName).fqn] = value;
return;
}
const attrFQN = attrs[0].name.fqn;
for (let i = 1; i < attrs.length; i++) {
if (attrFQN !== attrs[i].name.fqn) {
throw new Error(`attribute ${attrName} is ambigious`);
}
if (res.length > 1) {
throw new Error(`attribute ${attrName} is ambigious`);
}
for (let i = 0; i < attrs.length; i++) {
attrs[i].Value = value;
for (let i = 0; i < this._attributesFQNMap[res[0]].length; i++) {
this._attributesFQNMap[res[0]][i].Value = value;
}
} else {
const attrs = this._attributesFQNMap[attrName.fqn];
Expand All @@ -95,20 +90,20 @@ export default class AttributeManager {
}
}
}
public getAttribute(attrName: string | NSIdentity): Attribute {
public getAttribute(attrName: Name): Attribute {
if (typeof attrName === "string") {
const attrs = this._attributesMap[attrName];
if (attrs === void 0 || attrs.length === 0) {
let res = this._idResolver.get(Namespace.defineByArray(attrName.split(".")));
if (res.length === 0) {
const attrBuf = this._attrBuffer[Ensure.tobeNSIdentity(attrName).fqn];
if (attrBuf !== void 0) {
return attrBuf;
}
throw new Error(`attribute ${attrName} is not found.`);
}
if (attrs.length !== 1) {
if (res.length > 1) {
throw new Error(`attribute ${attrName} is ambigious`);
}
return attrs[0];
return this._attributesFQNMap[res[0]][0];
} else {
const attrs = this._attributesFQNMap[attrName.fqn];
if (attrs === void 0 || attrs.length === 0) {
Expand All @@ -125,14 +120,21 @@ export default class AttributeManager {
}
public removeAttribute(attr: Attribute): boolean {
if (this._attributesFQNMap[attr.name.fqn]) {
Utility.remove(this._attributesFQNMap[attr.name.fqn], attr);
Utility.remove(this._attributesMap[attr.name.name], attr);
delete this._attributesFQNMap[attr.name.fqn];
if (this._attributesMap[attr.name.name].length === 0) {
delete this._attributesMap[attr.name.name];
let attributes = this._attributesFQNMap[attr.name.fqn];
if (attributes.length === 1) {
this._idResolver.remove(attr.name);
}
Utility.remove(attributes, attr);
delete this._attributesFQNMap[attr.name.fqn];
return true;
}
return false;
}

public guess(name: Name): NSIdentity[] {
if (name instanceof NSIdentity) {
return [name];
}
return this._idResolver.get(Namespace.defineByArray(name.split("."))).map(fqn => NSIdentity.fromFQN(fqn));
}
}
6 changes: 2 additions & 4 deletions src/Base/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
class Constants {
export default class Constants {
public static get defaultNamespace(): string {
return "HTTP://GRIMOIRE.GL/NS/DEFAULT";
return "grimoirejs";
}
public static get x_gr_id(): string {
return "x-gr-id";
}
}

export default Constants;
33 changes: 22 additions & 11 deletions src/Base/Ensure.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import GrimoireInterface from "../GrimoireInterface";
import GrimoireInterface from "../Interface/GrimoireInterface";
import NSIdentity from "./NSIdentity";
// import Namespace from "./Namespace";
import NSDictionary from "./NSDictionary";
import {Name, Nullable, Ctor} from "./Types";
/**
* Provides static methods to ensure arguments are valid type.
*/
Expand Down Expand Up @@ -35,21 +37,23 @@ export default class Ensure {
}
}

public static tobeNSIdentity(name: string | NSIdentity): NSIdentity {
/**
* string or NSIdentity ensure to be NSIdentity.
* @param {Name} name [description]
* @return {NSIdentity} [description]
*/
public static tobeNSIdentity(name: Name): NSIdentity {
if (!name) {
return undefined;
throw Error(`argument can not be null or undefined.`);
}
if (typeof name === "string") {
if (name.indexOf("|") !== -1) {// name is fqn
return NSIdentity.fromFQN(name);
}
return NSIdentity.from(name);
return NSIdentity.guess(name);
} else {
return name;
}
}

public static tobeNSIdentityArray(names: (string | NSIdentity)[]): NSIdentity[] {
public static tobeNSIdentityArray(names: Name[]): NSIdentity[] {
if (!names) {
return [];
}
Expand All @@ -69,7 +73,7 @@ export default class Ensure {
} else {
const newDict = new NSDictionary<T>();
for (let key in dict) {
newDict.set(NSIdentity.from(key), dict[key]);
newDict.set(NSIdentity.guess(key), dict[key]);
}
return newDict;
}
Expand All @@ -86,13 +90,20 @@ export default class Ensure {
return "$$" + message;
}
}
public static tobeComponentConstructor<T>(c: string | NSIdentity | (new () => T)): (new () => T) {

/**
* string, NSIdentity or constructor ensure to be constructor.
* return null if identity is not registered as component.
* @param {[type]} typeofc==="function" [description]
* @return {[type]} [description]
*/
public static tobeComponentConstructor<T>(c: Name | Ctor<T>): Nullable<Ctor<T>> {
if (typeof c === "function") {
return c;
} else {
const dec = GrimoireInterface.componentDeclarations.get(c);
if (dec) {
return dec.ctor as any as (new () => T);
return dec.ctor as any as Ctor<T>;
}
return null;
}
Expand Down
Loading

0 comments on commit a871927

Please sign in to comment.