Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Merge branch 'development' into 280-persist-network-on-new-account
Browse files Browse the repository at this point in the history
  • Loading branch information
slaweet authored Jun 2, 2017
2 parents ce3388e + ef7aa90 commit a90e985
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
],
"globals": {
"angular": true,
"app": true
"app": true,
"ipc": true,
"PRODUCTION": true
},
"env": {
"browser": true,
Expand Down
5 changes: 5 additions & 0 deletions app/ipc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Add ipcRenderer to the window object
*/
const ipcRenderer = window.require('electron').ipcRenderer;
window.ipc = ipcRenderer;
9 changes: 9 additions & 0 deletions app/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const electron = require('electron'); // eslint-disable-line import/no-extraneous-dependencies
const path = require('path');

const { app } = electron;
const { BrowserWindow } = electron;
Expand All @@ -13,7 +14,15 @@ function createWindow() {
width: width > 2000 ? Math.floor(width * 0.5) : width - 250,
height: height > 1000 ? Math.floor(height * 0.7) : height - 150,
center: true,
webPreferences: {
// Avoid app throttling when Electron is in background
backgroundThrottling: false,
// Specifies a script that will be loaded before other scripts run in the page.
preload: path.resolve(__dirname, 'ipc.js'),
},
});
win.on('blur', () => win.webContents.send('blur'));
win.on('focus', () => win.webContents.send('focus'));

