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 #4

Closed
JackVeromeev opened this issue Nov 7, 2022 · 0 comments
Closed

Wire to Promise converter #4

JackVeromeev opened this issue Nov 7, 2022 · 0 comments
Assignees
Milestone

Comments

@JackVeromeev
Copy link
Contributor

JackVeromeev commented Nov 7, 2022

API

  • Mixin WirePromiseMixin
  • method to capture the data from wire
    @wire(getRecord, {...props}) setSomeRecord(recordData) { this.setWire('some record', recordData); }
  • method to get the data from storage
    this.getWire('some record') => Promise

Solution

class A {

    contactRecordTypeId;
  
    @wire(getObjectInfo, {objectApiName: CONTACT})
    setContactData(response) { this.setWire("WIRE_CONTACT", response); }
    
    @wire(getCurrentPageRef)
    setPageRef(ref) { this.setWire("PAGE_REF", ref); }
    
    @wire(getPicklistOptions, {fieldApiName: CONTACT_TYPE, recordTypeId: "$contactRecordTypeId")
    setPicklist(response) { this.setWire("WIRE_PICKLIST", response); }
  
    async init() {
        const contactData = await this.getWire("WIRE_CONTACT");
        this.contactRecordTypeId = contactDdata.defaultRecordTypeId;
        const pageRef = await this.getWire("PAGE_REF");
        const picklistOptions = await this.getWire("WIRE_PICKLIST");
        // now you can operate there with all data you need
    }
}

PREVIOUS 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]];
            });
        }
    }
}

Source

JackVeromeev/sldt#8

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