Skip to content

Added provider-consumer example (multiple related packages). #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3e2dfa5
Initial provider-consumer sample, WIP.
ericsnekbytes Aug 31, 2023
7442fe6
Fixed method name.
ericsnekbytes Aug 31, 2023
0c731cf
Added comments to the provider-consumer samples.
ericsnekbytes Sep 1, 2023
f3fe812
Added comments to provider/consumer example.
ericsnekbytes Sep 1, 2023
14350de
Comment cleanup.
ericsnekbytes Sep 1, 2023
2eb6512
Minor formatting.
ericsnekbytes Sep 1, 2023
9d115b5
Updated project description text.
ericsnekbytes Sep 1, 2023
03f9cc9
Updated project description text.
ericsnekbytes Sep 1, 2023
de11b88
Minor formatting.
ericsnekbytes Sep 1, 2023
7a5c3c0
Minor formatting.
ericsnekbytes Sep 1, 2023
82d1ad5
Added third provider-consumer sub example, an additional consumer.
ericsnekbytes Sep 1, 2023
76438b9
Added step counter signal for display updates.
ericsnekbytes Sep 26, 2023
c086518
Update readme
ericsnekbytes Sep 28, 2023
e46c5b9
Renamed folders to match poackage names.
ericsnekbytes Sep 28, 2023
9645adf
Updated local dependency paths.
ericsnekbytes Sep 28, 2023
8bab7f7
Updated extension specific READMEs.
ericsnekbytes Sep 28, 2023
44238f1
Added preview, updated README.
ericsnekbytes Sep 28, 2023
efbe9a2
Added singleton package metadata notes.
ericsnekbytes Sep 28, 2023
8b3872c
Update README/fix links.
ericsnekbytes Sep 29, 2023
afee7d7
Updated titles/links.
ericsnekbytes Sep 29, 2023
e1a5525
Update titles/links in READMEs.
ericsnekbytes Sep 29, 2023
2689a44
Updated root package metadata.
ericsnekbytes Oct 2, 2023
164b0ad
Merge branch 'main' into provider_consumer_example
ericsnekbytes Jan 24, 2024
b1452cd
Workaround for dependency 'any' usage and version conflicts.
ericsnekbytes Jun 9, 2025
e420ec7
Linting.
ericsnekbytes Jun 9, 2025
ea8a711
Fix linting errors for unrelated packages.
ericsnekbytes Jun 9, 2025
626490b
Modify tsconfigs: Re-enable implicit any checks, add skipLibCheck.
ericsnekbytes Jun 9, 2025
bfb24cc
Extra linting.
ericsnekbytes Jun 9, 2025
cb58d6c
Revised to address feedback.
ericsnekbytes Jun 9, 2025
101cfd6
Added explanation of declaration merging.
ericsnekbytes Jun 9, 2025
1e56e65
Add changes from PR feedback.
ericsnekbytes Jun 9, 2025
7b0f58e
Add updated version of entities with types.
ericsnekbytes Jun 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added comments to the provider-consumer samples.
  • Loading branch information
ericsnekbytes committed Sep 1, 2023
commit 0c731cf8d25e1c0283f499b7822654ff9c391d7b
54 changes: 53 additions & 1 deletion compat_4a/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,62 @@
// This is one of three related extension examples that demonstrate
// JupyterLab's provider-consumer pattern, where plugins can depend
// on and reuse features from one another. The three packages that
// make up the complete example are:
//
// 1. The step_counter package (this one). This holds a token, a
// class and interface that make up a stock implementation of
// the "step_counter" service, and a provider plugin that
// makes an instance of the Counter available to JupyterLab
// as a service object.
// 2. The step_counter_extension package, that holds a UI/interface
// in JupyterLab for users to count their steps that connects
// with/consumes the step_counter service object via a consumer plugin.
// 3. The leap_counter_extension package, that holds an alternate
// way for users to count leaps. Like the step_counter_extension
// package, this holds a UI/interface in JupyterLab, and a consumer
// plugin that also requests/consumes the step_counter service
// object. The leap_counter_extension package demonstrates how
// an unrelated plugin can depend on and reuse features from
// an existing plugin. Users can add install either the
// step_counter_extension, the leap_counter_extension or both
// to get whichever features they prefer (with both reusing
// the step_counter service object).

import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';

import { Token } from '@lumino/coreutils';

// The StepCounterItem interface is used as part of JupyterLab's
// provider-consumer pattern. This interface is supplied to the
// token instance (the StepCounter token), and JupyterLab will
// use it to type-check any service-object associated with the
// token that a provider plugin supplies to check that it conforms
// to the interface.
interface StepCounterItem {
// registerStatusItem(id: string, statusItem: IStatusBar.IItem): IDisposable;
getStepCount(): number;
incrementStepCount(count: number): void;
}

// TODO define an interface for type checking the associated service
// The token is used to identify a particular "service" in
// JupyterLab's extension system (here the StepCounter token
// identifies the example "Step Counter Service", which is used
// to store and increment step count data in JupyterLab). Any
// plugin can use this token in their "requires" or "activates"
// list to request the service object associated with this token!
const StepCounter = new Token<StepCounterItem>(
'step_counter:StepCounter',
'A service for counting steps.'
);

// This class holds step count data/utilities. An instance of
// this class will serve as the service object associated with
// the StepCounter token (Other developers can substitute their
// own implementation of a StepCounterItem instead of using this
// one, by becoming a provider of the StepCounter token).
class Counter implements StepCounterItem {

_stepCount: number;
Expand All @@ -34,15 +74,27 @@ class Counter implements StepCounterItem {
}
}

// This plugin is a "provider" in JupyterLab's provider-consumer pattern.
// For a plugin to become a provider, it must list the token it wants to
// provide a service object for in its "provides" list, and then it has
// to return that object (in this case, an instance of the example Counter
// class defined above) from the function supplied as its activate property.
// It also needs to supply the interface (the one the service object
// implements) to JupyterFrontEndPlugin when it's defined.
const plugin: JupyterFrontEndPlugin<StepCounterItem> = {
id: 'step_counter:provider_plugin',
description: 'Provider plugin for the step_counter\'s "counter" service object.',
autoStart: true,
provides: StepCounter,
// The activate function here will be called by JupyterLab when the plugin loads
activate: (app: JupyterFrontEnd) => {
console.log('JupyterLab X1 extension step_counter\'s provider plugin is activated!');
const counter = new Counter();

// Since this plugin "provides" the "StepCounter" service, make sure to
// return the object you want to use as the "service object" here (when
// other plugins request the StepCounter service, it is this object
// that will be supplied)
return counter;
}
};
Expand Down