From 7657bad07c15de33fdc78adeacdca7ce21868422 Mon Sep 17 00:00:00 2001 From: nklhrstv Date: Thu, 4 Aug 2022 17:07:00 +0300 Subject: [PATCH] worker & bridge moved to core-web --- src/App/App.js | 5 ++-- src/services/Core/CoreTransport.js | 2 +- src/services/Core/bridge.js | 47 ------------------------------ src/services/Core/worker.js | 25 ---------------- webpack.config.js | 2 +- 5 files changed, 5 insertions(+), 76 deletions(-) delete mode 100644 src/services/Core/bridge.js delete mode 100644 src/services/Core/worker.js diff --git a/src/App/App.js b/src/App/App.js index 570c655f0..248f553df 100644 --- a/src/App/App.js +++ b/src/App/App.js @@ -5,7 +5,7 @@ const React = require('react'); const { Router } = require('stremio-router'); const { Core, Shell, Chromecast, KeyboardShortcuts, ServicesProvider } = require('stremio/services'); const { NotFound } = require('stremio/routes'); -const { ToastProvider, CONSTANTS } = require('stremio/common'); +const { ToastProvider, sanitizeLocationPath, CONSTANTS } = require('stremio/common'); const CoreEventsToaster = require('./CoreEventsToaster'); const ErrorDialog = require('./ErrorDialog'); const routerViewsConfig = require('./routerViewsConfig'); @@ -19,7 +19,8 @@ const App = () => { core: new Core({ baseURI: document.baseURI, appVersion: process.env.VERSION, - shellVersion: null + shellVersion: null, + sanitizeLocationPath }), shell: new Shell(), chromecast: new Chromecast(), diff --git a/src/services/Core/CoreTransport.js b/src/services/Core/CoreTransport.js index 7a3a0c0ea..809a55fb4 100644 --- a/src/services/Core/CoreTransport.js +++ b/src/services/Core/CoreTransport.js @@ -1,7 +1,7 @@ // Copyright (C) 2017-2022 Smart code 203358507 const EventEmitter = require('eventemitter3'); -const Bridge = require('./bridge'); +const Bridge = require('@stremio/stremio-core-web/bridge'); function CoreTransport(args) { const events = new EventEmitter(); diff --git a/src/services/Core/bridge.js b/src/services/Core/bridge.js deleted file mode 100644 index 5d12ae44e..000000000 --- a/src/services/Core/bridge.js +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (C) 2017-2022 Smart code 203358507 - -function getId() { - return Math.random().toString(32).slice(2); -} - -function Bridge(context, scope) { - context.addEventListener('message', async ({ data: { request } }) => { - if (!request) return; - - const { id, path, args } = request; - try { - const object = path.reduce((obj, prop) => obj[prop], scope); - let data; - if (typeof object === 'function') { - const thisArg = path.slice(0, path.length - 1).reduce((obj, prop) => obj[prop], scope); - data = await object.apply(thisArg, args); - } else { - data = await object; - } - - context.postMessage({ response: { id, result: { data } } }); - } catch (error) { - context.postMessage({ response: { id, result: { error } } }); - } - }); - - this.call = async (path, args) => { - const id = getId(); - return new Promise((resolve, reject) => { - const onMessage = ({ data: { response } }) => { - if (!response || response.id !== id) return; - - context.removeEventListener('message', onMessage); - if ('error' in response.result) { - reject(response.result.error); - } else { - resolve(response.result.data); - } - }; - context.addEventListener('message', onMessage); - context.postMessage({ request: { id, path, args } }); - }); - }; -} - -module.exports = Bridge; diff --git a/src/services/Core/worker.js b/src/services/Core/worker.js deleted file mode 100644 index 393bc1a46..000000000 --- a/src/services/Core/worker.js +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (C) 2017-2022 Smart code 203358507 - -const sanitizeLocationPath = require('stremio/common/sanitizeLocationPath'); -const Bridge = require('./bridge'); - -const bridge = new Bridge(self, self); - -self.init = async ({ baseURI, appVersion, shellVersion }) => { - self.document = { baseURI }; - self.app_version = appVersion; - self.shell_version = shellVersion; - self.sanitize_location_path = sanitizeLocationPath; - self.get_location_hash = async () => bridge.call(['location', 'hash'], []); - self.local_storage_get_item = async (key) => bridge.call(['localStorage', 'getItem'], [key]); - self.local_storage_set_item = async (key, value) => bridge.call(['localStorage', 'setItem'], [key, value]); - self.local_storage_remove_item = async (key) => bridge.call(['localStorage', 'removeItem'], [key]); - const { default: initialize_api, initialize_runtime, get_state, get_debug_state, dispatch, analytics, decode_stream } = require('@stremio/stremio-core-web'); - self.getState = get_state; - self.getDebugState = get_debug_state; - self.dispatch = dispatch; - self.analytics = analytics; - self.decodeStream = decode_stream; - await initialize_api(require('@stremio/stremio-core-web/stremio_core_web_bg.wasm')); - await initialize_runtime((event) => bridge.call(['onCoreEvent'], [event])); -}; diff --git a/webpack.config.js b/webpack.config.js index 29fd80ca4..873679d58 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -17,7 +17,7 @@ module.exports = (env, argv) => ({ devtool: argv.mode === 'production' ? 'source-map' : 'eval-source-map', entry: { main: './src/index.js', - worker: './src/services/Core/worker.js' + worker: './node_modules/@stremio/stremio-core-web/worker.js' }, output: { path: path.join(__dirname, 'build'),