domenic / proposal-blocks

Former home of a proposal for a new syntactic construct for serializable blocks of JavaScript code
215 stars 5 forks source link

How do blöcks reuse other code? #25

Open aboodman opened 5 years ago

aboodman commented 5 years ago

I don't see an example of code reuse. Say I want to use protobufs implementation inside the blöck to serialize the data I send with fetch(). Does all code have to be inside the blöck?

dotproto commented 5 years ago

To the best of my undrestanding,I believe you would pass your protobuf in as an argument or import() it inside the blöck.

// Passing shared code in arguments
const messageHandler = {|
  // MyMessage and name vars provided by reification
  var message = new MyMessage();
  message.setName(name);
  return message.serializeBinary();
|};

import { messages } from './messages_pb.js';
messageHandler.reify({MyMessage: messages.MyMessage, name: "John Doe"});
// Importing shared code
const messageHandler {|
  const [name] = arguments;
  const {messages: {MyMessage}} = await fetch('./messages_pb');

  var message = new MyMessage();
  message.setName(name);
  return message.serializeBinary();
|};

messageHandler.reify({name: "John Doe"});