Skip to content
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

RPC functions to new service #1485

Merged
merged 12 commits into from Apr 8, 2021
Merged

RPC functions to new service #1485

merged 12 commits into from Apr 8, 2021

Conversation

kuosman
Copy link
Contributor

@kuosman kuosman commented Apr 1, 2021

This PR change RPC functions creations and bindings to new RpcService.

Moved RPC supported function creations to bundles/plugins.
RPC functions creations are separated into the following bundles/plugins:

  • RPC: getSupportedEvents, getSupportedFunctions, getSupportedRequests, getInfo, resetState, getCurrentState, useState, sendUIEvent
  • AbstractMapModule: getAllLayers, getMapBbox, getMapPosition, getZoomRange, zoomIn, zoomOut, zoomTo, getPixelMeasuresInScale, setCursorStyle
  • MapModuleClass.ol: getScreenshot
  • VectorLayerPlugin: getFeatures

Only RPC added functions are defaults (if not configured allowedfunctions), other are optional and exists only when view contains bundle/plugin.

How to add new function to RPC

This allows all bundles to add new function(s) to RPC (also application-specific) and use them in published map.

For example this adds example function to RPC:

// listen to application started event and register new RPC function.
Oskari.on('app.start', function (details) {
    const rpcService = Oskari.getSandbox().getService('Oskari.mapframework.bundle.rpc.service.RpcService');

    if (!rpcService) {
        return;
    }

    rpcService.addFunction('example', function () {
        console.log('New added RPC function');
        return 'my example result';
    });

    // async functions can return a promise and signal failure through reject
    rpcService.addFunction('example2', function () {
            return new Promise((resolve, reject) => {
                setTimeout(() => resolve('my async example result'), 500);
              });
    });
});

Now you can call created example function from your published map:

channel.example(function(data) {
    console.log(data);
});

channel.example2(function(data) {
    console.log(data);
});

@ZakarFin
Copy link
Member

ZakarFin commented Apr 6, 2021

This is awesome! Added a few suggestions to make this clearer for bundles registering functions:

  • the registered function name should be given as string as additional param so build-scripts won't change the names
  • the registered function should always return something. Async functions can return a promise which allows them to signal failure as well
  • functions not returning either a value or a Promise should be considered faulty and trigger the error handling

@sonarcloud
Copy link

sonarcloud bot commented Apr 8, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@ZakarFin ZakarFin added this to the 2.3.0 milestone Apr 8, 2021
@ZakarFin ZakarFin merged commit 02dc8e9 into oskariorg:develop Apr 8, 2021
@ZakarFin
Copy link
Member

ZakarFin commented Apr 8, 2021

Thanks!

@kuosman kuosman deleted the rpcservice branch April 9, 2021 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants