mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-01-11 22:40:31 +00:00
worker & bridge moved to core-web
This commit is contained in:
parent
3c529e3366
commit
7657bad07c
5 changed files with 5 additions and 76 deletions
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -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]));
|
||||
};
|
||||
|
|
@ -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'),
|
||||
|
|
|
|||
Loading…
Reference in a new issue