Table Lookup and Dispatch

The following code implements a Table Lookup and dispatch function using JavaScript. The purpose of this function is to handle interrupts, that is, signals indicating the occurrence of an Event.

Context: ⇒ Task LocationInterrupt Vector Table (IVT) ⇒ Dispatch TableMemoization: The basic idea is just to keep a table of previously computed input/result pairs.

// Define an array to hold the interrupt vector table let ivt = [];

The code defines an array called ivt, which represents the Interrupt Vector Table. This table contains entries that map interrupt numbers to corresponding Task functions. The handleInterrupt function takes an interrupt number as input, looks up the corresponding task function in the ivt array, and calls the function if it exists. If no task is assigned to the interrupt, the function outputs a message indicating that no task is assigned.

import { $compile } from "https://cdn.skypack.dev/@thi.ng/rdom";

// Define a function to perform table lookup and dispatch function handleInterrupt(interruptNumber, ...args) { let taskFunction = ivt[interruptNumber]; if (taskFunction) { taskFunction(...args); // pass the arguments using the spread syntax } else { console.log(`No task assigned to interrupt ${interruptNumber}.`); $compile( ["pre", {}, "No task assigned to interrupt ", interruptNumber, "."] ).mount(document.getElementById("output")); } }

Output frame: [⇒ Static Import Snippet, importjs.html]

//wiki.ralfbarkow.ch/assets/pages/js-snippet-template/importjs.html HEIGHT 150

We send a message to the Frame asking it to send us info about the page surrounding it. mdn

window.addEventListener("message", handler) let message = { action:"sendFrameContext" } window.parent.postMessage(message, "*")

We stop listening then process the data we got.

function handler ({data}) { if (data.action == "frameContext") { window.removeEventListener("message", handler) const {slug, item, page} = data

// Define a function to add a new entry to the interrupt vector table function addEntry(ivtIndex, taskFunction) { ivt[ivtIndex] = taskFunction; } // Define task functions function task1() { console.log("Task 1 executed."); $compile( ["pre", {}, "Task 1 executed."] ).mount(document.getElementById("output")); } function task2() { console.log("Task 2 executed."); $compile( ["pre", {}, "Task 2 executed."] ).mount(document.getElementById("output")); } function task3() { console.log("Task 3 executed."); $compile( ["pre", {}, "Task 3 executed."] ).mount(document.getElementById("output")); }

The code defines three task functions called task1, task2, and task3. These functions simply output a message indicating that they have been executed.

We add another task function that opens a Ghost Page. See also Create a Ghost Page. >> script ghost

// http://code.fed.wiki/frame-integration-promises.html function open(page, keepLineup=false, forks=[]) { const asCopy = obj => JSON.parse(JSON.stringify(obj)) let date = Date.now() page.journal ||= [{type:'create', date, item:asCopy(page)}, ...forks.map(site => ({type:'fork',date,site}))] let message = {action: "showResult", page, keepLineup} window.parent.postMessage(message, "*") }

function ghost() { const title = `a Ghost Page` const story = [ {type:'paragraph',text:`Ghost Page created at ${new Date().toLocaleTimeString()}`} ] open({title,story},event.shiftKey) }

The addEntry function is used to add entries to the ivt array. This function takes an index and a task function as input and adds the task function to the ivt array at the specified index.

// Add entries to the interrupt vector table addEntry(0, task1); addEntry(1, task2); addEntry(2, task3); addEntry(3, ghost);

show(page) } // end if

} // end of function handler

The show function calls the handleInterrupt function with different interrupt numbers to demonstrate how the function works. The outputs of these function calls are displayed in the output frame above using the $compile function from the @thi.ng/rdom library.

function show (page) {

// Call the handleInterrupt function with different interrupt numbers handleInterrupt(0); // Output: Task 1 executed. handleInterrupt(1); // Output: Task 2 executed. handleInterrupt(2); // Output: Task 3 executed. handleInterrupt(3); // Output: a Ghost Page handleInterrupt(4); // Output: No task assigned to interrupt 4.

} // end of function show

~

⇒ .innerHTML body generation use case ⇒ @thi.ng/hiccup@thi.ng/rdomUser Interaction GraphJournal to Graph