Factor out disk insertion

Before, several places in the code essentially inserted a new disk
image into the drive, which similar—but not always exactly the
same—code.  Now there is an `insertDisk` method that is responsible
for inserting a new `FloppyDisk`.

All tests pass, everything compiles, manually tested nibble disks and
WOZ disks.
This commit is contained in:
Ian Flanigan 2022-08-31 14:25:05 +02:00
parent c0ba9cca55
commit c64b36ec74
No known key found for this signature in database
GPG Key ID: 035F657DAE4AE7EC
3 changed files with 17 additions and 20 deletions

View File

@ -24,6 +24,7 @@ import {
MassStorageData,
DiskMetadata,
SupportedSectors,
FloppyDisk,
} from '../formats/types';
import {
@ -883,10 +884,7 @@ export default class DiskII implements Card<State>, MassStorage<NibbleFormat> {
} else {
const disk = createDiskFromJsonDisk(jsonDisk);
if (disk) {
const cur = this.drives[drive];
Object.assign(cur, disk);
this.updateDirty(drive, false);
this.callbacks.label(drive, disk.metadata.name, disk.metadata.side);
this.insertDisk(drive, disk);
return true;
}
}
@ -912,8 +910,8 @@ export default class DiskII implements Card<State>, MassStorage<NibbleFormat> {
};
this.worker.postMessage(message);
} else {
const cur = this.drives[drive];
Object.assign(cur, jsonDecode(json));
const disk = jsonDecode(json);
this.insertDisk(drive, disk);
}
return true;
}
@ -943,12 +941,7 @@ export default class DiskII implements Card<State>, MassStorage<NibbleFormat> {
} else {
const disk = createDisk(fmt, options);
if (disk) {
const cur = this.drives[drive];
const { name, side } = cur.metadata;
Object.assign(cur, disk);
this.updateDirty(drive, true);
this.callbacks.label(drive, name, side);
this.insertDisk(drive, disk);
return true;
}
}
@ -970,11 +963,7 @@ export default class DiskII implements Card<State>, MassStorage<NibbleFormat> {
{
const { drive, disk } = data.payload;
if (disk) {
const cur = this.drives[drive];
Object.assign(cur, disk);
const { name, side } = cur.metadata;
this.updateDirty(drive, true);
this.callbacks.label(drive, name, side);
this.insertDisk(drive, disk);
}
}
break;
@ -985,6 +974,14 @@ export default class DiskII implements Card<State>, MassStorage<NibbleFormat> {
}
}
private insertDisk(drive: DriveNumber, disk: FloppyDisk) {
const cur = this.drives[drive];
Object.assign(cur, disk);
const { name, side } = cur.metadata;
this.updateDirty(drive, true);
this.callbacks.label(drive, name, side);
}
// TODO(flan): Does not work with WOZ or D13 disks
getBinary(drive: DriveNumber, ext?: NibbleFormat): MassStorageData | null {
const cur = this.drives[drive];

View File

@ -218,7 +218,7 @@ export interface DiskProcessedResponse {
type: typeof DISK_PROCESSED;
payload: {
drive: DriveNumber;
disk: Disk | null;
disk: FloppyDisk | null;
};
}

View File

@ -6,12 +6,12 @@ import {
} from '../js/formats/create_disk';
import {
FormatWorkerMessage,
Disk,
DiskProcessedResponse,
DISK_PROCESSED,
PROCESS_BINARY,
PROCESS_JSON_DISK,
PROCESS_JSON,
FloppyDisk,
} from '../js/formats/types';
debug('Worker loaded');
@ -20,7 +20,7 @@ addEventListener('message', (message: MessageEvent<FormatWorkerMessage>) => {
debug('Worker started', message.type);
const data = message.data;
const { drive } = data.payload;
let disk: Disk | null = null;
let disk: FloppyDisk | null = null;
switch (data.type) {
case PROCESS_BINARY: {