Browse Source

Brikz is a function, return a function.

No `.rebuild`, so no `exclude`.

Also, `bak` is no passed as third arg,
but set as `FooBrik[backupKey]`
just before call to `new FooBrik(brikz, api)`.
Herby Vojčík 3 years ago
parent
commit
33f14adca9
3 changed files with 25 additions and 33 deletions
  1. 5 5
      README.md
  2. 9 14
      amd/brikz.js
  3. 11 14
      brikz.js

+ 5 - 5
README.md

@@ -16,10 +16,10 @@ Brikz is the system that assembles the resulting object (API) from parts. These
 How
 ===
 
-Brikz aims to be minimal, so just copy it and include it where appropriate (it is just one `Brikz` constructor). Then instantiate it:
+Brikz aims to be minimal, so just copy it and include it where appropriate (it is just one `Brikz` function). Then instantiate it:
 ```
-  var api = /* skeleton of my object */;
-  var brikz = new Brikz(api);
+  var api = {}/* skeleton of my object */;
+  var brikz = Brikz(api);
 ```
 
 Then, you should have some parts:
@@ -49,12 +49,12 @@ You then put the parts in `brikz` object, rebuild it and you have `api` ready fo
 ```
   brikz.foo = Foo;
   brikz.bar = Bar;
-  brikz.rebuild();
+  brikz();
 
   api.publishedBar(...);
 ```
 
-You can add / delete / change parts later and call `rebuild` again. Parts that are not functions, are left as is (their internal as well as API part); parts that are functions are instantiated again and merged in. Parts that are `null`ed are removed.
+You can add / delete / change parts later and call `brikz` again to rebuild. Parts that are not functions, are left as is (their internal as well as API part); parts that are functions are instantiated again and merged in. Parts that are `null`ed are removed.
 
 Advanced use
 ====

+ 9 - 14
amd/brikz.js

@@ -1,13 +1,13 @@
 define([], function () {
-    return function Brikz(api, apiKey, initKey) {
+    return function Brikz(api, apiKey, initKey, backupKey) {
         "use strict";
 
         //jshint eqnull:true
 
-        var brikz = this,
-            backup = {};
+        var backup = {};
         apiKey = apiKey || 'exports';
         initKey = initKey || '__init__';
+        backupKey = backupKey || 'backup';
 
         function mixin(src, target, what) {
             for (var keys = Object.keys(what || src), l = keys.length, i = 0; i < l; ++i) {
@@ -23,12 +23,7 @@ define([], function () {
             return target;
         }
 
-        Object.defineProperties(this, {
-            rebuild: { value: null, enumerable: false, configurable: true, writable: true }
-        });
-        var exclude = mixin(this, {});
-
-        this.rebuild = function () {
+        function brikz() {
             Object.keys(backup).forEach(function (key) {
                 mixin(null, api, (backup[key] || 0)[apiKey] || {});
             });
@@ -37,15 +32,13 @@ define([], function () {
                 chk = {};
 
             function ensure(key) {
-                if (key in exclude) {
-                    return null;
-                }
                 var b = brikz[key],
                     bak = backup[key];
                 mixin(null, api, api);
                 while (typeof b === "function") {
                     (b.deps || []).forEach(ensure);
-                    b = new b(brikz, api, bak);
+                    b[backupKey] = bak;
+                    b = new b(brikz, api);
                 }
                 brikz[key] = b;
                 if (b && !chk[key]) {
@@ -71,6 +64,8 @@ define([], function () {
                 }
             });
             backup = mixin(brikz, {});
-        };
+        }
+
+        return brikz;
     };
 });

+ 11 - 14
brikz.js

@@ -1,13 +1,14 @@
 export default (
 
-function Brikz (api, apiKey, initKey) {
+function Brikz(api, apiKey, initKey, backupKey) {
     "use strict";
 
     //jshint eqnull:true
 
-    var brikz = this, backup = {};
+    var backup = {};
     apiKey = apiKey || 'exports';
     initKey = initKey || '__init__';
+    backupKey = backupKey || 'backup';
 
     function mixin(src, target, what) {
         for (var keys = Object.keys(what || src), l = keys.length, i = 0; i < l; ++i) {
@@ -23,26 +24,20 @@ function Brikz (api, apiKey, initKey) {
         return target;
     }
 
-    Object.defineProperties(this, {
-        rebuild: {value: null, enumerable: false, configurable: true, writable: true}
-    });
-    var exclude = mixin(this, {});
-
-    this.rebuild = function () {
+    function brikz() {
         Object.keys(backup).forEach(function (key) {
             mixin(null, api, (backup[key] || 0)[apiKey] || {});
         });
         var oapi = mixin(api, {}), order = [], chk = {};
 
         function ensure(key) {
-            if (key in exclude) {
-                return null;
-            }
-            var b = brikz[key], bak = backup[key];
+            var b = brikz[key],
+                bak = backup[key];
             mixin(null, api, api);
             while (typeof b === "function") {
                 (b.deps || []).forEach(ensure);
-                b = new b(brikz, api, bak);
+                b[backupKey] = bak;
+                b = new b(brikz, api);
             }
             brikz[key] = b;
             if (b && !chk[key]) {
@@ -68,7 +63,9 @@ function Brikz (api, apiKey, initKey) {
             }
         });
         backup = mixin(brikz, {});
-    };
+    }
+
+    return brikz;
 }
 
 );