Skip to content

Instantly share code, notes, and snippets.

@shikelong
Last active January 28, 2022 06:57
Show Gist options
  • Save shikelong/20db01579cb78594c45ab1c847694611 to your computer and use it in GitHub Desktop.
Save shikelong/20db01579cb78594c45ab1c847694611 to your computer and use it in GitHub Desktop.
Tapable sample
import {
SyncHook,
SyncBailHook,
SyncWaterfallHook,
SyncLoopHook,
AsyncParallelHook,
AsyncParallelBailHook,
AsyncSeriesHook,
AsyncSeriesBailHook,
AsyncSeriesWaterfallHook,
} from "tapable";
// import { log } from "./utils";
function log(msg: string) {
console.log("-".repeat(33) + " " + (msg ?? "") + " " + "-".repeat(33));
}
log("1. SyncHook");
const sayHelloHook = new SyncHook(["name"]);
sayHelloHook.tap("1. first hello", (name) => {
console.log("Hello, ", name);
});
sayHelloHook.tap("2. second hello", (name) => {
console.log("Hello, ", name, ", again.");
});
sayHelloHook.call("world");
log("2. SyncBailHook");
const sayGoodbyeBailHook = new SyncBailHook(["name"]);
sayGoodbyeBailHook.tap("1. first goodbye", (name) => {
console.log("Goodbye, ", name);
});
sayGoodbyeBailHook.tap("2. second goodbye", (name) => {
console.log(" 🥰 Goodbye, ", name);
//If return un-undefined value. It will break the execution.
return false;
});
sayGoodbyeBailHook.tap("3. third goodbye", (name) => {
console.log("❤️‍🩹 Goodbye, ", name);
});
sayGoodbyeBailHook.call("world");
log("3. SyncWaterfallHook");
const sayGoodbyeWaterfallHook = new SyncWaterfallHook(["name"]);
sayGoodbyeWaterfallHook.tap("1. first goodbye", (name) => {
console.log("Goodbye, ", name);
return [name, "1"];
});
sayGoodbyeWaterfallHook.tap("2. second goodbye", (...args) => {
console.log("🥰 Goodbye, ", args);
return "2";
});
sayGoodbyeWaterfallHook.tap("3. third goodbye", (...args) => {
console.log("❤️‍🩹 Goodbye, ", args);
return "3";
});
sayGoodbyeWaterfallHook.call("world");
log("4. SyncLoopHook");
const sayGoodbyeLoopHook = new SyncLoopHook(["name"]);
sayGoodbyeLoopHook.tap("1. first goodbye", (name) => {
console.log("Goodbye, ", name);
});
let count = 0;
sayGoodbyeLoopHook.tap("2. second goodbye", (name) => {
console.log("🥰 Goodbye, ", name, ",", count);
count++;
if (count < 3) {
return true;
}
return undefined;
});
sayGoodbyeLoopHook.call("world");
log("5. AsyncParallelHook");
const sayGoodbyeAsynParallelcHook = new AsyncParallelHook(["name"]);
sayGoodbyeAsynParallelcHook.tap("1. first goodbye", async (name) => {
console.log("1. Goodbye, ", name);
});
sayGoodbyeAsynParallelcHook.tapAsync("2. second goodbye", (name, cb) => {
setTimeout(() => {
console.log("2. Goodbye, ", name);
cb();
}, 2000);
});
sayGoodbyeAsynParallelcHook.tapPromise("3. second goodbye", (name) => {
return new Promise((resolve) => {
setTimeout(() => {
console.log("3. Goodbye, ", name);
resolve();
}, 1000);
});
});
sayGoodbyeAsynParallelcHook.callAsync("world", () => {
console.log("AsyncParallelHook done");
});
log("6. AsyncSeriesHook");
const sayGoodbyeAsynSeriesHook = new AsyncSeriesHook(["name"]);
sayGoodbyeAsynSeriesHook.tapAsync("1. first goodbye", (name, cb) => {
setTimeout(() => {
console.info("🎾 1. Goodbye, ", name);
cb();
}, 2000);
});
sayGoodbyeAsynSeriesHook.tapPromise("2. second goodbye", (name) => {
return new Promise((resolve) => {
setTimeout(() => {
console.info("🎾 2. Goodbye, ", name);
resolve();
}, 1000);
});
});
sayGoodbyeAsynSeriesHook.callAsync("world", () => {
console.info("🎾 sayGoodbyeAsynSeriesHook done");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment