{"id":18369869,"url":"https://github.com/cawfree/react-native-elsewhere","last_synced_at":"2025-10-12T16:23:59.061Z","repository":{"id":34909413,"uuid":"187916685","full_name":"cawfree/react-native-elsewhere","owner":"cawfree","description":"Ridiculously simple React Native thread unblocking.","archived":false,"fork":false,"pushed_at":"2023-01-03T22:20:19.000Z","size":2094,"stargazers_count":15,"open_issues_count":18,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-22T04:31:51.882Z","etag":null,"topics":["background","lokijs","performance","react","react-native","task","thread","threading"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cawfree.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-21T21:34:18.000Z","updated_at":"2024-03-20T09:50:33.000Z","dependencies_parsed_at":"2022-08-26T02:10:59.293Z","dependency_job_id":null,"html_url":"https://github.com/cawfree/react-native-elsewhere","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cawfree%2Freact-native-elsewhere","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cawfree%2Freact-native-elsewhere/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cawfree%2Freact-native-elsewhere/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cawfree%2Freact-native-elsewhere/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cawfree","download_url":"https://codeload.github.com/cawfree/react-native-elsewhere/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247531351,"owners_count":20953937,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["background","lokijs","performance","react","react-native","task","thread","threading"],"created_at":"2024-11-05T23:32:48.799Z","updated_at":"2025-10-12T16:23:54.030Z","avatar_url":"https://github.com/cawfree.png","language":"JavaScript","funding_links":["https://www.buymeacoffee.com/cawfree"],"categories":[],"sub_categories":[],"readme":"# 🚨 Deprecation Notice\nThe `\u003cElsewhere /\u003e` component relies upon the `toString()` method to delegate native functions to a `\u003cWebView /\u003e`, which is [not possible to achieve in conjunction with the Hermes engine](https://github.com/facebook/hermes/issues/114).\n\nDevelopers are urged to migrate into [nodejs-mobile-react-native-bridge](https://github.com/cawfree/nodejs-mobile-react-native-bridge).\n\n# react-native-elsewhere\nRidiculously simple React Native thread unblocking.\n\n## 🚀 Getting Started\nUsing [npm](https://www.npmjs.com/package/@cawfree/react-native-elsewhere):\n```shell\nnpm install --save @cawfree/react-native-elsewhere\n```\nUsing [yarn](https://www.npmjs.com/package/@cawfree/react-native-elsewhere):\n```shell\nyarn add @cawfree/react-native-elsewhere\n```\nYeah, no linking. 👍\n\n## ⚙ How does it work?\nBy delegating stateless JavaScript computation to a [`\u003cWebView /\u003e`](https://facebook.github.io/react-native/docs/webview), your app can maintain responsive whilst you crunch through heavy computation in the background.\n\n```javascript\nimport React, {Component} from 'react';\nimport {WebView, Button, Platform, StyleSheet, Text, View, Alert} from 'react-native';\n\nimport Elsewhere from '@cawfree/react-native-elsewhere';\n\n// https://gist.github.com/sqren/5083d73f184acae0c5b7\nfunction doSomethingIntense(postMessage, { source }) {\n  const now = new Date();\n  let result = 0;   \n  for (var i = Math.pow(10, 7); i \u003e= 0; i--) {      \n    result += Math.atan(i) * Math.tan(i);\n  };\n  postMessage({\n    source,\n    result,\n    dt: new Date().getTime() - now.getTime(),\n  });\n}\n\ntype Props = {};\nexport default class App extends Component\u003cProps\u003e {\n  state = {\n    postMessage: () =\u003e null,\n  }\n  render() {\n    const {\n      postMessage,\n    } = this.state;\n    return (\n      \u003cView style={styles.container}\u003e\n        \u003cElsewhere\n          WebView={WebView}\n          engine={doSomethingIntense}\n          onMessage={data =\u003e Alert.alert(JSON.stringify(data))}\n          onPostMessage={(postMessage) =\u003e {\n            this.setState({\n              postMessage,\n            });\n          }}\n        /\u003e\n        \u003cText style={styles.welcome}\u003eWelcome to React Native!\u003c/Text\u003e\n        \u003cText style={styles.instructions}\u003eTo get started, open the debug menu and enable the performance monitor so we can watch the JS frame rate.\u003c/Text\u003e\n        \u003cText style={styles.instructions}\u003eIt should read a steady 60fps. ⏰ \u003c/Text\u003e\n        \u003cText style={styles.instructions}\u003e{'🤓'}\u003c/Text\u003e\n        \u003cText style={styles.instructions}\u003eTap the Button below to watch your frame rate plummet! 📉 \u003c/Text\u003e\n        \u003cView\n          style={{\n            padding: 10,\n          }}\n        \u003e\n          \u003cButton\n            title=\"Run on JS thread\"\n            style={styles.button}\n            onPress={() =\u003e doSomethingIntense(\n              (data) =\u003e Alert.alert(JSON.stringify(data)),\n              { source: 'ui' },\n            )}\n          /\u003e\n        \u003c/View\u003e\n        \u003cText style={styles.instructions}\u003eIntense, right? Now run the exact same operation inside of an Elsewhere. 📈 \u003c/Text\u003e\n        \u003cView\n          style={{\n            padding: 10,\n          }}\n        \u003e\n          \u003cButton\n            title=\"Run on Elsewhere\"\n            style={styles.button}\n            onPress={() =\u003e postMessage(\n              { source: 'web' },\n            )}\n          /\u003e\n        \u003c/View\u003e\n        \u003cText\u003eSee how the frame rate stays at 60? Magic. 🔮 \u003c/Text\u003e\n      \u003c/View\u003e\n    );\n  }\n}\n\nconst styles = StyleSheet.create({\n  container: {\n    flex: 1,\n    justifyContent: 'center',\n    alignItems: 'center',\n    backgroundColor: '#F5FCFF',\n  },\n  welcome: {\n    fontSize: 20,\n    textAlign: 'center',\n    margin: 10,\n  },\n  instructions: {\n    textAlign: 'center',\n    color: '#333333',\n    marginBottom: 5,\n  },\n  button: {\n    marginBottom: 15,\n  }\n});\n```\n\n## 💾 Persistence\nUsing the `scripts` prop, it is possible to define an array of urls that you'd like to import as `\u003cscript/\u003e`s within your JavaScript logic. Some scripts you call to may rely on localStorage for persistence between launches of your application; however for this to work successfully, your `engine` will need to be serialized to a file location so that thr browser can associate stored data with a given file `uri`.\n\nThis can be achieved using the following, which uses [`react-native-fs`](https://github.com/itinance/react-native-fs) as the file I/O utility.\n\n```javascript\nimport React from 'react';\nimport Elsewhere from '@cawfree/react-native-elsewhere';\nimport fs from 'react-native-fs';\n\n// XXX: Declare a uri where we'd like to store the evaluated\n//      engine on the device file system.\nconst uri = `${fs.CachesDirectoryPath}/elsewhere.html`;\n\nexport default class Persisted extends React.Component {\n  render() {\n    return (\n      \u003cElsewhere\n        engine={engine}\n        uri={uri}\n        scripts={[\n          // XXX: for example, you could use lokijs as a persistent database!\n          'https://rawgit.com/techfort/LokiJS/master/src/lokijs.js',\n          'https://rawgit.com/techfort/LokiJS/master/src/loki-indexed-adapter.js',\n          // XXX: or you could make lodash available to your engine\n          'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js',\n        ]}\n        onRequestPersist={(html, url) =\u003e {\n          // XXX: You must return a Promise, which when reslved guarantees\n          //      that the engine html has been saved to the requested uri.\n          return fs.writeFile(\n            url,\n            html,\n          );\n        }}\n      /\u003e\n    );\n  }\n}\n```\n\n## ✌️ License\n[MIT](https://opensource.org/licenses/MIT)\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.buymeacoffee.com/cawfree\"\u003e\n    \u003cimg src=\"https://cdn.buymeacoffee.com/buttons/default-orange.png\" alt=\"Buy @cawfree a coffee\" width=\"232\" height=\"50\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcawfree%2Freact-native-elsewhere","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcawfree%2Freact-native-elsewhere","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcawfree%2Freact-native-elsewhere/lists"}