Skip to content

Instantly share code, notes, and snippets.

@mcwhittemore
Last active January 3, 2016 00:09
Show Gist options
  • Save mcwhittemore/8381166 to your computer and use it in GitHub Desktop.
Save mcwhittemore/8381166 to your computer and use it in GitHub Desktop.
# Enitre - Module Access to Entire Core via the `entire_api`

Enitre - Module Access to Entire Core

One of the questions I've been struggling with is how entire_modules access entire_core. I think a lot of the struggle with understand this is coming from a lack of simply defining terms.

Terms

The entire_core is a unit of koa middleware designed to allow developers to build features for a website in the same way as they build modules for node. It does this by exposing an api to entire_modules.

An entire_module is a node_module designed in such a way that it when run by the entire_core it can 1) consume an api, 2) expose client side javascript 3) expose css 4) expose views and 5) expose public content such as images.

The entire_api is a collection of functions that enables an entire_module to register routes and update views modles with the entire_core

Problem

How to give an entire_module access the the entire_api without too strongly enforcing module design. The problem with enforcing module design might be debated. I can see why one could argue that a stright forward approch is easy to grasp and thus better. My worries about this are 1) that node_modules don't require much in the way of structure and 2) that by enforcing a system we'd force the entire entire_api into every module and thus into lots of closures.

Possible Solution Overview

  1. My first solution was to force all entire_modules to export a function that takes a single paramter, the entire_api.
  2. My second solution is to force all entire_modules to export a function that takes a single parameter, the lower level data structures of the api. In turn I'd write modules that consume this data and thus build the entire_api part by part.
  3. My third solution is to attach a simple api the the global scope and then write modules that consume this simple api to gain access to the under lying data and thus build the entire_api part by part.

Solution 1

Force entire_modules to export a function that takes the entire_api

entire api

module.exports = {
  router: { ... },
  views: { ... }
}

entire module

module.exports = function(api){
  api.router.get("/", function*(){ ... });
  api.view.before("module/index", function*(data){ ... });
}

entire core

//somewhere in the entire_core
var entire_api = require("path/to/api");
var module = require("path/to/module");
module(entire_api);

Solution 2

Force entire_module to export a function that takes a data structure. Force the developer to add the the structure correctly...

entire api

var router = require("path/to/router");
var views = require("path/to/views");

module.exports = function(data){
  router(data);
  view(data);
}

entire module

module.exports = function(data){
  data.router.get.push({path:"/", fn:funtion*(){ ... });
  data.view.before.push({view:"module/index", fn:function*(){ ... });
}

entire core

//somewhere in the entire_core
var baseData = require("base/data");
var newBaseData = baseData();
var module = require("path/to/module");
module(newBaseData);
var entire_api = require("path/to/api");
entire_api(newBaseData);

Solution 3

Add a data structure getter to the global scope. Write router and view controll modules to consume this global var. Let devs write their module how ever they like.

entire api

var moduleDataGetter = require("module/data/getter");
module.exports = function(){
  var name = "something"; //somehow get the name of module that requesting this...
  return moduleDataGetter(name);

entire router

module.exports = {
  get: function(path, fn){
    var moduleData = global.entireApi(); //note, global. is not required, just there to show its global.
    moduleData.router.get.push({path:path, fn:fn);
  }
}

entire module

var router = require("entire-router");
router.get("/", function*(){ ... });

entire core

globals.entireApi = require("entire/api");
var module = require("path/to/module");
@mako-taco
Copy link

Solution 1. Putting it in globals literally puts it in the scope of every single closure throughout the application. Solution 2 is too rigid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment