Skip to content

Save/load/navigate hooks to replace monkey-patching Story methods #129

Description

@rohal12

Problem

Host applications that need to inject behavior into Story.save(), Story.load(), and Story.goto() currently monkey-patch these methods:

const originalSave = Story.save.bind(Story);
Story.save = (slot, custom) => {
  Story.set('engine', serializeEngineState());
  originalSave(slot, custom);
};

This is fragile — on restart, each boot() wraps the methods again, creating a growing chain of closures. The host can't cleanly restore the originals without tracking them manually.

Proposed solution

Add event hooks that fire before/after these operations:

Story.on('beforesave', (slot, custom) => {
  Story.set('engine', serializeEngineState());
});

Story.on('afterload', (slot) => {
  restoreEngineState(Story.get('engine'));
});

Combined with #127 (auto-clean runtime handlers on restart), these hooks would be automatically cleaned up, eliminating the need to monkey-patch or manually track cleanup.

Suggested hooks

Hook Fires Use case
beforesave Before save writes to storage Inject engine state into story variables
aftersave After save completes Confirmation, analytics
beforeload Before load restores variables Cleanup current state
afterload After load restores variables Rebuild engine from restored state

Context

RoidRage wraps Story.save to serialize its engine snapshot into $engine before Spindle writes the save, and wraps Story.load/Story.goto for similar reasons. Making boot() idempotent requires either restoring original methods on teardown (manual tracking) or using proper hooks (clean, auto-cleaned by #127).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions