[Indigo]<Rosemary>Release>Rosemary2.DF=>RoseEventsImpl.Mesa
Last Edited by: Spreitzer, September 6, 1984 3:26:31 pm PDT
DIRECTORY Asserting, Atom, IO, List, RoseEvents, RoseTypes;
RoseEventsImpl: CEDAR PROGRAM
IMPORTS Asserting, Atom, IO, List
EXPORTS RoseEvents, RoseTypes =
BEGIN OPEN RoseTypes;
Error: PUBLIC ERROR [msg: ROPE, data: REF ANYNIL] = CODE;
Warning: PUBLIC SIGNAL [msg: ROPE, data: REF ANYNIL] = CODE;
Stop: PUBLIC SIGNAL [msg: ROPE, data: REF ANYNIL] = CODE;
watchersKey: ATOM ← Atom.MakeAtom["Spreitzer January 6, 1984 8:07 pm"];
AddWatcher: PUBLIC PROC [event: ATOM, watcher: Watcher, watched: REF ANYNIL] =
BEGIN
wll: List.AList;
wl: WatcherList;
IF watched # NIL THEN WITH watched SELECT FROM
node: Node => {
Add: PROC [priority: Priority] = {node.watchers[priority] ← CONS[watcher, node.watchers[priority]]};
SELECT event FROM
$ChangeEarly => Add[high];
$ChangeLate => Add[ordinary];
ENDCASE => BEGIN
wl ← NARROW[Asserting.FnVal[fn: event, from: node.other]];
wl ← CONS[watcher, wl];
node.other ← Asserting.AssertFn1[fn: event, val: wl, inAdditionTo: node.other];
END;
RETURN};
cell: Cell => {
SELECT event FROM
$Schedule => {IF cell.substantiality = Shadow THEN ERROR Error[IO.PutFR["Cell %g not Real", IO.rope[cell.name]]];
cell.realCellStuff.schedWatchers ← CONS[watcher, cell.realCellStuff.schedWatchers]};
$Eval => {IF cell.substantiality = Shadow THEN ERROR Error[IO.PutFR["Cell %g not Real", IO.rope[cell.name]]];
cell.realCellStuff.evalWatchers ← CONS[watcher, cell.realCellStuff.evalWatchers]};
ENDCASE => BEGIN
wl ← NARROW[Asserting.FnVal[fn: event, from: cell.other]];
wl ← CONS[watcher, wl];
cell.other ← Asserting.AssertFn1[fn: event, val: wl, inAdditionTo: cell.other];
END;
RETURN};
ENDCASE;
wll ← NARROW[Atom.GetProp[atom: event, prop: watchersKey]];
wl ← NARROW[List.Assoc[key: watched, aList: wll]];
wl ← CONS[watcher, wl];
wll ← List.PutAssoc[key: watched, val: wl, aList: wll];
Atom.PutProp[atom: event, prop: watchersKey, val: wll];
END;
RemoveWatcher: PUBLIC PROC [event: ATOM, watcher: Watcher, watched: REF ANYNIL] =
BEGIN
wll: List.AList;
wl: WatcherList;
IF watched # NIL THEN WITH watched SELECT FROM
node: Node => {
Sub: PROC [priority: Priority] = {node.watchers[priority] ← FilterWatcher[watcher, node.watchers[priority]]};
SELECT event FROM
$ChangeEarly => Sub[high];
$ChangeLate => Sub[ordinary];
ENDCASE => BEGIN
wl ← NARROW[Asserting.FnVal[fn: event, from: node.other]];
wl ← FilterWatcher[watcher, wl];
node.other ← Asserting.AssertFn1[fn: event, val: wl, inAdditionTo: node.other];
END;
RETURN};
cell: Cell => {
SELECT event FROM
$Schedule => {cell.realCellStuff.schedWatchers ← FilterWatcher[watcher, cell.realCellStuff.schedWatchers]};
$Eval => {cell.realCellStuff.evalWatchers ← FilterWatcher[watcher, cell.realCellStuff.evalWatchers]};
ENDCASE => BEGIN
wl ← NARROW[Asserting.FnVal[fn: event, from: cell.other]];
wl ← FilterWatcher[watcher, wl];
cell.other ← Asserting.AssertFn1[fn: event, val: wl, inAdditionTo: cell.other];
END;
RETURN};
ENDCASE;
wll ← NARROW[Atom.GetProp[atom: event, prop: watchersKey]];
wl ← NARROW[List.Assoc[key: watched, aList: wll]];
wl ← FilterWatcher[watcher, wl];
wll ← List.PutAssoc[key: watched, val: wl, aList: wll];
Atom.PutProp[atom: event, prop: watchersKey, val: wll];
END;
FilterWatcher: PROC [watcher: Watcher, watchers: WatcherList] RETURNS [ohne: WatcherList] =
BEGIN
cur, prev: WatcherList ← NIL;
ohne ← watchers;
FOR cur ← watchers, cur.rest WHILE cur # NIL DO
IF cur.first = watcher THEN
{IF prev = NIL THEN ohne ← cur.rest ELSE prev.rest ← cur.rest;
RETURN};
prev ← cur;
ENDLOOP;
END;
Notify: PUBLIC PROC [event: ATOM, watched: REF ANYNIL, handleAborted: BOOLEANFALSE, arg: REF ANYNIL] =
BEGIN
GetWatchers: PROC RETURNS [wl: WatcherList] =
BEGIN
wll: List.AList;
IF watched # NIL THEN WITH watched SELECT FROM
node: Node => {
SELECT event FROM
$ChangeEarly => RETURN [node.watchers[high]];
$ChangeLate => RETURN [node.watchers[ordinary]];
ENDCASE => RETURN [NARROW[Asserting.FnVal[fn: event, from: node.other]]]};
cell: Cell => {
SELECT event FROM
$Schedule => RETURN [cell.realCellStuff.schedWatchers];
$Eval => RETURN [cell.realCellStuff.evalWatchers];
ENDCASE => RETURN [NARROW[Asserting.FnVal[fn: event, from: cell.other]]]};
ENDCASE;
wll ← NARROW[Atom.GetProp[atom: event, prop: watchersKey]];
wl ← NARROW[List.Assoc[key: watched, aList: wll]];
END;
wl: WatcherList ← GetWatchers[];
abort: BOOLEANFALSE;
abortedWatchers: WatcherList ← NIL;
WHILE wl # NIL DO
wl.first.Notify[
event: event,
watched: watched,
watcherData: wl.first.watcherData,
arg: arg
!ABORTED => IF handleAborted THEN {
abort ← TRUE;
abortedWatchers ← CONS[wl.first, abortedWatchers];
CONTINUE}];
wl ← wl.rest;
ENDLOOP;
IF abort THEN ERROR ABORTED;
END;
END.