Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: internalize flow-router (from meteor 3 branch) #31365

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/meteor/packages/flow-router/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.build*
*.browserify.js.cached
*.browserify.js.map
1 change: 1 addition & 0 deletions apps/meteor/packages/flow-router/.npm/package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
7 changes: 7 additions & 0 deletions apps/meteor/packages/flow-router/.npm/package/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This directory and the files immediately inside it are automatically generated
when you change this package's NPM dependencies. Commit the files in this
directory (npm-shrinkwrap.json, .gitignore, and this README) to source control
so that others run the same versions of sub-dependencies.

You should NOT check in the node_modules directory that Meteor automatically
creates; if you are using git, the .gitignore file tells git to ignore it.
28 changes: 28 additions & 0 deletions apps/meteor/packages/flow-router/.npm/package/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions apps/meteor/packages/flow-router/client/_init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Export Router Instance
FlowRouter = new Router();
FlowRouter.Router = Router;
FlowRouter.Route = Route;

// Initialize FlowRouter
Meteor.startup(function () {
if(!FlowRouter._askedToWait) {
FlowRouter.initialize();
}
});
57 changes: 57 additions & 0 deletions apps/meteor/packages/flow-router/client/group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Group = function(router, options, parent) {
options = options || {};

if (options.prefix && !/^\/.*/.test(options.prefix)) {
var message = "group's prefix must start with '/'";
throw new Error(message);
}

this._router = router;
this.prefix = options.prefix || '';
this.name = options.name;
this.options = options;

this._triggersEnter = options.triggersEnter || [];
this._triggersExit = options.triggersExit || [];
this._subscriptions = options.subscriptions || Function.prototype;

this.parent = parent;
if (this.parent) {
this.prefix = parent.prefix + this.prefix;

this._triggersEnter = parent._triggersEnter.concat(this._triggersEnter);
this._triggersExit = this._triggersExit.concat(parent._triggersExit);
}
};

Group.prototype.route = function(pathDef, options, group) {
options = options || {};

if (!/^\/.*/.test(pathDef)) {
var message = "route's path must start with '/'";
throw new Error(message);
}

group = group || this;
pathDef = this.prefix + pathDef;

var triggersEnter = options.triggersEnter || [];
options.triggersEnter = this._triggersEnter.concat(triggersEnter);

var triggersExit = options.triggersExit || [];
options.triggersExit = triggersExit.concat(this._triggersExit);

return this._router.route(pathDef, options, group);
};

Group.prototype.group = function(options) {
return new Group(this._router, options, this);
};

Group.prototype.callSubscriptions = function(current) {
if (this.parent) {
this.parent.callSubscriptions(current);
}

this._subscriptions.call(current.route, current.params, current.queryParams);
};
2 changes: 2 additions & 0 deletions apps/meteor/packages/flow-router/client/modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
page = require('page');
qs = require('qs');
125 changes: 125 additions & 0 deletions apps/meteor/packages/flow-router/client/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
Route = function(router, pathDef, options, group) {
options = options || {};

this.options = options;
this.pathDef = pathDef

// Route.path is deprecated and will be removed in 3.0
this.path = pathDef;

if (options.name) {
this.name = options.name;
}

this._action = options.action || Function.prototype;
this._subscriptions = options.subscriptions || Function.prototype;
this._triggersEnter = options.triggersEnter || [];
this._triggersExit = options.triggersExit || [];
this._subsMap = {};
this._router = router;

this._params = new ReactiveDict();
this._queryParams = new ReactiveDict();
this._routeCloseDep = new Tracker.Dependency();

// tracks the changes in the URL
this._pathChangeDep = new Tracker.Dependency();

this.group = group;
};

Route.prototype.clearSubscriptions = function() {
this._subsMap = {};
};

Route.prototype.register = function(name, sub, options) {
this._subsMap[name] = sub;
};


Route.prototype.getSubscription = function(name) {
return this._subsMap[name];
};


Route.prototype.getAllSubscriptions = function() {
return this._subsMap;
};

Route.prototype.callAction = function(current) {
var self = this;
self._action(current.params, current.queryParams);
};

Route.prototype.callSubscriptions = function(current) {
this.clearSubscriptions();
if (this.group) {
this.group.callSubscriptions(current);
}

this._subscriptions(current.params, current.queryParams);
};

Route.prototype.getRouteName = function() {
this._routeCloseDep.depend();
return this.name;
};

Route.prototype.getParam = function(key) {
this._routeCloseDep.depend();
return this._params.get(key);
};

Route.prototype.getQueryParam = function(key) {
this._routeCloseDep.depend();
return this._queryParams.get(key);
};

Route.prototype.watchPathChange = function() {
this._pathChangeDep.depend();
};

Route.prototype.registerRouteClose = function() {
this._params = new ReactiveDict();
this._queryParams = new ReactiveDict();
this._routeCloseDep.changed();
this._pathChangeDep.changed();
};

Route.prototype.registerRouteChange = function(currentContext, routeChanging) {
// register params
var params = currentContext.params;
this._updateReactiveDict(this._params, params);

// register query params
var queryParams = currentContext.queryParams;
this._updateReactiveDict(this._queryParams, queryParams);

// if the route is changing, we need to defer triggering path changing
// if we did this, old route's path watchers will detect this
// Real issue is, above watcher will get removed with the new route
// So, we don't need to trigger it now
// We are doing it on the route close event. So, if they exists they'll
// get notify that
if(!routeChanging) {
this._pathChangeDep.changed();
}
};

Route.prototype._updateReactiveDict = function(dict, newValues) {
var currentKeys = _.keys(newValues);
var oldKeys = _.keys(dict.keyDeps);

// set new values
// params is an array. So, _.each(params) does not works
// to iterate params
_.each(currentKeys, function(key) {
dict.set(key, newValues[key]);
});

// remove keys which does not exisits here
var removedKeys = _.difference(oldKeys, currentKeys);
_.each(removedKeys, function(key) {
dict.set(key, undefined);
});
};
Loading
Loading