charmCityJs / talks

Come present something at a future meet-up!
36 stars 8 forks source link

1) Using Proxies to make singleton factories. 2) Singleton factories for promises (allowing "parallel programming"). #305

Open Codex-Orange opened 4 weeks ago

Codex-Orange commented 4 weeks ago

1) Using Proxies to make singleton factories. 2) Using a promise singleton factory for "state-based parallel programming").

In JavaScript, Proxies can be used to create singleton factories. One interesting use of singleton factories is to create a "promise singleton factory". Such a factory can be used to write a form of "state-based parallel programming". I personally have used this form of programming when my app opens, allowing me to do start up tasks in parallel, using a human-readable coding style.

Example code:

async main() {
    const sf = new StateFactory();
    sf.start.then(async () => {
        // sf.start is already resolved
        // login and sync here
        sf.synced.resolve();
    });
    sf.start.then(async () => {
        // sf.start is already resolved
        // initialize mixins here
        sf.initialized.resolve();
    });
    sf.initialized.then(async () => {
        // render here
        sf.rendered.resolve();
    });
    sf.rendered.then(async () => {
        // update here
        sf.updated.resolve();
    });
    sf.rendered.then(async () => {
        // after render here
        sf.afterRendered.resolve();
    });
    sf.all(sf.synced, sf.updated)
        .then(async () => {
            // go to today page here
            sf.today.resolve();
        });
    sf.all(sf.afterRendered, sf.today)
        .then(() => {
            console.log("==== main() end ====");
        });
}

Your name: Stan Silver

CCJS Slack username: Codex-Orange

If you're not a member of CCJS slack, sign up here so we can communicate about your talk, if you don't mind: http://slack.charmcityjs.org

Twitter username (optional):

Estimated Talk Length: (1-15 min please): 10 minutes

Availability: Please check off the dates in which you would be interested in presenting your talk:

asasmith commented 3 weeks ago

Thanks for submitting this talk @Codex-Orange! Scheduling your for our july (7/3) meetup.