Skip to content

Latest commit

 

History

History
114 lines (84 loc) · 4.07 KB

rn-ses-archived-notes.md

File metadata and controls

114 lines (84 loc) · 4.07 KB

React Native + SES: Archived Notes

The case of these prototypes

Facts

// Source: node_modules/promise/lib/e6s-extensions.js
Promise.resolve = function (value) {

// At the Bundle
_$$_REQUIRE(_dependencyMap[0], "./core.js").resolve = function (value) {
  • prototype is a non-configurable property, that's why we cannot delete it:
// Console Query (at the bundle)
Object.getOwnPropertyDescriptor(_$$_REQUIRE(_dependencyMap[0], "./core.js").resolve, 'prototype')

// Response
{value: Object, writable: true, enumerable: false, configurable: false} = $5

Function instances that can be used as a constructor have a prototype property. Whenever such a function instance is created another ordinary object is also created and is the initial value of the function’s prototype property.

Solution

Arrow functions can never be used as constructor functions. Hence, they can never be invoked with the new keyword. As such, a prototype property does not exist for an arrow function.

Applying two patches

We apply a patch at metro-react-native-babel-preset to avoid transpiling the arrow functions

diff --git a/node_modules/metro-react-native-babel-preset/src/configs/main.js b/node_modules/metro-react-native-babel-preset/src/configs/main.js
index caa45fe..4ab5085 100644
--- a/node_modules/metro-react-native-babel-preset/src/configs/main.js
+++ b/node_modules/metro-react-native-babel-preset/src/configs/main.js
@@ -79,7 +79,7 @@ const getPreset = (src, options) => {
   } // TODO(gaearon): put this back into '=>' indexOf bailout
   // and patch react-refresh to not depend on this transform.

-  extraPlugins.push([require("@babel/plugin-transform-arrow-functions")]);
+  // extraPlugins.push([require("@babel/plugin-transform-arrow-functions")]);

   if (!isHermes) {
     extraPlugins.push([require("@babel/plugin-transform-computed-properties")]);

And a patch to the promise library

diff --git a/node_modules/promise/setimmediate/es6-extensions.js b/node_modules/promise/setimmediate/es6-extensions.js
index 24665f5..cbb2992 100644
--- a/node_modules/promise/setimmediate/es6-extensions.js
+++ b/node_modules/promise/setimmediate/es6-extensions.js
@@ -21,7 +21,7 @@ function valuePromise(value) {
   p._W = value;
   return p;
 }
-Promise.resolve = function (value) {
+Promise.resolve = (value) => {
   if (value instanceof Promise) return value;

   if (value === null) return NULL;
@@ -58,7 +58,7 @@ var iterableToArray = function (iterable) {
   return Array.prototype.slice.call(iterable);
 }

-Promise.all = function (arr) {
+Promise.all = (arr) => {
   var args = iterableToArray(arr);

   return new Promise(function (resolve, reject) {
@@ -98,13 +98,13 @@ Promise.all = function (arr) {
   });
 };

-Promise.reject = function (value) {
+Promise.reject = (value) => {
   return new Promise(function (resolve, reject) {
     reject(value);
   });
 };

-Promise.race = function (values) {
+Promise.race = (values) => {
   return new Promise(function (resolve, reject) {
     iterableToArray(values).forEach(function(value){
       Promise.resolve(value).then(resolve, reject);

New Error

 ERROR  failed to delete intrinsics.%PromisePrototype%.then.prototype [TypeError: Unable to delete property.]
 ERROR  TypeError: Unable to delete property.