All files / packages/tao-router/src Router.js

0.67% Statements 1/150
0% Branches 0/150
0% Functions 0/27
0.67% Lines 1/149
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300                  1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
// import createHistory from 'history/createBrowserHistory';
import { createBrowserHistory as createHistory } from 'history';
import routington from 'routington';
import get from 'get-value';
import set from 'set-value';
import { AppCtx } from '@tao.js/core';
 
import makeRouteHandler, { deconstructPath, convertPath } from './routeHandler';
 
const CHANGE_ACTION_SIGNAL = 'POP';
 
function capitalize(str) {
  if (!str || typeof str !== 'string') {
    return str;
  }
  return `${str.substring(0, 1).toUpperCase()}${str.substring(1)}`;
}
 
function wrapAc(ac) {
  return ac instanceof AppCtx
    ? ac
    : new AppCtx(ac.t || ac.term, ac.a || ac.action, ac.o || ac.orient);
}
 
function wrapIgnore(handler, ignore) {
  if (!ignore) {
    return handler;
  }
  const ignoreACs = (Array.isArray(ignore) ? ignore : [ignore]).map(iac =>
    wrapAc(iac)
  );
  return (tao, data) => {
    const matchedIgnore = ignoreACs.some(ignoreAc => ignoreAc.isMatch(tao));
    if (!matchedIgnore) {
      return handler(tao, data);
    }
  };
}
 
function mergeData(pathData, defaultData, parentPath) {
  Object.entries(defaultData).forEach(([key, val]) => {
    const dataPath = parentPath ? `${parentPath}.${key}` : key;
    if (typeof get(pathData, dataPath) === 'undefined') {
      set(pathData, dataPath, val);
    } else if (typeof val === 'object' && !Array.isArray(val)) {
      mergeData(pathData, val, dataPath);
    }
  });
}
 
function reactToRoute(TAO, match, debug = false) {
  debug && console.log('Router::reacting to route');
  const pathMatched = match.node.deconstruction.reduce((pathData, pathItem) => {
    if (!pathItem.match) {
      return pathData;
    }
    const dataPath = pathItem.match[2];
    const pathParam = pathItem.use.substring(1);
    let paramData = match.param[pathParam];
    if (paramData) {
      set(pathData, dataPath, paramData);
    }
    return pathData;
  }, {});
  if (match.node.lowerCase) {
    pathMatched.t = capitalize(pathMatched.t);
    pathMatched.a = capitalize(pathMatched.a);
    pathMatched.o = capitalize(pathMatched.o);
  }
  debug &&
    console.log('reactToRoute::node.defaultData:', match.node.defaultData);
  debug && console.log('reactToRoute::node.attached:', match.node.attached);
  match.node.attached.forEach(trigram => {
    const defaultData = match.node.defaultData
      ? match.node.defaultData.get(trigram.key)
      : null;
    let acParts = pathMatched;
    if (defaultData) {
      mergeData(acParts, defaultData);
    }
    const { t, a, o, ...data } = acParts;
    const ac = new AppCtx(
      trigram.isTermWild ? t : trigram.t,
      trigram.isActionWild ? a : trigram.a,
      trigram.isOrientWild ? o : trigram.o,
      data
    );
 
    TAO.setCtx({ t: 'Route', a: 'Match', o: ac.o }, [match.node.route, ac]);
    debug && console.log('reactToRoute::TAO.setAppCtx::ac', ac);
    TAO.setAppCtx(ac);
  });
}
 
export default class Router {
  constructor(TAO, history, opts) {
    this._tao = TAO;
    if (!opts) {
      opts = history;
      history = null;
    }
    const { debug = false } = opts || {};
    this._debug = debug;
    this._history = history || createHistory();
    this.setupEvents = this.setupEvents.bind(this);
    this.historyChange = this.historyChange.bind(this);
    this.getPathFrom = this.getPathFrom.bind(this);
    this.getAcsFrom = this.getAcsFrom.bind(this);
    this._routes = new Map();
    this._router = routington();
    this.setupEvents(
      TAO,
      this._history,
      opts.initAc,
      opts.incomingAc,
      opts.defaultRoute,
      opts.orient
    );
  }
 
