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).
Problem
Host applications that need to inject behavior into
Story.save(),Story.load(), andStory.goto()currently monkey-patch these methods: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:
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
beforesaveaftersavebeforeloadafterloadContext
RoidRage wraps
Story.saveto serialize its engine snapshot into$enginebefore Spindle writes the save, and wrapsStory.load/Story.gotofor similar reasons. Makingboot()idempotent requires either restoring original methods on teardown (manual tracking) or using proper hooks (clean, auto-cleaned by #127).