npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-chess-oex-scanner-android

v0.1.2

Published

Use your installed chess OEX engines on your android device in your Nativescript applications.

Downloads

5

Readme

react-native ChessOexScannerAndroid

Use your installed chess OEX engines on your android device in your React Native applications.

Installation

npm install react-native-chess-oex-scanner-android

Usage

Here a full example in typescript. First, some notes :

  • all methods are asynchronous, returning a Promise,

  • in constructor, you call ChessOexScannerAndroid.setupEngineUtils(), giving it the application id,

  • the library manages what I call your store and the installed libraries. While the store handles what you can copy to the internal data, the installed libraries are the libraries you've choosen to copy. Please notice that you can only launch an engine installed in the internal data,

  • you can call ChessOexScannerAndroid.getMyStoreEnginesNames() to list engines names from store and ChessOexScannerAndroid.installEngineFromMyStore(index) to install an engine from the store. Please notice that the index given to installEngineFromMyStore() will match the order of engines returned from getMyStoreEnginesNames(),

  • you can call ChessOexScannerAndroid.listInstalledEngines() to list installed engines, and ChessOexScannerAndroid.executeInstalledEngine(index) to execute an engine. Please notice that here also the index given to executeInstalledEngine() will match the order of engines returned from executeInstalledEngine(),

  • you should call regularly ChessOexScannerAndroid.readCurrentEnginePendingOutputs() in order to read outputs from the engine process,

  • last but not least, you should call ChessOexScannerAndroid.stopCurrentRunningEngine() when releasing your component/app, in order to cleanup engine process.

import * as React from 'react';

import { StyleSheet, View, Text, ScrollView, Button } from 'react-native';
import Toast from 'react-native-easy-toast';
import Dialog from 'react-native-dialog';
import ChessOexScannerAndroid from 'react-native-chess-oex-scanner-android';

export default function App() {
  const toast = React.useRef<Toast | null>(null);
  const [commandValue, setCommandValue] = React.useState('');
  const [commandDialogVisible, setCommandDialogVisible] = React.useState(false);
  const [enginesStoreList, setEnginesStoreList] = React.useState<Array<string>>(
    []
  );
  const [enginesInstalledList, setEnginesInstalledList] = React.useState<
    Array<string>
  >([]);

  function askForCommand(_e: any) {
    setCommandDialogVisible(true);
  }

  async function readOutputsFromEngine() {
    try {
      const lines = await ChessOexScannerAndroid.readCurrentEnginePendingOutputs();
      if (lines.length > 0) {
        lines.forEach((singleLine) => console.log(singleLine));
      }
    } catch (err) {
      console.error(err);
    }
  }

  function handleCancelCommandDialog() {
    setCommandDialogVisible(false);
  }

  async function sendCommandToEngine() {
    setCommandDialogVisible(false);
    try {
      const command = commandValue;
      await ChessOexScannerAndroid.sendCommandToRunningEngine(command);
    } catch (err) {
      console.error(err);
    }
  }

  async function getStoreEnginesList() {
    const engines = await ChessOexScannerAndroid.getMyStoreEnginesNames();
    return engines;
  }

  async function getInstalledEnginesList() {
    function stripNameFromLibName(libName: string) {
      let result = libName;
      result = result.substring(3);
      result = result.substring(0, result.length - 3);
      return result;
    }

    const engines = await ChessOexScannerAndroid.listInstalledEngines();
    return engines.map((item) => stripNameFromLibName(item));
  }

  async function installStoreEngine(index: number) {
    try {
      await ChessOexScannerAndroid.installEngineFromMyStore(index);
      toast.current?.show('Engine installed');
    } catch (err) {
      console.error(err);
      toast.current?.show('Failed to install engine !');
    }
  }

  async function playWithEngine(index: number) {
    try {
      const allEngines = await ChessOexScannerAndroid.listInstalledEngines();
      const engineName = allEngines[index];

      console.log('Launching engine ' + engineName);
      await ChessOexScannerAndroid.executeInstalledEngine(index);
    } catch (err) {
      console.error(err);
      toast.current?.show('Failed to launch engine !');
    }
  }

  React.useEffect(() => {
    async function setup() {
        // In setupEngineUtils, you give the android application id
      await ChessOexScannerAndroid.setupEngineUtils(
        'com.example.reactnativechessoexscannerandroid'
      );
    }

    setup()
      .then(() => {})
      .catch((err) => console.error(err));
    getStoreEnginesList()
      .then((engines) => setEnginesStoreList(engines))
      .catch((err) => {
        console.error(err);
        toast.current?.show('Failed to get engines list from your store !');
      });
    getInstalledEnginesList()
      .then((engines) => setEnginesInstalledList(engines))
      .catch((err) => {
        console.error(err);
        toast.current?.show('Failed to get installed engines list !');
      });

    return function () {
      ChessOexScannerAndroid.stopCurrentRunningEngine();
    };
  }, []);

  React.useEffect(() => {
    let timer = setInterval(readOutputsFromEngine, 1000);
    return function () {
      clearInterval(timer);
    };
  }, []);

  return (
    <View style={styles.container}>
      <Toast ref={toast} />
      <Dialog.Container visible={commandDialogVisible}>
        <Dialog.Title>Send command</Dialog.Title>
        <Dialog.Input
          label="Command:"
          value={commandValue}
          onChangeText={setCommandValue}
        />
        <Dialog.Button label="Cancel" onPress={handleCancelCommandDialog} />
        <Dialog.Button label="Send" onPress={sendCommandToEngine} />
      </Dialog.Container>
      <Button onPress={askForCommand} title="Send command" />
      <View style={styles.storeZone}>
        <Text style={styles.listHeader}>Engines from store</Text>
        <ScrollView>
          {enginesStoreList.map((engineName, index) => {
            return (
              <Text
                key={engineName}
                onPress={() => installStoreEngine(index)}
                style={styles.listText}
              >
                {engineName}
              </Text>
            );
          })}
        </ScrollView>
      </View>
      <View style={styles.installedZone}>
        <Text style={styles.listHeader}>Engines installed</Text>
        <ScrollView>
          {enginesInstalledList.map((engineName, index) => {
            return (
              <Text
                key={engineName}
                onPress={() => playWithEngine(index)}
                style={styles.listText}
              >
                {engineName}
              </Text>
            );
          })}
        </ScrollView>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  storeZone: {
    width: '100%',
    height: '40%',
    backgroundColor: 'lightgreen',
    marginTop: 10,
  },
  installedZone: {
    width: '100%',
    height: '40%',
    backgroundColor: 'lightyellow',
  },
  listHeader: {
    color: 'blue',
    marginLeft: 'auto',
    marginRight: 'auto',
    marginTop: 5,
  },
  listText: {
    marginLeft: 10,
    marginTop: 8,
    marginBottom: 8,
    color: 'magenta',
  },
});

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT