Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

Commit

Permalink
Upgrade to 1.2.8 beta 1 with better logging mechanism and proxy console
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Biasi committed Jul 28, 2017
1 parent acfbe29 commit fe59f94
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 151 deletions.
68 changes: 47 additions & 21 deletions TerrificBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ export const getGlobalApp = () => {
return window[TerrificBridgeGlobalAppId];
};

// Simple non-operative method
const noop = () => {};

/**
* Get a proxy console instance
* @param {boolean} [false] debug
*/
const getProxyConsole = (debug = false) => ({
log: debug ? noop : global.console.log.bind(global) || noop,
group: debug ? noop : global.console.groupCollapsed.bind(global) || noop,
groupEnd: debug ? noop : global.console.groupEnd.bind(global) || noop
});

/**
* Terrific application handler for React
* @uses Terrific
Expand All @@ -40,6 +53,7 @@ export class TerrificBridge {
_app = void 0;
_processed = false;
_t = void 0;
_proxyConsole = getProxyConsole();

/**
* Instanciates the Bridge
Expand All @@ -59,9 +73,17 @@ export class TerrificBridge {
unregister: []
};

if (this._config.debug) {
this._proxyConsole = getProxyConsole(true);
}

return TerrificBridgeInstance;
}

get logger() {
return this._proxyConsole;
}

/**
* Get the application instance
* @return {Object}
Expand Down Expand Up @@ -194,11 +216,9 @@ export class TerrificBridge {
return void 0;
}

this.logger.group(`Register ${name}`);
const tModule = this._app.registerModule(node, name, decorator);

if (bridge._config.debug) {
console.log("Registering tModule %s%s on node", name, decorator ? `#${decorator[0]}` : "", node);
}
this.logger.log("Registering Module %s%s on node", name, decorator ? `#${decorator[0]}` : "", node);

if (tModule) {
tModule._bridge = bridge;
Expand All @@ -212,13 +232,12 @@ export class TerrificBridge {
*/
tModule.send = (selector, args = []) => {
const fn = compositeFactory[selector];
if (bridge._config.debug) {
console.log("React is receiving action '%s' from terrific", selector);
}
this.logger.log("React is receiving action '%s' from terrific", selector);

if (typeof fn === "function") {
try {
fn.apply(bridge, [...args]);
this.logger.log("Successfully registered component");
} catch (err) {
throw new Error(`TerrificBridge failed receiving action ${selector}: ${err.message}`);
}
Expand All @@ -228,6 +247,7 @@ export class TerrificBridge {
this._app.start([tModule]);
}

this.logger.groupEnd(`Register ${name}`);
return tModule;
};

Expand All @@ -250,25 +270,23 @@ export class TerrificBridge {
}

const id = node.getAttribute("data-t-id");
const name = node.getAttribute("data-t-name");

if (!id || id === null) {
return void 0;
}
const tModule = this._app.getModuleById(id);

if (bridge._config.debug) {
console.log("Unregistering terrific component #%s", id);
}
const tModule = this._app.getModuleById(id);
this.logger.group(`Unregister ${name}#${id}`);
this.logger.log("Unregistering terrific component #%s", id);

try {
tModule.stop.apply(tModule);
tModule.send = () => {};

this._app.unregisterModules([id]);
node.removeAttribute("data-t-id");

if (bridge._config.debug) {
console.log("Succesfully unregistered component #%s", id);
}
this.logger.log("Succesfully unregistered component #%s", id);

return true;
} catch (err) {
Expand All @@ -293,6 +311,7 @@ export class TerrificBridge {
const node = ReactDOM.findDOMNode(component);
const name = node.getAttribute("data-t-name");
const id = parseInt(node.getAttribute("data-t-id"), 10);
let updateSucceeded = false;

action = action.replace(/\./g, "-");
action = action.replace(/\//g, "-");
Expand All @@ -302,21 +321,28 @@ export class TerrificBridge {
}

if (node && name && id > 0) {
this.logger.group(`Send action ${action} to ${name}`);
const tModule = this._app.getModuleById(id);

if (bridge._config.debug) {
console.log("Send action %s to component %s#%d", action, name, id);
}
this.logger.log(`Send action ${action} to component ${name}#${id}`);

if (tModule && tModule.actions) {
if (typeof tModule.actions[action] === "function") {
tModule.actions[action].apply(tModule, [...args, component]);
return true;
updateSucceeded = true;
}
}
} else {
this.logger.log(`Cannot send action to invalid component, Name or ID missing`);
}

if (!updateSucceeded) {
this.logger.log(
`Action was not triggered successfully, maybe the action was not registered on <T>${name}`
);
}

return false;
this.logger.groupEnd(`Send action ${action} to ${name}`);
return updateSucceeded;
};

return this._app ? update() : this._queue.update.push(update);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-terrific-bridge",
"version": "1.2.7",
"version": "1.2.8-beta.1",
"main": "dist/index.js",
"author": "Jan Biasi <biasijan@gmail.com>",
"contributors": [
Expand Down Expand Up @@ -85,8 +85,8 @@
"webpack-dev-server": "~2.5.0"
},
"peerDependencies": {
"react": "^15.5.0",
"react-dom": "^15.5.0",
"react": ">=15.5.0",
"react-dom": ">=15.5.0",
"terrific": "~3.0.0"
},
"jest": {
Expand Down

0 comments on commit fe59f94

Please sign in to comment.