Skip to content

Commit

Permalink
nearbyDevicesList refactor, dmt gui small simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhq committed Dec 5, 2020
1 parent 15f8ed9 commit d38e76b
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 73 deletions.
2 changes: 1 addition & 1 deletion core/node/apps/loadApps.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AppLoader {
const promises = [];

appList.forEach(appDir => {
const appInit = path.join(appDir, 'index.js');
const appInit = path.join(appDir, 'backend/index.js');

if (fs.existsSync(appInit)) {
const appName = path.basename(appDir);
Expand Down
2 changes: 2 additions & 0 deletions core/node/aspect-meta/replicate/endpoints/streamDmtZip.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ function replicateAllowed(entry) {
entry.relpath != 'tagversion' &&
entry.relpath != 'core/node/.gitignore' &&
entry.relpath != 'core/node/connectome/.gitignore' &&
entry.relpath != 'core/node/connectome/dist' &&
entry.relpath != 'core/node/connectome/server' &&
entry.relpath != 'shell/.bash_staging' &&
entry.relpath != 'etc/.bash_aliases_bundle' &&
entry.relpath != 'etc/.bash_aliases_slim' &&
Expand Down
74 changes: 25 additions & 49 deletions core/node/aspect-nearby/nearby/lib/nearby.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import dmt from 'dmt/bridge';
const { log } = dmt;

import attachNearbyDeviceAttributes from './attachNearbyDeviceAttributes';
import nearbyDevicesListRefresh from './state/nearbyDevicesListRefresh';
import updateDeviceList from './state/updateDeviceList';

class Nearby {
constructor(program) {
Expand Down Expand Up @@ -62,10 +64,29 @@ class Nearby {
}
}

refreshNearbyDevicesList(removeStaleImmediately = false) {
if (this.program.state().nearbyDevices) {
const { nearbyDevices } = this.program.state();

const nearbyDevicesNew = nearbyDevicesListRefresh(
{
nearbyDevices,
ourMessage: this.ourMessage(),
broadcastInterval: this.broadcastInterval
},
{ removeStaleImmediately }
);

this.program.store.replaceSlot('nearbyDevices', nearbyDevicesNew, { announce: false });

this.program.emit('nearby_devices', nearbyDevicesNew);
}
}

setupNearbyDevicesListRefresh() {
this.nearbyDevicesListRefresh({ removeStaleImmediately: true });
this.refreshNearbyDevicesList(true);

this.program.on('tick', () => this.nearbyDevicesListRefresh());
this.program.on('tick', () => this.refreshNearbyDevicesList());
}

initNearbyHelloMessagesListener() {
Expand All @@ -90,63 +111,18 @@ class Nearby {
announce = true;
}

const hiddenInGui = showOnly && !showOnly.includes(obj.deviceName);
const hiddenInGui = showOnly && !showOnly.includes(device.deviceName);

if (hiddenInGui) {
device.hiddenInGui = true;
}

const { nearbyDevices } = this.program.state();

let found;

for (let i = 0; i < nearbyDevices.length; i++) {
if (nearbyDevices[i].deviceKey == obj.deviceKey) {
nearbyDevices[i] = device;
found = true;
}
}

if (!found) {
nearbyDevices.push(device);
}

if (announce) {
this.program.store.announceStateChange();
}
updateDeviceList({ device, announce, program: this.program });
} catch (e) {
log.red(e);
}
});
}

nearbyDevicesListRefresh({ removeStaleImmediately = false } = {}) {
if (this.program.state().nearbyDevices) {
const nearbyDevicesNew = [];

const now = Date.now();

const { nearbyDevices } = this.program.state();

for (const device of nearbyDevices.filter(({ deviceKey }) => deviceKey != dmt.keypair().publicKeyHex)) {
if (now - device.lastSeenAt > 2.2 * this.broadcastInterval) {
if (!device.stale && !removeStaleImmediately) {
nearbyDevicesNew.push({ ...device, ...{ stale: true, staleDetectedAt: now } });
} else if (device.staleDetectedAt && now - device.staleDetectedAt < 3000) {
nearbyDevicesNew.push({ ...device });
}
} else {
nearbyDevicesNew.push({ ...device, ...{ stale: false, staleDetectedAt: undefined } });
}
}

nearbyDevicesNew.push({ ...this.ourMessage(), ...{ thisDevice: true, stale: false, staleDetectedAt: undefined } });

this.program.store.replaceSlot('nearbyDevices', nearbyDevicesNew, { announce: false });

this.program.emit('nearby_devices', nearbyDevicesNew);
}
}
}

export default Nearby;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default function nearbyDevicesListRefresh({ nearbyDevices, ourMessage, broadcastInterval }, { removeStaleImmediately = false } = {}) {
const list = [];

const now = Date.now();

for (const device of nearbyDevices.filter(({ thisDevice }) => !thisDevice)) {
if (now - device.lastSeenAt > 2.2 * broadcastInterval) {
if (!device.stale && !removeStaleImmediately) {
list.push({ ...device, ...{ stale: true, staleDetectedAt: now } });
} else if (device.staleDetectedAt && now - device.staleDetectedAt < 3000) {
list.push({ ...device });
}
} else {
list.push({ ...device, ...{ stale: false, staleDetectedAt: undefined } });
}
}

list.push({ ...ourMessage, ...{ thisDevice: true, stale: false, staleDetectedAt: undefined } });

return list;
}
20 changes: 20 additions & 0 deletions core/node/aspect-nearby/nearby/lib/state/updateDeviceList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default function updateDeviceList({ device, announce, program }) {
const { nearbyDevices } = program.state();

let found;

for (let i = 0; i < nearbyDevices.length; i++) {
if (nearbyDevices[i].deviceKey == device.deviceKey) {
nearbyDevices[i] = device;
found = true;
}
}

if (!found) {
nearbyDevices.push(device);
}

if (announce) {
program.store.announceStateChange();
}
}
28 changes: 14 additions & 14 deletions core/node/gui/gui-frontend-core/app/public/bundle.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/node/gui/gui-frontend-core/app/public/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/node/gui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import staticServerSetup from './lib/subcomponent-static-http-server/setup';
import guiServerOptions from './guiServerOptions';

import reduceSizeOfStateForGUI from './lib/protocol/dmtGUI/helpers/reduceSizeOfStateForGUI';
import setupDMTGUIProtocol from './lib/protocol/dmtGUI/setup';
import setupDMTGUIProtocol from './lib/protocol/dmtGUI/index';

function init(program) {
setupDMTGUIProtocol({ program });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import colors from 'colors';

import path from 'path';
import fs from 'fs';

import dmt from 'dmt/bridge';
const { log } = dmt;

import loadGuiViewsDef from '../../../loadGuiViewsDef';
import onConnect from './onConnect';

export default function setup({ program }) {
export default function initProtocol({ program }) {
loadGuiViewsDef(program);

// 💡 hook program store actions (these are always received over appropriate gui protocol)
Expand All @@ -19,7 +16,6 @@ export default function setup({ program }) {

log.dev('⚠️ Reminder: remove this GUITarget after dmt gui moves to Svelte3');

// 💡 initial state is sent on each channel creation (in ./endpoint.js)
program.on('send_to_connected_guis', ({ action, payload }) => {
log.cyan(
`Received request to send action ${colors.magenta(`gui:${action}`)} to frontend${
Expand Down
2 changes: 0 additions & 2 deletions core/node/gui/lib/protocol/dmtGUI/onConnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ function onConnect({ program, channel }) {
// 💡 different parts of the system (core or through included middleware / dmt app hooks)
program.emit('dmt_gui_action', { action, namespace, payload });
});

// 💡 we setup sending state_diff to all dmtgui channels in protocol ./setup.js
}

function loadInitialView(channel) {
Expand Down

0 comments on commit d38e76b

Please sign in to comment.