Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v2' into bdo/number-edgetypes
Browse files Browse the repository at this point in the history
  • Loading branch information
thebriando committed Apr 20, 2021
2 parents 99c1761 + 3baf990 commit b453252
Show file tree
Hide file tree
Showing 29 changed files with 1,779 additions and 1,128 deletions.
217 changes: 138 additions & 79 deletions packages/core/core/src/AssetGraph.js

Large diffs are not rendered by default.

513 changes: 305 additions & 208 deletions packages/core/core/src/BundleGraph.js

Large diffs are not rendered by default.

81 changes: 81 additions & 0 deletions packages/core/core/src/ContentGraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// @flow strict-local

import Graph, {type GraphOpts} from './Graph';
import type {ContentKey, Node, NodeId} from './types';
import nullthrows from 'nullthrows';

export type SerializedContentGraph<TNode: Node, TEdgeType: number = 0> = {|
...GraphOpts<TNode, TEdgeType>,
_contentKeyToNodeId: Map<ContentKey, NodeId>,
|};

export default class ContentGraph<
TNode: Node,
TEdgeType: number = 0,
> extends Graph<TNode, TEdgeType> {
_contentKeyToNodeId: Map<ContentKey, NodeId>;

constructor(opts: ?SerializedContentGraph<TNode, TEdgeType>) {
if (opts) {
let {_contentKeyToNodeId, ...rest} = opts;
super(rest);
this._contentKeyToNodeId = _contentKeyToNodeId;
} else {
super();
this._contentKeyToNodeId = new Map();
}
}

// $FlowFixMe[prop-missing]
static deserialize(
opts: SerializedContentGraph<TNode, TEdgeType>,
): ContentGraph<TNode, TEdgeType> {
return new ContentGraph(opts);
}

// $FlowFixMe[prop-missing]
serialize(): SerializedContentGraph<TNode, TEdgeType> {
return {
...super.serialize(),
_contentKeyToNodeId: this._contentKeyToNodeId,
};
}

addNodeByContentKey(contentKey: ContentKey, node: TNode): NodeId {
if (!this.hasContentKey(contentKey)) {
let nodeId = super.addNode(node);
this._contentKeyToNodeId.set(contentKey, nodeId);
return nodeId;
} else {
let existingNodeId = this.getNodeIdByContentKey(contentKey);
let existingNode = nullthrows(this.getNodeByContentKey(contentKey));
existingNode.value = node.value;
this.updateNode(existingNodeId, existingNode);
return existingNodeId;
}
}

getNodeByContentKey(contentKey: ContentKey): ?TNode {
let nodeId = this._contentKeyToNodeId.get(contentKey);
if (nodeId != null) {
return super.getNode(nodeId);
}
}

getNodeIdByContentKey(contentKey: ContentKey): NodeId {
return nullthrows(
this._contentKeyToNodeId.get(contentKey),
'Expected content key to exist',
);
}

hasContentKey(contentKey: ContentKey): boolean {
return this._contentKeyToNodeId.has(contentKey);
}

removeNode(nodeId: NodeId) {
this._assertHasNodeId(nodeId);
this._contentKeyToNodeId.delete(nullthrows(this.getNode(nodeId)).id);
super.removeNode(nodeId);
}
}
Loading

0 comments on commit b453252

Please sign in to comment.