use state query in repl
+ move locations to context
This commit is contained in:
parent
29bdfb2d90
commit
f1362b1c94
5 changed files with 25 additions and 23 deletions
|
|
@ -18,7 +18,7 @@ export default function CodeMirror({ value, onChange, options, editorDidMount }:
|
|||
}
|
||||
|
||||
export const markEvent = (editor) => (event) => {
|
||||
const locs = event.value.locations;
|
||||
const locs = event.context.locations;
|
||||
if (!locs || !editor) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
1
repl/src/types.d.ts
vendored
1
repl/src/types.d.ts
vendored
|
|
@ -15,6 +15,7 @@ export declare interface Hap<T = any> {
|
|||
whole: TimeSpan;
|
||||
part: TimeSpan;
|
||||
value: T;
|
||||
context: any;
|
||||
show: () => string;
|
||||
}
|
||||
export declare interface Pattern<T = any> {
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import type { ToneEventCallback } from 'tone';
|
||||
import * as Tone from 'tone';
|
||||
import { TimeSpan } from '../../strudel.mjs';
|
||||
import { TimeSpan, State } from '../../strudel.mjs';
|
||||
import type { Hap } from './types';
|
||||
|
||||
export declare interface UseCycleProps {
|
||||
onEvent: ToneEventCallback<any>;
|
||||
onQuery?: (query: TimeSpan) => Hap[];
|
||||
onQuery?: (state: State) => Hap[];
|
||||
onSchedule?: (events: Hap[], cycle: number) => void;
|
||||
ready?: boolean; // if false, query will not be called on change props
|
||||
}
|
||||
|
|
@ -21,7 +21,7 @@ function useCycle(props: UseCycleProps) {
|
|||
// pull events with onQuery + count up to next cycle
|
||||
const query = (cycle = activeCycle()) => {
|
||||
const timespan = new TimeSpan(cycle, cycle + 1);
|
||||
const events = onQuery?.(timespan) || [];
|
||||
const events = onQuery?.(new State(timespan)) || [];
|
||||
onSchedule?.(events, cycle);
|
||||
// cancel events after current query. makes sure no old events are player for rescheduled cycles
|
||||
// console.log('schedule', cycle);
|
||||
|
|
@ -46,6 +46,7 @@ function useCycle(props: UseCycleProps) {
|
|||
time: event.part.begin.valueOf(),
|
||||
duration: event.whole.end.sub(event.whole.begin).valueOf(),
|
||||
value: event.value,
|
||||
context: event.context,
|
||||
};
|
||||
onEvent(time, toneEvent);
|
||||
}, event.part.begin.valueOf());
|
||||
|
|
|
|||
|
|
@ -81,9 +81,9 @@ function useRepl({ tune, defaultSynth, autolink = true, onEvent }: any) {
|
|||
[onEvent]
|
||||
),
|
||||
onQuery: useCallback(
|
||||
(span) => {
|
||||
(state) => {
|
||||
try {
|
||||
return pattern?.query(span) || [];
|
||||
return pattern?.query(state) || [];
|
||||
} catch (err: any) {
|
||||
err.message = 'query error: ' + err.message;
|
||||
setError(err);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue