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

traffic-exchange-prototype-js-lib

v1.1.6

Published

Task Manager Library

Downloads

1,189

Readme

Traffic Exchange Prototype JS Library

The traffic-exchange-prototype-js-lib is a modular JavaScript library designed for managing tasks in a traffic exchange system. It supports integration with various frameworks such as React, Angular, and Vanilla JavaScript. This README provides usage examples, configuration, and API references to help you integrate the library effectively.


Installation

npm install traffic-exchange-prototype-js-lib

Configuration

Initialization

To use the library, initialize the TaskManagerApp instance with your app's configuration:

import { TaskManagerApp } from "traffic-exchange-prototype-js-lib";

const appConfig = {
    apiUrl: 'http://localhost:8585', // Base API URL
    appKey: '123456789',            // Your application key
};

const taskManagerApp = new TaskManagerApp(appConfig);
taskManagerApp.init(); // Initialize the library

Usage

React Example

This example demonstrates how to use the library in a React component:

import React, { useState, useEffect, useRef } from "react";
import { TaskManagerApp } from "traffic-exchange-prototype-js-lib";
import { Events } from "traffic-exchange-prototype-js-lib/types/events";

const appConfig = { apiUrl: 'http://localhost:8585', appKey: '123456789' };
const taskManagerApp = new TaskManagerApp(appConfig);
taskManagerApp.init();

export const TaskManager = () => {
    const [tasks, setTasks] = useState([]);
    const userID = "telegram_user_id";
    const isRegistered = useRef(false);

    useEffect(() => {
        if (isRegistered.current) return;

        taskManagerApp.registerEvent(Events.TaskClaimSucceed, (task) => {
            alert(`Task claimed: ${task.name}`);
        });

        taskManagerApp.registerEvent(Events.TaskClaimFailed, (task) => {
            alert(`Failed to claim task: ${task.name}`);
        });

        isRegistered.current = true;
    }, []);

    useEffect(() => {
        taskManagerApp.getTaskList(userID).then(setTasks);
    }, []);

    return (
        <div>
            {tasks.map(task => (
                <div key={task.id}>
                    <h3>{task.name}</h3>
                    <button onClick={() => taskManagerApp.claimProcess(userID, task)}>Claim</button>
                </div>
            ))}
        </div>
    );
};

export default TaskManager;

Angular Example

import { Component, OnInit } from '@angular/core';
import { TaskManagerApp } from 'traffic-exchange-prototype-js-lib';

@Component({
  selector: 'app-task-manager',
  template: `
    <div *ngFor="let task of tasks">
      <h3>{{ task.name }}</h3>
      <button (click)="claimTask(task)">Claim</button>
    </div>
  `,
})
export class TaskManagerComponent implements OnInit {
  tasks = [];
  taskManagerApp: TaskManagerApp;

  constructor() {
    this.taskManagerApp = new TaskManagerApp({
      apiUrl: 'http://localhost:8585',
      appKey: '123456789',
    });
    this.taskManagerApp.init();
  }

  ngOnInit(): void {
    this.taskManagerApp.getTaskList('telegram_user_id').then((tasks) => {
      this.tasks = tasks;
    });
  }

  claimTask(task: any): void {
    this.taskManagerApp.claimProcess('telegram_user_id', task);
  }
}

Vanilla JavaScript Example

<!DOCTYPE html>
<html lang="en">
<head>
  <script src="path-to-traffic-exchange-prototype-js-lib.js"></script>
  <script>
    const appConfig = {
        apiUrl: 'http://localhost:8585',
        appKey: '123456789'
    };

    const taskManagerApp = new TaskManagerApp(appConfig);
    taskManagerApp.init();

    document.addEventListener('DOMContentLoaded', () => {
        taskManagerApp.getTaskList('telegram_user_id').then(tasks => {
            const taskContainer = document.getElementById('task-container');
            tasks.forEach(task => {
                const taskDiv = document.createElement('div');
                taskDiv.innerHTML = `
                    <h3>${task.name}</h3>
                    <button onclick="claimTask('${task.id}')">Claim</button>
                `;
                taskContainer.appendChild(taskDiv);
            });
        });

        window.claimTask = (taskId) => {
            const task = tasks.find(t => t.id === taskId);
            taskManagerApp.claimProcess('telegram_user_id', task);
        };
    });
  </script>
</head>
<body>
  <div id="task-container"></div>
</body>
</html>

API

TaskManagerApp

Methods

  • init(): Initializes the task manager library.
  • getTaskList(userID: string): Promise<TaskList>: Fetches the list of tasks for the user.
  • claimProcess(userID: string, task: Task): void: Claims the specified task.
  • goProcess(userID: string, task: Task): void: Processes the specified task.

Events

  • Events.TaskClaimSucceed: Triggered when a task is successfully claimed.
  • Events.TaskClaimFailed: Triggered when task claiming fails.

License

This project is licensed under the MIT License.