jupyterlab/jupyterlab

Document how to extend tokens

Open

#7,464 opened on Nov 2, 2019

View on GitHub
 (1 comment) (0 reactions) (0 assignees)TypeScript (13,454 stars) (2,937 forks)batch import
documentationenhancementhelp wanted

Description

Description

This was originally reported on phosphor.

Paraphrasing the issue: We should document how a token would extend another token. In other words, (the object registered with) token B implements the interface of (the object registered with) token A, plus some extra stuff. Normal plugins would require token A, but some might require token B. It would be nice if the dependency injection framework could register that token B could be served to those requesting token A.

@vidartf suggested this pattern:

// File defining tokens
import {TokenA} from 'amodule';
import {Token} from '@phosphor/...';

export interface TokenB extends TokenA {
  myProp: string;
}

export const TokenB = new Token(...);
// File defining plugins
import {TokenA, activateA} from 'amodule';
import {TokenB} from 'bmodule/lib/tokens';

const MyPluginB = {
  provides: TokenB;
  activate: (...) => {
    const A = activateA(...);
    A.myProp = 'foo!';
    return A;
  }
}

const MyPluginA = {
  provides: TokenA;
  requires: [TokenB];
  activate: (..., B: TokenB) => {
    return B;
  }
}

export {MyPluginA, MyPluginB};

This should be documented on the extension dev page.

Contributor guide

Document how to extend tokens · jupyterlab/jupyterlab#7464 | Good First Issue