Skip to content

Instantly share code, notes, and snippets.

@pofulu
Last active January 27, 2023 08:45
Show Gist options
  • Save pofulu/85c314df0b6cf359733418c1a22e59ec to your computer and use it in GitHub Desktop.
Save pofulu/85c314df0b6cf359733418c1a22e59ec to your computer and use it in GitHub Desktop.
Create EventSource in Meta Spark Studio.
import Reactive from 'Reactive';
export class Subject<T> {
private readonly _signalSource: StringSignalSource;
private readonly _eventSource: Omit<EventSource<T>, 'subscribe'> & { subscribe(callback: (args: T) => void): Subscription };
constructor() {
this._signalSource = Reactive.stringSignalSource(uuid());
this._eventSource = this._signalSource.signal.monitor() as any;
this._eventSource.subscribe = (callback: (value: T) => void) =>
this._signalSource.signal.monitor().subscribe(({ newValue }) =>
callback(JSON.parse(newValue).value)
);
}
get eventSource() {
return this._eventSource;
}
next(value: T) {
this._signalSource.set(JSON.stringify({ value, id: uuid() }));
}
dispose() {
this._signalSource.dispose();
}
}
function uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
import Diagnostics from 'Diagnostics';
import Reactive from 'Reactive';
import TouchGestures from 'TouchGestures';
const subject = new Subject<void>();
subject.eventSource.take(1).subscribe(v => {
Diagnostics.log(1)
})
subject.eventSource.skip(1).take(1).subscribe(v => {
Diagnostics.log(2)
})
TouchGestures.onTap().subscribe(() => {
subject.next()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment