From 630a6d6532ab5a13d3431bd9160197b1ded18421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dandelion=20Man=C3=A9?= Date: Mon, 28 May 2018 13:08:01 -0700 Subject: [PATCH] Add `DelegateNodeReference` to `graph` module (#307) Also, rename `NodeDelegateReference` to `DelegateNodeReference` in the design doc. Test plan: Didn't add tests, as the implementation is trivial. Flow passes. --- .../address_payload_unification_design.md | 8 ++++---- src/core2/graph.js | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/core2/address_payload_unification_design.md b/src/core2/address_payload_unification_design.md index d168302..eaab9b7 100644 --- a/src/core2/address_payload_unification_design.md +++ b/src/core2/address_payload_unification_design.md @@ -191,10 +191,10 @@ convenient implementation detail. Clients won’t directly use `IndexedNodeReference`, though; this will be a non-exported class of the `core/graph.js` module. Instead, they will -provide a semantic extension of `NodeDelegateReference`: +provide a semantic extension of `DelegateNodeReference`: ```javascript -class NodeDelegateReference implements NodeReference { +class DelegateNodeReference implements NodeReference { _base: NodeReference; constructor(base: NodeReference) { this._base = base; @@ -208,14 +208,14 @@ class NodeDelegateReference implements NodeReference { } ``` -(Note: `NodeDelegateReference` should actually have `_base` as a +(Note: `DelegateNodeReference` should actually have `_base` as a `Symbol`, but doing so requires Flow-appeasing hackery that I don’t want to include in this high-level overview.) By extending this class, plugins gain great power: ```javascript -class GithubNodeReference extends NodeDelegateReference { +class GithubNodeReference extends DelegateNodeReference { constructor(base: NodeReference) { super(base); const address = base.address(); diff --git a/src/core2/graph.js b/src/core2/graph.js index 9c2e2ec..00b55aa 100644 --- a/src/core2/graph.js +++ b/src/core2/graph.js @@ -167,3 +167,23 @@ export class Graph { } export type GraphJSON = any; + +export class DelegateNodeReference implements NodeReference { + // TODO(@wchargin): Use a Symbol here. + __DelegateNodeReference_base: NodeReference; + constructor(base: NodeReference) { + this.__DelegateNodeReference_base = base; + } + graph() { + return this.__DelegateNodeReference_base.graph(); + } + address() { + return this.__DelegateNodeReference_base.address(); + } + get() { + return this.__DelegateNodeReference_base.get(); + } + neighbors(options?: NeighborsOptions) { + return this.__DelegateNodeReference_base.neighbors(options); + } +}