CORE for JS - write code close to how you think
Write less, do more
This framework is the simple JS implementation of CORE design pattern (acronym: Contexts, Objects, Requests and Events)
This design pattern and the framework provide you ability to easily design and implement well structured, high cohesioned, and low coupled modular systems using Events and Requests. The benefit of that approach is a semantic which is highly close to Business Logic, which allows to write less code doing more, and have less bugs and debugging with it.
The ideas in the base of it are similar to following approaches, and it collects the best benefits of them:
- automata-based programming
- actor model
- system of signals and slots
- event-oriented programming
Event is a complex object, that means that something has already happend.
Request is a complex object, that means that something asks to perform its request.
Other objects of the system can subscribe to Events and Requests. Subscription is a static process (which is different to usual obj.fire('event') and obj.on('event) dynamic-style subscriptions).
During initialization CORE framework parses the code of methods and subscribes objects on Events and Requests statically.
Note that 99% of real-world cases don't need a dynamic behaviour, which is a good base to simplify this process
Static approach also offers you an ability to build a map how objects/methods are connected, and analyse it, which can extend your Static Code Analysis Tool benefits of your project.
High-level description: https://medium.com/@i_am_os/core-design-pattern-the-way-out-from-overly-complicated-code-b8804449941
How to help the project: https://medium.com/ux-of-programming-languages/lets-build-a-community-around-core-701eb8ebb02c
Installing
API
Events
Description
There are three steps for using Events: initialization, firing, catching.
You can pass some data with Event.
Example
Initialization
To initialize Event object call Core.registerEventPoint
with Event name. Event name consists of Object name that has this Event and action name.
Core;
Firing
To fire Event call FireEvent
function with created Event.
var Player = mediaTag: document { thismediaTag; ; }
Catching
The main twist is that you can catch the fired Event at any spaces of your code.
So this can cut your code several times.
Also you can dinamically subscribe to the event. It is useful in different cases, for example, in angular directives.
Single Event Catching
var GoogleTrackingObject = { var event = ; /* event.data === 'some-data' //true */ ; }
Multiple Event Catching
var GoogleTrackingObject = { var event = ; ; }
Requests
Description
There are three steps for using them: initialization, firing, catching. You can pass some data with the Request.
Example
Initialization
Just create Request object.
Core
Firing
Fire it and ask something to perform your request.
var PlayerUi = { }
Catching
Catch the Request and perform it.
var PlayerAudio = { var request = ; /* request.data === 'some-data' //true */ return { /* start playing audio player logic */ ; } }
There can be several objects that can resolve Requests. When one of them can't process Request it call error callback function and next object start processing.
var PlayerAudio = mediaTag: null { ; return { if !PlayerAudiomediaTag return ; /* start playing audio player logic */ ; } } var Player = { ; return { /* start playing audio player logic */ ; } }
States
It's an example how we can implement more complicated object's behaviour and use it in CORE-style.
Description
Usage
Core
Params
String '' // name of the state
Returns
Object value: String // current state value go : Function // method to change state
Examples
Initialization
var Object = mainState: Core
When the object has been inited, its state goes to the first value of the set.
Changing State
ObjectmainState;
When state has been changed, the Event Object.mainState.GoRunning
fires. And it can be catched at any space of the application.
var MiddleObject = { Core; if ObjectmainStatevalue === 'Running' // your code here }