Skip to content

Commit

Permalink
fix part of #681
Browse files Browse the repository at this point in the history
  • Loading branch information
Domiii committed Mar 6, 2022
1 parent 93fcd70 commit e220ed3
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 46 deletions.
4 changes: 2 additions & 2 deletions dbux-graph-client/src/graph/syncGraph/ContextNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class ContextNode extends ClientComponentEndpoint {
<div>
<span class="value-label" data-el="valueLabel"></span>
</div>
<div data-el="statsNTreeContexts" class="context-stats" title="Amount of child contexts in subgraph"></div>
<div data-el="statsNTreeStaticContexts" class="context-stats" title="Amount of static contexts involved in subgraph"></div>
<div data-el="statsNTreeFileCalled" class="context-stats" title="Amount of files involved in subgraph"></div>
<div data-el="statsNTreeStaticContexts" class="context-stats" title="Amount of static contexts involved in subgraph"></div>
<div data-el="statsNTreeContexts" class="context-stats" title="Amount of child contexts in subgraph"></div>
<div data-el="statsNTreeTraces" class="context-stats" title="Amount of traces in subgraph. This is a rough measure."></div>
</div>
<div class="flex-row">
Expand Down
7 changes: 6 additions & 1 deletion dbux-graph-common/src/componentLib/ComponentEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import NestedError from '@dbux/common/src/NestedError';
import isFunction from 'lodash/isFunction';
import RemoteCommandProxy from './RemoteCommandProxy';


// eslint-disable-next-line no-unused-vars
const DefaultLogger = newLogger('ComponentEndpoint (pre init)');

/**
* @template {ComponentEndpoint} C
*/
Expand Down Expand Up @@ -50,8 +54,8 @@ class ComponentEndpoint {
constructor() {
// TODO: `this.constructor.name` won't work on Host when enabling minifcation/obfuscation in webpack/bundler
// NOTE: Client already has a better way for this
this.logger = DefaultLogger;
this._componentName = this.constructor._componentName || this.constructor.name;
this.logger = newLogger(this.debugTag);
}

_build(componentManager, parent, componentId, initialStateArg) {
Expand All @@ -63,6 +67,7 @@ class ComponentEndpoint {
this._remoteInternal = new RemoteCommandProxy(componentManager.ipc, componentId, '_publicInternal');

this.state = this.makeInitialState(initialStateArg);
this.logger = newLogger(this.debugTag);
}

makeInitialState(initialStateArg) {
Expand Down
19 changes: 10 additions & 9 deletions dbux-graph-host/src/componentLib/HostComponentEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import ComponentEndpoint from '@dbux/graph-common/src/componentLib/ComponentEndp
import NestedError from '@dbux/common/src/NestedError';
import HostComponentList from './HostComponentList';

// const Verbose = true;
const Verbose = false;
const Verbose = 1;
// const Verbose = 0;
// const Verbose = 2;

/**
* The Host endpoint controls the Client endpoint.
Expand Down Expand Up @@ -43,7 +44,7 @@ class HostComponentEndpoint extends ComponentEndpoint {

constructor() {
super();

this.aliases = this.aliases || [this._componentName];

this.children = new HostComponentList(this, 'child');
Expand Down Expand Up @@ -193,9 +194,9 @@ class HostComponentEndpoint extends ComponentEndpoint {
try {
this._waitingForUpdate = true;
this._preInit(); // 0. host: preInit
Verbose && this.logger.debug('init start');
Verbose > 1 && this.logger.debug('init start');
this.init(); // 1. host: init
Verbose && this.logger.debug('update start');
Verbose > 1 && this.logger.debug('update start');
this.update(); // 2. host: update
}
catch (err) {
Expand All @@ -210,7 +211,7 @@ class HostComponentEndpoint extends ComponentEndpoint {
Verbose && this.logger.debug('_doInitClient start');
const resultFromClientInit = this.parent && await this.componentManager._initClient(this); // 3. client: init -> update (ignore `internal root component`)
// success // 4. waitForInit resolved
Verbose && this.logger.debug('initialized');
Verbose > 1 && this.logger.debug('initialized');
this._isInitialized = true;
return resultFromClientInit;
}
Expand Down Expand Up @@ -397,7 +398,7 @@ class HostComponentEndpoint extends ComponentEndpoint {
* First disposes all descendants (removes recursively) and then removes itself.
*/
dispose(silent = false) {
Verbose && this.logger.debug('dispose start');
Verbose > 1 && this.logger.debug('dispose start');
super.dispose();

// Promise.resolve(this.waitForInit()).then(() => {
Expand All @@ -417,14 +418,14 @@ class HostComponentEndpoint extends ComponentEndpoint {
Promise.resolve(this.waitForInit())
.then(async () => {
await this._remoteInternal.dispose();
Verbose && this.logger.debug('disposed');
Verbose > 1 && this.logger.debug('disposed');
})
.catch((err) => {
if (!silent) {
this.logger.error('error while disposing -', err);
}
else {
Verbose && this.logger.debug('error while disposing');
Verbose > 1 && this.logger.debug('error while disposing');
}
})
.finally(() => {
Expand Down
79 changes: 50 additions & 29 deletions dbux-graph-host/src/graph/SyncGraphBase.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import NanoEvents from 'nanoevents';
import minBy from 'lodash/minBy';
import difference from 'lodash/difference';
import isPlainObject from 'lodash/isPlainObject';
import { newLogger } from '@dbux/common/src/log/logger';
import allApplications from '@dbux/data/src/applications/allApplications';
Expand All @@ -18,6 +19,7 @@ import HostComponentEndpoint from '../componentLib/HostComponentEndpoint';
const { log, debug, warn, error: logError } = newLogger('SyncGraphBase');

const Verbose = 1;
// const Verbose = 1;

/**
* @param {{ applicationId, contextId }} applicationIdHolder
Expand Down Expand Up @@ -157,18 +159,22 @@ class CallGraphNodes {
floodHole(contexts, frontier, context, goUp = true, goLeft = true, goRight = true, goDown = true, parentContext = undefined, siblingIndex = undefined) {
const dp = getDp(context);

if (context.contextId === 1) {
console.debug(`[flood] ${context.contextId} ${goUp && '↑' || ''}${goLeft && '←' || ''}${goDown && '↓' || ''}${goRight && '→' || ''}`);
}

parentContext = parentContext === undefined ?
(context.parentContextId && dp.collections.executionContexts.getById(context.parentContextId)) :
null;

// 1. go up (parent)
if (goUp && parentContext) {
if (this.isHole(parentContext)) {
// add to hole: ←↑→
contexts.push(parentContext);
this.floodHole(contexts, frontier, parentContext, true, true, true, false);
}
}
(parentContext || null);

// // 1. go up (parent) → NOTE: we DON'T go up, because the node creation algorithm is always top-down
// if (goUp && parentContext) {
// if (this.isHole(parentContext)) {
// // add to hole: ←↑→
// contexts.push(parentContext);
// this.floodHole(contexts, frontier, parentContext, true, true, true, false);
// }
// }

if (goLeft || goRight) {
const contextsOfParent = parentContext &&
Expand Down Expand Up @@ -394,6 +400,12 @@ class SyncGraphBase extends GraphBase {
* @type {Map<ExecutionContext, ContextNode>}
*/
contextNodesByContext;

/**
* @type {ExecutionContext[]}
*/
roots;

/**
* @type {CallGraphNodes}
*/
Expand All @@ -403,7 +415,7 @@ class SyncGraphBase extends GraphBase {
const includePredicate = this.componentManager.externals.getContextFilter();
this._nodes = new CallGraphNodes(this, includePredicate);

this.roots = new Set();
this.roots = [];
this.state.applications = [];
this._emitter = new NanoEvents();
this._unsubscribeOnNewData = [];
Expand Down Expand Up @@ -443,7 +455,7 @@ class SyncGraphBase extends GraphBase {
}

clear() {
this.roots = new Set();
this.roots = [];
return this._nodes.clear();
}

Expand All @@ -452,37 +464,43 @@ class SyncGraphBase extends GraphBase {
// ###########################################################################

/**
* @param {*} newRootsArr
*
*/
updateAllRoots() {
const allRoots = this.getAllRootContexts();
const newRoots = new Set(allRoots);
const newRoots = allRoots;
const oldRoots = this.roots;

const removedRoots = difference(oldRoots, newRoots);
const addedRoots = difference(newRoots, oldRoots);

// always re-subscribe since applicationSet clears subscribtion everytime it changes
this._resubscribeOnData();

// remove old roots
for (const root of oldRoots) {
if (!newRoots.has(root)) {
this._removeContextNode(root);
}
for (const root of removedRoots) {
this._removeContextNode(root);
}

// remove new roots if exists as an old children, then add
for (const root of newRoots) {
const node = this.getContextNodeByContext(root);
if (node) {
if (oldRoots.has(root)) {
continue;
}
else {
this._removeContextNode(root);
}
}
// add new roots
for (const root of addedRoots) {
this._addRoot(root);
}

// // remove new roots if exists as an old children, then re-add (serves as a refresh)
// for (const root of addedRoots) {
// const node = this.getContextNodeByContext(root);
// if (node) {
// if (oldRoots.has(root)) {
// continue;
// }
// else {
// this._removeContextNode(root);
// }
// }
// this._addRoot(root);
// }

this.roots = newRoots;

this._setApplicationState();
Expand Down Expand Up @@ -596,6 +614,9 @@ class SyncGraphBase extends GraphBase {
* State management
* #########################################################################*/

/**
* Basically a `forceUpdate()` with `state.applications` updated
*/
_setApplicationState() {
const update = {
applications: this.makeApplicationState()
Expand Down
29 changes: 24 additions & 5 deletions dbux-graph-host/src/graph/syncGraph/ContextNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,31 @@ import GraphNodeMode from '@dbux/graph-common/src/shared/GraphNodeMode';
import ExecutionContext from '@dbux/common/src/types/ExecutionContext';
import HostComponentEndpoint from '../../componentLib/HostComponentEndpoint';

/** @typedef { import("@dbux/data/src/RuntimeDataProvider").default } RuntimeDataProvider */

/**
* @param {{ applicationId, contextId }} applicationIdHolder
* @return {RuntimeDataProvider}
*/
function getDp(applicationIdHolder) {
return allApplications.getById(applicationIdHolder.applicationId).dataProvider;
}

class ContextNode extends HostComponentEndpoint {
get debugTag() {
const dp = getDp(this.state.context);
return `[${this.componentName}: ${dp.util.makeContextInfo(this.state.context)}]`;
// return super.debugTag;
}

init() {
this.childrenBuilt = false;
this.state.visible = this.hiddenNodeManager ? this.hiddenNodeManager.shouldBeVisible(this) : true;

const {
/**
* @type {ExecutionContext}
*/
context
} = this.state;

Expand Down Expand Up @@ -98,7 +117,7 @@ class ContextNode extends HostComponentEndpoint {
get allContextStats() {
return this.dp.queries.statsByContext(this.contextId);
}

get hiddenNodeManager() {
return this.context.graphRoot.controllers.getComponent('HiddenNodeManager');
}
Expand Down Expand Up @@ -155,9 +174,8 @@ class ContextNode extends HostComponentEndpoint {
* @return {ExecutionContext[]}
*/
getAllChildContexts() {
const { applicationId, contextId } = this.state.context;
const dp = allApplications.getById(applicationId).dataProvider;
return dp.indexes.executionContexts.children.get(contextId) || EmptyArray;
const dp = getDp(this.state.context);
return dp.indexes.executionContexts.children.get(this.state.context.contextId) || EmptyArray;
}

/**
Expand All @@ -167,8 +185,9 @@ class ContextNode extends HostComponentEndpoint {
*/
getActualChildContexts() {
const childContexts = this.getAllChildContexts();
const dp = getDp(this.state.context);
return childContexts.filter(childContext => {
if (this.context.graphRoot.roots.has(childContext)) {
if (dp.util.isRootContext(childContext.contextId)) {
return false;
}

Expand Down

0 comments on commit e220ed3

Please sign in to comment.