Ex10: New Virtual adapter (interface)

The point of a virtual adapter is to run one dapplet on many adapters. In this exercise you should implement a virtual adapter for two search adapters.

Here is the initial code for this example: ex10-new-virtual-adapter-exercise.

The starting point of exercise is the solution for the Ex08: New Site specific adapter. There is an adapter for Google Search and a dapplet for it.

  1. Add a search adapter for Yahoo. See the code on GitHub: example-yahoo-adapter.

  2. Add /example-virtual-adapter/ folder that should have a structure similar to the Google Adapter Example.

1example-virtual-adapter
2├── dapplet.json
3├── package-lock.json
4├── package.json
5├── rollup.config.js
6├── src
7| └── index.ts
8└── tsconfig.json
  1. The /example-virtual-adapter/src/ folder should contain only index.ts that exports an empty object.
export default {};
  1. In the /example-virtual-adapter/dapplet.json set "type": "INTERFACE" and remove "contextIds" and "dependencies".
1{
2 "name": { "$ref": "package.json#/name" },
3 "branch": "default",
4 "version": { "$ref": "package.json#/version" },
5 "type": "INTERFACE",
6 "title": "Virtual Adapter Example",
7 "description": { "$ref": "package.json#/description" },
8 "main": { "$ref": "package.json#/main" }
9}
  1. Add virtual adapter to "interfaces: []" in dapplet.json of Google and Yahoo adapters and to "contextIds" and "dependencies" of the dapplet-feature.
1// example-google-adapter/dapplet.json example-yahoo-adapter/dapplet.json
2{
3 ...
4 "interfaces": {
5 "example-virtual-adapter.dapplet-base.eth": "0.2.0"
6 }
7}
1// dapplet-feature/dapplet.json
2{
3 ...
4 "contextIds": ["example-virtual-adapter.dapplet-base.eth"],
5 ...
6 "dependencies": {
7 "example-virtual-adapter.dapplet-base.eth": "0.2.0"
8 }
9}
  1. Change injecting of Google adapter to virtual adapter in /dapplet-feature/src/index.ts.
@Inject('example-virtual-adapter.dapplet-base.eth') public adapter: any;

Add to /package.json to install and run the virtual Google adapters, Yahoo adapters and the dapplet.

1// scripts
2
3"postinstall": "concurrently -c \"yellow,magenta,green,blue\" -n \"example-google-adapter,example-yahoo-adapter,dapplet,example-virtual-adapter\" \"cd example-google-adapter && npm i\" \"cd example-yahoo-adapter && npm i\" \"cd dapplet-feature && npm i\" \"cd example-virtual-adapter && npm i\"",
4"start": "concurrently -c \"yellow,magenta,green,blue\" -n \"example-google-adapter,example-yahoo-adapter,dapplet,example-virtual-adapter\" \"cd example-google-adapter && npm start\" \"cd example-yahoo-adapter && npm start\" \"cd dapplet-feature && npm start\" \"cd example-virtual-adapter && npm start\"",

Here is the result: ex10-new-virtual-adapter-solution.

Run the dapplet:

npm i
npm start

In this example we run four servers concurrently. This means you have to add four registry addresses to the Dapplet extension in the Development tab. Click here for instructions.