Giter VIP home page Giter VIP logo

crc2json's People

Contributors

hyperlink avatar

Stargazers

 avatar

Watchers

 avatar  avatar

crc2json's Issues

Synchronous version required

It seems like the usage of callbacks is asynchronous in that events are getting hung up and are not completing prior to the next invocation.

I've got a scenario in which I've got two files I want to compare: A (source) and B (destination). I was able to more or less orchestrate this by wrapping a handler around the core c2cjson which accepts not only the target directory and file info but also a callback which gets invoked when the desired file is discovered.

I invoke the same in a nested manner via the callback in order to discover B CRC. And, after a couple of comparisons deciding whether a file was being added or potentially updated, then I want to respond with subsequent processing.

// i.e. file names to consider for copy from source directory to destination directory.
var items = [
    { name: "1" },
    { name: "2" },
    { name: "3" }
];

What I am finding is that these callbacks are still resolving after the main files loop, which leaves me wanting to do some "last" comparisons on the array of intended files. Sometimes it works, sometimes it does not; I just had an instance where file 1 and file 3 were flagged as adding, but file 2 was not, for instance.

var processPathCrc = (rootDir, info, callback) => {
    crc2json(rootDir,
        map => {
            var value = undefined;
            for (var k in map) {
                if (k.endsWith(info.name)) {
                    value = map[k];
                    break;
                }
            }
            callback(info, value);
        });
};

All this to say, I really need a synchronous version, preferably on in which I can target a specific file, not necessarily the entire directory. That would really work best in my scenario, without having to worry, much less wonder, about nested callbacks and functional wrappers.

With example usage in my scenario. I translated from my Gulpfile, so I think this is accurate, but I will correct it if necessary:

// Assuming i is a loop iterator
var last = items[items.length - 1];
var addSrcs = [];
var srcDir = "/path/to/src";
var destDir = "/path/to/dest";
// This is incomplete code; assuming an outer file items loop with i iterator.
processPathCrc(srcDir,
    items[i],
    (a, x) => {
        processPathCrc(destDir,
            a,
            (b, y) => {
                var src = path.join(srcDir, b.name).replace(/\\/g, "/");
                var dest = path.join(destDir, b.name).replace(/\\/g, "/");
                b.adding = x && y === undefined;
                b.updating = x && y && x !== y;
                if (b.adding) {
                    addSrcs.push(b.name);
                }
                // Using fs-jetpack, or your favorite, file system package.
                fs.copy(src, dest, { overwrite: b.adding || b.updating });
                if (b=== last) {
                    archive();
                }
            });
    });

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.