const template = [
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bugs": "https://github.com/LiskHQ/lisk-nano/issues",
"main": "main.js",
"scripts": {
"build": "webpack --profile --progress --display-modules --display-exclude --display-chunks --display-cached --display-cached-assets",
"build": "export NODE_ENV=prod && webpack --profile --progress --display-modules --display-exclude --display-chunks --display-cached --display-cached-assets",
"dev": "webpack-dev-server --host 0.0.0.0 --profile --progress",
"e2e-test": "protractor protractor.conf.js",
"test": "grunt eslint && export NODE_ENV=test && karma start",
Expand Down
8 changes: 7 additions & 1 deletion src/components/main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ app.component('main', {
*/
controller: class main {
constructor($scope, $rootScope, $timeout, $q, $state, Peers,
dialog, SendModal, Account, AccountApi) {
dialog, SendModal, Account, AccountApi, Notification) {
this.$scope = $scope;
this.$rootScope = $rootScope;
this.$timeout = $timeout;
Expand All @@ -26,6 +26,7 @@ app.component('main', {
this.$state = $state;
this.account = Account;
this.accountApi = AccountApi;
this.notify = Notification.init();

this.activeTab = this.init();
}
Expand Down Expand Up @@ -108,6 +109,11 @@ app.component('main', {
delete res.publicKey;
}

if (res.balance > this.account.get().balance) {
const amount = res.balance - this.account.get().balance;
this.notify.about('deposit', amount);
}

this.account.set(res);
})
.catch((res) => {
Expand Down
1 change: 1 addition & 0 deletions src/liskNano.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import './services/dialog';
import './services/lsk';
import './services/signVerify';
import './services/sync';
import './services/notification';

import './filters/lsk';

Expand Down
67 changes: 67 additions & 0 deletions src/services/notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @description This factory provides methods to call Notification
*
* @module app
* @submodule Notify
*/
app.factory('Notification', ($window, lsk) => {
/**
* The Notify factory constructor class
* @class Notify
* @constructor
*/
class Notification {
constructor() {
this.isFocused = true;
}

/**
* Initialize event listeners
*
* @returns {this}
* @method init
* @memberof Notify
*/
init() {
if (PRODUCTION) {
const { ipc } = $window;
ipc.on('blur', () => this.isFocused = false);
ipc.on('focus', () => this.isFocused = true);
}
return this;
}

/**
* Routing to specific Notification creator based on type param
* @param {string} type
* @param {any} data
*
* @method about
* @public
* @memberof Notify
*/
about(type, data) {
if (this.isFocused) return;
switch (type) {
case 'deposit':
this._deposit(data);
break;
default: break;
}
}

/**
* Creating notification about deposit
*
* @param {number} amount
* @private
* @memberof Notify
*/
_deposit(amount) { // eslint-disable-line
const body = `You've received ${lsk.normalize(amount)} LSK.`;
new $window.Notification('LSK received', { body }); // eslint-disable-line
}
}

return new Notification();
});
24 changes: 22 additions & 2 deletions src/services/sync.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const intervals = {
activeApp: 10000,
inactiveApp: 60000,
};

app.factory('Sync', ($rootScope, $window) => {
const config = {
updateInterval: 10000,
updateInterval: intervals.activeApp,
freeze: false,
};
let lastTick = new Date();
Expand Down Expand Up @@ -33,6 +38,19 @@ app.factory('Sync', ($rootScope, $window) => {
$window.requestAnimationFrame(step);
}
};

const toggleSyncTimer = (inFocus) => {
config.updateInterval = (inFocus) ?
intervals.activeApp :
intervals.inactiveApp;
};

const initIntervalToggler = () => {
const { ipc } = $window;
ipc.on('blur', () => toggleSyncTimer(false));
ipc.on('focus', () => toggleSyncTimer(true));
};

/**
* Starts the first frame by calling requestAnimationFrame.
* This will be
Expand All @@ -41,6 +59,9 @@ app.factory('Sync', ($rootScope, $window) => {
if (!running) {
$window.requestAnimationFrame(step);
}
if (PRODUCTION) {
initIntervalToggler();
}
};

/**
Expand All @@ -50,7 +71,6 @@ app.factory('Sync', ($rootScope, $window) => {
config.freeze = false;
};

init();
return {
init, config, end,
};
Expand Down
50 changes: 50 additions & 0 deletions test/services/notification.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const chai = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');

const expect = chai.expect;
chai.use(sinonChai);

describe('Factory: Notification', () => {
let lsk;
let $window;
let notify;

beforeEach(angular.mock.module('app'));

beforeEach(inject((_Notification_, _lsk_, _$window_) => {
lsk = _lsk_;
$window = _$window_;
notify = _Notification_.init();
}));

describe('about(data)', () => {
const amount = 100000000;
const mockNotification = sinon.spy();

it('should call this._deposit', () => {
const spy = sinon.spy(notify, '_deposit');
notify.isFocused = false;
notify.about('deposit', amount);
expect(spy).to.have.been.calledWith(amount);
});

it('should call $window.Notification', () => {
$window.Notification = mockNotification;
const msg = `You've received ${lsk.normalize(amount)} LSK.`;

notify.isFocused = false;
notify.about('deposit', amount);
expect(mockNotification).to.have.been.calledWith(
'LSK received', { body: msg },
);
mockNotification.reset();
});

it('should not call $window.Notification if app is focused', () => {
notify.about('deposit', amount);
expect(mockNotification).to.have.been.not.calledWith();
mockNotification.reset();
});
});
});
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require('./services/api/peers.spec');
require('./services/lsk.spec');
require('./services/passphrase.spec');
require('./services/signVerify.spec');
require('./services/notification.spec');

require('./run.spec');

Expand Down
12 changes: 10 additions & 2 deletions webpack.config.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ const provide = () => ({
],
});

const define = () => ({
plugins: [
new webpack.DefinePlugin({
PRODUCTION: JSON.stringify(nodeEnvironment === 'prod'),
}),
],
});

const bundleAnalyzer = () => ({
plugins: [
new BundleAnalyzerPlugin({
Expand All @@ -182,10 +190,10 @@ let config;

switch (process.env.npm_lifecycle_event) {
case 'build':
config = merge(common, clean(path.join(PATHS.build, 'dist')), html(), provide(), eslint(), babel(), pug(), less(), css(), json(), png(), fonts(), bundleAnalyzer());
config = merge(common, clean(path.join(PATHS.build, 'dist')), html(), provide(), define(), eslint(), babel(), pug(), less(), css(), json(), png(), fonts(), bundleAnalyzer());
break;
default:
config = merge(common, devServer(), { devtool: 'eval-source-map' }, html(), provide(), eslint(), babel(), pug(), less(), css(), json(), png(), fonts());
config = merge(common, devServer(), { devtool: 'eval-source-map' }, html(), provide(), define(), eslint(), babel(), pug(), less(), css(), json(), png(), fonts());
break;
}

Expand Down

0 comments on commit a90e985

Please sign in to comment.