Giter Club home page Giter Club logo

go_flow's Introduction

Go Flow

This package aims to bring a deferred execution mechanism similar to Go's defer statement into the Dart programming language. This mechanism allows you to schedule code to run when a certain scope or function exits, ensuring that necessary cleanup or finalization tasks are performed regardless of how the scope exits (either normally or due to an error).

this package provides you with two functions for use within your application:

  1. syncGoFlow
  2. asyncGoFlow

As a simple example:

func main() {
    file := File("test")
    defer:
        file.Close()
    file.Write("text")
}

This can be implemented using this package as:

syncGoFlow((defer) {
  final io = File("test").openSync(mode: FileMode.append);
  defer((result, recover) {
    io.closeSync();
  });
  io.writeStringSync('text');
});

The result and recover parameters given in the defer statement are not lazy, but they are functions, and once you call them, they will return null afterward. So, if you call result inside a defer statement, you have to return something if you don't want to receive null.

Execution order

One of the key features of this mechanism is the execution order of deferred actions, which follows a Last-In-First-Out (LIFO) pattern.

the LIFO principle means that the order in which deferred actions are executed is the reverse of the order in which they are defined. The most recently defined deferred action will be the first one to execute when the scope exits, and the earliest defined deferred action will be the last one to execute.

syncGoFlow((defer) {
  // This will be executed last when the scope exits
  defer((result, recover) {
    // Clean up or finalize action
  });
  
  // ... Other code and defer statements ...
  
  // This will be executed first when the scope exits
  defer((result, recover) {
    // Clean up or finalize action
  });
});

This order ensures that the most recently defined deferred action has the opportunity to perform any cleanup or finalization before the earlier defined actions.

go_flow's People

Contributors

fmotalleb avatar

Watchers

 avatar

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.