الفرق بين المراجعتين ل"ReactNative/interactionmanager"

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
ط (مراجعة)
ط (مراجعة)
سطر 1: سطر 1:
<noinclude>{{DISPLAYTITLE:InteractionManager في React Native}}</noinclude>  
+
<noinclude>{{DISPLAYTITLE:الواجهة InteractionManager في React Native}}</noinclude>  
 
يسمح <code>InteractionManager</code> بجدولة الأعمال طويلة الأمد بعد انتهاء أيّ تفاعلٍ، أو حركةٍ، ممّا يسمح لمحركات [[JavaScript]] بالعمل بسلاسةٍ، ويمكن للتطبيقات جدولة مهامّها لتعمل بعد التفاعلات (interactions)، وذلك كما يلي:<syntaxhighlight lang="java">
 
يسمح <code>InteractionManager</code> بجدولة الأعمال طويلة الأمد بعد انتهاء أيّ تفاعلٍ، أو حركةٍ، ممّا يسمح لمحركات [[JavaScript]] بالعمل بسلاسةٍ، ويمكن للتطبيقات جدولة مهامّها لتعمل بعد التفاعلات (interactions)، وذلك كما يلي:<syntaxhighlight lang="java">
 
InteractionManager.runAfterInteractions(() => {
 
InteractionManager.runAfterInteractions(() => {
سطر 12: سطر 12:
 
تَعتبر منظومة معالجة اللّمسات (touch handling system) التفاعل (interaction) هو لمسةٌ فعّالةٌ واحدةٌ، أو أكثر، وتقوم بتأخير استجابات <code>()runAfterInteractions</code> حتى انتهاء جميع اللّمسات، أو إلغائها.
 
تَعتبر منظومة معالجة اللّمسات (touch handling system) التفاعل (interaction) هو لمسةٌ فعّالةٌ واحدةٌ، أو أكثر، وتقوم بتأخير استجابات <code>()runAfterInteractions</code> حتى انتهاء جميع اللّمسات، أو إلغائها.
  
يسمح InteractionManager كذلك للتّطبيقات بتسجيل الحركات، وذلك بإنشاء التفاعل <code>handle</code> عند بداية الحركة، وحذفه عند اكتمالها:<syntaxhighlight lang="javascript">
+
يسمح <code>InteractionManager</code> كذلك للتّطبيقات بتسجيل الحركات، وذلك بإنشاء التفاعل <code>handle</code> عند بداية الحركة، وحذفه عند اكتمالها:<syntaxhighlight lang="javascript">
 
var handle = InteractionManager.createInteractionHandle();
 
var handle = InteractionManager.createInteractionHandle();
 
// تشغيل الحركة... (`runAfterInteractions` جدولة المهام)
 
// تشغيل الحركة... (`runAfterInteractions` جدولة المهام)
سطر 18: سطر 18:
 
InteractionManager.clearInteractionHandle(handle);
 
InteractionManager.clearInteractionHandle(handle);
 
// المهام المجدولة التي ستعمل بعد حذف جميع المعالجات
 
// المهام المجدولة التي ستعمل بعد حذف جميع المعالجات
</syntaxhighlight>يأخذ التابع <code>()runAfterInteractions</code> دالة استجابةٍ صريحةٍ، أو الكائن <code>PromiseTask</code> مع التابع <code>gen</code> الذي يعيد وعدًا (Promise)، حيث يقبّل بشكلٍ تامٍّ (متضمناً الاعتماديّات غير المتزامنة التي تجدوِل مزيداً من المهام عن طريق <code>()runAfterInteractions</code>، إذا زُوِّد بالكائن <code>PromiseTask</code>، وذلك قبل مباشرة المهمّة التالية التي قد تكون جُدوِلت مسبقاً.
+
</syntaxhighlight>يأخذ التابع <code>()runAfterInteractions</code> دالة استجابةٍ صريحةٍ، أو الكائن <code>PromiseTask</code> مع التابع <code>gen</code> الذي يعيد وعدًا (Promise)، حيث يقبّل بشكلٍ تامٍّ (متضمنًا الاعتماديّات غير المتزامنة التي تجدوِل مزيداً من المهام عن طريق <code>()runAfterInteractions</code>، إذا زُوِّد بالكائن <code>PromiseTask</code>، وذلك قبل مباشرة المهمّة التالية التي قد تكون جُدوِلت مسبقًا.
  
 
تُنفَّذ المهام المجدولة مع بعضها على شكل حلقةٍ تكراريةٍ ضمن دُفعة <code>setImmediate</code> واحدةٍ؛ أمّا عند استدعاء <code>setDeadline</code> مع عددٍ موجبٍ، فيجدوَل تنفيذ المهام حتى الحدّ الأقصى فقط، وبتعابير JS، هو عبارة عن زمن تنفيذ حلقة الحدث من خلال <code>setTimeout</code>، ممّا يسمح للأحداث كاللمسات ببدء التّفاعلات، ويوقف تنفيذ المهام المجدولة، ويجعل التّطبيق أكثر استجابةً.
 
تُنفَّذ المهام المجدولة مع بعضها على شكل حلقةٍ تكراريةٍ ضمن دُفعة <code>setImmediate</code> واحدةٍ؛ أمّا عند استدعاء <code>setDeadline</code> مع عددٍ موجبٍ، فيجدوَل تنفيذ المهام حتى الحدّ الأقصى فقط، وبتعابير JS، هو عبارة عن زمن تنفيذ حلقة الحدث من خلال <code>setTimeout</code>، ممّا يسمح للأحداث كاللمسات ببدء التّفاعلات، ويوقف تنفيذ المهام المجدولة، ويجعل التّطبيق أكثر استجابةً.
 
__toc__
 
__toc__
 
== مثال ==
 
== مثال ==
 
+
إليك هذا المثال الأساسي التالي ([https://snack.expo.dev/@hsoubwiki/interactionmanager-function-component-basic-example تجربة حية]):<syntaxhighlight lang="javascript">
=== أساسي ===
 
<syntaxhighlight lang="javascript">
 
 
import React, { useState, useEffect } from "react";
 
import React, { useState, useEffect } from "react";
 
import {
 
import {
 Alert,
+
  Alert,
 Animated,
+
  Animated,
 InteractionManager,
+
  InteractionManager,
 Platform,
+
  Platform,
 StyleSheet,
+
  StyleSheet,
 Text,
+
  Text,
 View,
+
  View,
 
} from "react-native";
 
} from "react-native";
+
 
 
const instructions = Platform.select({
 
const instructions = Platform.select({
 ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
+
  ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
 android:
+
  android:
   "Double tap R on your keyboard to reload,\n" +
+
    "Double tap R on your keyboard to reload,\n" +
   "Shake or press menu button for dev menu",
+
    "Shake or press menu button for dev menu",
 
});
 
});
+
 
 
const useMount = func => useEffect(() => func(), []);
 
const useMount = func => useEffect(() => func(), []);
+
 
 
const useFadeIn = (duration = 5000) => {
 
const useFadeIn = (duration = 5000) => {
 const [opacity] = useState(new Animated.Value(0));
+
  const [opacity] = useState(new Animated.Value(0));
+
 
 // تشغيل الحركة بعد تثبيت المكون
+
  // Running the animation when the component is mounted
 useMount(() => {
+
  useMount(() => {
   // Animated.timing() ينشئ معالج التفاعل بشكل افتراضي
+
    // Animated.timing() create a interaction handle by default, if you want to disabled that
// false مساوية  isInteraction ويلغى هذا السلوك بجعل قيمة
+
    // behaviour you can set isInteraction to false to disabled that.
   
+
    Animated.timing(opacity, {
   Animated.timing(opacity, {
+
      toValue: 1,
     toValue: 1,
+
      duration,
     duration,
+
    }).start();
   }).start();
 
 
   });
 
   });
+
 
 return opacity;
+
  return opacity;
 
};
 
};
+
 
 
const Ball = ({ onShown }) => {
 
const Ball = ({ onShown }) => {
 const opacity = useFadeIn();
+
  const opacity = useFadeIn();
+
 
 // تشغيل التابع بعد الحركة
+
  // Running a method after the animation
 useMount(() => {
+
  useMount(() => {
   const interactionPromise = InteractionManager.runAfterInteractions(() => onShown());
+
    const interactionPromise = InteractionManager.runAfterInteractions(() => onShown());
   return () => interactionPromise.cancel();
+
    return () => interactionPromise.cancel();
 
   });
 
   });
+
 
 return <Animated.View style={[styles.ball, { opacity }]} />;
+
  return <Animated.View style={[styles.ball, { opacity }]} />;
 
};
 
};
+
 
 
const App = () => {
 
const App = () => {
 return (
+
  return (
   <View style={styles.container}>
+
    <View style={styles.container}>
     <Text>{instructions}</Text>
+
      <Text>{instructions}</Text>
     <Ball onShown={() => Alert.alert("Animation is done")} />
+
      <Ball onShown={() => Alert.alert("Animation is done")} />
   </View>
+
    </View>
 
   );
 
   );
 
};
 
};
+
 
 
const styles = StyleSheet.create({
 
const styles = StyleSheet.create({
 container: { flex: 1, justifyContent: "center", alignItems: "center" },
+
  container: { flex: 1, justifyContent: "center", alignItems: "center" },
 ball: {
+
  ball: {
   width: 100,
+
    width: 100,
   height: 100,
+
    height: 100,
   backgroundColor: "salmon",
+
    backgroundColor: "salmon",
   borderRadius: 100,
+
    borderRadius: 100,
 
   },
 
   },
 
});
 
});
+
 
 
export default App;
 
export default App;
</syntaxhighlight>
+
</syntaxhighlight>إليك أيضًا هذا المثال المتقدم ([https://snack.expo.dev/@hsoubwiki/interactionmanager-function-component-advanced-example تجربة حية]):<syntaxhighlight lang="javascript">
 
 
=== متقدم ===
 
<syntaxhighlight lang="javascript">
 
 
import React, { useEffect } from "react";
 
import React, { useEffect } from "react";
 
import {
 
import {
 Alert,
+
  Alert,
 Animated,
+
  Animated,
 InteractionManager,
+
  InteractionManager,
 Platform,
+
  Platform,
 StyleSheet,
+
  StyleSheet,
 Text,
+
  Text,
 View,
+
  View,
 
} from "react-native";
 
} from "react-native";
+
 
 
const instructions = Platform.select({
 
const instructions = Platform.select({
 ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
+
  ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
 android:
+
  android:
   "Double tap R on your keyboard to reload,\n" +
+
    "Double tap R on your keyboard to reload,\n" +
   "Shake or press menu button for dev menu",
+
    "Shake or press menu button for dev menu",
 
});
 
});
+
 
 
const useMount = func => useEffect(() => func(), []);
 
const useMount = func => useEffect(() => func(), []);
+
 
// يمكنك إنشاء تفاعل أو حركة شخصية وتضيف دعم
+
// You can create a custom interaction/animation and add
// InteractionManager
+
// support for InteractionManager
 
const useCustomInteraction = (timeLocked = 2000) => {
 
const useCustomInteraction = (timeLocked = 2000) => {
 useMount(() => {
+
  useMount(() => {
   const handle = InteractionManager.createInteractionHandle();
+
    const handle = InteractionManager.createInteractionHandle();
+
 
   setTimeout(
+
    setTimeout(
     () => InteractionManager.clearInteractionHandle(handle),
+
      () => InteractionManager.clearInteractionHandle(handle),
     timeLocked
+
      timeLocked
   );
+
    );
+
 
   return () => InteractionManager.clearInteractionHandle(handle);
+
    return () => InteractionManager.clearInteractionHandle(handle);
 
   });
 
   });
 
};
 
};
+
 
 
const Ball = ({ onInteractionIsDone }) => {
 
const Ball = ({ onInteractionIsDone }) => {
 useCustomInteraction();
+
  useCustomInteraction();
+
 
 // تشغيل التابع بعد التفاعل
+
  // Running a method after the interaction
 useMount(() => {
+
  useMount(() => {
   InteractionManager.runAfterInteractions(() => onInteractionIsDone());
+
    InteractionManager.runAfterInteractions(() => onInteractionIsDone());
 
   });
 
   });
+
 
 return <Animated.View style={[styles.ball]} />;
+
  return <Animated.View style={[styles.ball]} />;
 
};
 
};
+
 
 
const App = () => {
 
const App = () => {
 return (
+
  return (
   <View style={styles.container}>
+
    <View style={styles.container}>
     <Text>{instructions}</Text>
+
      <Text>{instructions}</Text>
     <Ball onInteractionIsDone={() => Alert.alert("Interaction is done")} />
+
      <Ball onInteractionIsDone={() => Alert.alert("Interaction is done")} />
   </View>
+
    </View>
 
   );
 
   );
 
};
 
};
+
 
 
const styles = StyleSheet.create({
 
const styles = StyleSheet.create({
 container: { flex: 1, justifyContent: "center", alignItems: "center" },
+
  container: { flex: 1, justifyContent: "center", alignItems: "center" },
 ball: {
+
  ball: {
   width: 100,
+
    width: 100,
   height: 100,
+
    height: 100,
   backgroundColor: "salmon",
+
    backgroundColor: "salmon",
   borderRadius: 100,
+
    borderRadius: 100,
 
   },
 
   },
 
});
 
});
+
 
 
export default App;
 
export default App;
 
</syntaxhighlight>'''ملاحظة:''' لا يعمل التّابع <code>()InteractionManager.runAfterInteractions</code> كما ينبغي في الويب، إذ يُطلق آنيًّا دون انتظار انتهاء التفاعل.
 
</syntaxhighlight>'''ملاحظة:''' لا يعمل التّابع <code>()InteractionManager.runAfterInteractions</code> كما ينبغي في الويب، إذ يُطلق آنيًّا دون انتظار انتهاء التفاعل.

مراجعة 18:42، 4 أكتوبر 2021

يسمح InteractionManager بجدولة الأعمال طويلة الأمد بعد انتهاء أيّ تفاعلٍ، أو حركةٍ، ممّا يسمح لمحركات JavaScript بالعمل بسلاسةٍ، ويمكن للتطبيقات جدولة مهامّها لتعمل بعد التفاعلات (interactions)، وذلك كما يلي:

InteractionManager.runAfterInteractions(() => {
  // ...مهمة متزامنة طويلة الأمد...
});

كما توجد خياراتُ جدولةٍ بديلةٌ:

  • ‎‏‏()requestAnimationFrame: للشّفرة البرمجية التي تحرّك الواجهة مع مرور الوقت.
  • ()setImmediate/setTimeout: لتشغيل الشفرة البرمجية لاحقًا، مع ملاحظة إمكانية تأخّر الحركات.
  • ()runAfterInteractions: لتشغيل الشفرة البرمجية لاحقًا، دون تأخير الحركات الفعّالة.

تَعتبر منظومة معالجة اللّمسات (touch handling system) التفاعل (interaction) هو لمسةٌ فعّالةٌ واحدةٌ، أو أكثر، وتقوم بتأخير استجابات ()runAfterInteractions حتى انتهاء جميع اللّمسات، أو إلغائها.

يسمح InteractionManager كذلك للتّطبيقات بتسجيل الحركات، وذلك بإنشاء التفاعل handle عند بداية الحركة، وحذفه عند اكتمالها:

var handle = InteractionManager.createInteractionHandle();
// تشغيل الحركة... (`runAfterInteractions` جدولة المهام)
// فيما بعد, عند إتمام الحركة:
InteractionManager.clearInteractionHandle(handle);
// المهام المجدولة التي ستعمل بعد حذف جميع المعالجات

يأخذ التابع ()runAfterInteractions دالة استجابةٍ صريحةٍ، أو الكائن PromiseTask مع التابع gen الذي يعيد وعدًا (Promise)، حيث يقبّل بشكلٍ تامٍّ (متضمنًا الاعتماديّات غير المتزامنة التي تجدوِل مزيداً من المهام عن طريق ()runAfterInteractions، إذا زُوِّد بالكائن PromiseTask، وذلك قبل مباشرة المهمّة التالية التي قد تكون جُدوِلت مسبقًا.

تُنفَّذ المهام المجدولة مع بعضها على شكل حلقةٍ تكراريةٍ ضمن دُفعة setImmediate واحدةٍ؛ أمّا عند استدعاء setDeadline مع عددٍ موجبٍ، فيجدوَل تنفيذ المهام حتى الحدّ الأقصى فقط، وبتعابير JS، هو عبارة عن زمن تنفيذ حلقة الحدث من خلال setTimeout، ممّا يسمح للأحداث كاللمسات ببدء التّفاعلات، ويوقف تنفيذ المهام المجدولة، ويجعل التّطبيق أكثر استجابةً.

مثال

إليك هذا المثال الأساسي التالي (تجربة حية):

import React, { useState, useEffect } from "react";
import {
  Alert,
  Animated,
  InteractionManager,
  Platform,
  StyleSheet,
  Text,
  View,
} from "react-native";

const instructions = Platform.select({
  ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
  android:
    "Double tap R on your keyboard to reload,\n" +
    "Shake or press menu button for dev menu",
});

const useMount = func => useEffect(() => func(), []);

const useFadeIn = (duration = 5000) => {
  const [opacity] = useState(new Animated.Value(0));

  // Running the animation when the component is mounted
  useMount(() => {
    // Animated.timing() create a interaction handle by default, if you want to disabled that
    // behaviour you can set isInteraction to false to disabled that.
    Animated.timing(opacity, {
      toValue: 1,
      duration,
    }).start();
  });

  return opacity;
};

const Ball = ({ onShown }) => {
  const opacity = useFadeIn();

  // Running a method after the animation
  useMount(() => {
    const interactionPromise = InteractionManager.runAfterInteractions(() => onShown());
    return () => interactionPromise.cancel();
  });

  return <Animated.View style={[styles.ball, { opacity }]} />;
};

const App = () => {
  return (
    <View style={styles.container}>
      <Text>{instructions}</Text>
      <Ball onShown={() => Alert.alert("Animation is done")} />
    </View>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: "center", alignItems: "center" },
  ball: {
    width: 100,
    height: 100,
    backgroundColor: "salmon",
    borderRadius: 100,
  },
});

export default App;

إليك أيضًا هذا المثال المتقدم (تجربة حية):

import React, { useEffect } from "react";
import {
  Alert,
  Animated,
  InteractionManager,
  Platform,
  StyleSheet,
  Text,
  View,
} from "react-native";

const instructions = Platform.select({
  ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
  android:
    "Double tap R on your keyboard to reload,\n" +
    "Shake or press menu button for dev menu",
});

const useMount = func => useEffect(() => func(), []);

// You can create a custom interaction/animation and add
// support for InteractionManager
const useCustomInteraction = (timeLocked = 2000) => {
  useMount(() => {
    const handle = InteractionManager.createInteractionHandle();

    setTimeout(
      () => InteractionManager.clearInteractionHandle(handle),
      timeLocked
    );

    return () => InteractionManager.clearInteractionHandle(handle);
  });
};

const Ball = ({ onInteractionIsDone }) => {
  useCustomInteraction();

  // Running a method after the interaction
  useMount(() => {
    InteractionManager.runAfterInteractions(() => onInteractionIsDone());
  });

  return <Animated.View style={[styles.ball]} />;
};

const App = () => {
  return (
    <View style={styles.container}>
      <Text>{instructions}</Text>
      <Ball onInteractionIsDone={() => Alert.alert("Interaction is done")} />
    </View>
  );
};

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: "center", alignItems: "center" },
  ball: {
    width: 100,
    height: 100,
    backgroundColor: "salmon",
    borderRadius: 100,
  },
});

export default App;

ملاحظة: لا يعمل التّابع ()InteractionManager.runAfterInteractions كما ينبغي في الويب، إذ يُطلق آنيًّا دون انتظار انتهاء التفاعل.

التوابع

‎runAfterInteractions()‎

static runAfterInteractions(task)

يقوم هذا التابع بجدولة الدالة لتعمل بعد إتمام جميع التفاعلات، ويعيد وعداً (promise) قابلاً للإنهاء.

‎createInteractionHandle()‎

static createInteractionHandle()

ينبّه التابع ()createInteractionHandle المدير إلى بدء التفاعل.

‎clearInteractionHandle()‎

static clearInteractionHandle(handle)

ينبّه هذا التابع المدير إلى اكتمال التفاعل.

‎setDeadline()‎

static setDeadline(deadline)

يستخدم setTimeout مع عددٍ موجبٍ لجدولة أيّ مهمّةٍ حيث يصل زمن تنفيذ حلقة الحدث إلى قيمة deadline، وإلا فسينفذ افتراضيًّا جميع المهام، ضمن دُفعة setImmediate واحدةٍ.

مصادر