Skip to content

Conversation

@jamos-tay
Copy link
Contributor

@jamos-tay jamos-tay commented Aug 27, 2018

What is the purpose of this pull request? (put "X" next to an item, remove the rest)

• [X] New feature

Fixes #403

What is the rationale for this request?

Changing certain files will not trigger a rebuild. User might want a way to force a rebuild of all files.

What changes did you make? (Give an overview)

Add a --force-reload flag to markbind serve to allow user to rebuild all files whenever a file is changed.

When set, any change detected in the directory will immediately trigger a rebuild of all files.

@acjh
Copy link
Contributor

acjh commented Aug 27, 2018

@jamos-tay
Copy link
Contributor Author

Hi @acjh, I've looked through the two comment threads and sorry, but could you explain a little more about what changes I should make?

My rationale for changing the delay function was to add an extra parameter buildAsset(filepaths, forceReload), which was giving me problems as delay only allows one array parameter.

What I gathered from the first thread was that the old definition of delay adds a [] so it converts single arguments arg to an array:

  return function (arg) {
    
    // arg is a single value "test.html"
    
    context = this;
    if (Array.isArray(arg)) {
      pendingArgs = pendingArgs.concat(arg);
    } else {
      pendingArgs.push(arg);
    }
    
    // pendingArgs contains ["test.html"]
    
    ...

    const funcPromise = func.apply(context, [pendingArgs]); 
    // => func.apply(context, [["test.html"]])
    // => func(["test.html"]) is called instead of func("test.html")

Which is necessary for cases like buildAsset("filepath") -> buildAsset(["filepath"]).

But this seems strange to me because I expected delay to be a wrapper function that can be used with any func, it shouldn't be dependent on func's definition. Is there any reason that we should enforce delay to only work with functions that take in one array?

Regarding the use of ...args, the purpose was just to allow variable number of parameters for func. It should still work even if func doesn't use rest parameters.

For example:

function buildAsset(filepaths, forceReload) { // Not using rest
   ...
}

delayedbuildAsset = delay(buildAsset, 1000);

/*
    delayedbuildAsset is set to function(...args){
        ...
        buildAsset.apply(args);
    }
*/

delayedbuildAsset(["some filepath"], true); // Expect to call buildAsset(["some filepath"], true)

/*
    function(...args){
        // args is set to [["some filepath"], true]
        buildAsset.apply(args);
        // => buildAsset.apply([["some filepath"], true])
        // => calls buildAsset(["some filepath"], true) correctly
    }
*/

@acjh
Copy link
Contributor

acjh commented Aug 28, 2018

If I recall correctly, your change would cause this:

pendingArgs = [];
// ...
pendingArgs = [filePaths, forceReload];
// ...
pendingArgs = [filePaths, forceReload, filePaths, forceReload];

Did you test?

@jamos-tay
Copy link
Contributor Author

Ah I see... I didn't know delay aggregated the params and executed them all at once, I thought it was just a simple delay function, my bad.

I moved the forceReload param to the Site constructor, I think that might be simpler for now.

|---|---|
| `-p`, `--port <port>` | The port used for serving your website. |
| --no-open | Don't open browser automatically. |
| --force-reload | Forces a full reload of all site files when a file is changed. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a -f short flag? If yes, move above -p, else move above --no-open.

index.js Outdated
.description('build then serve a website from a directory')
.option('-p, --port <port>', 'port for server to listen on (Default is 8080)')
.option('--no-open', 'do not automatically open the site in browser')
.option('--force-reload', 'forces a full reload of all site files when a file is changed')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"forcesforce a full reload ..." — let's use imperative mood.

lib/Site.js Outdated
this.baseUrlMap = {};
this.siteConfig = {};
this.userDefinedVariablesMap = {};
this.forceReload = forceReload;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alphabetical order.

lib/Site.js Outdated

Site.prototype._rebuildAffectedSourceFiles = function (filePaths) {
const uniquePaths = _.uniq(filePaths);
const wrappedFilepaths = Array.isArray(filePaths) ? filePaths : [filePaths];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • wrapped seems like an odd way to describe an "ensuring an array", but I don't have a better name at the moment.
  • We should not lose the capital P for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just filePathArray?

@acjh
Copy link
Contributor

acjh commented Aug 28, 2018

Ah I see... I didn't know delay aggregated the params and executed them all at once, I thought it was just a simple delay function, my bad.

It was named after Promise.delay, but we can rename if there is a better name used by other libraries.

  • Allow delay to support multiple arguments

Remember to update PR description.

@jamos-tay
Copy link
Contributor Author

I see, thanks for the information

@yamgent yamgent added this to the v1.12.1 milestone Sep 1, 2018
@yamgent yamgent merged commit 8d70678 into MarkBind:master Sep 1, 2018
@damithc
Copy link
Contributor

damithc commented Oct 10, 2018

@jamos-tay this feature doesn't seem to be reloading nunjucks code though :-(

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add --full-reload option for markbind serve

4 participants