Skip to content

Instantly share code, notes, and snippets.

@Novout
Last active February 13, 2021 01:30
Show Gist options
  • Select an option

  • Save Novout/c9baf9bec5f9cc8ab2e5dd9f03e358e5 to your computer and use it in GitHub Desktop.

Select an option

Save Novout/c9baf9bec5f9cc8ab2e5dd9f03e358e5 to your computer and use it in GitHub Desktop.
Ticker example in typescript
//.ts not highlight :/
// ticker.ts
/**
* @category Context
* @param base Content object.
* @param cb_stop Callback for break loop condition.
* @param cb_loop Array with callback's for loop.
* @param cb_initial Array with callback's for initial loop creation.
* @param cb_finish Array with callback's for finish loop destruction.
*/
export const ticker = (
base: ContentContext,
cb_stop: () => boolean,
cb_loop: Array<(base?: ContentContext) => void>,
cb_initial: Array<(base?: ContentContext) => void>,
cb_finish: Array<(base?: ContentContext) => void>,
): void => {
if (!base.map || !base.context) {
throw new Error('Content not exists in context.');
}
cb_initial?.forEach((fn: (base?: ContentContext) => void) => {
fn && fn();
});
const _interval = setInterval(() => {
cb_loop?.forEach((fn: (base?: ContentContext) => void) => {
fn && fn(base);
});
}, base.context.cycle_loop); // default: 1,666667 | 100 / 60
if (!cb_stop) {
cb_finish?.forEach((fn: (base?: ContentContext) => void) => {
fn();
});
clearInterval(_interval);
}
};
// main.ts
ticker(
{ map, context }, // content
() => true, // loop break
[() => keyboardSimplePlayer({ map, context }), () => simpleChunkLoader()], // loop
[() => globalHandler()], // initial
[() => { console.log(":(") }], // finish
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment