Browse Source

kernel: internal events

Herbert Vojčík 6 years ago
parent
commit
fe1efeeeaf
3 changed files with 55 additions and 23 deletions
  1. 21 3
      support/kernel-fundamentals.js
  2. 22 10
      support/kernel-language.js
  3. 12 10
      support/kernel-runtime.js

+ 21 - 3
support/kernel-fundamentals.js

@@ -61,6 +61,17 @@ define(function () {
         this.globals = globals;
     }
 
+    function EventBrik (brikz, st) {
+        var emit = {};
+
+        this.emit = emit;
+
+        this.declareEvent = function (event) {
+            if (!emit[event]) emit[event] = function () {
+            };
+        }
+    }
+
     function RootBrik (brikz, st) {
         /* Smalltalk foundational objects */
 
@@ -172,11 +183,13 @@ define(function () {
         st.classes = this.classes = classes;
     }
 
-    MethodsBrik.deps = ["selectors", "root", "selectorConversion"];
+    MethodsBrik.deps = ["event", "selectors", "root", "selectorConversion"];
     function MethodsBrik (brikz, st) {
         var registerSelector = brikz.selectors.registerSelector;
         var SmalltalkObject = brikz.root.Object;
         var coreFns = brikz.root.coreFns;
+        var emit = brikz.event.emit;
+        var declareEvent = brikz.event.declareEvent;
 
         function SmalltalkMethod () {
         }
@@ -214,6 +227,8 @@ define(function () {
             updateMethod(method.selector, traitOrBehavior);
         };
 
+        declareEvent("selectorsAdded");
+
         function registerNewSelectors (method) {
             var newSelectors = [];
 
@@ -226,7 +241,7 @@ define(function () {
 
             selectorInUse(method.selector);
             method.messageSends.forEach(selectorInUse);
-            if (st._selectorsAdded) st._selectorsAdded(newSelectors);
+            emit.selectorsAdded(newSelectors);
         }
 
         st.removeMethod = function (method, traitOrBehavior) {
@@ -241,6 +256,8 @@ define(function () {
             traitOrBehavior.methods = Object.create(null);
         };
 
+        declareEvent("methodReplaced");
+
         function updateMethod (selector, traitOrBehavior) {
             var oldMethod = traitOrBehavior.methods[selector],
                 newMethod = traitOrBehavior.localMethods[selector];
@@ -256,7 +273,7 @@ define(function () {
                 delete traitOrBehavior.methods[selector];
                 traitOrBehavior.methodRemoved(oldMethod);
             }
-            if (st._methodReplaced) st._methodReplaced(newMethod, oldMethod, traitOrBehavior);
+            emit.methodReplaced(newMethod, oldMethod, traitOrBehavior);
         }
 
         this.updateMethod = updateMethod;
@@ -375,6 +392,7 @@ define(function () {
         brikz.smalltalkGlobals = SmalltalkGlobalsBrik;
         brikz.root = RootBrik;
         brikz.nil = NilBrik;
+        brikz.event = EventBrik;
         brikz.arraySet = ArraySetBrik;
         brikz.selectorConversion = SelectorConversionBrik;
         brikz.selectors = SelectorsBrik;

+ 22 - 10
support/kernel-language.js

@@ -59,13 +59,15 @@ define(function () {
         });
     }
 
-    TraitsBrik.deps = ["behaviors", "methods", "composition", "root"];
+    TraitsBrik.deps = ["event", "behaviors", "methods", "composition", "root"];
     function TraitsBrik (brikz, st) {
         var coreFns = brikz.root.coreFns;
         var SmalltalkObject = brikz.root.Object;
         var setupMethods = brikz.methods.setupMethods;
         var traitMethodChanged = brikz.composition.traitMethodChanged;
         var buildTraitOrClass = brikz.behaviors.buildTraitOrClass;
+        var emit = brikz.event.emit;
+        var declareEvent = brikz.event.declareEvent;
 
         function SmalltalkTrait () {
         }
@@ -77,25 +79,29 @@ define(function () {
         defineMethod(SmalltalkTrait, "toString", function () {
             return 'Smalltalk Trait ' + this.className;
         });
+        declareEvent("traitAdded");
         defineMethod(SmalltalkTrait, "added", function () {
-            if (st._traitAdded) st._traitAdded(this);
+            emit.traitAdded(this);
         });
+        declareEvent("traitRemoved");
         defineMethod(SmalltalkTrait, "removed", function () {
-            if (st._traitRemoved) st._traitRemoved(this);
+            emit.traitRemoved(this);
         });
+        declareEvent("traitMethodAdded");
         defineMethod(SmalltalkTrait, "methodAdded", function (method) {
             var self = this;
             this.traitUsers.forEach(function (each) {
                 traitMethodChanged(method.selector, method, self, each);
             });
-            if (st._traitMethodAdded) st._traitMethodAdded(method, this);
+            emit.traitMethodAdded(method, this);
         });
+        declareEvent("traitMethodRemoved");
         defineMethod(SmalltalkTrait, "methodRemoved", function (method) {
             var self = this;
             this.traitUsers.forEach(function (each) {
                 traitMethodChanged(method.selector, null, self, each);
             });
-            if (st._traitMethodRemoved) st._traitMethodRemoved(method, this);
+            emit.traitMethodRemoved(method, this);
         });
 
         function traitBuilder (className) {
@@ -243,7 +249,7 @@ define(function () {
         this.traitMethodChanged = traitMethodChanged;
     }
 
-    ClassesBrik.deps = ["root", "behaviors", "methods", "arraySet", "smalltalkGlobals"];
+    ClassesBrik.deps = ["root", "event", "behaviors", "methods", "arraySet", "smalltalkGlobals"];
     function ClassesBrik (brikz, st) {
         var SmalltalkRoot = brikz.root.Root;
         var coreFns = brikz.root.coreFns;
@@ -254,6 +260,8 @@ define(function () {
         var removeTraitOrClass = brikz.behaviors.removeTraitOrClass;
         var addElement = brikz.arraySet.addElement;
         var removeElement = brikz.arraySet.removeElement;
+        var emit = brikz.event.emit;
+        var declareEvent = brikz.event.declareEvent;
 
         function SmalltalkBehavior () {
         }
@@ -284,19 +292,23 @@ define(function () {
         defineMethod(SmalltalkMetaclass, "toString", function () {
             return 'Smalltalk Metaclass ' + this.instanceClass.className;
         });
+        declareEvent("classAdded");
         defineMethod(SmalltalkClass, "added", function () {
             addSubclass(this);
-            if (st._classAdded) st._classAdded(this);
+            emit.classAdded(this);
         });
+        declareEvent("classRemoved");
         defineMethod(SmalltalkClass, "removed", function () {
-            if (st._classRemoved) st._classRemoved(this);
+            emit.classRemoved(this);
             removeSubclass(this);
         });
+        declareEvent("behaviorMethodAdded");
         defineMethod(SmalltalkBehavior, "methodAdded", function (method) {
-            if (st._behaviorMethodAdded) st._behaviorMethodAdded(method, this);
+            emit.behaviorMethodAdded(method, this);
         });
+        declareEvent("behaviorMethodRemove");
         defineMethod(SmalltalkBehavior, "methodRemoved", function (method) {
-            if (st._behaviorMethodRemoved) st._behaviorMethodRemoved(method, this);
+            emit.behaviorMethodRemoved(method, this);
         });
 
         this.bootstrapHierarchy = function () {

+ 12 - 10
support/kernel-runtime.js

@@ -61,13 +61,14 @@ define(function () {
         this.installJSMethod = installJSMethod;
     }
 
-    RuntimeClassesBrik.deps = ["selectors", "dnu", "behaviors", "classes", "manipulation"];
+    RuntimeClassesBrik.deps = ["event", "selectors", "dnu", "behaviors", "classes", "manipulation"];
     function RuntimeClassesBrik (brikz, st) {
         var selectors = brikz.selectors;
         var classes = brikz.behaviors.classes;
         var wireKlass = brikz.classes.wireKlass;
         var installMethod = brikz.manipulation.installMethod;
         var installJSMethod = brikz.manipulation.installJSMethod;
+        var emit = brikz.event.emit;
 
         var detachedRootClasses = [];
 
@@ -96,20 +97,20 @@ define(function () {
             if (!traitOrClass.trait) initClassAndMetaclass(traitOrClass);
         });
 
-        st._classAdded = function (klass) {
+        emit.classAdded = function (klass) {
             initClassAndMetaclass(klass);
             klass._enterOrganization();
         };
 
-        st._traitAdded = function (trait) {
+        emit.traitAdded = function (trait) {
             trait._enterOrganization();
         };
 
-        st._classRemoved = function (klass) {
+        emit.classRemoved = function (klass) {
             klass._leaveOrganization();
         };
 
-        st._traitRemoved = function (trait) {
+        emit.traitRemoved = function (trait) {
             trait._leaveOrganization();
         };
 
@@ -167,31 +168,32 @@ define(function () {
         }
     }
 
-    RuntimeMethodsBrik.deps = ["manipulation", "dnu", "runtimeClasses"];
+    RuntimeMethodsBrik.deps = ["event", "manipulation", "dnu", "runtimeClasses"];
     function RuntimeMethodsBrik (brikz, st) {
         var installMethod = brikz.manipulation.installMethod;
         var installJSMethod = brikz.manipulation.installJSMethod;
         var makeDnuHandler = brikz.dnu.makeDnuHandler;
         var detachedRootClasses = brikz.runtimeClasses.detachedRootClasses;
+        var emit = brikz.event.emit;
 
-        st._behaviorMethodAdded = function (method, klass) {
+        emit.behaviorMethodAdded = function (method, klass) {
             installMethod(method, klass);
             propagateMethodChange(klass, method, klass);
         };
 
-        st._selectorsAdded = function (newSelectors) {
+        emit.selectorsAdded = function (newSelectors) {
             var targetClasses = detachedRootClasses();
             newSelectors.forEach(function (pair) {
                 makeDnuHandler(pair, targetClasses);
             });
         };
 
-        st._behaviorMethodRemoved = function (method, klass) {
+        emit.behaviorMethodRemoved = function (method, klass) {
             delete klass.fn.prototype[method.jsSelector];
             propagateMethodChange(klass, method, null);
         };
 
-        st._methodReplaced = function (newMethod, oldMethod, traitOrBehavior) {
+        emit.methodReplaced = function (newMethod, oldMethod, traitOrBehavior) {
             traitOrBehavior._methodOrganizationEnter_andLeave_(newMethod, oldMethod);
         };