0.9.5 • Published 3 years ago

maraca v0.9.5

Weekly downloads
4
License
MIT
Repository
-
Last release
3 years ago

Maraca JavaScript runtime

The JavaScript runtime for the Maraca language.

Install

yarn add maraca

or

npm install maraca --save

Maraca documentation

Full documentation for the Maraca language itself can be found at https://maraca-lang.org.

Table of contents

API

The core runtime API parses and runs Maraca source code.

maraca(source, config?, output?);

The config parameter can be provided to setup the @ and # Maraca language features, and the output parameter can be provided to enable streaming.

import maraca from 'maraca';

const source = '[x: 1, y: 2, z: x? * y?]';

// snapshot
const data = maraca(source);

// stream
maraca(source, (data) => console.log(data));

source

The Maraca source can either be a string, or a nested set of objects, with each key defining a module:

type Source =
  | string
  | {
      [key]: Source;
    };

If the object form is used, evaluation starts with the 'start' script, which can then load the modules using the normal ? syntax.

library (optional)

If provided, the library parameter sets up custom streams that are then available to your source code, again using the normal ? syntax.

Custom streams can either be a constant Data value (see Data format below), or a stream generator function, with the following API:

type Generator = (
  set: (data: Data) => void,
  get: (stream: Stream) => Data,
  create: (generator: Generator) => Stream,
) =>
  | void
  | (dispose?) => void;

So the library has the type:

type Library = {
  [key]: Data | Generator;
};

output (optional)

If provided, the runtime will run in stream mode, outputting the results to the given callback.

Data format

The data output by Maraca has the following format:

type Data = ValueData | BlockData;

type ValueData = {
  type: 'value';
  value: string;
  push: (value: Data) => void;
};

type BlockData = {
  type: 'block';
  value: Block;
  push: (value: Data) => void;
};

type Block = {
  toPairs: () => { key: Data; value: Data }[];
};

The push method on each individual data value can be used to manually push updates into your Maraca code.

Utilities

parse: (source: string) => AST

Convert a Maraca script to AST.

fromJS: (value: any) => Data

Convert a JavaScript value into Maraca data.

Example

import maraca, { fromJs } from 'maraca';

const source = ['module? + tick?', { module: '#data?' }];

const library = {
  data: fromJs({ a: 1, b: 2, c: 3 }),
  tick: (set) => {
    let count = 1;
    set(fromJs(count++));
    const interval = setInterval(() => set(fromJs(count++)), 1000);
    return (dispose) => dispose && clearInterval(interval);
  },
};

maraca(source, library, (data) => console.log(data));
0.9.5

3 years ago

0.9.4

3 years ago

0.9.3

3 years ago

0.9.2

3 years ago

0.9.1

3 years ago

0.9.0

3 years ago

0.8.4

4 years ago

0.8.3

4 years ago

0.8.2

4 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.7.4

4 years ago

0.7.3

4 years ago

0.7.2

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.5

4 years ago

0.6.4

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.4

4 years ago

0.5.5

4 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.5

5 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.1

6 years ago