Simplify
This commit is contained in:
parent
edfc38f811
commit
c318aca51a
30
actor.ts
30
actor.ts
|
@ -68,55 +68,47 @@ let nextHandle = 0;
|
|||
type LocalAction = (t: Turn) => void;
|
||||
|
||||
export class Turn {
|
||||
readonly actor: Actor | null; // whose turn it is to act during this Turn
|
||||
readonly actor: Actor;
|
||||
readonly queues: Map<Actor, LocalAction[]> = new Map();
|
||||
readonly localActions: Array<LocalAction> = [];
|
||||
completed = false;
|
||||
|
||||
static for(actor: Actor | null, f: (t: Turn) => void): void {
|
||||
static for(actor: Actor, f: (t: Turn) => void): void {
|
||||
const t = new Turn(actor);
|
||||
f(t);
|
||||
t.complete();
|
||||
}
|
||||
|
||||
private constructor(actor: Actor | null) {
|
||||
private constructor(actor: Actor) {
|
||||
this.actor = actor;
|
||||
}
|
||||
|
||||
_ensureActor(what: string): Actor {
|
||||
if (this.actor === null) throw new Error(`Cannot ${what} from non-Actor context`);
|
||||
return this.actor;
|
||||
}
|
||||
|
||||
ref<T>(t: T, what: string = "ref"): Ref<T> {
|
||||
return new Ref(this._ensureActor(what), t);
|
||||
ref<T>(t: T): Ref<T> {
|
||||
return new Ref(this.actor, t);
|
||||
}
|
||||
|
||||
spawn(bootProc: (t: Turn) => void, initialAssertions = new IdentitySet<Handle>()): void {
|
||||
if (initialAssertions.size > 0) this._ensureActor("spawn with initialAssertions");
|
||||
this.localActions.push(() => {
|
||||
const child = new Actor(this.actor === null
|
||||
? void 0
|
||||
: extractFromMap(this.actor.outbound, initialAssertions));
|
||||
const child = new Actor(extractFromMap(this.actor.outbound, initialAssertions));
|
||||
child.execute(() => Turn.for(child, bootProc));
|
||||
});
|
||||
}
|
||||
|
||||
quit(): void {
|
||||
this.localActions.push(t => this._ensureActor("quit").terminateWith(t, { ok: true }));
|
||||
this.localActions.push(t => this.actor.terminateWith(t, { ok: true }));
|
||||
}
|
||||
|
||||
assert(location: Ref<Entity>, assertion: Assertion): Handle {
|
||||
const h = nextHandle++;
|
||||
this.enqueue(location.actor, t => {
|
||||
this._ensureActor("assert").outbound.set(h, location);
|
||||
this.actor.outbound.set(h, location);
|
||||
location.target[assert]?.(t, assertion, h);
|
||||
});
|
||||
return h;
|
||||
}
|
||||
|
||||
retract(h: Handle): void {
|
||||
this._retract(this._ensureActor("retract").outbound.get(h)!, h);
|
||||
this._retract(this.actor.outbound.get(h)!, h);
|
||||
}
|
||||
|
||||
replace(location: Ref<Entity>, h: Handle | undefined, assertion: Assertion): Handle {
|
||||
|
@ -127,14 +119,14 @@ export class Turn {
|
|||
|
||||
_retract(location: Ref<Entity>, handle: Handle): void {
|
||||
this.enqueue(location.actor, t => {
|
||||
this.actor!.outbound.delete(handle);
|
||||
this.actor.outbound.delete(handle);
|
||||
location.target[retract]?.(t, handle);
|
||||
});
|
||||
}
|
||||
|
||||
sync(location: Ref<any>): Promise<Turn> {
|
||||
return new Promise(resolve =>
|
||||
this.enqueue(location.actor, t => location.sync(t, this.ref(resolve, "sync"))));
|
||||
this.enqueue(location.actor, t => location.sync(t, this.ref(resolve))));
|
||||
}
|
||||
|
||||
message(location: Ref<Entity>, assertion: Assertion): void {
|
||||
|
|
2
main.ts
2
main.ts
|
@ -58,7 +58,7 @@ const SetBox = Record.makeConstructor<Ref<Entity>>('SetBox', ['newValue']);
|
|||
let startTime = Date.now();
|
||||
let prevValue = 0;
|
||||
const LIMIT = 500000;
|
||||
Turn.for(null, async (t: Turn) => {
|
||||
Turn.for(new Actor(), async (t: Turn) => {
|
||||
const ds = new Ref(new Actor(), new Dataspace());
|
||||
|
||||
// Box
|
||||
|
|
Loading…
Reference in New Issue