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

fix(core): publish events on first render #1024

Merged
merged 4 commits into from
Jan 10, 2024

Conversation

etowahadams
Copy link
Contributor

Fix #1020
Toward #

Change List

  • Compile a blank spec in the initial render so that the Gosling ref can get associated with the DOM.

This enables subscription events to work properly.

Screen.Recording.2023-12-23.at.2.11.00.PM.mov

Checklist

  • Ensure the PR works with all demos on the online editor
  • Unit tests added or updated
  • Examples added or updated
  • Documentation updated (e.g., added API functions)
  • Screenshots for visual changes (e.g., new encoding support or UI change on Editor)

@etowahadams
Copy link
Contributor Author

Here is what I used to test the subscription events (basically the example that Thomas provided, thanks Thomas!)

import React, { useEffect, useRef, useState } from 'react';
import { GoslingComponent, type GoslingRef, type GoslingSpec } from 'gosling.js';

const goslingSpec = {
    title: 'Basic Marks: bar',
    subtitle: 'Tutorial Examples',
    tracks: [
        {
            layout: 'linear',
            width: 800,
            height: 180,
            data: {
                url: 'https://resgen.io/api/v1/tileset_info/?d=UvVPeLHuRDiYA3qwFlm7xQ',
                type: 'multivec',
                row: 'sample',
                column: 'position',
                value: 'peak',
                categories: ['sample 1'],
                binSize: 5
            },
            mark: 'bar',
            x: { field: 'start', type: 'genomic', axis: 'bottom' },
            xe: { field: 'end', type: 'genomic' },
            y: { field: 'peak', type: 'quantitative', axis: 'right' },
            size: { value: 5 }
        }
    ]
} as GoslingSpec;

function App() {
    const gosRef = useRef<GoslingRef>(null);

    useEffect(() => {
        if (gosRef.current) {
            const currentRef = gosRef.current;
            // specProcessed
            currentRef.api.subscribe('onNewView', (_, data) => {
                console.warn('onNewView', data);
            });
            currentRef.api.subscribe('onNewTrack', (_, data) => {
                console.warn('onNewTrack', data);
            });
            currentRef.api.subscribe('specProcessed', (_, data) => {
                console.warn('specProcessed', data);
            });
        }

    }, [gosRef.current]);

    return (
        <div>
            <GoslingComponent ref={gosRef} spec={goslingSpec} />
        </div>
    );
}

export default App;

Copy link
Member

@sehilyi sehilyi left a comment

Choose a reason for hiding this comment

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

Ah, I see how this is fixed – Great!

@etowahadams etowahadams merged commit bf470c1 into main Jan 10, 2024
4 checks passed
@etowahadams etowahadams deleted the etowahadams/events-published branch January 10, 2024 16:05
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.

Events published during compiling are not captured due to rendering cycle
2 participants