Pocket Redux

  • 7 Replies
  • 2553 Views
*

Zero

  • Eve
  • ***********
  • 1287
Pocket Redux
« on: March 30, 2022, 09:02:35 pm »
Now that I have a central blackboard, I don't want random parts of my program to mess with my state. So, inspired from Redux, here is an intent manager.

Code
// Intent manager

class IntentManager {

    intentions = [];
    target = null;
    actions = {};
    intentID = 0n;

    constructor(target) {
        this.target = target;
    }

    intend(action, parameters) {
        this.intentions.push({ action, parameters, id: 'i' + this.intentID });
        return 'i' + (this.intentID++);
    }

    action(id, handler) {
        this.actions[id] = handler;
    }

    execute(intent) {
        return this.actions[intent.action](intent.parameters, this.target, this);
    }

    run() {
        let intentList = Array.from(this.intentions);
        this.intentions = [];
        let result = {};
        for (let intent of intentList)
            result[intent.id] = { intent, result: this.execute(intent) };
        return result;
    }
}

The point (in my use case) is to separate the choice of an action from the execution of an action. The only (but big) difference between my IntentManager and ReduxJS, is that Redux wants you to use an immutable central store. In my use case, this is not an option because I consider that cloning the entire mind of an Ai every time you're going to increment some ridiculous counter somewhere, is not realistic. But, even with a mutable store, you still get all the expected benefits of the pattern, as long as you stick to the rules.

The manager doesn't enforce anything, but you're supposed to respect 3 simple rules to use it correctly.

1) All of your stuff should be in one place, which is the target of the intent manager. In my case, this is an SMap (which I talked about in the other thread) but of course it could be anything else.

2) Access to the store should be done only through actions. It's especially important when things are triggered from outside events, which could occur right in the middle of a dbase mutation. Simple enough.

3) Action handlers should always give the same result when given the same arguments. I'm not saying they should be pure though, because I don't care about side-effects.

And... that's about it! The "action" becomes the processing unit inside of which you're safe: inside of an action, you cannot encounter inconsistent data in the store.

Edit: added return values.
« Last Edit: March 31, 2022, 04:43:10 pm by Zero »

*

MagnusWootton

  • Replicant
  • ********
  • 634
Re: Pocket Redux
« Reply #1 on: March 30, 2022, 11:30:46 pm »
I dont see the importance of this,  how can the state be changed unless u did it on purpose?    ?

*

Zero

  • Eve
  • ***********
  • 1287
Re: Pocket Redux
« Reply #2 on: March 31, 2022, 01:35:46 am »
This is mainly about race condition...

https://en.m.wikipedia.org/wiki/Race_condition#In_software

https://medium.com/@slavik57/async-race-conditions-in-javascript-526f6ed80665

These two links should explain it better than I can   ::)

*

MagnusWootton

  • Replicant
  • ********
  • 634
Re: Pocket Redux
« Reply #3 on: March 31, 2022, 01:58:20 am »
Ah, so uve got a parallel implementation.   I see now.

*

Zero

  • Eve
  • ***********
  • 1287
Re: Pocket Redux
« Reply #4 on: March 31, 2022, 09:17:58 pm »
I've been thinking about your input here. To be perfectly honest, nothing can be really parallel in Javascript, since it is single threaded. But the IntentManager makes the call sequences explicit, which is a good thing. What matters is the order in which things are done.

*

ivan.moony

  • Trusty Member
  • ************
  • Bishop
  • *
  • 1723
    • mind-child
Re: Pocket Redux
« Reply #5 on: April 01, 2022, 06:50:13 am »
Javascript supports multitasking for a while now. See webworkers. Though, we can't manipulate DOM from webworkers.

*

Zero

  • Eve
  • ***********
  • 1287
Re: Pocket Redux
« Reply #6 on: April 01, 2022, 09:11:08 am »
Yes I know that, I even made a game using web workers. But they are isolated, and when you want to retrieve data from them, you do it from the main single thread. In Javascript, you cannot have two pieces of code trying to access the same resource at the same time.

*

MagnusWootton

  • Replicant
  • ********
  • 634
Re: Pocket Redux
« Reply #7 on: April 01, 2022, 07:19:50 pm »
you cannot have two pieces of code trying to access the same resource at the same time.

Its cause they are happening in parallel.

 


OpenAI Speech-to-Speech Reasoning Demo
by MikeB (AI News )
March 31, 2024, 01:00:53 pm
Say good-bye to GPUs...
by MikeB (AI News )
March 23, 2024, 09:23:52 am
Google Bard report
by ivan.moony (AI News )
February 14, 2024, 04:42:23 pm
Elon Musk's xAI Grok Chatbot
by MikeB (AI News )
December 11, 2023, 06:26:33 am
Nvidia Hype
by 8pla.net (AI News )
December 06, 2023, 10:04:52 pm
How will the OpenAI CEO being Fired affect ChatGPT?
by 8pla.net (AI News )
December 06, 2023, 09:54:25 pm
Independent AI sovereignties
by WriterOfMinds (AI News )
November 08, 2023, 04:51:21 am
LLaMA2 Meta's chatbot released
by 8pla.net (AI News )
October 18, 2023, 11:41:21 pm

Users Online

291 Guests, 0 Users

Most Online Today: 376. Most Online Ever: 2369 (November 21, 2020, 04:08:13 pm)

Articles