  setupEvents = (TAO, history, initAc, incomingAc, defaultRoute, orient) => {
    this._unlistenHistory = history.listen(this.historyChange);
    const incoming = !incomingAc
      ? []
      : (Array.isArray(incomingAc) ? incomingAc : [incomingAc]).map(ac =>
          wrapAc(ac)
        );
    TAO.addInlineHandler(initAc, (tao, data) => {
      return new AppCtx('Router', 'Init', tao.o);
    });
    TAO.addInlineHandler(
      { t: 'Routes', a: 'Configure', o: orient || '' },
      (tao, { Routes, Configure }) => {
        const routes = Array.isArray(Routes) ? Routes : [Routes];
        // routes.forEach((r, i) => {
        //   const config = Configure[i];
        routes.forEach(({ Route, Add, Remove, Attach, Detach }) => {
          if (Add) {
            TAO.setCtx({ t: 'Route', a: 'Add', o: tao.o }, [Route, Add]);
          }
          if (Remove) {
            TAO.setCtx({ t: 'Route', a: 'Remove', o: tao.o }, [Route, Remove]);
          }
          if (Attach) {
            TAO.setCtx({ t: 'Route', a: 'Attach', o: tao.o }, [Route, Attach]);
          }
          if (Detach) {
            TAO.setCtx({ t: 'Route', a: 'Detach', o: tao.o }, [Route, Detach]);
          }
        });
        return incoming[0];
      }
    );
    TAO.addInlineHandler(
      { t: 'Route', a: 'Add', o: orient || '' },
      (tao, { Route, Add }) => {
        // add Route to the Router's configuration
        this._debug && console.log(`adding to Route:`, Route, Add);
        const routeHandler = makeRouteHandler(history, Route, this._debug);
        const trigram = wrapAc(Add.tao || Add);
        const oldHandler = this._routes.get(trigram.key);
        if (oldHandler) {
          TAO.removeAsyncHandler(trigram.unwrapCtx(), oldHandler);
        }
        const handler = wrapIgnore(routeHandler, Add.ignore);
        this._routes.set(trigram.key, handler);
        this._debug &&
          console.log('route.add::trigram.unwrapCtx():', trigram.unwrapCtx());
        TAO.addAsyncHandler(trigram.unwrapCtx(), handler);
        if (Add.attach) {
          return new AppCtx('Route', 'Attach', tao.o, Route, Add);
        }
      }
    );
    TAO.addInlineHandler(
      { t: 'Route', a: 'Remove', o: orient || '' },
      (tao, { Route, Remove }) => {
        // remove Route from the Router's configuration
        this._debug && console.log(`removing from Route ${Route}:`, Remove);
        const trigram = wrapAc(Remove);
        const routeHandler = this._routes.get(trigram.key);
        if (!routeHandler) {
          return;
        }
        TAO.removeAsyncHandler(trigram.unwrapCtx(), routeHandler);
        this._routes.delete(trigram.key);
        if (Remove.detach) {
          return new AppCtx('Route', 'Detach', tao.o, Route, Remove);
        }
      }
    );
    TAO.addInlineHandler(
      { t: 'Route', a: 'Attach', o: orient || '' },
      (tao, { Route, Attach }) => {
        // attach Trigram to an incoming route
        this._debug && console.log('Going to Attach Route:', { Route, Attach });
        const routeToUse = Route.path || Route;
        const deconstruction = deconstructPath(routeToUse);
        const convertedPath = convertPath(deconstruction);
        const node = this._router.define(convertedPath)[0];
        node.route = routeToUse;
        node.lowerCase = Route.lowerCase;
        node.deconstruction = deconstruction;
        node.attached = node.attached || [];
        // const attachTo = wrapAc({ o: tao.o, ...(Attach.tao || Attach) });
        const attachTo = wrapAc(Attach.tao || Attach);
        node.attached.push(attachTo);
        node.defaultData = node.defaultData || new Map();
        if (Attach.tao) {
          node.defaultData.set(attachTo.key, Attach.data || {});
        }
      }
    );
    TAO.addInlineHandler(
      { t: 'Route', a: 'Detach', o: orient || '' },
      (tao, { Route, Detach }) => {
        // detach Trigram from an incoming route
        this._debug && console.log('Going to Detach Route:', { Route, Detach });
        const routeToUse = Route.path || Route;
        const deconstruction = deconstructPath(routeToUse);
        const convertedPath = convertPath(deconstruction);
        const node = this._router.define(convertedPath)[0];
        if (node.attached && node.attached.length) {
          // const attachTo = wrapAc({ o: tao.o, ...(Attach.tao || Attach) });
          const attachTo = wrapAc(Attach.tao || Attach);
          node.attached = node.attached.filter(
            trigram => !trigram.isMatch(attachTo, true)
          );
          if (node.defaultData && node.defaultData.has(attachTo.key)) {
            node.defaultData.delete(attachTo.key);
          }
        }
      }
    );
    // TAO.addAsyncHandler({}, (tao, data) => {
    //   const current = new AppCtx(tao.t, tao.a, tao.o, data);
    //   // ignore the initial AC b/c we use that to trigger {Router,Init,}
    //   if (current.key === initAc.key) {
    //     return;
    //   }
    //   const updatePath = this.getPathFrom(tao, data);
    //   if (updatePath != null) {
    //     this._history.push(updatePath);
    //   }
    // });
    if (incoming.length) {
      this._debug && console.log('adding handlers for incoming:', incoming);
      incoming.forEach(inAc => {
        TAO.addInlineHandler(inAc, (tao, data) => {
          this._debug &&
            console.log('Router::incoming AC handler:', { tao, data });
          let match = this._router.match(this._history.location.pathname);
          if (!match && defaultRoute) {
            match = this._router.match(defaultRoute);
            this._debug &&
              console.log(
                'Router::no match::match from default route "%s"',
                defaultRoute,
                match
              );
          }
          if (match) {
            reactToRoute(this._tao, match, this._debug);
          }
          // const loc = this._history.location;
          // const acs = this.getAcsFrom(loc, tao, data);
          // acs.forEach(appCtx => {
          //   if (appCtx != null) {
          //     TAO.setAppCtx(appCtx);
          //   }
          // });
        });
      });
    }
  }; //;
 
  // this should move to just being an added Async Handler on the individual routes
  // OR would that make the TAO sets too big?
  // they would certainly be completely directed with no middle man interpreter
  getPathFrom = (tao, data) => {
    if (tao.a !== 'View') {
      return;
    }
    return tao.t === 'Space' ? '/space' : '';
  };
 
  getAcsFrom = (location, tao, data) => {
    return [];
  };
 
  historyChange = (location, action) => {
    // match the new location to our route tree to find Trigrams and fire ACs from path data
    this._debug && console.log('history change:', { location, action });
    const match = this._router.match(location.pathname);
    if (CHANGE_ACTION_SIGNAL === action && match) {
      reactToRoute(this._tao, match, this._debug);
    }
  };
}