parallax/jsPDF · error · Error

Invalid arguments passed to PubSub.subscribe (jsPDF-module)

Error message

Invalid arguments passed to PubSub.subscribe (jsPDF-module)

What it means

PubSub's `subscribe` (src/jspdf.js:29) validates its three parameters: `topic` must be a string, `callback` must be a function, and `once` must be a boolean. The check at src/jspdf.js:31-35 fires before any registration happens, so a bad subscription is rejected cleanly rather than producing a silent no-op token. End users normally subscribe through higher-level event helpers; the error typically comes from a plugin or integration code that hands a non-function callback, a numeric topic, or a truthy non-boolean `once`.

Source

Thrown at src/jspdf.js:36

 * @name PubSub
 * @ignore
 */
function PubSub(context) {
  if (typeof context !== "object") {
    throw new Error(
      "Invalid Context passed to initialize PubSub (jsPDF-module)"
    );
  }
  var topics = {};

  this.subscribe = function(topic, callback, once) {
    once = once || false;
    if (
      typeof topic !== "string" ||
      typeof callback !== "function" ||
      typeof once !== "boolean"
    ) {
      throw new Error(
        "Invalid arguments passed to PubSub.subscribe (jsPDF-module)"
      );
    }

    if (!topics.hasOwnProperty(topic)) {
      topics[topic] = {};
    }

    var token = Math.random().toString(35);
    topics[topic][token] = [callback, !!once];

    return token;
  };

  this.unsubscribe = function(token) {
    for (var topic in topics) {
      if (topics[topic][token]) {
        delete topics[topic][token];

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Verify the topic is a non-empty string before subscribing (`typeof topic === 'string'`).
  2. Ensure the callback is a real function reference (`typeof callback === 'function'`) — not `callback()` (which calls it) or an undefined property access.
  3. Omit `once` or pass an explicit boolean; rely on the `once = once || false` default rather than passing a string/number.
  4. If subscribing from user-supplied config, coerce and validate all three fields before the call.

Example fix

// before
internal.events.subscribe(topic, handler, handler.once || 'yes');

// after
internal.events.subscribe(String(topic), handler, Boolean(handler.once));
Defensive patterns

Strategy: validation

Validate before calling

function safeSubscribe(events, topic, cb, once) {
  if (typeof topic !== 'string') throw new TypeError('topic must be a string');
  if (typeof cb !== 'function') throw new TypeError('callback must be a function');
  return events.subscribe(topic, cb, once === true);
}

Type guard

function isValidSubscription(topic, cb, once) {
  return typeof topic === 'string' &&
         typeof cb === 'function' &&
         typeof once === 'boolean';
}

Try / catch

try {
  token = events.subscribe(topic, handler, Boolean(once));
} catch (e) {
  if (/Invalid arguments passed to PubSub.subscribe/.test(e.message)) {
    console.warn('Skipping invalid subscription for topic', topic);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `events.subscribe(123, fn)` (non-string topic); `events.subscribe('addPage', null)` or passing the result of an expression that is undefined as callback; `events.subscribe('addPage', fn, 'true')` where once is a string rather than boolean; a plugin forwarding user-supplied values into subscribe without coercion.

Common situations: Plugin authors registering listeners with a topic pulled from dynamic config that is sometimes undefined; passing a method reference that lost its `this` binding and resolved to undefined; deserialized JSON config where a callback key is missing; version mismatches where a plugin expects an extra parameter and passes garbage as `once`.

Related errors


AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13). Data as JSON: /api/errors/2fbab3f98fe57142. Report an issue: GitHub.