Code Monkey home page Code Monkey logo

before's Introduction

Before

What is it?

Playing with an idea based on aspect oriented programming. In this take, the flow of execution is determined by a sequence of fragments which may only specify one ancestor. This restriction imposes a stream-like flow to processing program logic.

It is called "Before" because the programmer can only extend functionality by adding fragments that execute "Before" something that already exists.

What is a fragment?

Fragments are simple units that are passed a context object and specify their ancestor, e.g:

$before = "terminate";

$execute = function($context) {
	echo $context->hello;
};

This will execute before the terminate function, and echo the contents of the $context->hello property.

What is context?

The $context object is passed to every fragment. Fragments are free to access and mutate any data on the context object.

Does it work?

In theory, yes. For complex use-cases, I don't know. One could envision a flow that starts by setting a route context based on the current visitor URL, accessing and manipulating data, generating template output, and rendering to the screen.

Manipulating the execution flow could then be as simple as adding a fragment at the point before you want to change the $context. Whether it's possible to build a complex application in a stream-like way is something I haven't played with before.

It could be fun to play with some more complex use cases.

What could be improved.

A LOT of stuff.

  • Optimise load speed by caching the stack after initial generation
  • Add rewiring capability, to override the ancestry of individual fragments, or replace them entirely
  • Security protections to prevent nasty folk creating fragments that leak into other fragments or do mean things to the context object
  • On that note, if the context were based on a common interface, a lot of these protections would be easier to write
  • Anonymous function debugging can be difficult to follow, so improved exception handling
  • Find a more efficient way to sort out the call stack using some kind of topological sorting algorithm (to also support multiple "before" statements
  • Do I want to add "after" and "mutate" capability... I dunno. Maybe? I kind of like the logical simplicity of only being able to go before something else that already exists.

Why use it?

I wouldn't recommend it beyond curiosity at this point, or ever :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.