LoginSignup
0
0

More than 3 years have passed since last update.

JSモジュール

Posted at

fun-hooks

import funHooks from 'fun-hooks';

let createHook = funcHooks({
  useProxy: false,
  ready: funHook.ASYNC | funcHooks.QUEUE
});

// Sync (before, after)
function sum(a, b) {
  return a + b;
}

let hookedSum = createHook('sync', sum);

// before
hookedSum.before(function(next, a, b) {
  a = a + 1  
  next(a, b);
});

// after
hookedSum.after(function(next, result) {
  next(result + 1);
});

let result = hookedSum(1, 1);

// hookedSum(1, 1) => hookedSum.before(1, 1) => sum(2, 1) => hookedSum.after(3) => 4

// Async (before, after)
function increment(a, b, callback) {
  callback(a + 1, b + 1);
}
let hookedIncrement = createHook('async', increment);

hookedIncrement.before(function(next, a, b) {
  a = a + 1;
  next(a, b);
});

hookedIncrement.after(function(next, a, b) {
  next(a, b + 1);
});

hookedIncrement(1, 1, function(a, b) {
  console.log(a, b); // 3, 3
});
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0