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

Wire to Promise converter #8

Closed
JackVeromeev opened this issue Dec 22, 2021 · 2 comments
Closed

Wire to Promise converter #8

JackVeromeev opened this issue Dec 22, 2021 · 2 comments
Assignees

Comments

@JackVeromeev
Copy link
Owner

JackVeromeev commented Dec 22, 2021

API

  • Mixin WireHelperMixin (naming TBD)
  • method to capture the data from wire
    @wire(getRecord, {...props}) setSomeRecord(recordData) { this.storeWired('some record', recordData); }
  • method to get the data from storage
    this.getWired('some record') => Promise

Solution draft

class A {
    someProp;
    @wire(getSomeProp)
    setSomeProp({data, error}) {
        // proto for "setSomeRecord"
        if (data) {
            this.someProp = data;
            this.somePropAwaitList.forEach(([resolve]) => resolve(this.someProp));
        }
        if (error) {
            this.somePropAwaitList.forEach(([resolve, reject]) => reject(error));
            console.error(error);
        }
    }
    somePropAwaitList = [];
    awaitSomeProp = () => { // proto for "getWired"
        if (this.someProp !== undefined) {
            return Promise.resolve(this.someProp);
        } else {
            return new Promise((resolve, reject) => {
                this.somePropAwaitList = [...this.somePropAwaitList, [resolve, reject]];
            });
        }
    }
}
@JackVeromeev JackVeromeev self-assigned this Dec 22, 2021
@JackVeromeev
Copy link
Owner Author

JackVeromeev commented Dec 22, 2021

Actually, you can return the same promise to each requestor. Proof:

let resolve;
let reject;
const resolved = new Promise((r,j) => {
    resolve = r;
    reject = j;
});
setTimeout(
    () => {
        console.log("now let's resolve");
        resolve();
    },
    3000
);

setTimeout(
    () => {
        console.log("add first listener");
        resolved.then(() => console.log('1'));
    },
    1000
);
setTimeout(
    () => {
        console.log("add second listener");
        resolved.then(() => console.log('2'));
    },
    2000
);
setTimeout(
    () => {
        console.log("add third listener");
        resolved.then(() => console.log('3'));
    },
    4000
);

Result

add first listener
add second listener
now let's resolve
1
2
add third listener
3

@JackVeromeev
Copy link
Owner Author

Moved to sldt-lib/sldt#4

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

No branches or pull requests

1 participant